Repository navigation
fix(query): make empty Tag filter values match nothing instead of everything - #846
Open
vishal-bala wants to merge 4 commits into
Open
vishal-bala wants to merge 4 commits into
vishal-bala wants to merge 4 commits into
Conversation
…erything
An empty collection passed to a Tag filter rendered `*`, and `*` is elided from
every `&` and `|`, so `(Tag("tenant") == "acme") & (Tag("groups") == [])`
rendered `@tenant:{acme}`. A permission-style filter built from an empty group
list therefore searched everything the other clauses allowed.
Follow how Redis treats empty values, which is never as "no constraint":
- An empty collection (`[]`, `()`, `set()`, or one holding only `None`) selects
no tag. `==` and `%` render a contradiction that matches nothing, which
absorbs an `&` and drops out of a `|`. `!=` renders `*`, since excluding
nothing excludes nothing.
- `""` is a tag of its own and renders `@field:{""}` (DIALECT 2+, field needs
INDEXEMPTY), instead of vanishing. Items that are falsy but real, such as 0,
are kept rather than dropped.
- `None` keeps meaning "no constraint" on every filter type.
- `Text` refuses `""`: beside another clause the server reads `@f:("")` against
the preceding clause's field, so the filter's meaning would depend on
operand order.
BREAKING CHANGE: `Tag(f) == []`, `== ""`, `% ""` and `Text(f) == ""` no longer
mean "no constraint". Pass `None` for that.
Drop the private flag threaded through FilterExpression. The contradiction clause already matches nothing wherever it sits, so it narrows an AND and adds nothing to an OR without any special composition, and operands keep rendering verbatim. Review follow-ups: tighten tests that could pass vacuously, and correct the docs on what INDEXEMPTY does and does not guarantee.
Pin that Text refuses a collection in its type check, make the empty-tag AND test match a real document, and drop assertions that duplicate others. Remove a comment that repeated the Tag docstring, say why Tag renders an empty string while Text refuses one, and stop describing an empty Tag list as match-all in render_filter and the bulk-operation guard docs.
vishal-bala
marked this pull request as ready for review
November 16, 2026 17:41
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #846 +/- ##
==========================================
- Coverage 80.25% 79.47% -0.78%
==========================================
Files 121 121
Lines 14871 15115 +244
Branches 2603 2686 +83
==========================================
+ Hits 11934 12012 +78
- Misses 2219 2390 +171
+ Partials 718 713 -5
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
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
A
Tagfilter given an empty list rendered*, andFilterExpressiondrops a*operand from every&and|. So(Tag("tenant") == "acme") & (Tag("groups") == [])rendered@tenant:{acme}, and the groups clause disappeared. Any permission-style filter built from a list of allowed values therefore searched everything its other clauses allowed whenever that list came back empty, which is the opposite of what an empty allow-list means. The same happened for"",[""],[None]and a list of falsy values such as[0], and forTextwith"".Redis itself never reads an empty value as "no constraint".
@f:{}is a syntax error, and@f:{""}matches the documents that store an empty tag. This PR aligns RedisVL with that, keepingNoneas the one way to say "no constraint".Changes
Empty values in
TagTag._set_tag_valueandTag.__str__inredisvl/query/filter.pynow distinguishNonefrom an empty value. RediSearch has no match-nothing literal (-*is a syntax error), so an empty collection renders a contradiction that no document can satisfy,(@f:{redisvl_no_match} -@f:{redisvl_no_match}). It needs no special handling inFilterExpression: inside an&it narrows the result to nothing, and inside a|it adds nothing to the other side.Tag(f) == [],(),set(),[None];Tag(f) % []*Tag(f) != []**Tag(f) == "",[""]*@f:{""}Tag(f) == ["a", ""]@f:{a}@f:{a|""}Tag(f) == [0]*@f:{0}Tag(f) == None**!=an empty collection stays*, because excluding nothing excludes nothing. Items are now dropped only when they areNone, so a falsy but real tag such as0is kept.Textrefuses an empty stringText(f) == "",!= ""and% ""now raiseValueError, via a_set_valueoverride, where they rendered*. Rendering@f:("")is not safe: when a TAG clause precedes it in the same query, the server checks the empty phrase against that tag field'sINDEXEMPTYsetting instead of the text field's. Measured on Redis 8.4,@t:{a} @x:("")fails even thoughxis indexed withINDEXEMPTY, while@x:("") @t:{a}succeeds.Nonestill renders*.Documentation and callers
docs/user_guide/02_complex_filtering.ipynb: the "empty case" cells now passNone, and the surrounding text explains that an empty list matches nothing. The stored outputs are unchanged, becauseNonerenders*as before.docs/concepts/mcp.mdanddocs/concepts/search-and-indexing.md: corrected statements about which values count as match-all.redisvl/extensions/router/semantic.py: comment only. The router already refuses empty reference ids before building a filter.FilterQuery,CountQuery,VectorQueryandVectorRangeQuery, and that""matches only empty-string documents on anINDEXEMPTYfield.Notes
@f:{""}needs DIALECT 2 or later andINDEXEMPTYon the field. On a field withoutINDEXEMPTY, the server rejects it when it stands alone and, beside some other clauses, quietly treats it as matching nothing; it never widened a query in measurements on Redis 8.4.Tagtherefore renders""whileTextrefuses it: theTagfailure only affects fields indexed withoutINDEXEMPTY, whereas theTextfailure affects correctly indexed fields too.Nonecombined with|keeps its existing behaviour:*is dropped from either operator, so(Tag(f) == None) | (Tag(g) == [])matches nothing. That is fail-closed, and changing how*composes is outside this PR.MessageHistoryandSemanticMessageHistoryfall back to the default session whensession_tagis falsy, sosession_tag=""does not reach the new rendering.Release Notes
An empty
Tagfilter value no longer means "no constraint":Tag(f) == []matches no documents, even when combined with other filters, andTag(f) == ""matches the empty tag.Text(f) == ""raisesValueError. PassNoneto mean "no constraint".Reviewer Brief
Start here:
Tag._set_tag_valueandTag.__str__inredisvl/query/filter.py. Together they decide how every empty value is stored and rendered;FilterExpressioncomposition did not change.Where it could fail
redisvl/query/filter.py,Tag.__str__: an empty collection now renders a real clause instead of*, so code that checks a rendered filter for"*"treats it as a real filter.is_match_all_filter,render_filterand the MCP claim-injection guard handle it correctly; the bulk*_by_filtermethods now run such a filter and change nothing, instead of refusing it as match-all.redisvl/query/filter.py,Text._set_value:""now raises. No caller inside the repository passes it, because MCP filter parsing already refuses empty operands; external callers that usedText(f) == ""as "no filter" will break, as the Release Notes say.Not verified: cluster mode; Redis versions other than 8.4; tests that need provider API keys. The notebook was edited by hand and not re-executed.
Beyond CI: 16 of the new or changed unit tests fail when
main'sfilter.pyis swapped in. On Redis 8.4 the no-match clause returned no documents at DIALECT 1, 2 and 4, alone and intersected with another clause, and(no-match | @t:{acme})returned exactly the@t:{acme}documents.Reviewed
3510da84, fixes in9c58c139· Claude Code (claude-opus-5-5) · lenses: correctness, security, design, tests, docs, Redis behaviourNote
High Risk
Changes core filter semantics and breaks callers who used empty Tag/Text values as “no filter”; fixes a permission-bypass pattern but alters bulk match-all detection and tenant-scoping behavior.
Overview
Fixes a fail-open security bug:
Tag(f) == [](and similar empty collections) used to render*and disappear from&combinations, so permission-style filters could return every document when an allow-list was empty.Tagnow treatsNoneas the only “no constraint” (still*), while empty lists/sets match nothing via a contradiction clause (redisvl_no_match).""is a real tag value (@f:{""}), not a wildcard; falsy values like0are kept.TextraisesValueErroron""because@f:("")can behave inconsistently beside other clauses.Docs and the filtering notebook steer callers to
Noneinstead of[]or""for optional filters.is_match_all_filter,render_filter, MCP claim-injection tests, and integration tests are updated for the new semantics.Reviewed by Cursor Bugbot for commit 39039bc. Bugbot is set up for automated code reviews on this repo. Configure here.