batuhnzdmr / module-catalog-search-spam-filter
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
Requires
- php: >=7.4
- magento/framework: ^103.0
- magento/module-catalog-search: ^100.4
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
π‘οΈ CatalogSearchSpamFilter
Stop spam bots from polluting your Magento 2 search terms β before the search even runs.
π€ 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_querytable 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)
- Download
CatalogSearchSpamFilter-vX.Y.Z.zipfrom the latest release - Extract it into your Magento root directory. The archive already contains the
app/code/Batuhnzdmr/CatalogSearchSpamFilterstructure. - 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 resultshttps://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.
- Fork the repository
- Create a branch:
git checkout -b feature/my-improvement - Commit your changes:
git commit -m "Add my improvement" - 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 β