Skip to content

Commit 92c8c57

Browse files
committed
Merge branch 'ACP2E-3308' of https://github.com/adobe-commerce-tier-4/magento2ce into Tier4-09-12-2024
2 parents e3e8224 + 278b9d6 commit 92c8c57

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
});

0 commit comments

Comments
 (0)