|
| 1 | +# Basecom_CspSplitHeader Magento 2 Module |
| 2 | + |
| 3 | +<div style="text-align: center;"> |
| 4 | + |
| 5 | +[![Software License][ico-license]](LICENSE) |
| 6 | +![Supported Magento Versions][ico-compatibility] |
| 7 | + |
| 8 | +</div> |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +> [!IMPORTANT] |
| 13 | +> As of Magento 2.4.7 it is no longer possible to deactivate the Magento CSP module. |
| 14 | +
|
| 15 | +With a growing _Content Security Policies_ (CSP) whitelist, the problem can arise that the |
| 16 | +headers `Content-Security-Policy-Report-Only` and/or `Content-Security-Policy` become so large that they exceed the |
| 17 | +maximum permitted size of a header field, causing the web server to not process the response any further. |
| 18 | + |
| 19 | +The CSP mechanism allows multiple policies to be specified for a resource, including via the `Content-Security-Policy` |
| 20 | +header, the `Content-Security-Policy-Report-Only` header and a `meta` |
| 21 | +element [[MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy#multiple_content_security_policies)]. |
| 22 | +__Therefore, the headers can be specified more than once.__ |
| 23 | + |
| 24 | +This is where the module comes into play. It implements an _after method plugin_ for the |
| 25 | +method `Magento\Csp\Model\Policy\Renderer\SimplePolicyHeaderRenderer::render`, which replaces the existing CSP headers |
| 26 | +via the method `\Magento\Framework\App\Response\HttpInterface::setHeader`. The header is read, split so that the syntax |
| 27 | +remains valid, and replaced by the new headers. The result is a separate header for each directive, each of which should |
| 28 | +no longer exceed the maximum permitted length of the web server. |
| 29 | + |
| 30 | +> [!TIP] |
| 31 | +> If the headers are too large even after splitting, try to identify unnecessary Magento modules and remove them. |
| 32 | +
|
| 33 | +## Installation |
| 34 | + |
| 35 | +1. Install it into your Magento 2 project with composer: |
| 36 | + |
| 37 | + ```console |
| 38 | + composer require basecom/magento2-csp-split-header |
| 39 | + ``` |
| 40 | + |
| 41 | +2. Enable module |
| 42 | + |
| 43 | + ```console |
| 44 | + bin/magento setup:upgrade |
| 45 | + ``` |
| 46 | + |
| 47 | +## Configuration |
| 48 | + |
| 49 | +| Config | Default Value | Description | |
| 50 | +|-------------------------------------------------------------|----------------|------------------------------------------------------------| |
| 51 | +| `basecom_csp_split_header/settings/header_splitting_enable` | 0 _(disabled)_ | enables (1) / disables (0) the splitting of the CSP header | |
| 52 | +| `basecom_csp_split_header/settings/max_header_size` | 8000 | maximum allowed header size | |
| 53 | + |
| 54 | +These values can be updated in the system configuration under `Basecom -> Content Security Policy -> Enable`. |
| 55 | + |
| 56 | +## Example |
| 57 | + |
| 58 | +1. CSP splitting _disabled_ |
| 59 | + |
| 60 | + ```HTTP |
| 61 | + Content-Security-Policy: default-src 'self' https://example.com; connect-src 'none'; script-src https://example.com/; |
| 62 | + ``` |
| 63 | + |
| 64 | +2. CSP splitting _enabled_ |
| 65 | + |
| 66 | + ```HTTP |
| 67 | + Content-Security-Policy: default-src 'self' https://example.com; |
| 68 | + Content-Security-Policy: connect-src 'none'; |
| 69 | + Content-Security-Policy: script-src https://example.com/; |
| 70 | + ``` |
| 71 | + |
| 72 | +## Contributing |
| 73 | + |
| 74 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 75 | + |
| 76 | +## Security |
| 77 | + |
| 78 | +If you discover any security related issues, please email <[email protected]> instead of using the issue tracker. |
| 79 | + |
| 80 | +## License |
| 81 | + |
| 82 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
| 83 | + |
| 84 | +## Copyright |
| 85 | + |
| 86 | +© 2024 basecom GmbH & Co. KG |
| 87 | + |
| 88 | +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square |
| 89 | + |
| 90 | +[ico-compatibility]: https://img.shields.io/badge/magento-2.4-brightgreen.svg?logo=magento&longCache=true&style=flat-square |
0 commit comments