@@ -82,6 +82,8 @@ class JavascriptRenderer
82
82
83
83
protected $ openHandlerUrl ;
84
84
85
+ protected $ cspNonce ;
86
+
85
87
/**
86
88
* @param \DebugBar\DebugBar $debugBar
87
89
* @param string $baseUrl
@@ -183,6 +185,9 @@ public function setOptions(array $options)
183
185
if (array_key_exists ('open_handler_url ' , $ options )) {
184
186
$ this ->setOpenHandlerUrl ($ options ['open_handler_url ' ]);
185
187
}
188
+ if (array_key_exists ('csp_nonce ' , $ options )) {
189
+ $ this ->setCspNonce ($ options ['csp_nonce ' ]);
190
+ }
186
191
}
187
192
188
193
/**
@@ -606,6 +611,28 @@ public function getOpenHandlerUrl()
606
611
return $ this ->openHandlerUrl ;
607
612
}
608
613
614
+ /**
615
+ * Sets the CSP Nonce (or remove it by setting to null)
616
+ *
617
+ * @param string|null $nonce
618
+ * @return $this
619
+ */
620
+ public function setCspNonce ($ nonce = null )
621
+ {
622
+ $ this ->cspNonce = $ nonce ;
623
+ return $ this ;
624
+ }
625
+
626
+ /**
627
+ * Get the CSP Nonce
628
+ *
629
+ * @return string|null
630
+ */
631
+ public function getCspNonce ()
632
+ {
633
+ return $ this ->cspNonce ;
634
+ }
635
+
609
636
/**
610
637
* Add assets stored in files to render in the head
611
638
*
@@ -905,6 +932,8 @@ public function renderHead()
905
932
list ($ cssFiles , $ jsFiles , $ inlineCss , $ inlineJs , $ inlineHead ) = $ this ->getAssets (null , self ::RELATIVE_URL );
906
933
$ html = '' ;
907
934
935
+ $ nonce = $ this ->getNonceAttribute ();
936
+
908
937
foreach ($ cssFiles as $ file ) {
909
938
$ html .= sprintf ('<link rel="stylesheet" type="text/css" href="%s"> ' . "\n" , $ file );
910
939
}
@@ -918,15 +947,15 @@ public function renderHead()
918
947
}
919
948
920
949
foreach ($ inlineJs as $ content ) {
921
- $ html .= sprintf ('<script type="text/javascript">%s</script> ' . "\n" , $ content );
950
+ $ html .= sprintf ('<script type="text/javascript" %s >%s</script> ' . "\n" , $ nonce , $ content );
922
951
}
923
952
924
953
foreach ($ inlineHead as $ content ) {
925
954
$ html .= $ content . "\n" ;
926
955
}
927
956
928
957
if ($ this ->enableJqueryNoConflict && !$ this ->useRequireJs ) {
929
- $ html .= '<script type="text/javascript">jQuery.noConflict(true);</script> ' . "\n" ;
958
+ $ html .= '<script type="text/javascript" ' . $ nonce . ' >jQuery.noConflict(true);</script> ' . "\n" ;
930
959
}
931
960
932
961
return $ html ;
@@ -1013,10 +1042,12 @@ public function render($initialize = true, $renderStackedData = true)
1013
1042
$ suffix = !$ initialize ? '(ajax) ' : null ;
1014
1043
$ js .= $ this ->getAddDatasetCode ($ this ->debugBar ->getCurrentRequestId (), $ this ->debugBar ->getData (), $ suffix );
1015
1044
1045
+ $ nonce = $ this ->getNonceAttribute ();
1046
+
1016
1047
if ($ this ->useRequireJs ){
1017
- return "<script type= \"text/javascript \"> \nrequire(['debugbar'], function(PhpDebugBar){ $ js }); \n</script> \n" ;
1048
+ return "<script type= \"text/javascript \" { $ nonce } > \nrequire(['debugbar'], function(PhpDebugBar){ $ js }); \n</script> \n" ;
1018
1049
} else {
1019
- return "<script type= \"text/javascript \"> \n$ js \n</script> \n" ;
1050
+ return "<script type= \"text/javascript \" { $ nonce } > \n$ js \n</script> \n" ;
1020
1051
}
1021
1052
1022
1053
}
@@ -1149,4 +1180,17 @@ protected function getAddDatasetCode($requestId, $data, $suffix = null)
1149
1180
);
1150
1181
return $ js ;
1151
1182
}
1183
+
1184
+ /**
1185
+ * If a nonce it set, create the correct attribute
1186
+ * @return string
1187
+ */
1188
+ protected function getNonceAttribute ()
1189
+ {
1190
+ if ($ nonce = $ this ->getCspNonce ()) {
1191
+ return 'nonce=" ' . $ nonce .'" ' ;
1192
+ }
1193
+
1194
+ return '' ;
1195
+ }
1152
1196
}
0 commit comments