Skip to content

Commit 0f0ec3b

Browse files
authored
Merge pull request dokuwiki#4220 from dokuwiki/csp-nonce
CSP nonce handling
2 parents f55c501 + a77ab27 commit 0f0ec3b

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

inc/Ui/Editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function show()
150150
// start editor html output
151151
if ($wr) {
152152
// sets changed to true when previewed
153-
echo '<script>/*<![CDATA[*/textChanged = ' . ($mod ? 'true' : 'false') . '/*!]]>*/</script>';
153+
tpl_inlineScript('textChanged = ' . ($mod ? 'true' : 'false') . ';');
154154
}
155155

156156
// print intro locale text (edit, rditrev, or read.txt)

inc/template.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ function tpl_metaheaders($alt = true)
379379
}
380380
jsinfo();
381381
$script .= 'var JSINFO = ' . json_encode($JSINFO, JSON_THROW_ON_ERROR) . ';';
382+
$script .= '(function(H){H.className=H.className.replace(/\bno-js\b/,\'js\')})(document.documentElement);';
382383
$head['script'][] = ['_data' => $script];
383384

384385
// load jquery
@@ -411,39 +412,58 @@ function tpl_metaheaders($alt = true)
411412
* For tags having a body attribute specify the body data in the special
412413
* attribute '_data'. This field will NOT BE ESCAPED automatically.
413414
*
415+
* Inline scripts will use any nonce provided in the environment variable 'NONCE'.
416+
*
414417
* @param array $data
415418
*
416419
* @author Andreas Gohr <[email protected]>
417420
*/
418421
function _tpl_metaheaders_action($data)
419422
{
423+
$nonce = getenv('NONCE');
420424
foreach ($data as $tag => $inst) {
421-
if ($tag == 'script') {
422-
echo "<!--[if gte IE 9]><!-->\n"; // no scripts for old IE
423-
}
424425
foreach ($inst as $attr) {
425426
if (empty($attr)) {
426427
continue;
427428
}
429+
if ($nonce && $tag == 'script' && !empty($attr['_data'])) {
430+
$attr['nonce'] = $nonce; // add nonce to inline script tags
431+
}
428432
echo '<', $tag, ' ', buildAttributes($attr);
429433
if (isset($attr['_data']) || $tag == 'script') {
430-
if ($tag == 'script' && isset($attr['_data']))
431-
$attr['_data'] = "/*<![CDATA[*/" .
432-
$attr['_data'] .
433-
"\n/*!]]>*/";
434-
435434
echo '>', $attr['_data'] ?? '', '</', $tag, '>';
436435
} else {
437436
echo '/>';
438437
}
439438
echo "\n";
440439
}
441-
if ($tag == 'script') {
442-
echo "<!--<![endif]-->\n";
443-
}
444440
}
445441
}
446442

443+
/**
444+
* Output the given script as inline script tag
445+
*
446+
* This function will add the nonce attribute if a nonce is available.
447+
*
448+
* The script is NOT automatically escaped!
449+
*
450+
* @param string $script
451+
* @param bool $return Return or print directly?
452+
* @return string|void
453+
*/
454+
function tpl_inlineScript($script, $return = false)
455+
{
456+
$nonce = getenv('NONCE');
457+
if ($nonce) {
458+
$script = '<script nonce="' . $nonce . '">' . $script . '</script>';
459+
} else {
460+
$script = '<script>' . $script . '</script>';
461+
}
462+
463+
if ($return) return $script;
464+
echo $script;
465+
}
466+
447467
/**
448468
* Print a link
449469
*

lib/tpl/dokuwiki/detail.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<?php echo hsc(tpl_img_getTag('IPTC.Headline', $IMG))?>
2020
[<?php echo strip_tags($conf['title'])?>]
2121
</title>
22-
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
2322
<?php tpl_metaheaders()?>
2423
<meta name="viewport" content="width=device-width,initial-scale=1" />
2524
<?php echo tpl_favicon(['favicon', 'mobile']) ?>

lib/tpl/dokuwiki/main.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<head>
1919
<meta charset="utf-8" />
2020
<title><?php tpl_pagetitle() ?> [<?php echo strip_tags($conf['title']) ?>]</title>
21-
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
2221
<?php tpl_metaheaders() ?>
2322
<meta name="viewport" content="width=device-width,initial-scale=1" />
2423
<?php echo tpl_favicon(['favicon', 'mobile']) ?>

lib/tpl/dokuwiki/mediamanager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<?php echo hsc($lang['mediaselect'])?>
1919
[<?php echo strip_tags($conf['title'])?>]
2020
</title>
21-
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
2221
<?php tpl_metaheaders()?>
2322
<meta name="viewport" content="width=device-width,initial-scale=1" />
2423
<?php echo tpl_favicon(['favicon', 'mobile']) ?>

0 commit comments

Comments
 (0)