fix(mongodb): guard aggregate execution paths

This commit is contained in:
徐文彬
2026-05-28 21:29:08 +08:00
parent c56bcb8e2d
commit 5ea146e08e
19 changed files with 166 additions and 26 deletions
@@ -1,6 +1,8 @@
import { strict as assert } from "node:assert";
import test from "node:test";
import {
evaluateMongoAggregateSafety,
mongoAggregateWriteStage,
mongoCountToQueryResult,
mongoDocumentsToQueryResult,
parseMongoAggregateCommand,
@@ -76,6 +78,19 @@ test("parseMongoAggregateCommand normalises ObjectId arguments with either quote
}
});
test("evaluateMongoAggregateSafety blocks write stages unless MCP write flags allow them", () => {
const out = parseMongoAggregateCommand('db.products.aggregate([{"$out":"products_copy"}])');
assert.ok(out);
assert.equal(mongoAggregateWriteStage(out.pipeline), "$out");
assert.match(evaluateMongoAggregateSafety(out, {}).reason || "", /DBX_MCP_ALLOW_WRITES=1/);
const merge = parseMongoAggregateCommand('db.products.aggregate([{"$merge":{"into":"products_copy"}}])');
assert.ok(merge);
assert.equal(mongoAggregateWriteStage(merge.pipeline), "$merge");
assert.match(evaluateMongoAggregateSafety(merge, { allowWrites: true }).reason || "", /DBX_MCP_ALLOW_DANGEROUS_SQL=1/);
assert.equal(evaluateMongoAggregateSafety(merge, { allowWrites: true, allowDangerous: true }).allowed, true);
});
test("mongoCountToQueryResult returns a single count row", () => {
assert.deepEqual(mongoCountToQueryResult(42, 5), {
columns: ["count"],