-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1113 from buckaroo-it/BP-3689-Add-CSP-Content-Sec…
…urity-Policy-improvements Bp 3689 add csp content security policy improvements
- Loading branch information
Showing
12 changed files
with
280 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Buckaroo\Magento2\Factory; | ||
|
||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\Csp\Helper\CspNonceProvider as MagentoCspNonceProvider; | ||
use Buckaroo\Magento2\Helper\CustomCspNonceProvider; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Factory to provide the appropriate CspNonceProvider | ||
*/ | ||
class CspNonceProviderFactory | ||
{ | ||
/** | ||
* @var ObjectManagerInterface | ||
*/ | ||
private ObjectManagerInterface $objectManager; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private LoggerInterface $logger; | ||
|
||
public function __construct( | ||
ObjectManagerInterface $objectManager, | ||
LoggerInterface $logger | ||
) { | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Buckaroo\Magento2\Helper; | ||
|
||
use Magento\Framework\Math\Random; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Csp\Model\Collector\DynamicCollector; | ||
use Magento\Csp\Model\Policy\FetchPolicy; | ||
|
||
/** | ||
* Custom CSP Nonce Provider for Magento versions without Magento\Csp\Helper\CspNonceProvider | ||
*/ | ||
class CustomCspNonceProvider | ||
{ | ||
private const NONCE_LENGTH = 32; | ||
|
||
private string $nonce; | ||
|
||
private Random $random; | ||
|
||
private DynamicCollector $dynamicCollector; | ||
|
||
public function __construct( | ||
Random $random, | ||
DynamicCollector $dynamicCollector | ||
) { | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Buckaroo\Magento2\Observer; | ||
|
||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\View\Element\Template; | ||
use Buckaroo\Magento2\Factory\CspNonceProviderFactory; | ||
|
||
class AddCspNonce implements ObserverInterface | ||
{ | ||
/** | ||
* @var \Magento\Csp\Helper\CspNonceProvider|\Buckaroo\Magento2\Helper\CustomCspNonceProvider|null | ||
*/ | ||
private $cspNonceProvider; | ||
|
||
public function __construct( | ||
CspNonceProviderFactory $cspNonceProviderFactory | ||
) { | ||
$this->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) { | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the MIT License | ||
* It is available through the world-wide-web at this URL: | ||
* https://tldrlegal.com/license/mit-license | ||
* If you are unable to obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer | ||
* versions in the future. If you wish to customize this module for your | ||
* needs please contact [email protected] for more information. | ||
* | ||
* @copyright Copyright (c) Buckaroo B.V. | ||
* @license https://tldrlegal.com/license/mit-license | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="view_block_abstract_to_html_before"> | ||
<observer name="buckaroo_magento2_update_order_status" instance="Buckaroo\Magento2\Observer\AddCspNonce" /> | ||
</event> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters