|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Basecom\CspSplitHeader\Plugin\Model\Policy\Renderer; |
| 4 | + |
| 5 | +use Basecom\CspSplitHeader\Model\Config; |
| 6 | +use Basecom\CspSplitHeader\Plugin\Model\Policy\PolicyHeader; |
| 7 | +use Laminas\Http\AbstractMessage; |
| 8 | +use Laminas\Http\Header\ContentSecurityPolicy; |
| 9 | +use Laminas\Http\Header\ContentSecurityPolicyReportOnly; |
| 10 | +use Laminas\Http\Header\HeaderInterface; |
| 11 | +use Laminas\Loader\PluginClassLoader; |
| 12 | +use Magento\Csp\Api\Data\PolicyInterface; |
| 13 | +use Magento\Csp\Model\Policy\Renderer\SimplePolicyHeaderRenderer; |
| 14 | +use Magento\Framework\App\Response\HttpInterface as HttpResponse; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Plugin for Simple Policy Header |
| 19 | + */ |
| 20 | +class CspHeaderSplitter |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly LoggerInterface $logger, |
| 24 | + private readonly Config $config, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + private const PLUGINS = [ |
| 29 | + 'contentsecuritypolicyreportonly' => ContentSecurityPolicyReportOnly::class, |
| 30 | + 'contentsecuritypolicy' => ContentSecurityPolicy::class, |
| 31 | + ]; |
| 32 | + |
| 33 | + private array $contentHeaders = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 37 | + * @param null $result |
| 38 | + */ |
| 39 | + public function afterRender( |
| 40 | + SimplePolicyHeaderRenderer $subject, |
| 41 | + $result, |
| 42 | + PolicyInterface $policy, |
| 43 | + HttpResponse $response |
| 44 | + ): void { |
| 45 | + $headerName = $this->getHeaderName($response); |
| 46 | + /** @var HeaderInterface $header */ |
| 47 | + $header = $response->getHeader($headerName); |
| 48 | + $policyValue = $header->getFieldValue(); |
| 49 | + $isHeaderSplittingEnabled = $this->config->isHeaderSplittingEnabled(); |
| 50 | + |
| 51 | + $maxHeaderSize = $this->config->getMaxHeaderSize(); |
| 52 | + $currentHeaderSize = strlen($policyValue); |
| 53 | + |
| 54 | + if ($isHeaderSplittingEnabled) { |
| 55 | + $this->registerCspHeaderPlugins($response); |
| 56 | + $this->splitUpCspHeaders($response, $policy->getId(), $policyValue); |
| 57 | + } else { |
| 58 | + if ($maxHeaderSize >= $currentHeaderSize) { |
| 59 | + $response->setHeader($headerName, $policyValue, true); |
| 60 | + } else { |
| 61 | + $this->logger->error( |
| 62 | + sprintf( |
| 63 | + 'Unable to set the CSP header. The header size of %d bytes exceeds the '. |
| 64 | + 'maximum size of %d bytes.', |
| 65 | + $currentHeaderSize, |
| 66 | + $maxHeaderSize |
| 67 | + ) |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * The CSP headers normally use the GenericHeader class, which does not support multi-header values. |
| 75 | + * The Laminas framework includes multi-value supported special classes for CSP headers. |
| 76 | + * With this registration we enable the usage of the special classes by registering the definitions to the |
| 77 | + * plugin loader class. |
| 78 | + */ |
| 79 | + private function registerCspHeaderPlugins(HttpResponse $response): void |
| 80 | + { |
| 81 | + /** @var AbstractMessage $response */ |
| 82 | + /** @var PluginClassLoader $pluginClassLoader */ |
| 83 | + $pluginClassLoader = $response->getHeaders()->getPluginClassLoader(); |
| 84 | + $pluginClassLoader->registerPlugins(self::PLUGINS); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Make sure that the CSP headers are handled as several headers ("multi-header") |
| 89 | + */ |
| 90 | + private function splitUpCspHeaders(HttpResponse $response, string $policyId, string $policyValue): void |
| 91 | + { |
| 92 | + $headerName = $this->getHeaderName($response); |
| 93 | + |
| 94 | + if (!$headerName) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + $newHeader = $policyId.' '.$policyValue.';'; |
| 99 | + $maxHeaderSize = $this->config->getMaxHeaderSize(); |
| 100 | + $newHeaderSize = strlen($policyValue); |
| 101 | + |
| 102 | + if ($newHeaderSize <= $maxHeaderSize) { |
| 103 | + $this->contentHeaders[] = $newHeader; |
| 104 | + } else { |
| 105 | + $this->logger->error( |
| 106 | + sprintf( |
| 107 | + 'Unable to set the CSP header. The header size of %d bytes exceeds the '. |
| 108 | + 'maximum size of %d bytes.', |
| 109 | + $newHeaderSize, |
| 110 | + $maxHeaderSize |
| 111 | + ) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + foreach ($this->contentHeaders as $i => $headerPart) { |
| 116 | + $isFirstEntry = ($i === 0); |
| 117 | + $response->setHeader($headerName, $headerPart.';', $isFirstEntry); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private function getHeaderName(HttpResponse $response): string |
| 122 | + { |
| 123 | + $headerName = ''; |
| 124 | + foreach (PolicyHeader::HEADER_NAMES as $name) { |
| 125 | + $headerContent = $response->getHeader($name); |
| 126 | + if ($headerContent) { |
| 127 | + $headerName = $name; |
| 128 | + } |
| 129 | + } |
| 130 | + return $headerName; |
| 131 | + } |
| 132 | +} |
0 commit comments