File tree Expand file tree Collapse file tree 4 files changed +89
-0
lines changed Expand file tree Collapse file tree 4 files changed +89
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Copyright 2024 Adobe
4+ * All Rights Reserved.
5+ */
6+ declare (strict_types=1 );
7+
8+ namespace Magento \Csp \ViewModel ;
9+
10+ use Magento \Csp \Helper \CspNonceProvider ;
11+ use Magento \Framework \Exception \LocalizedException ;
12+ use Magento \Framework \View \Element \Block \ArgumentInterface ;
13+
14+ /**
15+ * This class provides a nonce for the Content Security Policy (CSP) header.
16+ */
17+ class NonceProvider implements ArgumentInterface
18+ {
19+ /**
20+ * @var CspNonceProvider
21+ */
22+ private $ cspNonceProvider ;
23+
24+ /**
25+ * @param CspNonceProvider $cspNonceProvider
26+ */
27+ public function __construct (
28+ CspNonceProvider $ cspNonceProvider ,
29+ ) {
30+ $ this ->cspNonceProvider = $ cspNonceProvider ;
31+ }
32+
33+ /**
34+ * Returns a nonce for the Content Security Policy (CSP) header.
35+ *
36+ * @return string
37+ * @throws LocalizedException
38+ */
39+ public function getNonce (): string
40+ {
41+ return $ this ->cspNonceProvider ->generateNonce ();
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Copyright 2024 Adobe
3+ * All Rights Reserved.
4+ */
5+
6+ var config = {
7+ map : {
8+ '*' : {
9+ 'nonceInjector' : 'Magento_Csp/js/nonce-injector'
10+ }
11+ }
12+ } ;
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Copyright 2024 Adobe
4+ * All Rights Reserved.
5+ */
6+
7+ /**
8+ * @var \Magento\Framework\Escaper $escaper
9+ */
10+
11+ $ nonceProvider = $ block ->getNonceProvider ();
12+
13+ ?>
14+ <script type="text/x-magento-init">
15+ {
16+ "*": {
17+ "nonceInjector": {
18+ "nonce": "<?= $ escaper ->escapeJs ($ nonceProvider ->getNonce ()); ?> "
19+ }
20+ }
21+ }
22+ </script>
Original file line number Diff line number Diff line change 1+ /**
2+ * Copyright 2024 Adobe
3+ * All Rights Reserved.
4+ */
5+
6+ define ( 'Magento_Csp/js/nonce-injector' , [ ] , function ( ) {
7+ 'use strict' ;
8+
9+ return function ( config ) {
10+ window . cspNonce = config . nonce ;
11+ } ;
12+ } ) ;
You can’t perform that action at this time.
0 commit comments