|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
|
5
|
|
|
* @link https://github.com/nicoSWD |
|
6
|
|
|
* @author Nicolas Oelgart <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace nicoSWD\SecHeaderCheck\Domain\Validator\Header; |
|
9
|
|
|
|
|
10
|
|
|
use nicoSWD\SecHeaderCheck\Domain\Result\Result\ReferrerPolicyHeaderResult; |
|
11
|
|
|
use nicoSWD\SecHeaderCheck\Domain\Validator\AbstractHeaderParser; |
|
12
|
|
|
|
|
13
|
|
|
final class ReferrerPolicyHeader extends AbstractHeaderParser |
|
14
|
|
|
{ |
|
15
|
4 |
|
public function parse(): ReferrerPolicyHeaderResult |
|
16
|
|
|
{ |
|
17
|
4 |
|
return (new ReferrerPolicyHeaderResult($this->getName(), $this->getValue())) |
|
18
|
4 |
|
->setMayLeakOrigin($this->mayLeakOrigin()) |
|
19
|
4 |
|
->setDoesNotLeakReferrer($this->doesNotLeakReferrer()); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
4 |
|
private function doesNotLeakReferrer(): bool |
|
23
|
|
|
{ |
|
24
|
|
|
$secureReferrerOptions = [ |
|
25
|
4 |
|
'no-referrer', |
|
26
|
|
|
'no-referrer-when-downgrade', |
|
27
|
|
|
'same-origin', |
|
28
|
|
|
'strict-origin', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
4 |
|
return $this->valueIsIn($secureReferrerOptions); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
4 |
|
private function mayLeakOrigin(): bool |
|
35
|
|
|
{ |
|
36
|
|
|
$leakyReferrerOptions = [ |
|
37
|
4 |
|
'origin', |
|
38
|
|
|
'origin-when-cross-origin', |
|
39
|
|
|
'strict-origin-when-cross-origin', |
|
40
|
|
|
'unsafe-url', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
4 |
|
return $this->valueIsIn($leakyReferrerOptions); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
private function valueIsIn(array $options): bool |
|
47
|
|
|
{ |
|
48
|
4 |
|
$value = strtolower($this->getValue()); |
|
49
|
4 |
|
$values = preg_split('~\s*,\s*~', $value, -1, PREG_SPLIT_NO_EMPTY); |
|
50
|
|
|
|
|
51
|
4 |
|
return ($options !== array_intersect($options, $values)); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|