diff --git a/Block/Cart/BuckarooConfig.php b/Block/Cart/BuckarooConfig.php
index 416e29e35..89c3eae1a 100644
--- a/Block/Cart/BuckarooConfig.php
+++ b/Block/Cart/BuckarooConfig.php
@@ -71,4 +71,14 @@ public function getBuckarooConfigJson()
$configProvider = $this->configProviderFactory->get('buckaroo_fee');
return $this->jsonEncoder->serialize($configProvider->getConfig());
}
+
+ /**
+ * Get CSP nonce
+ *
+ * @return string
+ */
+ public function getCspNonce()
+ {
+ return $this->getData('cspNonce') ?: '';
+ }
}
diff --git a/Block/Checkout/Success.php b/Block/Checkout/Success.php
index 75f5d81f5..25ae1a0d0 100644
--- a/Block/Checkout/Success.php
+++ b/Block/Checkout/Success.php
@@ -28,7 +28,7 @@ class Success extends \Magento\Checkout\Block\Onepage\Success
protected $currentCustomer;
/**
- * @param Template\Context $context
+ * @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Sales\Model\Order\Config $orderConfig
* @param \Magento\Framework\App\Http\Context $httpContext
@@ -52,4 +52,14 @@ public function __construct(
);
$this->currentCustomer = $currentCustomer;
}
+
+ /**
+ * Get CSP nonce
+ *
+ * @return string
+ */
+ public function getCspNonce()
+ {
+ return $this->getData('cspNonce') ?: '';
+ }
}
diff --git a/Factory/CspNonceProviderFactory.php b/Factory/CspNonceProviderFactory.php
new file mode 100644
index 000000000..2f1b49f1d
--- /dev/null
+++ b/Factory/CspNonceProviderFactory.php
@@ -0,0 +1,63 @@
+objectManager = $objectManager;
+ $this->logger = $logger;
+ }
+
+ /**
+ * Create an instance of CspNonceProvider
+ *
+ * @return MagentoCspNonceProvider|CustomCspNonceProvider|null
+ */
+ public function create()
+ {
+ // Attempt to use Magento's CspNonceProvider if it exists
+ if (class_exists(MagentoCspNonceProvider::class)) {
+ try {
+ return $this->objectManager->get(MagentoCspNonceProvider::class);
+ } catch (\Exception $e) {
+ $this->logger->error('Failed to instantiate Magento CspNonceProvider: ' . $e->getMessage());
+ }
+ }
+
+ // Fallback to custom CspNonceProvider
+ if (class_exists(CustomCspNonceProvider::class)) {
+ try {
+ return $this->objectManager->get(CustomCspNonceProvider::class);
+ } catch (\Exception $e) {
+ $this->logger->error('Failed to instantiate Custom CspNonceProvider: ' . $e->getMessage());
+ }
+ }
+
+ // If neither class is available, log a warning
+ $this->logger->warning('No CspNonceProvider available.');
+ return null;
+ }
+}
diff --git a/Helper/CustomCspNonceProvider.php b/Helper/CustomCspNonceProvider.php
new file mode 100644
index 000000000..246921d68
--- /dev/null
+++ b/Helper/CustomCspNonceProvider.php
@@ -0,0 +1,63 @@
+random = $random;
+ $this->dynamicCollector = $dynamicCollector;
+ }
+
+ /**
+ * Generate nonce and add it to the CSP header
+ *
+ * @return string
+ * @throws LocalizedException
+ */
+ public function generateNonce(): string
+ {
+ if (empty($this->nonce)) {
+ $this->nonce = $this->random->getRandomString(
+ self::NONCE_LENGTH,
+ Random::CHARS_DIGITS . Random::CHARS_LOWERS
+ );
+
+ $policy = new FetchPolicy(
+ 'script-src',
+ false,
+ [],
+ [],
+ false,
+ false,
+ false,
+ [$this->nonce],
+ []
+ );
+
+ $this->dynamicCollector->add($policy);
+ }
+
+ return base64_encode($this->nonce);
+ }
+}
diff --git a/Observer/AddCspNonce.php b/Observer/AddCspNonce.php
new file mode 100644
index 000000000..b399e557a
--- /dev/null
+++ b/Observer/AddCspNonce.php
@@ -0,0 +1,46 @@
+cspNonceProvider = $cspNonceProviderFactory->create();
+ }
+
+ public function execute(Observer $observer)
+ {
+ /** @var Template $block */
+ $block = $observer->getEvent()->getBlock();
+ if (false === $block instanceof Template) {
+ return;
+ }
+
+ // Retrieve the block name
+ $nameInLayout = $block->getNameInLayout();
+ // Check if $nameInLayout is a non-empty string
+ if (!is_string($nameInLayout) || strpos($nameInLayout, 'buckaroo_magento2') === false) {
+ return;
+ }
+
+ if ($this->cspNonceProvider) {
+ try {
+ $nonce = $this->cspNonceProvider->generateNonce();
+ $block->assign('cspNonce', $nonce);
+ } catch (\Exception $e) {
+ }
+ }
+ }
+}
diff --git a/etc/frontend/events.xml b/etc/frontend/events.xml
new file mode 100644
index 000000000..77833e767
--- /dev/null
+++ b/etc/frontend/events.xml
@@ -0,0 +1,26 @@
+
+
+