1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message\Header; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Sunrise\Http\Message\Exception\InvalidHeaderValueParameterException; |
18
|
|
|
use Sunrise\Http\Message\Header; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Import functions |
22
|
|
|
*/ |
23
|
|
|
use function implode; |
24
|
|
|
use function sprintf; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @link https://www.w3.org/TR/CSP3/#csp-header |
28
|
|
|
*/ |
29
|
|
|
class ContentSecurityPolicyHeader extends Header |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Regular Expression for a directive name validation |
34
|
|
|
* |
35
|
|
|
* @link https://www.w3.org/TR/CSP3/#framework-directives |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public const VALID_DIRECTIVE_NAME = '/^[0-9A-Za-z\-]+$/'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Regular Expression for a directive value validation |
43
|
|
|
* |
44
|
|
|
* @link https://www.w3.org/TR/CSP3/#framework-directives |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public const VALID_DIRECTIVE_VALUE = '/^[\x09\x20-\x2B\x2D-\x3A\x3C-\x7E]*$/'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array<string, string> |
52
|
|
|
*/ |
53
|
|
|
private array $parameters; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor of the class |
57
|
|
|
* |
58
|
|
|
* @param array<array-key, mixed> $parameters |
|
|
|
|
59
|
|
|
* |
60
|
|
|
* @throws InvalidHeaderValueParameterException |
61
|
|
|
* If the parameters aren't valid. |
62
|
|
|
*/ |
63
|
26 |
|
public function __construct(array $parameters = []) |
64
|
|
|
{ |
65
|
26 |
|
$parameters = $this->validateParametersByRegex( |
66
|
26 |
|
$parameters, |
67
|
26 |
|
self::VALID_DIRECTIVE_NAME, |
68
|
26 |
|
self::VALID_DIRECTIVE_VALUE |
69
|
26 |
|
); |
70
|
|
|
|
71
|
18 |
|
$this->parameters = $parameters; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
7 |
|
public function getFieldName(): string |
78
|
|
|
{ |
79
|
7 |
|
return 'Content-Security-Policy'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
14 |
|
public function getFieldValue(): string |
86
|
|
|
{ |
87
|
14 |
|
$directives = []; |
88
|
14 |
|
foreach ($this->parameters as $directive => $value) { |
89
|
|
|
// the directive can be without value... |
90
|
|
|
// e.g. sandbox, upgrade-insecure-requests, etc. |
91
|
12 |
|
if ($value === '') { |
92
|
4 |
|
$directives[] = $directive; |
93
|
4 |
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
10 |
|
$directives[] = sprintf('%s %s', $directive, $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
14 |
|
return implode('; ', $directives); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|