1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Csp\Profiles; |
4
|
|
|
|
5
|
|
|
use Spatie\Csp\Directive; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Spatie\Csp\Exceptions\InvalidDirective; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
abstract class Profile |
11
|
|
|
{ |
12
|
|
|
protected $directives = []; |
13
|
|
|
|
14
|
|
|
protected $reportOnly = false; |
15
|
|
|
|
16
|
|
|
abstract public function configure(); |
17
|
|
|
|
18
|
|
|
public function addDirective(string $directive, string $value): self |
19
|
|
|
{ |
20
|
|
|
$this->guardAgainstInvalidDirectives($directive); |
21
|
|
|
|
22
|
|
|
$this->directives[$directive][] = $value; |
23
|
|
|
|
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function reportOnly(): self |
28
|
|
|
{ |
29
|
|
|
$this->reportOnly = true; |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function enforce(): self |
35
|
|
|
{ |
36
|
|
|
$this->reportOnly = false; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function reportTo(string $uri): self |
42
|
|
|
{ |
43
|
|
|
$this->directives['report-uri'] = [$uri]; |
44
|
|
|
|
45
|
|
|
$reportToContents = json_encode([ |
46
|
|
|
'url' => $uri, |
47
|
|
|
'group-name' => class_basename(static::class), |
48
|
|
|
'max-age' => 60 * 60 * 24 * 7 * 30, |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
$this->directives['report-to'] = [$reportToContents]; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function shouldBeApplied(Request $request, Response $response): bool |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
return config('csp.enabled'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function addNonceForDirective(string $directive): self |
62
|
|
|
{ |
63
|
|
|
return $this->addDirective($directive, "nonce-" . app('csp-nonce')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function applyTo(Response $response) |
67
|
|
|
{ |
68
|
|
|
$this->configure(); |
69
|
|
|
|
70
|
|
|
$headerName = $this->reportOnly |
71
|
|
|
? 'Content-Security-Policy-Report-Only' |
72
|
|
|
: 'Content-Security-Policy'; |
73
|
|
|
|
74
|
|
|
if ($response->headers->has($headerName)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$response->headers->set($headerName, (string) $this); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function __toString() |
82
|
|
|
{ |
83
|
|
|
return collect($this->directives) |
84
|
|
|
->map(function (array $values, string $directive) { |
85
|
|
|
$valueString = implode(' ', $values); |
86
|
|
|
|
87
|
|
|
return "{$directive} {$valueString}"; |
88
|
|
|
}) |
89
|
|
|
->implode(';'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function guardAgainstInvalidDirectives(string $directive) |
93
|
|
|
{ |
94
|
|
|
if (! Directive::isValid($directive)) { |
95
|
|
|
throw InvalidDirective::notSupported($directive); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.