Skip to content

Commit 425e6e2

Browse files
anandgupta42claude
andcommitted
fix(drivers): refuse the ambiguous Mongo distinct shorthand instead of querying the wrong collection
A collection shorthand that also carried the native `key` field, such as `{ collection: "items", distinct: "category", key: "status" }`, left the shorthand argument undefined and fell through to the native-command parser, which read `"category"` as the collection name. Shorthand `distinct` is now validated against both `key` and `field`: agreeing values are accepted, conflicting values are refused with an error naming both, and any document carrying `collection` never reaches the native parser. Tests: conflicting `key`/`field` refused naming `category` and `status`; agreeing aliases accepted; distinct-as-field keeps the collection; unsupported, empty and null `collection` documents do not fall through. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 2bfd6bb commit 425e6e2

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎packages/drivers/src/mongodb.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,19 @@ export function normalizeMqlCommand<T extends Record<string, unknown>>(parsed: T
7878
? "filter"
7979
: command === "aggregate" && Array.isArray(value)
8080
? "pipeline"
81-
: command === "distinct" && typeof value === "string" && parsed["key"] === undefined
81+
: command === "distinct" && typeof value === "string"
8282
? "field"
8383
: undefined
8484
if (argument) {
85-
const explicitArguments = command === "countDocuments" ? [argument, "query"] : [argument]
85+
const explicitArguments =
86+
command === "countDocuments" ? [argument, "query"] : command === "distinct" ? [argument, "key"] : [argument]
8687
for (const explicit of explicitArguments) {
8788
if (parsed[explicit] !== undefined && !isDeepStrictEqual(parsed[explicit], value)) {
89+
if (command === "distinct") {
90+
throw new Error(
91+
`Ambiguous distinct form: 'distinct' value ${JSON.stringify(value)} conflicts with explicit '${explicit}' value ${JSON.stringify(parsed[explicit])}`,
92+
)
93+
}
8894
throw new Error(`Ambiguous MQL command: '${key}' conflicts with explicit '${explicit}' argument`)
8995
}
9096
}
@@ -95,6 +101,8 @@ export function normalizeMqlCommand<T extends Record<string, unknown>>(parsed: T
95101
return normalized as T & MqlQuery
96102
}
97103
}
104+
// A collection-bearing document is shorthand, even when its arguments are invalid.
105+
if (Object.hasOwn(parsed, "collection")) return parsed as T & MqlQuery
98106
for (const [key, command] of Object.entries(NATIVE_COMMAND_KEYS)) {
99107
const value = parsed[key]
100108
if (typeof value !== "string" || !value) continue

‎packages/drivers/test/mongodb-unit.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,43 @@ describe("MongoDB native command filters", () => {
154154
})
155155

156156
describe("MongoDB collection command shorthand", () => {
157+
test.each([
158+
{ name: "key", aliases: { key: "status" } },
159+
{ name: "field", aliases: { field: "status" } },
160+
{ name: "field with an agreeing key", aliases: { key: "category", field: "status" } },
161+
{ name: "key with an agreeing field", aliases: { key: "status", field: "category" } },
162+
])("refuses ambiguous distinct form with conflicting $name and names both values", ({ aliases }) => {
163+
expect(() => normalizeMqlCommand({ collection: "items", distinct: "category", ...aliases })).toThrow(
164+
/ambiguous distinct form.*category.*status/i,
165+
)
166+
})
167+
168+
test("normalizes distinct-as-field shorthand without changing the collection", () => {
169+
expect(normalizeMqlCommand({ collection: "items", distinct: "category" })).toEqual({
170+
collection: "items",
171+
command: "distinct",
172+
field: "category",
173+
})
174+
})
175+
176+
test.each([
177+
{ name: "key", aliases: { key: "category" } },
178+
{ name: "field", aliases: { field: "category" } },
179+
{ name: "key and field", aliases: { key: "category", field: "category" } },
180+
])("accepts distinct shorthand with agreeing $name", ({ aliases }) => {
181+
const normalized = normalizeMqlCommand({ collection: "items", distinct: "category", ...aliases })
182+
expect(normalized).toMatchObject({ collection: "items", command: "distinct", field: "category" })
183+
expect(normalized).not.toHaveProperty("distinct")
184+
})
185+
186+
test.each([
187+
{ collection: "items", find: "other" },
188+
{ collection: "", distinct: "category", key: "category" },
189+
{ collection: null, distinct: "category", key: "category" },
190+
])("does not fall through to native parsing when collection is present: %j", (input) => {
191+
expect(normalizeMqlCommand(input)).toEqual(input)
192+
})
193+
157194
test.each([
158195
{ name: "find and filter", input: { collection: "items", find: { status: "x" }, filter: { status: "y" } } },
159196
{ name: "count and query", input: { collection: "items", count: { status: "x" }, query: { status: "y" } } },

0 commit comments

Comments
 (0)