Search by

batuhnzdmr / module-catalog-search-spam-filter

batuhnzdmr

Magento 2 module that blocks spam catalog search queries using a character whitelist (Turkish-aware).

Package info

github.com/batuhnzdmr/magento2-catalog-search-spam-filter

Type:magento2-module

pkg:composer/batuhnzdmr/module-catalog-search-spam-filter

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-11-22 15:02 UTC

This package is auto-updated.

Last update: 2026-11-22 15:26:01 UTC


README

πŸ›‘οΈ CatalogSearchSpamFilter

Stop spam bots from polluting your Magento 2 search terms β€” before the search even runs.

Magento 2 PHP License: MIT Latest release Turkish ready

πŸ€” The problem

If you run a Magento store, you have probably opened Marketing β†’ SEO & Search β†’ Search Terms and found thousands of entries like these:

buy cheap viagra online http://spam-site.xyz
<script>alert(1)</script>
ε¨θ€Œι‹Ό θ³Όθ²· 線上
SELECT * FROM customer_entity

Bots hit /catalogsearch/result/?q=... with junk queries. Magento happily runs a full search for each one, stores every term in the search_query table, and can even expose them in Popular Search Terms pages that search engines crawl. The result:

  • πŸ—‘οΈ A bloated search_query table and noisy search reports
  • 🐒 Wasted CPU and Elasticsearch/OpenSearch load on fake searches
  • πŸ” SEO spam links indexed under your own domain

✨ The solution

CatalogSearchSpamFilter hooks into the request before the search controller executes. If the query contains any character outside a strict whitelist, the request is forwarded to Magento's standard 404 page β€” no search is performed and nothing is written to the database.

⚑ Zero overhead One regex check per search request. No database queries, no extra tables.
🧩 No configuration Install, enable, done.
πŸ‡ΉπŸ‡· Turkish-aware Correctly handles Γ§ ğ Δ± i ΓΆ ş ΓΌ and their uppercase forms, including the tricky Δ°.
πŸͺΆ Tiny footprint One observer, no rewrites, no preferences β€” won't conflict with other modules.

βš™οΈ How it works

Visitor / Bot
     β”‚
     β–Ό
GET /catalogsearch/result/?q=...
     β”‚
     β–Ό
controller_action_predispatch_catalogsearch_result_index
     β”‚
     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Does q match the whitelist?  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     β”‚ yes                 β”‚ no
     β–Ό                     β–Ό
Normal Magento search   404 page (noroute)
                        β€’ search not executed
                        β€’ term not saved

πŸ“‹ Whitelist

A query is allowed only if every character belongs to this set:

Type Characters
Latin letters a–z, A–Z (including q, w, x)
Turkish letters Γ§ Γ‡ ğ Ğ Δ± I i Δ° ΓΆ Γ– ş Ş ΓΌ Ü
Digits 0–9
Other - (hyphen) and space

Examples

Query Result
nike air max βœ… Allowed
Δ°STANBUL βœ… Allowed
şapka-42 βœ… Allowed
<script>alert(1)</script> β›” 404
buy viagra http://spam.xyz β›” 404
δΈ­ζ–‡ spam β›” 404

Note

Characters such as ., ', /, & and + are blocked on purpose, since they appear in most spam URLs and injection attempts. If your catalog relies on them (for example 2.5mm or Levi's), adjust ALLOWED_PATTERN in the observer.

πŸ“¦ Installation

Option 1: Composer (recommended)

composer require batuhnzdmr/module-catalog-search-spam-filter
bin/magento module:enable Batuhnzdmr_CatalogSearchSpamFilter
bin/magento setup:upgrade
bin/magento setup:di:compile        # production mode only
bin/magento cache:flush

Option 2: Manual (ZIP)

  1. Download CatalogSearchSpamFilter-vX.Y.Z.zip from the latest release
  2. Extract it into your Magento root directory. The archive already contains the app/code/Batuhnzdmr/CatalogSearchSpamFilter structure.
  3. Run:
bin/magento module:enable Batuhnzdmr_CatalogSearchSpamFilter
bin/magento setup:upgrade
bin/magento setup:di:compile        # production mode only
bin/magento cache:flush

βœ… Verify

bin/magento module:status Batuhnzdmr_CatalogSearchSpamFilter

Then open these URLs on your store:

  • https://your-store.com/catalogsearch/result/?q=shoes β†’ normal search results
  • https://your-store.com/catalogsearch/result/?q=%3Cscript%3E β†’ 404 page

πŸ› οΈ Customization

The whitelist lives in a single constant in Observer/ControllerActionPredispatchObserver.php:

private const ALLOWED_PATTERN = '/^[a-zA-Z0-9Γ§Γ‡ΔŸΔžΔ±Δ°ΓΆΓ–ΕŸΕžΓΌΓœ\- ]+$/u';

For example, to also allow dots and apostrophes:

private const ALLOWED_PATTERN = "/^[a-zA-Z0-9Γ§Γ‡ΔŸΔžΔ±Δ°ΓΆΓ–ΕŸΕžΓΌΓœ\\-. ']+$/u";

Tip

Building a store for another language? Swap the Turkish letters for your own alphabet (for example Àâüß for German).

🧹 Cleaning up existing spam

The module stops new spam terms. To remove the ones already in your database, back it up first, then run:

-- Preview
SELECT query_id, query_text FROM search_query
WHERE query_text NOT REGEXP '^[a-zA-Z0-9Γ§Γ‡ΔŸΔžΔ±Δ°ΓΆΓ–ΕŸΕžΓΌΓœ -]+$';

-- Delete
DELETE FROM search_query
WHERE query_text NOT REGEXP '^[a-zA-Z0-9Γ§Γ‡ΔŸΔžΔ±Δ°ΓΆΓ–ΕŸΕžΓΌΓœ -]+$';

Warning

Unicode support in REGEXP depends on your database. MySQL 8.0+ handles it correctly; on older MySQL or MariaDB versions, review the preview output carefully before deleting.

πŸ—‚οΈ Module structure

CatalogSearchSpamFilter/
β”œβ”€β”€ Observer/
β”‚   └── ControllerActionPredispatchObserver.php   # Whitelist check + 404 forward
β”œβ”€β”€ etc/
β”‚   β”œβ”€β”€ frontend/
β”‚   β”‚   └── events.xml                            # Predispatch event binding
β”‚   └── module.xml                                # Depends on Magento_CatalogSearch
β”œβ”€β”€ composer.json
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
└── registration.php

πŸ“Œ Scope and limitations

This module protects the frontend quick search results page (catalogsearch/result/index). It does not currently filter:

  • Advanced search (catalogsearch/advanced/result)
  • Search autocomplete (search/ajax/suggest)
  • GraphQL products(search: ...) queries

πŸ—ΊοΈ Roadmap

  • Admin configuration for the whitelist (Stores β†’ Configuration)
  • Optional maximum query length
  • Coverage for advanced search and autocomplete
  • Logging of blocked queries
  • Unit tests

🀝 Contributing

Issues and pull requests are welcome. If you have spam patterns this filter misses, please open an issue with an example query.

  1. Fork the repository
  2. Create a branch: git checkout -b feature/my-improvement
  3. Commit your changes: git commit -m "Add my improvement"
  4. Push and open a pull request

πŸ“„ License

Released under the MIT License.

Made with β˜• by @batuhnzdmr

If this module saved your search terms table, consider giving it a ⭐