Repository navigation
fix(query): keep Text phrase values inside their quoted phrase (FDE-70) - #868
Draft
vishal-bala wants to merge 9 commits into
Draft
vishal-bala wants to merge 9 commits into
vishal-bala wants to merge 9 commits into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
…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
changed the base branch from
main
to
fix/empty-filter-values-fail-closed
November 25, 2026 11:32
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Textvalue 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 onmain, 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_valueinredisvl/query/filter.pynow renders everyText==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, sofoo\-barstill matches a document holdingfoo\-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, soC:\Users\bobrendersC:Usersboband 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. TheTextdocstring 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.Textnow raisesValueErrorfor such a value, checked inText._set_valuebefore the value is stored, after the empty-string refusal it builds on, and the MCP filter parser reports it asINVALID_FILTER.Nonestill renders*, and stopwords are not checked, since they depend on the index.The MCP backstop agrees with the renderer
Before ANDing a caller filter into a locked one,
_reject_escapable_filterinredisvl/mcp/tools/search.pychecks 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_endaccepts a phrase only in the shapes_phrase_valueemits: 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.mdand the Text Filters section ofdocs/user_guide/02_complex_filtering.ipynbdescribe the phrase behaviour.tests/unit/test_voyageai_reranker_rank_by.pyis reformatted: it reachedmainin a form the pinnedblackrewrites, which made the pre-commit hook abort every commit.tests/integration/test_query.pycover 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 raisesValueErrorwhere it silently matched nothing (or everything with!=).AI Assistance
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. EveryText==/!=value is rendered through it, and the MCP backstop_phrase_endinredisvl/mcp/tools/search.pyaccepts 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_valueadds 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.HYBRIDandFT.AGGREGATEwith these filters, checked by reading only.Beyond CI: on Redis 8.4, a caller filter escapes a locked tenant filter on
mainin 3 of 36 probes and in none on this branch. Onmain's code, 14 of the new and changed unit tests fail, as do all threetest_filtersvariants and 3 of the 4 per-character live cases; the tests that pass there pin behaviour that must not change, such asfoo\-barkeeping its escape pair. Removing the trailing space makes everytest_filtersvariant 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_valueruns that PR's empty-string refusal before this one's term check.Reviewed
e3227e73, fixes in85be94a5,43cf8ff4· Claude Code (claude-opus-5-5) · lenses: correctness, security, design, tests, docs, Redis behaviour