├── registration.php ├── etc ├── module.xml ├── di.xml ├── acl.xml └── adminhtml │ └── system.xml ├── composer.json ├── LICENSE ├── Helper └── Configuration.php ├── Block └── Adminhtml │ └── Form │ └── Field │ ├── TypeColumn.php │ └── Policies.php ├── README.md ├── Model └── Source │ └── Type.php └── Plugin └── CspWhitelist.php /registration.php: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ctidigital/magento2-csp-whitelist", 3 | "version": "1.0.1", 4 | "description": "Control CSP via the admin area", 5 | "type": "magento2-module", 6 | "license": [ 7 | "MIT" 8 | ], 9 | "autoload": { 10 | "files": [ 11 | "registration.php" 12 | ], 13 | "psr-4": { 14 | "CtiDigital\\CspWhitelist\\": "" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /etc/di.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Magento\Csp\Model\Collector\CspWhitelistXml\Data 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /etc/acl.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 CTI Digital 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Helper/Configuration.php: -------------------------------------------------------------------------------- 1 | serialize = $serialize; 27 | parent::__construct($context); 28 | } 29 | 30 | /** 31 | * check to see if module is enabled 32 | * 33 | * @return bool 34 | */ 35 | public function isEnabled(): bool 36 | { 37 | return $this->scopeConfig->isSetFlag(self::CONFIG_ENABLE); 38 | } 39 | 40 | /** 41 | * get a list of policies 42 | * 43 | * @return array 44 | */ 45 | public function getPolicies(): array 46 | { 47 | $data = []; 48 | $policies = $this->scopeConfig->getValue(self::CONFIG_POLICIES); 49 | if (!$policies) { 50 | return $data; 51 | } 52 | foreach ($this->serialize->unserialize($policies) as $policy) { 53 | $data[$policy['policy']][] = $policy['value']; 54 | } 55 | return $data; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Block/Adminhtml/Form/Field/TypeColumn.php: -------------------------------------------------------------------------------- 1 | type = $type; 26 | parent::__construct($context, $data); 27 | } 28 | 29 | /** 30 | * Set "name" for element 42 | * 43 | * @param $value 44 | * @return $this 45 | */ 46 | public function setInputId($value): TypeColumn 47 | { 48 | return $this->setId($value); 49 | } 50 | 51 | /** 52 | * Render block HTML 53 | * 54 | * @return string 55 | * @SuppressWarnings(PHPMD.CamelCaseMethodName) 56 | */ 57 | public function _toHtml(): string 58 | { 59 | if (!$this->getOptions()) { 60 | $this->setOptions($this->type->toOptionArray()); 61 | } 62 | return parent::_toHtml(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /etc/adminhtml/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 |
9 | separator-top 10 | 11 | cti 12 | CtiDigital_CspWhitelist::config 13 | 14 | 15 | 17 | 18 | Magento\Config\Model\Config\Source\Yesno 19 | 20 | 21 | 23 | 24 | CtiDigital\CspWhitelist\Block\Adminhtml\Form\Field\Policies 25 | Magento\Config\Model\Config\Backend\Serialized\ArraySerialized 26 | 27 | 28 |
29 |
30 |
31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Magento 2 CSP Whitelist 2 | A Magento 2 module created by [CTI Digital] to create and maintain Content Security Policies via the admin panel. 3 | 4 | ## Installation 5 | - `composer require ctidigital/magento2-csp-whitelist` 6 | - `php bin/magento module:enable CtiDigital_CspWhitelist` 7 | - `php bin/magento setup:upgrade` 8 | 9 | ## Usage 10 | Identify the resource blocked by the Content Security Policy: 11 | ``` 12 | Refused to load https://www.google-analytics.com/analytics.js because it does not appear in the script-src directive of the Content Security Policy. 13 | ``` 14 | 1. Take note of the resource `google-analytics.com` or `*.google-analytics.com`. 15 | 2. Check which policy it violates `script-src`. 16 | 3. Navigate to admin panel `Stores->Configuration->Cti->CSP Whitelist` 17 | 4. Ensure the module is enabled. Add a new row, select a resource and add the value. 18 | 5. Save and flush the relevant caches. 19 | 20 | ## Policies 21 | ``` 22 | POLICY NAME DESCRIPTION 23 | default-src The default policy. 24 | base-uri Defines which URLs can appear in a page’s element. 25 | child-src Defines the sources for workers and embedded frame contents. 26 | connect-src Defines the sources that can be loaded using script interfaces. 27 | font-src Defines which sources can serve fonts. 28 | form-action Defines valid endpoints for submission from
tags. 29 | frame-ancestors Defines the sources that can embed the current page. 30 | frame-src Defines the sources for elements such as and