Skip to content

fix(query): make empty Tag filter values match nothing instead of everything - #846

Open
vishal-bala wants to merge 4 commits into
mainfrom
fix/empty-filter-values-fail-closed
Open

vishal-bala wants to merge 4 commits into
mainfrom
fix/empty-filter-values-fail-closed

Conversation

@vishal-bala

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

Copy link
Copy Markdown
Collaborator

Motivation

A Tag filter given an empty list rendered *, and FilterExpression drops 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 for Text with "".

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, keeping None as the one way to say "no constraint".

Changes

Empty values in Tag

Tag._set_tag_value and Tag.__str__ in redisvl/query/filter.py now distinguish None from 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 in FilterExpression: inside an & it narrows the result to nothing, and inside a | it adds nothing to the other side.

Input Before After
Tag(f) == [], (), set(), [None]; Tag(f) % [] * the no-match clause
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 are None, so a falsy but real tag such as 0 is kept.

Text refuses an empty string

Text(f) == "", != "" and % "" now raise ValueError, via a _set_value override, 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's INDEXEMPTY setting instead of the text field's. Measured on Redis 8.4, @t:{a} @x:("") fails even though x is indexed with INDEXEMPTY, while @x:("") @t:{a} succeeds. None still renders *.

Documentation and callers

  • docs/user_guide/02_complex_filtering.ipynb: the "empty case" cells now pass None, and the surrounding text explains that an empty list matches nothing. The stored outputs are unchanged, because None renders * as before.
  • docs/concepts/mcp.md and docs/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.
  • Tests: unit tests for every row of the table above and their combinations; integration tests showing that a filter combined with an empty list returns no documents through FilterQuery, CountQuery, VectorQuery and VectorRangeQuery, and that "" matches only empty-string documents on an INDEXEMPTY field.

Notes

@f:{""} needs DIALECT 2 or later and INDEXEMPTY on the field. On a field without INDEXEMPTY, 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. Tag therefore renders "" while Text refuses it: the Tag failure only affects fields indexed without INDEXEMPTY, whereas the Text failure affects correctly indexed fields too.

None combined 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.

MessageHistory and SemanticMessageHistory fall back to the default session when session_tag is falsy, so session_tag="" does not reach the new rendering.

Release Notes

An empty Tag filter value no longer means "no constraint": Tag(f) == [] matches no documents, even when combined with other filters, and Tag(f) == "" matches the empty tag. Text(f) == "" raises ValueError. Pass None to mean "no constraint".

Reviewer Brief

Start here: Tag._set_tag_value and Tag.__str__ in redisvl/query/filter.py. Together they decide how every empty value is stored and rendered; FilterExpression composition 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_filter and the MCP claim-injection guard handle it correctly; the bulk *_by_filter methods 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 used Text(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's filter.py is 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 in 9c58c139 · Claude Code (claude-opus-5-5) · lenses: correctness, security, design, tests, docs, Redis behaviour


Note

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.

Tag now treats None as 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 like 0 are kept. Text raises ValueError on "" because @f:("") can behave inconsistently beside other clauses.

Docs and the filtering notebook steer callers to None instead 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.

…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
vishal-bala marked this pull request as ready for review November 16, 2026 17:41
@vishal-bala vishal-bala added the auto:patch Increment the patch version when merged label Nov 16, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.47%. Comparing base (1a64961) to head (39039bc).
⚠️ Report is 2 commits behind head on main.

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     
Flag Coverage Δ
core 79.27% <100.00%> (+0.03%) ⬆️
service 79.90% <100.00%> (+0.02%) ⬆️

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.

@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

auto:patch Increment the patch version when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants