Security: ILIKE pair search accepts unescaped wildcards, single-char query matches all pairs [SEC-I04] (F02) #459
Labels
No labels
agent:fix_bugfix
agent:fix_conflicts
agent:fix_security
agent:gap_analysis
agent:implement
agent:implement
agent:implement
agent:open_issues
agent:ready
agent:research
agent:security_audit
agent:verify
architecture
backend
blocker:hybrid
blocker:launch
blocker:limit-orders
blocker:v2
block:log_only
block:security
bug
ci
contracts
correctness
deploy
dev
devops
docs
documentation
duplicate
e2e
enhancement
epic
feature
frontend
functional-completion
gas
good first issue
governance
help wanted
high-risk
hooks
hybrid
indexer
infra
infrastructure
integrators
invalid
launch-blocker
limit-orders
localnet
localterra
low priority
missing-implementation
needs-design
ops
performance
priority
high
priority
medium
product
qa
QA
question
ready
ready
research
scripts
security
security-hardening
smartcontracts
tech-debt
testing
ux
UX
v2
verification
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
code/cl8y-dex-terraclassic#459
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
The
GET /api/v1/pairs?q=search handler passes the query string into an ILIKE pattern without escaping%and_wildcard metacharacters. A request with?q=%produces a leading-wildcardILIKE '%'clause that matches every row in the pairs table. Because leading-wildcard ILIKE cannot use B-tree indexes, each such request forces a sequential scan. This bypasses the intent of the 128-character truncation guard and creates a search-amplification vector: a burst of single-character or wildcard queries can saturate Postgres CPU ahead of the 30-second statement timeout circuit breaker.What Was Checked
indexer/src/db/queries/pairs.rslines 95-213 (push_pair_list_filtersandpush_pair_relevance_score): user query string embedded into ILIKE patterns viapush_bind(format!("%{}%", q))without escaping%or_first.indexer/src/api/pairs.rslines 173-178:qparameter truncated to 128 chars only. No metacharacter stripping or minimum length check.Expected (per checklist)
The
qparameter metacharacters%and_are escaped before being embedded in the ILIKE pattern (e.g., replace%with\%and_with\_), or a minimum query length of 2+ characters is enforced so single-character and wildcard-only queries are rejected.Actual
?q=%sendsILIKE '%'to Postgres, matching every pair. No metacharacter escaping or minimum length check is present.Suggested Fix
In
push_pair_list_filters, escape the query string before embedding: replace%with\%and_with\_(and ensure the ILIKE call includesESCAPE '\'). Alternatively enforce a minimum query length of 2 characters in the handler and reject with 400 if shorter. Additionally consider adding a pg_trgm GIN index onsymbol,name, andcontract_addresscolumns to make the leading-wildcard case index-backed even when metacharacters are present.Verification Checklist
push_pair_list_filtersescapes%and_in the query string before embedding in ILIKE?q=%returns an empty list or 400, not all pairs?q=_does not match all single-character-symbol assetsCc: @PlasticDigits
mentioned in issue #453
mentioned in issue #381
mentioned in merge request !984
Fixed. The pair search built ILIKE patterns with
format!("%{}%", q)and never escaped metacharacters, so?q=%->ILIKE '%%%'matched every pair (and forced a seq scan),?q=_matched any single char.Added
escape_like_pattern()— escapes\,%,_(backslash first to avoid double-escaping). Postgres' default LIKE/ILIKE escape char is backslash, so the bound pattern needs no explicitESCAPEclause. Applied at every pattern site: the relevance score, the list filters, and the split pair-symbol sub-tokens. I went with escaping rather than a min-length reject so single-char symbol search still works.Tests: 4 unit tests on the escaper, plus a Postgres integration test (
search_wildcard_query_does_not_match_all_pairs) — ran it against the live test DB:?q=%and?q=_both return empty,?q=LUNCstill returns the seeded pair. Full api_pairs suite 16/0 (incl relevance ordering, so normal search is intact).MR !984, branch
qa/459-ilike-escape, commitef0c850d. Needs review/merge @PlasticDigits — leaving open for verification.mentioned in commit
16cdb4336bVerification — GitLab #459 (SEC-I04 F02)
Verified on
mainat merge16cdb433(MR !984,ef0c850d).Checklist
push_pair_list_filtersescapes%and_before ILIKEescape_like_pattern()inindexer/src/db/queries/pairs.rs; applied inpush_pair_list_filters,push_pair_relevance_score, and split pair-symbol sub-tokens?q=%returns empty list or 400, not all pairssearch_wildcard_query_does_not_match_all_pairsinindexer/tests/api_pairs.rs?q=_does not match all single-char-symbol assetsescape_like_pattern).pg_trgmGIN on searchable columns would help legitimate substring-scan performance but is optional follow-up — not required for SEC-I04 F02 closureHow verified
Code review:
escape_like_patternescapes\,%,_(backslash first); patterns bound viapush_bindwith Postgres default backslash escape (no explicitESCAPEclause). Handler still truncatesqto 128 chars only — wildcard abuse neutralized by escaping, not min-length reject.Live
curlagainst a running indexer API was not run (requires full deployFACTORY_ADDRESS+ seeded pairs); integration test with seeded Postgres covers?q=%/?q=_/?q=LUNCbehavior.Follow-ups
pg_trgmGIN indexes on pair-search columns if substring ILIKE scan latency becomes an ops concern at scale (rate limits + 30s timeout already bound abuse).mentioned in issue #337
mentioned in issue #481