hk2 / search-sanitizer
Sanitizes Magento storefront search queries by removing suspicious SQL-related keywords and characters.
HK2 Search Sanitizer
Overview
The HK2 Search Sanitizer module sanitizes storefront search queries by removing potentially harmful SQL-related keywords and special characters before they are processed by Magento's search engine. This helps reduce the impact of malformed or suspicious search input while preserving normal search functionality.
Potentially suspicious SQL-related keywords and characters (such as select,insert, update, delete, drop, union, exec, truncate etc.) are removed from storefront search queries. It intercepts search requests before they are processed by the query factory, ensuring that malicious strings do not affect
the database or application stability.
🎯 Use Cases
- Target audience: Magento merchants and developers who want an additional layer of search input sanitization.
- Business problems solved: Reduces the impact of malformed or suspicious search queries and provides visibility into modified search requests.
- Example implementation scenarios: Automatically sanitizes storefront search queries and logs any modifications for auditing.
🚀 Features
- 🛡️ Sanitizes suspicious SQL-related keywords and special characters from storefront search queries.
- 🛠 Provides configurable admin settings to enable or disable sanitization.
- 📝 Logs detected sanitized search queries for monitoring.
- 📦 Composer-installable Magento 2 module.
🏗 Architecture
- Plugin: Intercepts
Magento\Search\Model\QueryFactory::get()before the search query is created. - Logging: Uses a custom Monolog logger
HK2\SearchSanitizer\Logger\Loggerto record sanitized queries. - Configuration: Exposes a toggle in the admin panel to enable or disable the sanitization globally or per store
view.
🧩 Magento Components
- Plugins:
hk2_search_sanitizer_plugin(HK2\SearchSanitizer\Plugin\SearchSanitizer) - Virtual Types:
HK2\SearchSanitizer\Logger\Logger
📦 Requirements
- Magento version requirements: Magento Open Source 2.4.4 - 2.4.9
- Adobe Commerce compatibility: Adobe Commerce 2.4.4 - 2.4.9
- PHP requirements: 8.1, 8.2, 8.3, 8.4
- Composer requirements:
hk2/core ^1.0,magento/framework ^103.0.0
⚙️ Installation
composer require hk2/search-sanitizerbin/magento module:enable HK2_SearchSanitizerbin/magento setup:upgradebin/magento setup:di:compilebin/magento cache:flush
🔧 Configuration
Stores > Configuration > Search Sanitizer > General > Enable Search Sanitization
Usage
Once installed and enabled, the module automatically sanitizes input in the storefront search bar. If a user searches for "shoes select * from users", the query will be sanitized and processed without the SQL keywords, and a warning will be logged.
To view the logged warnings, you can check the following log file:
/var/log/hk2-search-sanitizer.log
📂 Module Structure
.
├── composer.json
├── etc
│ ├── acl.xml
│ ├── adminhtml
│ │ ├── menu.xml
│ │ └── system.xml
│ ├── config.xml
│ ├── di.xml
│ └── module.xml
├── i18n
│ ├── en_US.csv
│ ├── hi_IN.csv
│ └── ru_RU.csv
├── Logger
│ └── Handler.php
├── Plugin
│ └── SearchSanitizer.php
├── registration.php
📈 Performance Considerations
- Frontend performance impact: Minimal impact, uses simple regular expressions to sanitize search strings before processing.
🔐 Security Considerations
- Input validation: Enforces strict sanitization rules on search queries to prevent SQL injection and unauthorized operations.
Compatibility
Reference: docs/compatibility.md
| Platform | Supported |
|---|---|
| Magento Open Source 2.4.x | ✅ |
| Adobe Commerce 2.4.x | ✅ |
🛠 Troubleshooting
- Module not enabled: Verify module status using
bin/magento module:status HK2_SearchSanitizer. - Search not sanitized: Verify that the module is enabled in the configuration (
Stores > Configuration > Search Sanitizer > General > Enable Search Sanitization).
🤝 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.
Requires 3
| Package | Constraint |
|---|---|
| hk2/core | ^1.0 |
| magento/framework | ^103.0.0 |
| php | ^8.1 || ^8.2 || ^8.3 || ^8.4 |
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.
PHPStan
Type-checks the module's PHP against a real Magento install at the configured gate level. Re-runs per Magento and PHP version because resolvable symbols differ between releases.
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.
Magento 2 module to manage CSP whitelisted URLs
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.