hk2 / search-sanitizer

hk2/search-sanitizer

Sanitizes Magento storefront search queries by removing suspicious SQL-related keywords and characters.

magento2-module Compatibility: 2.4.7-2.4.9 Code Quality: Info Tests: N/A Security: Pass OSL-3.0

Are you the maintainer of hk2?

Packagento pulls hk2's Composer packages from the public registry so buyers can find them here.

Claim the namespace to take ownership, publish new releases directly, and start charging for premium versions.

Claim this namespace →

HK2 Search Sanitizer

Version
License
Magento
PHP
Downloads

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\Logger to 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

  1. composer require hk2/search-sanitizer
  2. bin/magento module:enable HK2_SearchSanitizer
  3. bin/magento setup:upgrade
  4. bin/magento setup:di:compile
  5. bin/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:

License

This project is licensed under the OSL 3.0 License. See the LICENSE.txt file for details.

1.0.0 (2026-07-21)

⚠ BREAKING CHANGES

  • add HK2 Search Sanitizer module to sanitize storefront search queries

✨ Features

  • add HK2 Search Sanitizer module to sanitize storefront search queries (55c2180)

📝 Documentation

  • update README with detailed module overview and add SECURITY policy (dab41a1)
Versions
Version Stability QA Status Compatibility Released
1.0.0 stable Pass Magento 2.4.7-2.4.9 Details 2026-07-21 12:58:34

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.

Compatibility matrix (Magento × PHP)
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Pass Pass
2.4.8 Pass Pass
2.4.9 Pass Pass

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.

Static analysis results
Tool Status Findings Summary
PHPCS Pass 0
PHPMD Pass 0
Cpd Pass 0
Composer validate Info 1 valid; 1 advisory note (composer validate --strict)

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.

PHPStan results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Pass Pass
2.4.8 Pass Pass
2.4.9 Pass Pass

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

Unit tests results by Magento and PHP version
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

Integration tests results by Magento and PHP version
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.

Security results
Tool Status Findings Summary
Composer audit Pass 0
Malware scan Pass 0
License
OSL-3.0
Authors

More from hk2

View vendor
Make it pay

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.