diff --git a/src/Event/TwigGeneric.php b/src/Event/TwigGeneric.php index 686127b..6e9dad2 100644 --- a/src/Event/TwigGeneric.php +++ b/src/Event/TwigGeneric.php @@ -34,6 +34,7 @@ class TwigGeneric extends Event private string $slot = ''; private array $scripts = []; private array $slotConfig = []; + private string $nonce = ''; /** Set or reemplace banner selected. */ public function setBanner(string $banner): self @@ -104,4 +105,18 @@ public function setScripts(array $scripts): self return $this; } + + /** Get nonce (CSP) string */ + public function getNonce(): string + { + return $this->nonce; + } + + /** Set a nonce string for CSP script/style */ + public function setNonce(string $nonce): self + { + $this->nonce = $nonce; + + return $this; + } } diff --git a/src/Twig/Extension/AdvertisingGeneric.php b/src/Twig/Extension/AdvertisingGeneric.php index 75fe6bf..23b87e4 100644 --- a/src/Twig/Extension/AdvertisingGeneric.php +++ b/src/Twig/Extension/AdvertisingGeneric.php @@ -112,6 +112,15 @@ public function showScripts(?string $network = null): string return ''; } - return ''; + $attributes = array_filter([ + 'nonce' => (string) $event->getNonce(), + ]); + + $attributes = urldecode(http_build_query(array_map(fn($v) => '"'.$v.'"', $attributes), '', ' ')); + + return sprintf( + '', + $attributes + ); } }