hk2 / csp
Magento 2 module to manage CSP whitelisted URLs
HK2 CSP Whitelisting
Overview
HK2 CSP is a Magento 2 extension that enables administrators to manage Content Security Policy (CSP) whitelists dynamically. In addition to a robust set of pre-configured whitelist hosts for third-party scripts, styles, fonts, images, frames, and connections, it provides an admin interface to add custom domains without touching configuration files.
🎯 Use Cases
- Third-Party Integrations: Safe whitelisting for Google Analytics, Stripe, Facebook, and other standard services.
- Admin Management: Direct, safe administrative management of CSP hosts dynamically.
🚀 Features
- 🛡 Declares standard CSP policy parameters in
etc/csp.xml. - 📦 Out-of-the-box whitelists for popular services (Google, Facebook, Stripe, Hotjar, Sentry, etc.).
- ⚙️ Dynamic admin configurations for directive hosts (
script-src,style-src,img-src,connect-src,font-src,frame-src). - 🔌 Backend validation plugin preventing invalid hosts or header injections.
- 🔄 One-click dynamic "Reset CSP to Defaults" button via Ajax backend redirect.
🏗 Architecture
- Plugin:
HK2\Csp\Plugin\PolicyListPlugininterceptsMagento\Csp\Model\Policy\PolicyList::getAllPoliciesto dynamically merge administrative hosts. - Controller:
HK2\Csp\Controller\Adminhtml\Reset\ResetConfighandles configuration wipe operations.
🧩 Magento Components
Blocks
HK2\Csp\Block\Adminhtml\System\Config\ResetButton
Plugins
HK2\Csp\Plugin\PolicyListPluginonMagento\Csp\Model\Policy\PolicyList
Controllers
HK2\Csp\Controller\Adminhtml\Reset\ResetConfig
📦 Requirements
- Magento version: 2.4.4 - 2.4.9
- PHP requirements: 8.1 || 8.2 || 8.3 || 8.4
- Required Extension:
HK2_Core
⚙️ Installation
composer require hk2/cspbin/magento module:enable HK2_Cspbin/magento setup:upgradebin/magento setup:di:compilebin/magento cache:flush
🔧 Configuration
Configure settings under Stores > Configuration > HK2 > CSP Whitelisting:
| Field | Description |
|---|---|
| Script Src URLs | Comma-separated list of script host domains (e.g. *.example.com). |
| Style Src URLs | Comma-separated list of style host domains. |
| Image Src URLs | Comma-separated list of image host domains. |
| Connect Src URLs | Comma-separated list of connect/API host domains. |
| Font Src URLs | Comma-separated list of font host domains. |
| Frame Src URLs | Comma-separated list of frame/iframe host domains. |
| Reset CSP | Button to clear all scopes and restore default whitelists. |
Usage
Navigate to Stores > Configuration > HK2 > CSP Whitelisting, populate domains matching your external integrations, and save. Run bin/magento cache:flush to apply.
🗄 Database Changes
Not Applicable
📂 Module Structure
Block/
└── Adminhtml/
└── System/
└── Config/
└── ResetButton.php
Controller/
└── Adminhtml/
└── Reset/
└── ResetConfig.php
Plugin/
└── PolicyListPlugin.php
etc/
├── adminhtml/
│ ├── menu.xml
│ ├── routes.xml
│ └── system.xml
├── acl.xml
├── config.xml
├── csp.xml
├── csp_whitelist.xml
├── di.xml
└── module.xml
view/
└── adminhtml/
└── templates/
└── system/
└── config/
└── reset_button.phtml
📈 Performance Considerations
The policies list is cached inside Magento's standard configuration cache, preventing database queries or regex calculations on standard page loads.
🔐 Security Considerations
- Input Validation:
isValidCspHostprevents semicolons, commas, and white spaces in custom values, protecting the headers against header splitting or policy injection attacks.
Compatibility
Reference: docs/compatibility.md
| Platform | Supported Versions |
|---|---|
| Magento | 2.4.4 - 2.4.9 |
| PHP | 8.1, 8.2, 8.3, 8.4 |
🛠 Troubleshooting
Dynamic values not appearing in CSP header
Verify you have flushed the configuration cache after making changes: bin/magento cache:flush.
🤝 Contributing
Contributions are welcome! If you'd like to improve the installer:
- ⭐ Star this repository (Helps others find it!)
- 🍴 Fork the project
- 🐛 Report bugs
- 💡 Suggest new features
- 🤝 Contribute improvements
Every ⭐ helps increase the visibility of the project and motivates further development.
⚖️ Disclaimer
The author provides this installation script "as is" without any warranties. Users are responsible for ensuring that running this script complies with their internal security and software requirements.
🤝 Support
For bug reports, feature requests, and general support:
- Author: Basant Mandal
- Email: [email protected]
- Website: https://www.basantmandal.in
License
This project is licensed under the OSL 3.0 License. See the LICENSE.txt file for details.
2.1.0 (2026-07-16)
✨ Features
- update placeholder version in bug report template (a7c10b9)
1.0.0 (2026-07-16)
⚠ BREAKING CHANGES
- Implement CSP Whitelisting Module
✨ Features
- add contributing guide, bug report template, and pull request template for improved collaboration (85d915d)
- add localization files for English, Hindi, and Russian languages (3f71f73)
- add semantic release configuration for automated versioning and changelog generation (a90c684)
- addition of Installation Guide (060225b)
- Implement CSP Whitelisting Module (b23e35f)
📝 Documentation
- update README.md to enhance clarity and structure, including use cases and features (ca79814)
1.0.0 (2026-07-16)
⚠ BREAKING CHANGES
- Implement CSP Whitelisting Module
✨ Features
- add contributing guide, bug report template, and pull request template for improved collaboration (85d915d)
- add localization files for English, Hindi, and Russian languages (3f71f73)
- add semantic release configuration for automated versioning and changelog generation (a90c684)
- Implement CSP Whitelisting Module (b23e35f)
📝 Documentation
- update README.md to enhance clarity and structure, including use cases and features (ca79814)
| Version | Stability | QA Status | Compatibility | Released |
|---|---|---|---|---|
| v2.1.0 | stable | Not tested | Not yet tested Details | 2026-07-16 15:40:32 |
| v2.0.0 | stable | Not tested | Not yet tested Details | 2026-07-16 15:36:30 |
| 1.0.3 | stable | Fail | Magento 2.4.7-2.4.9 Details | 2024-12-25 13:11:02 |
| 1.0.2 | stable | Not tested | Not yet tested Details | 2023-07-15 12:12:07 |
| 1.0.0 | stable | Not tested | Not yet tested Details | 2022-06-16 05:29:15 |
Requires 2
| Package | Constraint |
|---|---|
| magento/framework | >=100.0.0 |
| php | ^7.3.0|^7.4.0|^8.0|^8.1.0|^8.2.0 |
Compatibility
Each Magento release line is installed on its supported PHP versions, then the module is built (DI compilation + static-content deploy) and its unit and integration suites are run. The matrix shows the lines and PHP versions the module is confirmed to install and run on. Code-quality results further down (phpstan, phpcs, …) are reported separately and never affect compatibility.
Code Quality
Advisory checks against the module's source. Static analysis runs once across the whole module; PHPStan re-runs per Magento + PHP version because resolvable symbols differ between releases. These NEVER affect the Compatibility badge. A phpcs finding can't make a module incompatible.
Static analysis
Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.
Tests
Unit and integration suites, run for each applicable Magento and PHP version. A test failure speaks to the module's behaviour, not its compatibility with a Magento line, so it is reported here separately and never reddens the compatibility matrix.
Unit tests
| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | N/A | N/A | ||
| 2.4.8 | N/A | N/A | ||
| 2.4.9 | N/A | N/A |
Integration tests
| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | N/A | N/A | ||
| 2.4.8 | N/A | N/A | ||
| 2.4.9 | N/A | N/A |
Security
Security checks run directly against the module: an audit of its declared dependencies for known vulnerabilities (composer audit) and a scan of its source for malware and web-shell signatures. Each runs once. A malware detection fails the version outright.
More from hk2
View vendorMagento 2 Marketing Analytics: Google Analytics, Facebook Pixel, Product Feed & Conversion Tracking
HK2 Core module - The foundational base module for all Magento 2 HK2 extensions.
Sanitizes Magento storefront search queries by removing suspicious SQL-related keywords and characters.
Turn an existing module into recurring revenue.
If you already maintain a Magento 2 module on GitHub or GitLab, listing it on Packagento takes about five minutes. We mirror your tags, handle distribution signing, and route paid licenses through Stripe Connect, so you can keep shipping the way you already do.