|
| 1 | +import test from 'ava'; |
| 2 | +import { isReadOnlyMongoAggregationPipeline } from '../../../src/ai-core/tools/query-validators.js'; |
| 3 | + |
| 4 | +test('allows a plain read-only pipeline', (t) => { |
| 5 | + t.true(isReadOnlyMongoAggregationPipeline('[{"$match":{"status":"active"}},{"$group":{"_id":"$type"}}]')); |
| 6 | +}); |
| 7 | + |
| 8 | +test('allows a $lookup read pipeline', (t) => { |
| 9 | + t.true( |
| 10 | + isReadOnlyMongoAggregationPipeline( |
| 11 | + '[{"$lookup":{"from":"orders","localField":"id","foreignField":"user_id","as":"o"}},{"$unwind":"$o"}]', |
| 12 | + ), |
| 13 | + ); |
| 14 | +}); |
| 15 | + |
| 16 | +test('rejects $out (collection overwrite)', (t) => { |
| 17 | + t.false(isReadOnlyMongoAggregationPipeline('[{"$match":{}},{"$limit":0},{"$out":"users"}]')); |
| 18 | +}); |
| 19 | + |
| 20 | +test('rejects $merge (collection write)', (t) => { |
| 21 | + t.false(isReadOnlyMongoAggregationPipeline('[{"$merge":{"into":"users","whenMatched":"replace"}}]')); |
| 22 | +}); |
| 23 | + |
| 24 | +test('rejects $where (server-side JS)', (t) => { |
| 25 | + t.false(isReadOnlyMongoAggregationPipeline('[{"$match":{"$where":"sleep(10000) || true"}}]')); |
| 26 | +}); |
| 27 | + |
| 28 | +test('rejects $function (server-side JS)', (t) => { |
| 29 | + t.false( |
| 30 | + isReadOnlyMongoAggregationPipeline( |
| 31 | + '[{"$addFields":{"x":{"$function":{"body":"function(){return 1;}","args":[],"lang":"js"}}}}]', |
| 32 | + ), |
| 33 | + ); |
| 34 | +}); |
| 35 | + |
| 36 | +test('rejects $accumulator (server-side JS)', (t) => { |
| 37 | + t.false( |
| 38 | + isReadOnlyMongoAggregationPipeline( |
| 39 | + '[{"$group":{"_id":"$k","v":{"$accumulator":{"init":"function(){return 0}","accumulate":"function(){}","accumulateArgs":[],"merge":"function(){}","lang":"js"}}}}]', |
| 40 | + ), |
| 41 | + ); |
| 42 | +}); |
| 43 | + |
| 44 | +test('rejects a write stage nested inside a $lookup sub-pipeline', (t) => { |
| 45 | + t.false( |
| 46 | + isReadOnlyMongoAggregationPipeline('[{"$lookup":{"from":"orders","as":"o","pipeline":[{"$out":"stolen"}]}}]'), |
| 47 | + ); |
| 48 | +}); |
| 49 | + |
| 50 | +test('returns false (fail-closed) for an unparseable pipeline', (t) => { |
| 51 | + t.false(isReadOnlyMongoAggregationPipeline('not valid json {')); |
| 52 | +}); |
0 commit comments