Skip to content

Commit 8a3bab7

Browse files
authored
Add nonce to <script> elements (#1464)
1 parent 88aef02 commit 8a3bab7

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/JavascriptRenderer.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@ public function renderHead()
6363
$cssRoute = preg_replace('/\Ahttps?:/', '', $cssRoute);
6464
$jsRoute = preg_replace('/\Ahttps?:/', '', $jsRoute);
6565

66+
$nonce = $this->getNonceAttribute();
67+
6668
$html = "<link rel='stylesheet' type='text/css' property='stylesheet' href='{$cssRoute}' data-turbolinks-eval='false' data-turbo-eval='false'>";
67-
$html .= "<script src='{$jsRoute}' data-turbolinks-eval='false' data-turbo-eval='false'></script>";
69+
$html .= "<script{$nonce} src='{$jsRoute}' data-turbolinks-eval='false' data-turbo-eval='false'></script>";
6870

6971
if ($this->isJqueryNoConflictEnabled()) {
70-
$html .= '<script data-turbo-eval="false">jQuery.noConflict(true);</script>' . "\n";
72+
$html .= "<script{$nonce} data-turbo-eval='false'>jQuery.noConflict(true);</script>" . "\n";
7173
}
7274

73-
$html .= $this->getInlineHtml();
75+
$inlineHtml = $this->getInlineHtml();
76+
if ($nonce != '') {
77+
$inlineHtml = preg_replace("/<script>/", "<script{$nonce}>", $inlineHtml);
78+
}
79+
$html .= $inlineHtml;
7480

7581

7682
return $html;

0 commit comments

Comments
 (0)