1 | <?php |
||
12 | abstract class Policy |
||
13 | { |
||
14 | protected $directives = []; |
||
15 | |||
16 | protected $reportOnly = false; |
||
17 | |||
18 | abstract public function configure(); |
||
19 | |||
20 | /** |
||
21 | * @param string $directive |
||
22 | * @param string|array $values |
||
23 | * |
||
24 | * @return \Spatie\Csp\Policies\Policy |
||
25 | * |
||
26 | * @throws \Spatie\Csp\Exceptions\InvalidDirective |
||
27 | */ |
||
28 | public function addDirective(string $directive, $values): self |
||
29 | { |
||
30 | $this->guardAgainstInvalidDirectives($directive); |
||
31 | |||
32 | $rules = array_flatten(array_map(function ($values) { |
||
33 | return empty($values) ? $values : array_filter(explode(' ', $values)); |
||
34 | }, array_wrap($values))); |
||
35 | |||
36 | foreach ($rules as $rule) { |
||
37 | $sanitizedValue = $this->sanitizeValue($rule); |
||
38 | |||
39 | if (! in_array($sanitizedValue, $this->directives[$directive] ?? [])) { |
||
40 | $this->directives[$directive][] = $sanitizedValue; |
||
41 | } |
||
42 | } |
||
43 | |||
44 | return $this; |
||
45 | } |
||
46 | |||
47 | public function reportOnly(): self |
||
53 | |||
54 | public function enforce(): self |
||
60 | |||
61 | public function reportTo(string $uri): self |
||
67 | |||
68 | public function shouldBeApplied(Request $request, Response $response): bool |
||
72 | |||
73 | public function addNonceForDirective(string $directive): self |
||
77 | |||
78 | public function applyTo(Response $response) |
||
92 | |||
93 | public function __toString() |
||
94 | { |
||
95 | return collect($this->directives) |
||
96 | ->map(function (array $values, string $directive) { |
||
97 | $valueString = implode(' ', $values); |
||
98 | |||
99 | return empty($valueString) ? "{$directive}" : "{$directive} {$valueString}"; |
||
100 | }) |
||
101 | ->implode(';'); |
||
102 | } |
||
103 | |||
104 | protected function guardAgainstInvalidDirectives(string $directive) |
||
110 | |||
111 | protected function isHash(string $value): bool |
||
121 | |||
122 | protected function isKeyword(string $value): bool |
||
128 | |||
129 | protected function sanitizeValue(string $value): string |
||
140 | } |
||
141 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.