core/storage: LIKE patterns lack an ESCAPE clause (% and _ in search terms act as wildcards) #14

Closed
opened 2026-07-07 13:19:31 +02:00 by buildagent · 1 comment
Member

Severity: Medium · Category: Correctness

Search terms are correctly bound as parameters (no SQL injection), but they are spliced into LIKE patterns (format!("{query}%"), %{query}%) with no ESCAPE clause:

  • crates/daemon/src/local_index.rs:461,491 (search_symbols)
  • :753-766 (similar_symbol_names)
  • :588-591 (import branch)

A term containing % or _ is silently interpreted as a LIKE wildcard — e.g. get_ matches getX, and a lone % matches everything (the count path scans the whole symbols table before the limit). Correctness/surprise bug, not memory-safety.

Fix

  • Escape %, _, and the escape char in the term, then append ESCAPE '\' to each LIKE. A literal-prefix escape still allows the NOCASE index range scan.

Verified good (context)

  • No dynamic SQL from user data anywhere; every format!-built statement splices only &'static str fragments; FTS5 query construction is well-defended (fts5_safe_query + regression suite).

Filed from a deep multi-agent code review of the workspace (v0.5.7). Severity/category per the review; file:line refs were accurate at review time — verify against current master.

**Severity:** Medium · **Category:** Correctness Search terms are correctly **bound as parameters** (no SQL injection), but they are spliced into `LIKE` patterns (`format!("{query}%")`, `%{query}%`) with **no `ESCAPE` clause**: - `crates/daemon/src/local_index.rs:461,491` (`search_symbols`) - `:753-766` (`similar_symbol_names`) - `:588-591` (import branch) A term containing `%` or `_` is silently interpreted as a LIKE wildcard — e.g. `get_` matches `getX`, and a lone `%` matches everything (the count path scans the whole symbols table before the limit). Correctness/surprise bug, not memory-safety. ### Fix - Escape `%`, `_`, and the escape char in the term, then append `ESCAPE '\'` to each `LIKE`. A literal-prefix escape still allows the `NOCASE` index range scan. ### Verified good (context) - No dynamic SQL from user data anywhere; every `format!`-built statement splices only `&'static str` fragments; FTS5 query construction is well-defended (`fts5_safe_query` + regression suite). --- _Filed from a deep multi-agent code review of the workspace (v0.5.7). Severity/category per the review; file:line refs were accurate at review time — verify against current `master`._
Author
Member

Fixed in 53dbc19 (branch fix/ultradeep-review-findings).

Reassessed on current master — still present at all three sites (search_symbols name LIKE + the four import alias/module LIKEs, similar_symbol_names' three matchers). Fix adds an escape_like helper (escapes \, %, _, backslash first) and ESCAPE '\' on every LIKE. The prefix % a caller appends is added after escaping so it stays a real wildcard; similar_symbol_names takes its 3-char head raw then escapes, so a truncation can't leave a dangling \. The exact-match tier uses = on the raw query and is unaffected. The NOCASE prefix-range-scan optimization is preserved (a literal-prefix escape still allows the index range).

Tests: like_escape_tests::underscore_in_query_is_literal_not_wildcard (get_ matches get_user, not getXuser) and percent_in_query_is_literal_not_match_all (% matches nothing literal, not every row). Full daemon suite green (195).

Fixed in `53dbc19` (branch `fix/ultradeep-review-findings`). Reassessed on current master — still present at all three sites (`search_symbols` name LIKE + the four import alias/module LIKEs, `similar_symbol_names`' three matchers). Fix adds an `escape_like` helper (escapes `\`, `%`, `_`, backslash first) and `ESCAPE '\'` on every LIKE. The prefix `%` a caller appends is added *after* escaping so it stays a real wildcard; `similar_symbol_names` takes its 3-char head raw **then** escapes, so a truncation can't leave a dangling `\`. The exact-match tier uses `=` on the raw query and is unaffected. The NOCASE prefix-range-scan optimization is preserved (a literal-prefix escape still allows the index range). **Tests:** `like_escape_tests::underscore_in_query_is_literal_not_wildcard` (`get_` matches `get_user`, not `getXuser`) and `percent_in_query_is_literal_not_match_all` (`%` matches nothing literal, not every row). Full daemon suite green (195).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
h-dv/code-index#14
No description provided.