Skip to content

Commit b852178

Browse files
committed
Fix csp errrors, DEV-1113
Signed-off-by: Iman Aboheydary <iman@customgento.com>
1 parent e497ed6 commit b852178

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Model/ScriptGenerator.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace CustomGento\Cookiebot\Model;
66

7+
use Magento\Csp\Helper\CspNonceProvider;
8+
79
class ScriptGenerator
810
{
911
private const COOKIEBOT_SCRIPT_FORMAT = '<script
@@ -12,35 +14,34 @@ class ScriptGenerator
1214
src="https://consent.cookiebot.com/uc.js"
1315
data-cbid="%s"
1416
%s
17+
%s
1518
type="text/javascript" async></script>';
1619
private const EU_COOKIEBOT_SCRIPT_FORMAT = '<script
1720
id="Cookiebot"
1821
data-cfasync="false"
1922
src="https://consent.cookiebot.eu/uc.js"
2023
data-cbid="%s"
2124
%s
25+
%s
2226
type="text/javascript" async></script>';
2327

24-
/**
25-
* @var Config
26-
*/
27-
private $config;
28-
29-
public function __construct(Config $config)
30-
{
31-
$this->config = $config;
28+
public function __construct(
29+
private readonly Config $config,
30+
private readonly CspNonceProvider $cspNonceProvider
31+
) {
3232
}
3333

3434
public function generate(): string
3535
{
3636
$cookiebotId = $this->config->getId();
3737
$dataCulture = $this->config->getDataCulture() ?
3838
sprintf('data-culture="%s"', $this->config->getDataCulture()) : '';
39+
$nonce = sprintf('nonce="%s"', $this->cspNonceProvider->generateNonce());
3940

4041
if ($this->config->useEuCdn()) {
41-
return sprintf(self::EU_COOKIEBOT_SCRIPT_FORMAT, $cookiebotId, $dataCulture);
42+
return sprintf(self::EU_COOKIEBOT_SCRIPT_FORMAT, $cookiebotId, $dataCulture, $nonce);
4243
}
4344

44-
return sprintf(self::COOKIEBOT_SCRIPT_FORMAT, $cookiebotId, $dataCulture);
45+
return sprintf(self::COOKIEBOT_SCRIPT_FORMAT, $cookiebotId, $dataCulture, $nonce);
4546
}
4647
}

etc/csp_whitelist.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
<policy id="script-src">
66
<values>
77
<value id="cookiebot" type="host">consent.cookiebot.com</value>
8+
<value id="cookiebot_cdn" type="host">consentcdn.cookiebot.com</value>
89
<value id="cookiebot_eu" type="host">consent.cookiebot.eu</value>
10+
<value id="cookiebot_cdn_eu" type="host">consentcdn.cookiebot.eu</value>
11+
<!-- Hash for Cookiebot inline script (nonce not fully supported by uc.js) -->
12+
<value id="cookiebot_inline" type="hash" algorithm="sha256">izGUmFn9PZE6G7QuIdXAy77nhcrcwBISVZL+PdWAZFA=</value>
913
</values>
1014
</policy>
1115
<policy id="frame-src">

0 commit comments

Comments
 (0)