@@ -411,15 +411,17 @@ public function enabled(): bool
411411 */
412412 public function getStyleNonce (): string
413413 {
414+ if (! $ this ->enableStyleNonce ) {
415+ $ this ->styleNonce = null ;
416+ return '' ;
417+ }
418+
414419 if ($ this ->styleNonce === null ) {
415420 $ this ->styleNonce = base64_encode (random_bytes (12 ));
421+ $ this ->addStyleSrc ('nonce- ' . $ this ->styleNonce );
416422
417- if ($ this ->enableStyleNonce ) {
418- $ this ->addStyleSrc ('nonce- ' . $ this ->styleNonce );
419-
420- if ($ this ->styleSrcElem !== []) {
421- $ this ->addStyleSrcElem ('nonce- ' . $ this ->styleNonce );
422- }
423+ if ($ this ->styleSrcElem !== []) {
424+ $ this ->addStyleSrcElem ('nonce- ' . $ this ->styleNonce );
423425 }
424426 }
425427
@@ -431,15 +433,17 @@ public function getStyleNonce(): string
431433 */
432434 public function getScriptNonce (): string
433435 {
436+ if (! $ this ->enableScriptNonce ) {
437+ $ this ->scriptNonce = null ;
438+ return '' ;
439+ }
440+
434441 if ($ this ->scriptNonce === null ) {
435442 $ this ->scriptNonce = base64_encode (random_bytes (12 ));
443+ $ this ->addScriptSrc ('nonce- ' . $ this ->scriptNonce );
436444
437- if ($ this ->enableScriptNonce ) {
438- $ this ->addScriptSrc ('nonce- ' . $ this ->scriptNonce );
439-
440- if ($ this ->scriptSrcElem !== []) {
441- $ this ->addScriptSrcElem ('nonce- ' . $ this ->scriptNonce );
442- }
445+ if ($ this ->scriptSrcElem !== []) {
446+ $ this ->addScriptSrcElem ('nonce- ' . $ this ->scriptNonce );
443447 }
444448 }
445449
@@ -963,7 +967,20 @@ protected function generateNonces(ResponseInterface $response)
963967 return '' ;
964968 }
965969
966- $ nonce = $ match [0 ] === $ this ->styleNonceTag ? $ this ->getStyleNonce () : $ this ->getScriptNonce ();
970+ if ($ match [0 ] === $ this ->styleNonceTag ) {
971+ if (! $ this ->enableStyleNonce ) {
972+ return '' ;
973+ }
974+
975+ $ nonce = $ this ->getStyleNonce ();
976+ } else {
977+ if (! $ this ->enableScriptNonce ) {
978+ return '' ;
979+ }
980+
981+ $ nonce = $ this ->getScriptNonce ();
982+ }
983+
967984 $ attr = 'nonce=" ' . $ nonce . '" ' ;
968985
969986 return $ jsonEscape ? str_replace ('" ' , '\\" ' , $ attr ) : $ attr ;
0 commit comments