-
Notifications
You must be signed in to change notification settings - Fork 0
/
d7csp.module
137 lines (125 loc) · 3.25 KB
/
d7csp.module
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
<?php
/**
* @file
* Implement hooks and callbacks for the d7csp module.
*/
/**
* Generate a nonce for the current page load.
*/
function d7csp_nonce(): string {
$nonce = &drupal_static(__FUNCTION__);
if (!$nonce) {
$nonce = drupal_random_key(16);
}
return $nonce;
}
/**
* Implements hook_element_info_alter().
*/
function d7csp_element_info_alter(&$type) {
$type['scripts']['#pre_render'][] = 'd7csp_pre_render_scripts';
}
/**
* Callback for #pre_render on scripts.
*
* Add a nonce attribute to the script tag.
*/
function d7csp_pre_render_scripts(array $elements) {
if (current_path() == 'system/ajax') {
return $elements;
}
$nonce = d7csp_nonce();
foreach (['scripts', 'settings'] as $type) {
foreach (element_children($elements[$type]) as $key) {
$elements[$type][$key]['#attributes']['nonce'] = $nonce;
}
}
return $elements;
}
/**
* Collect all hosts configured by modules and themes.
*
* @return array
*/
function d7csp_hosts(){
$hosts = &drupal_static(__FUNCTION__);
if (is_null($hosts )) {
$hosts = module_invoke_all('d7csp_hosts');
drupal_alter('d7csp_hosts', $hosts);
}
return $hosts;
}
/**
* Implements hook_d7csp_hosts().
*/
function d7csp_d7csp_hosts(): array {
$hosts['script-src'][] = "'nonce-" . d7csp_nonce() . "'";
return $hosts;
}
/**
* Implements hook_seckit_options_alter().
*/
function d7csp_seckit_options_alter(array &$options) {
$csp = &$options['seckit_xss']['csp'];
$hosts = d7csp_hosts();
// Merge all configured src-directives with the hook-defined hosts.
foreach ($csp as $directive => $config) {
if (substr($directive, -4) != '-src') {
continue;
}
$seckit_hosts = array_filter(explode(' ', $config));
$hosts[$directive] = array_merge($seckit_hosts, $hosts[$directive] ?? []);
}
// Merge default-src into all other src-directives.
foreach ($hosts as $directive => $host_list) {
if ($directive != 'default-src') {
$hosts[$directive] = array_merge($hosts['default-src'], $host_list);
}
}
// Put everything back together.
foreach ($hosts as $directive => $host_list) {
$csp[$directive] = ' ' . implode(' ', array_unique($host_list));
}
}
/**
* Implements hook_html_head_alter().
*/
function d7csp_html_head_alter(array &$head_elements) {
$nonce = d7csp_nonce();
foreach ($head_elements as &$element) {
if (($element['#tag'] ?? '') == 'script') {
$element['#attributes']['nonce'] = $nonce;
}
}
}
/**
* Implements hook_filter_info().
*/
function d7csp_filter_info() {
$filters = [];
$filters['d7csp_add_nonce'] = [
'title' => t('Add CSP nonces'),
'description' => t('Add a nonce to all script tags.'),
'process callback' => 'd7csp_filter_add_nonce',
'tips callback' => 'd7csp_filter_tips',
];
return $filters;
}
/**
* Callback for d7csp_add_nonce filter.
*/
function d7csp_filter_tips($filter, $format, $long = FALSE) {
return t('Put this at the end after markup has been generated.');
}
/**
* Add nonces to all script tags.
*
* Callback for d7csp_add_nonce filter.
*/
function d7csp_filter_add_nonce($text) {
$nonce = d7csp_nonce();
return preg_replace('/<script/', '<script nonce="' . $nonce . '"', $text);
}
if (module_exists('wysiwyg')) {
require_once 'modules/wysiwyg.php';
}