Skip to content

fix(query): keep Text phrase values inside their quoted phrase (FDE-70) - #868

Draft
vishal-bala wants to merge 9 commits into
fix/empty-filter-values-fail-closedfrom
fix/neutralise-backslash-in-text-phrases
Draft

vishal-bala wants to merge 9 commits into
fix/empty-filter-values-fail-closedfrom
fix/neutralise-backslash-in-text-phrases

Conversation

@vishal-bala

@vishal-bala vishal-bala commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Text == and != render their value as a quoted phrase, @f:("value"), and until now replaced only a double quote in it. Measured on Redis 8.2, 8.4 and 8.10, RediSearch treats a backslash anywhere before a phrase's closing quote as escaping that quote, even when the backslash is itself escaped, and pairs the quote with a later one instead. A value ending in a backslash therefore moved where its own phrase ended, and when a query holds two such phrases the re-paired string can parse without error, with the text between them read as query syntax.

That matters most in the MCP server. A profile can lock a filter, for example a tenant filter built from a verified token claim, and every filter a caller sends is ANDed inside it, so a caller can only narrow results within the lock. A caller-supplied Text value that moved its phrase's end could take query syntax outside its own clause and match documents the lock excludes. On Redis 8.4 a set of 36 such caller filters got past the lock 3 times on main, and none get past it on this branch.

The same mismatch caused silent misses outside the MCP server. The query parser and the indexer read backslashes, a bare $ and control characters differently, so == found nothing for stored text such as a Windows path, a price or multi-line text.

Changes

Phrase values rendered the way the indexer read them

_phrase_value in redisvl/query/filter.py now renders every Text == and != value, character by character, the way the indexer tokenised the stored text. A backslash before a term separator or another backslash stays as an escape pair, since the parser joins that pair into the term as the indexer did, so foo\-bar still matches a document holding foo\-bar. Replacing every backslash with a space, the simplest fix, would lose exactly those matches. Any other backslash is dropped, as the indexer dropped it, so C:\Users\bob renders C:Usersbob and matches the stored text, where it previously matched nothing. A quote or a bare $ becomes a space and control characters are dropped, again as the indexer treats them. A body that would end in a backslash gets a trailing space, so no backslash ever sits directly before the closing quote.

The set of characters a backslash may escape, _PHRASE_ESCAPABLE, was measured character by character on Redis 8.2, 8.4 and 8.10: the indexer's term separators minus the quote and the tab, plus the backslash itself. Some stored text stays out of reach of ==, because the parser reads it differently from the indexer and no phrase can carry it: an escaped quote, and an escaped backslash before _ or before another escaped backslash. The Text docstring records this.

A value with no searchable term is refused

A value made only of separators, such as ", ..., or a lone backslash, used to render a phrase that asked for no term, so == matched no document and != matched every document, without any error. Text now raises ValueError for such a value, checked in Text._set_value before the value is stored, after the empty-string refusal it builds on, and the MCP filter parser reports it as INVALID_FILTER. None still renders *, and stopwords are not checked, since they depend on the index.

Text("job") == "C:\\Users\\bob"   # before: @job:("C:\Users\bob")   after: @job:("C:Usersbob")
Text("job") != "..."               # before: matched every document  after: ValueError

The MCP backstop agrees with the renderer

Before ANDing a caller filter into a locked one, _reject_escapable_filter in redisvl/mcp/tools/search.py checks its rendering for structure that could escape the lock, skipping the contents of quoted phrases. It used to end a phrase at the next quote, which is where the parser disagreed. Its new _phrase_end accepts a phrase only in the shapes _phrase_value emits: every backslash escapes a character in _PHRASE_ESCAPABLE, and none sits directly before the closing quote. Counting backslashes before the quote would not work, because an escaped backslash in that position re-pairs the quote too. Anything else is refused, so a rendering path added later that leaves a backslash raw fails loudly instead of re-pairing quotes.

  • docs/concepts/mcp.md and the Text Filters section of docs/user_guide/02_complex_filtering.ipynb describe the phrase behaviour.
  • tests/unit/test_voyageai_reranker_rank_by.py is reformatted: it reached main in a form the pinned black rewrites, which made the pre-commit hook abort every commit.
  • Live tests in tests/integration/test_query.py cover each character class against its own index, and two combined-phrase queries whose quotes would re-pair if a backslash reached the closing quote.

Release Notes

Text == and != filters now match values containing backslashes, $ or control characters, such as Windows paths, as the stored text was indexed. A value with no searchable term, such as one of only punctuation or whitespace, now raises ValueError where it silently matched nothing (or everything with !=).

AI Assistance

  • No AI assistance was used for this PR.
  • This PR is AI-assisted. I have reviewed and understood everything in it, and I take responsibility for it.

Claude Code (claude-opus-5-5): implementation, tests, live measurements, pre-PR review and this description.

Reviewer Brief

Start here: redisvl/query/filter.py, _phrase_value. Every Text ==/!= value is rendered through it, and the MCP backstop _phrase_end in redisvl/mcp/tools/search.py accepts only the phrase shapes it emits.

Where it could fail

  • redisvl/query/filter.py, _PHRASE_ESCAPABLE: it mirrors the RediSearch term separators measured on Redis 8.2 to 8.10, and the backstop reads it too, so a missing character silently breaks matching for text that escaped it.
  • redisvl/mcp/tools/search.py, _phrase_end: containment rests on refusing a backslash directly before a phrase's closing quote; check this agrees with the space _phrase_value adds after a trailing escape pair.

Not verified: Redis versions below 8.2 and cluster mode; indexes with LANGUAGE chinese, which tokenise query phrases differently from documents; FT.HYBRID and FT.AGGREGATE with these filters, checked by reading only.

Beyond CI: on Redis 8.4, a caller filter escapes a locked tenant filter on main in 3 of 36 probes and in none on this branch. On main's code, 14 of the new and changed unit tests fail, as do all three test_filters variants and 3 of the 4 per-character live cases; the tests that pass there pin behaviour that must not change, such as foo\-bar keeping its escape pair. Removing the trailing space makes every test_filters variant a Redis syntax error.

Since the last brief: the branch now stacks on the empty-filter-values PR and merges it with current main; Text._set_value runs that PR's empty-string refusal before this one's term check.

Reviewed e3227e73, fixes in 85be94a5, 43cf8ff4 · Claude Code (claude-opus-5-5) · lenses: correctness, security, design, tests, docs, Redis behaviour

A backslash anywhere before a phrase's closing quote makes RediSearch pair
that quote with a later one, even when the backslash is itself escaped.
Two `Text ==` values ending in a backslash therefore re-pair silently, and
the MCP locked-filter backstop, which ended a phrase at the next quote,
disagreed with the parser about which characters were live syntax.

`Text` now replaces `\` in eq/ne phrases with a space, as it already did
for `"`, and the backstop refuses any phrase that still holds a backslash.

Resolves FDE-70.
The tokenizer drops a backslash into the term around it, so `C:\Users\bob`
indexes `usersbob`. Replacing the backslash with a space asked for terms no
document stored; deleting it matches what was indexed. A value of nothing
but backslashes becomes a single space, since an empty phrase raises
INDEXEMPTY.

Behaviour change: `==`/`!=` no longer match text stored with a deliberate
escape before a separator or another backslash, such as `foo\-bar`.

Also tightens the backstop's refuse rows (a backslash mid-phrase, and an
escaped one that hides a union from a quote-pairing scan), drops a
tenant-injection case that tested recall rather than confinement, and
de-duplicates the comments.
Deleting every backslash lost documents the query parser could still
reach. The indexer joins an escaped separator into its term (`foo\-bar`
indexes `foo-bar`) and the parser does the same inside a phrase, so that
pair now stays. A backslash before anything else is still dropped, since
the indexer dropped it while the parser would keep it (`C:\Users\bob`
indexes `usersbob`). A body ending in a backslash gets a trailing space,
because a backslash right before the closing quote re-pairs it.

The backstop now accepts exactly those shapes: inside a phrase, a
backslash may only escape a character from the shared set, and may not
sit right before the closing quote.

Measured on redis:8.4 over 1,500 hostile values: the backstop accepts
every rendering, none errors or re-pairs, and `==` finds its own document
for 1,073 of 1,280 values, against 698 on main. The values main matched
and this does not all hold an escaped quote, which cannot be kept.
The indexer drops control characters from a term, along with a backslash
escaping one, and drops an escaped tab; it splits terms at a bare `$`,
which the query parser would read as a parameter. Phrases now do the
same, so stored text holding any of them is matched. Dropping NUL also
stops it truncating the query string.

Tests: a live case per character class against its own small index,
where a misread phrase finds a neighbouring document; the backstop's
escaped-quote row now balances its quotes, so only refusing the escape
catches it; a redundant accept row is removed. Docs name the two kinds
of stored text no phrase can reach.
…kslashes

Inside a quoted phrase the parser keeps `$` in the term rather than reading
a parameter; the split exists because the indexer separates terms there.
An escaped backslash before `_` is also out of reach, not only a run of
them.
A value of nothing but separators (punctuation, whitespace, quotes, a bare
`$`, control characters, a lone backslash) rendered a phrase that asked
for no term, so `==` silently matched nothing and `!=` matched every
document. `Text` now raises ValueError for it, and the MCP filter boundary
reports it as INVALID_FILTER. Stopwords are not checked, since they depend
on the index.

Behaviour change: `Text("f") == '"'` and similar values, which used to
match no document, now raise.

Also adds a live case for a phrase ending in a kept escape pair followed
by another phrase, the one shape where the trailing space matters to
Redis.
Validating in `Text._set_value`, as `Num` does, means a refused value is
never stored, so rendering a field never raises. Documents the ValueError
on `__eq__` and `__ne__`, and trims the refusal test to one operator,
since both share the check.
@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.64%. Comparing base (39039bc) to head (b1a5b9b).

Additional details and impacted files
@@                           Coverage Diff                           @@
##           fix/empty-filter-values-fail-closed     #868      +/-   ##
=======================================================================
- Coverage                                79.47%   77.64%   -1.83%     
=======================================================================
  Files                                      121      121              
  Lines                                    15115    16039     +924     
  Branches                                  2686     2949     +263     
=======================================================================
+ Hits                                     12012    12454     +442     
- Misses                                    2390     2848     +458     
- Partials                                   713      737      +24     
Flag Coverage Δ
core 79.35% <100.00%> (+0.08%) ⬆️
service 79.99% <100.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…e-backslash-in-text-phrases

Stacks this branch on the empty-filter-values PR. Text._set_value keeps
that PR's empty-string refusal and runs this branch's term check after it.
Also reformats tests/unit/test_voyageai_reranker_rank_by.py, which arrived
from main in a form the pinned black rewrites.
@vishal-bala
vishal-bala changed the base branch from main to fix/empty-filter-values-fail-closed November 25, 2026 11:32
@vishal-bala
vishal-bala added this pull request to stack #870 November 25, 2026 11:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants