Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/matches-where.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ await test('matchesWhere', async (t) => {
[{ c: { endsWith: 'z' } }, false],
[{ a: { endsWith: '1' } }, false],
[{ c: { endsWith: 1 } }, false],
// Regression tests for issue #1731: nested _where fail-open
// When intermediate field is not an object, nested predicates should fail (return false)
[{ a: { nested: { eq: 'zzz' } } }, false], // a is a number, not an object
[{ c: { nested: { eq: 'zzz' } } }, false], // c is a string, not an object
[{ or: [{ a: { nested: { eq: 'zzz' } } }] }, false],
]

for (const [query, expected] of cases) {
Expand Down
15 changes: 15 additions & 0 deletions src/matches-where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ function getKnownOperators(value: unknown): WhereOperator[] {
return ops
}

function hasKnownOperatorsAtAnyLevel(value: unknown): boolean {
if (!isJSONObject(value)) return false

const knownOps = getKnownOperators(value)
if (knownOps.length > 0) return true

for (const v of Object.values(value)) {
if (hasKnownOperatorsAtAnyLevel(v)) return true
}

return false
}

export function matchesWhere(obj: JsonObject, where: JsonObject): boolean {
for (const [key, value] of Object.entries(where)) {
if (key === 'or') {
Expand Down Expand Up @@ -74,6 +87,8 @@ export function matchesWhere(obj: JsonObject, where: JsonObject): boolean {

if (isJSONObject(field)) {
if (!matchesWhere(field, value)) return false
} else if (hasKnownOperatorsAtAnyLevel(value)) {
return false
}

continue
Expand Down