|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\Security; |
|
10
|
|
|
|
|
11
|
|
|
use Waca\SiteConfiguration; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class ContentSecurityPolicyManager |
|
14
|
|
|
{ |
|
15
|
|
|
private $policy = [ |
|
16
|
|
|
'default-src' => [], |
|
17
|
|
|
'script-src' => ['self', 'nonce'], |
|
18
|
|
|
'script-src-elem' => ['self', 'nonce'], |
|
19
|
|
|
'script-src-attr' => [], |
|
20
|
|
|
'connect-src' => ['self'], |
|
21
|
|
|
'style-src' => ['self'], |
|
22
|
|
|
'style-src-elem' => ['self'], |
|
23
|
|
|
'style-src-attr' => [], |
|
24
|
|
|
'img-src' => ['self', 'data:', 'https://upload.wikimedia.org', 'https://accounts-dev.wmflabs.org/'], |
|
25
|
|
|
'font-src' => ['self'], |
|
26
|
|
|
'form-action' => ['self', 'oauth'], |
|
27
|
|
|
'frame-ancestors' => [], |
|
28
|
|
|
]; |
|
29
|
|
|
private $nonce = null; |
|
30
|
|
|
private $reportOnly = false; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var SiteConfiguration |
|
33
|
|
|
*/ |
|
34
|
|
|
private $configuration; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* ContentSecurityPolicyManager constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param SiteConfiguration $configuration |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(SiteConfiguration $configuration) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->configuration = $configuration; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getNonce() |
|
47
|
|
|
{ |
|
48
|
|
|
if ($this->nonce === null) { |
|
49
|
|
|
$this->nonce = base64_encode(openssl_random_pseudo_bytes(32)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->nonce; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getHeader(): string |
|
56
|
|
|
{ |
|
57
|
|
|
$reportOnly = ''; |
|
58
|
|
|
if ($this->reportOnly) { |
|
59
|
|
|
$reportOnly = '-Report-Only'; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$constructedPolicy = "Content-Security-Policy{$reportOnly}: "; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->policy as $item => $values) { |
|
65
|
|
|
$constructedPolicy .= $item . ' '; |
|
66
|
|
|
$policyIsSet = false; |
|
67
|
|
|
|
|
68
|
|
|
if (count($values) > 0) { |
|
69
|
|
|
foreach ($values as $value) { |
|
70
|
|
|
switch ($value) { |
|
71
|
|
|
case 'none': |
|
72
|
|
|
case 'self': |
|
73
|
|
|
case 'strict-dynamic': |
|
74
|
|
|
$policyIsSet = true; |
|
75
|
|
|
$constructedPolicy .= "'{$value}' "; |
|
76
|
|
|
break; |
|
77
|
|
|
case 'nonce': |
|
78
|
|
|
if ($this->nonce !== null) { |
|
79
|
|
|
$policyIsSet = true; |
|
80
|
|
|
$constructedPolicy .= "'nonce-{$this->nonce}' "; |
|
81
|
|
|
} |
|
82
|
|
|
break; |
|
83
|
|
|
case 'oauth': |
|
84
|
|
|
$policyIsSet = true; |
|
85
|
|
|
$constructedPolicy .= "{$this->configuration->getOauthMediaWikiCanonicalServer()} "; |
|
86
|
|
|
break; |
|
87
|
|
|
default: |
|
88
|
|
|
$policyIsSet = true; |
|
89
|
|
|
$constructedPolicy .= $value . ' '; |
|
90
|
|
|
break; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!$policyIsSet) { |
|
95
|
|
|
$constructedPolicy .= "'none' "; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
else { |
|
99
|
|
|
$constructedPolicy .= "'none' "; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$constructedPolicy .= '; '; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if ($this->configuration->getCspReportUri() !== null) { |
|
106
|
|
|
$constructedPolicy .= 'report-uri ' . $this->configuration->getCspReportUri(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $constructedPolicy; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths