-
Notifications
You must be signed in to change notification settings - Fork 0
/
csp.install
166 lines (143 loc) · 4.1 KB
/
csp.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* @file
* Installation hooks for csp.module.
*/
/**
* Implements hook_requirements().
*/
function csp_requirements($phase) {
$requirements = [];
if ($phase === 'runtime') {
$cspSettingsConfig = \Drupal::config('csp.settings');
$enabledPolicies = array_filter(['report-only', 'enforce'], function ($policyTypeKey) use ($cspSettingsConfig) {
return $cspSettingsConfig->get($policyTypeKey . '.enable');
});
if (empty($enabledPolicies)) {
$requirements['csp_enabled'] = [
'title' => 'Content Security Policy',
'value' => t('No Content Security Policy headers are currently enabled.'),
'description' => t(
'Enable a header via <a href=":csp-settings">the Content Security Policy settings</a>.',
[
':csp-settings' => \Drupal::urlGenerator()
->generateFromRoute('csp.settings'),
]
),
'severity' => REQUIREMENT_WARNING,
];
}
// Warn if CSP is also enabled in Security Kit module configuration.
if (
\Drupal::moduleHandler()->moduleExists('seckit')
&&
\Drupal::config('seckit.settings')->get('seckit_xss.csp.checkbox')
) {
$requirements['csp_seckit'] = [
'title' => 'Content Security Policy - Security Kit',
'value' => t('Enabling Content Security Policy in Security Kit is likely to cause policy conflicts.'),
'description' => t(
'Disable the Content Security Policy settings in <a href=":seckit-settings">Security Kit configuration</a>.',
[
':seckit-settings' => \Drupal::urlGenerator()
->generateFromRoute('seckit.settings'),
]
),
'severity' => REQUIREMENT_WARNING,
];
}
}
return $requirements;
}
/**
* Create module configuration.
*/
function csp_update_8001() {
\Drupal::configFactory()->getEditable('csp.settings')
->set('enforce', FALSE)
->save();
}
/**
* Set default reporting settings.
*/
function csp_update_8002() {
\Drupal::configFactory()->getEditable('csp.settings')
->set('report.handler', 'csp-module')
->save();
}
/**
* Update configuration format.
*/
function csp_update_8003() {
$config = \Drupal::configFactory()->getEditable('csp.settings');
$enabledPolicy = 'report-only';
$disabledPolicy = 'enforce';
if ($config->get('enforce')) {
$enabledPolicy = 'enforce';
$disabledPolicy = 'report-only';
}
$config
->set($enabledPolicy, [
'enable' => TRUE,
'directives' => [
'script-src' => [
'base' => 'self',
'flags' => [
'unsafe-inline',
],
],
'style-src' => [
'base' => 'self',
],
],
])
->set($disabledPolicy, [
'enable' => FALSE,
])
->save();
}
/**
* Update configuration for Reporting Plugins.
*/
function csp_update_8101() {
$config = \Drupal::configFactory()->getEditable('csp.settings');
$pluginMap = [
'' => 'none',
'report-uri-com' => 'report-uri-com',
'csp-module' => 'sitelog',
'uri' => 'uri',
];
$reportConfig = $config->get('report');
$reportConfig['plugin'] = $pluginMap[$reportConfig['handler']];
unset($reportConfig['handler']);
$config
->set('report', $reportConfig)
->save();
}
/**
* Update configuration with per-policy reporting settings.
*/
function csp_update_8102() {
$config = \Drupal::configFactory()->getEditable('csp.settings');
$reportingOptions = $config->get('report');
$config->clear('report');
foreach (['enforce', 'report-only'] as $policyType) {
if (!$config->get($policyType . '.enable')) {
continue;
}
$config->set($policyType . '.reporting', $reportingOptions);
}
$config->save();
}
/**
* Remove navigate-to directive.
*/
function csp_update_8103() {
// navigate-to was not implemented in browsers and has been removed.
// @see https://github.com/w3c/webappsec-csp/pull/564
$config = \Drupal::configFactory()->getEditable('csp.settings');
foreach (['enforce', 'report-only'] as $policyType) {
$config->clear($policyType . '.directives.navigate-to');
}
$config->save();
}