Skip to content

Commit 9bbe024

Browse files
committed
giving the nonce to the different generated script tags
1 parent 0def872 commit 9bbe024

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/AnalyticCookiesCategory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function google(string $id, bool $anonymizeIp = true): static
3333
->description(__('cookieConsent::cookies.defaults._gat'))
3434
)
3535
->accepted(fn(Consent $consent) => $consent
36-
->script('<script async src="https://www.googletagmanager.com/gtag/js?id=' . $id . '"></script>')
36+
->script('<script nonce="" async src="https://www.googletagmanager.com/gtag/js?id=' . $id . '"></script>')
3737
->script(
38-
'<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag(\'js\',new Date());gtag(\'config\',\'' . $id . '\', {\'anonymize_ip\':' . $anonymizeIp . '});</script>'
38+
'<script nonce="">window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag(\'js\',new Date());gtag(\'config\',\'' . $id . '\', {\'anonymize_ip\':' . $anonymizeIp . '});</script>'
3939
)
4040
);
4141
});

src/ConsentResponse.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ConsentResponse
2626
/**
2727
* Transform the collected data into a JSON response-object.
2828
*/
29-
public function handleConsent(Cookie|CookiesGroup $instance): static
29+
public function handleConsent(Cookie|CookiesGroup $instance, string|null $nonce): static
3030
{
3131
if(! $instance->hasConsentCallback()) {
3232
return $this;
@@ -35,7 +35,7 @@ public function handleConsent(Cookie|CookiesGroup $instance): static
3535
$consent = $instance->getConsentResult();
3636

3737
$this->attachCookies($consent->getCookies());
38-
$this->attachScripts($consent->getScripts());
38+
$this->attachScripts($consent->getScripts(), $nonce);
3939

4040
return $this;
4141
}
@@ -65,20 +65,24 @@ public function attachCookie(CookieComponent $cookie): static
6565
/**
6666
* Add multiple script tags to the consent response.
6767
*/
68-
public function attachScripts(array $tags): static
68+
public function attachScripts(array $tags, string|null $nonce): static
6969
{
7070
foreach ($tags as $tag) {
71-
$this->attachScript($tag);
71+
$this->attachScript($tag, $nonce);
7272
}
73-
73+
7474
return $this;
7575
}
7676

7777
/**
7878
* Add a single script tag to the consent response.
7979
*/
80-
public function attachScript(string $tag): static
80+
public function attachScript(string $tag, ?string $nonce = null): static
8181
{
82+
if ($nonce && str_contains($tag, 'nonce=""')) {
83+
$tag = str_replace('nonce=""', 'nonce="' . $nonce . '"', $tag);
84+
}
85+
8286
$this->scripts[] = $tag;
8387

8488
return $this;

src/CookiesManager.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
class CookiesManager
1010
{
11+
protected string|null $nonce = null;
1112
/**
1213
* The cookies registrar.
1314
*/
@@ -141,13 +142,22 @@ public function accept(string|array $categories = '*'): ConsentResponse
141142
*/
142143
protected function getConsentResponse(): ConsentResponse
143144
{
144-
return array_reduce($this->registrar->getCategories(), function($response, $category) {
145-
return array_reduce($category->getDefined(), function(ConsentResponse $response, Cookie|CookiesGroup $instance) {
146-
return $this->hasConsentFor($instance->name)
147-
? $response->handleConsent($instance)
148-
: $response;
149-
}, $response);
150-
}, new ConsentResponse());
145+
$nonce = $this->nonce;
146+
return array_reduce(
147+
$this->registrar->getCategories(),
148+
function($response, $category) use ($nonce) {
149+
return array_reduce(
150+
$category->getDefined(),
151+
function(ConsentResponse $response, Cookie|CookiesGroup $instance) use ($nonce) {
152+
return $this->hasConsentFor($instance->name)
153+
? $response->handleConsent($instance, $nonce)
154+
: $response;
155+
},
156+
$response
157+
);
158+
},
159+
new ConsentResponse()
160+
);
151161
}
152162

153163
/**
@@ -169,6 +179,7 @@ protected function makeConsentCookie(): CookieComponent
169179
*/
170180
public function renderScripts(string|null $nonce, bool $withDefault = true): string
171181
{
182+
$this->nonce = $nonce;
172183
$output = $this->shouldDisplayNotice()
173184
? $this->getNoticeScripts($nonce, $withDefault)
174185
: $this->getConsentedScripts($nonce, $withDefault);
@@ -283,16 +294,14 @@ public function replaceInfoTag(string $wysiwyg): string
283294
$cookieConsentInfo = view('cookie-consent::info', [
284295
'cookies' => $this->registrar,
285296
])->render();
286-
287-
$formattedString = preg_replace(
297+
298+
return preg_replace(
288299
[
289300
'/\<(\w)[^\>]+\>\@cookieconsentinfo\<\/\1\>/',
290301
'/\@cookieconsentinfo/',
291302
],
292303
$cookieConsentInfo,
293304
$wysiwyg,
294305
);
295-
296-
return $formattedString;
297306
}
298307
}

0 commit comments

Comments
 (0)