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 @@ + + + + + + + diff --git a/view/frontend/templates/cart/buckaroo_config.phtml b/view/frontend/templates/cart/buckaroo_config.phtml index aed3c186b..bb37e98c0 100644 --- a/view/frontend/templates/cart/buckaroo_config.phtml +++ b/view/frontend/templates/cart/buckaroo_config.phtml @@ -17,10 +17,12 @@ * @copyright Copyright (c) Buckaroo B.V. * @license https://tldrlegal.com/license/mit-license */ + +/** @var string $cspNonce */ ?>
-
diff --git a/view/frontend/templates/catalog/product/view/applepay.phtml b/view/frontend/templates/catalog/product/view/applepay.phtml index 47759c26d..f6fb60822 100644 --- a/view/frontend/templates/catalog/product/view/applepay.phtml +++ b/view/frontend/templates/catalog/product/view/applepay.phtml @@ -17,12 +17,14 @@ * @copyright Copyright (c) Buckaroo B.V. * @license https://tldrlegal.com/license/mit-license */ + +/** @var string $cspNonce */ ?> canShowButton('Product')): ?>
- diff --git a/view/frontend/templates/checkout/payconiq/pay.phtml b/view/frontend/templates/checkout/payconiq/pay.phtml index 663465f40..1a65781f7 100755 --- a/view/frontend/templates/checkout/payconiq/pay.phtml +++ b/view/frontend/templates/checkout/payconiq/pay.phtml @@ -1,3 +1,25 @@ +
@@ -10,11 +32,9 @@
-getTransactionKey(); - $script = << require(['buckaroo/payconiq/pay'], function (payconiqPay) { - var transactionKey = "$key"; + var transactionKey = "getTransactionKey(); ?>"; payconiqPay.setTransactionKey(transactionKey); payconiqPay.showQrCode(); @@ -24,6 +44,4 @@ payconiqPay.cancelPayment(); }); } -JS; -?> -renderTag('script', [], $script, false); ?> \ No newline at end of file + diff --git a/view/frontend/templates/checkout/success.phtml b/view/frontend/templates/checkout/success.phtml index 0a00aa914..6d2a7d4aa 100644 --- a/view/frontend/templates/checkout/success.phtml +++ b/view/frontend/templates/checkout/success.phtml @@ -17,8 +17,10 @@ * @copyright Copyright (c) Buckaroo B.V. * @license https://tldrlegal.com/license/mit-license */ + +/** @var string $cspNonce */ ?> -