[BUG]: reject operators as top-level where keys#7448
Open
chuenchen309 wants to merge 1 commit into
Open
Conversation
validate_where already rejects $contains/$not_contains when used as a
top-level where key (where a metadata field name or $and/$or is
expected), but the other query operators ($gt, $gte, $lt, $lte, $ne,
$eq, $in, $nin) slipped through: a scalar operand passed the value
type check, and $in/$nin were explicitly excluded from it, so a
malformed filter like {"$in": [1, 2]} (field name forgotten) was
silently accepted instead of raising the same clear ValueError.
Extend the existing top-level-key guard to the full operator set and
drop the now-dead $in/$nin exclusion from the value type check. Query
operators inside a field expression ({"field": {"$in": [...]}}) and
$and/$or logical keys are unaffected.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Reviewer ChecklistPlease leverage this checklist to ensure your code review is thorough before approving Testing, Bugs, Errors, Logs, Documentation
System Compatibility
Quality
|
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.
Description of changes
validate_wherealready rejects$contains/$not_containswhen they appear as a top-levelwherekey — a spot where a metadata field name or a logical operator ($and/$or) is expected. That guard exists precisely to catch the common typo of writing an operator where a field name belongs.The remaining query operators leaked through, though:
Two things combined to let them slip:
{"$gt": 5}) satisfied the value type-check (intis allowed), and no later branch handled the operator-as-key case, so the function returned normally;$in/$ninwere explicitly excluded from that value type-check (and key != "$in" and key != "$nin"), which is exactly what suppressed the type error their list operand would otherwise have raised.So a user who forgets the field name —
collection.get(where={"$in": [1, 2]})instead ofwhere={"field": {"$in": [1, 2]}}— got no client-side error, unlike the analogous{"$contains": ...}typo which is caught with a clear message.This PR extends the existing top-level-key guard to the full operator set (
$gt $gte $lt $lte $ne $eq $in $nin $contains $not_contains) and drops the now-dead$in/$ninexclusion. Operators inside a field expression ({"field": {"$in": [...]}}) and$and/$orkeys are unaffected.Test plan
Added
test_validate_where_rejects_top_level_operator_key(parametrized over all ten operators) andtest_validate_where_accepts_operator_in_field_expressiontochromadb/test/api/test_types.py.mainthe eight previously-leaking operators fail the new rejection assertions; with this change all ten are rejected and legitimate field/$and/$orfilters still validate.pytest chromadb/test/api/test_types.py→ 13 passed.black --checkandflake8clean on both files.Authored with assistance from Claude Code (Anthropic). Bug, fix, and tests were reviewed and verified locally by the contributor.