# hk2/search-sanitizer

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

`composer require hk2/search-sanitizer`

Canonical URL: https://packagento.com/hk2/search-sanitizer

## At a glance

- **Vendor**: hk2 (https://packagento.com/hk2.md)
- **Latest version**: 1.0.0 — released 2026-07-21
- **Pricing**: Free
- **Package type**: Magento 2 module
- **Status**: active, accepting new buyers

## Installation

Packagento is licence-gated, so even free packages need a licence on a project before Composer can resolve them.

1. **Sign in or create an account** at https://packagento.com/customer/account/.

2. **Add the package to your account.** Open https://packagento.com/hk2/search-sanitizer and complete the free checkout. A licence is minted automatically.

3. **Create or pick a project, then activate the licence on it.**
   - Projects represent the Magento installs you deploy to. Manage them at https://packagento.com/projects/.
   - Activate the new licence on the project you'll deploy this package to. Activation is what generates the Composer credentials scoped to that project.

4. **Add the project credentials to your Magento codebase.**

   Grab the project's public + private key from https://packagento.com/projects/ (open the project, then its Credentials tab), and add them to `auth.json`:

   ```json
   {
     "http-basic": {
       "packagento.com": {
         "username": "ppk_live_...",
         "password": "psk_live_..."
       }
     }
   }
   ```

   Add the Packagento Composer repository to `composer.json`:

   ```json
   {
     "repositories": [
       { "type": "composer", "url": "https://packagento.com" }
     ]
   }
   ```

5. **Install and apply.**

   ```bash
   composer require hk2/search-sanitizer:*
   bin/magento setup:upgrade
   bin/magento setup:di:compile
   bin/magento cache:flush
   ```

## What it does

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

## README

![Version](https://img.shields.io/badge/version-v1.0.0-blue?style=flat-square)
![License](https://img.shields.io/badge/license-OSL--3.0-green?style=flat-square)
![Magento](https://img.shields.io/badge/Magento-2.4.4%20--%202.4.9-f97316?style=flat-square&logo=magento&logoColor=white)
![PHP](https://img.shields.io/badge/PHP-8.1%20--%208.4-7c3aed?style=flat-square&logo=php&logoColor=white)
[![Downloads](https://img.shields.io/packagist/dt/hk2/search-sanitizer?style=flat-square)](https://packagist.org/packages/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\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

```text
.
├── 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](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**: <support@basantmandal.in>
- **Website**: <https://www.basantmandal.in>

### License

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

## Changelog

#### ⚠ BREAKING CHANGES

* add HK2 Search Sanitizer module to sanitize storefront search queries

#### ✨ Features

* add HK2 Search Sanitizer module to sanitize storefront search queries ([55c2180](https://github.com/basantmandal/magento2-search-sanitizer-module/commit/55c21803d5fed771f98b7079ae7425c4eaf762fe))

#### 📝 Documentation

* update README with detailed module overview and add SECURITY policy ([dab41a1](https://github.com/basantmandal/magento2-search-sanitizer-module/commit/dab41a17a4f90a5cb0adbaa3f9ab7f24c65c7666))

## Recent Versions

| Version | Released |
|---|---|
| 1.0.0 | 2026-07-21 |

## Dependencies

### Require

| Package | Constraint |
|---|---|
| hk2/core | ^1.0 |
| magento/framework | ^103.0.0 |
| php | ^8.1 \|\| ^8.2 \|\| ^8.3 \|\| ^8.4 |

## Quality

Latest release (1.0.0) passes the Packagento QA pipeline. Verdicts below are per-cell (Magento line × PHP version) for the matrixed tools, and run-once for the static / security tiers.


### Compatibility

Each Magento line is installed on its supported PHP versions, then the module is built (DI compile + static-content deploy). Cells show passed / failed / untested; staircase gaps render as `–`.

| 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. Never affect the Compatibility verdict — 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.

| 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 against a real Magento install. Re-runs per Magento + PHP version because resolvable symbols differ between releases.

| 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 per Magento + PHP cell. Test failures speak to the module's behaviour, not its compatibility with a line, so they're reported here separately.

#### 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

Dependency-advisory audit (composer audit) plus a source malware scan. A malware detection fails the version outright.

| Tool | Status | Findings | Summary |
|---|---|---|---|
| Composer audit | Pass | 0 |  |
| Malware scan | Pass | 0 |  |

## Licence and pricing

Free. A licence is still minted on checkout and bound to your project for Composer access — no payment step.

Refundable within 14 days of first purchase via https://packagento.com/account/refunds/.

## Install via Claude Code or any MCP client

The Packagento MCP server can run the licence + project + Composer steps above in one tool call:

```
purchase_and_install_packages(
  composer_names=["hk2/search-sanitizer"],
  project_id="proj_xxx"
)
```

This handles cart, checkout, licence minting, project activation, and writes auth.json credentials. Connect a client with `claude mcp add packagento https://mcp.packagento.com`. Full setup at https://packagento.com/docs/mcp-setup.

## Vendor

hk2 is a Magento 2 vendor on Packagento. See https://packagento.com/hk2.md for their full catalogue.

