|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +use Nette\Http; |
| 4 | +use Tester\Assert; |
| 5 | + |
| 6 | +require __DIR__ . '/../bootstrap.php'; |
| 7 | + |
| 8 | + |
| 9 | +test('matches both headers', function () { |
| 10 | + $request = new Http\Request(new Http\UrlScript, headers: [ |
| 11 | + 'Sec-Fetch-Site' => 'same-origin', |
| 12 | + 'Sec-Fetch-Dest' => 'document', |
| 13 | + ]); |
| 14 | + |
| 15 | + Assert::true($request->isFrom('same-origin', 'document')); |
| 16 | +}); |
| 17 | + |
| 18 | + |
| 19 | +test('fails when expected header missing', function () { |
| 20 | + $request = new Http\Request(new Http\UrlScript, headers: [ |
| 21 | + 'Sec-Fetch-Site' => 'same-origin', |
| 22 | + ]); |
| 23 | + |
| 24 | + Assert::false($request->isFrom('same-origin', 'document')); |
| 25 | +}); |
| 26 | + |
| 27 | + |
| 28 | +test('accepts multiple expected values', function () { |
| 29 | + $request = new Http\Request(new Http\UrlScript, headers: [ |
| 30 | + 'Sec-Fetch-Site' => 'cross-site', |
| 31 | + 'Sec-Fetch-Dest' => 'image', |
| 32 | + ]); |
| 33 | + |
| 34 | + Assert::true($request->isFrom(['same-origin', 'cross-site'], ['document', 'image'])); |
| 35 | + Assert::false($request->isFrom(['cross-site'], ['Document'])); |
| 36 | + Assert::false($request->isFrom(['Cross-Site'], ['image'])); |
| 37 | +}); |
| 38 | + |
| 39 | + |
| 40 | +test('fallback same-origin from Origin header', function () { |
| 41 | + $url = new Http\UrlScript('https://nette.org/app/'); |
| 42 | + $request = new Http\Request($url, headers: [ |
| 43 | + 'Origin' => 'https://nette.org', |
| 44 | + ]); |
| 45 | + |
| 46 | + Assert::true($request->isFrom('same-origin')); |
| 47 | +}); |
| 48 | + |
| 49 | + |
| 50 | +test('fallback cross-site from Origin header', function () { |
| 51 | + $url = new Http\UrlScript('https://nette.org/'); |
| 52 | + $request = new Http\Request($url, headers: [ |
| 53 | + 'Origin' => 'https://example.com', |
| 54 | + ]); |
| 55 | + |
| 56 | + Assert::true($request->isFrom('cross-site')); |
| 57 | +}); |
| 58 | + |
| 59 | + |
| 60 | +test('fallback missing without Origin header', function () { |
| 61 | + $url = new Http\UrlScript('https://nette.org/'); |
| 62 | + $request = new Http\Request($url); |
| 63 | + |
| 64 | + Assert::false($request->isFrom('same-origin')); |
| 65 | +}); |
| 66 | + |
| 67 | + |
| 68 | +test('fallback not used when header present', function () { |
| 69 | + $url = new Http\UrlScript('https://nette.org/'); |
| 70 | + $request = new Http\Request($url, headers: [ |
| 71 | + 'Sec-Fetch-Site' => 'none', |
| 72 | + 'Origin' => 'https://nette.org', |
| 73 | + ]); |
| 74 | + |
| 75 | + Assert::false($request->isFrom('same-origin')); |
| 76 | +}); |
| 77 | + |
| 78 | + |
| 79 | +test('fallback cross-site when port differs', function () { |
| 80 | + $url = new Http\UrlScript('https://nette.org:443'); |
| 81 | + $request = new Http\Request($url, headers: [ |
| 82 | + 'Origin' => 'https://nette.org:444', |
| 83 | + ]); |
| 84 | + |
| 85 | + Assert::true($request->isFrom('cross-site')); |
| 86 | +}); |
| 87 | + |
| 88 | + |
| 89 | +test('fallback ignored for invalid Origin', function () { |
| 90 | + $url = new Http\UrlScript('https://nette.org/'); |
| 91 | + $request = new Http\Request($url, headers: [ |
| 92 | + 'Origin' => 'null', |
| 93 | + ]); |
| 94 | + |
| 95 | + Assert::false($request->isFrom('same-origin')); |
| 96 | +}); |
0 commit comments