T-7: M0.3a — Harden dev Mongo safety: env-safety throws on unsafe data-pipeline QA reads, scheduled jobs default off, read-only bypass tests
This commit is contained in:
parent
7cf940afce
commit
abbab4b5a8
10 changed files with 644 additions and 18 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import type {
|
||||
AggregateOptions,
|
||||
AggregationCursor,
|
||||
BulkWriteOptions,
|
||||
CountDocumentsOptions,
|
||||
CreateCollectionOptions,
|
||||
|
|
@ -185,6 +186,170 @@ export class MongoDBClient {
|
|||
return this.client.db(databaseName || this.defaultDatabase);
|
||||
}
|
||||
|
||||
private wrapReadOnlyAggregationCursor<T extends Document>(
|
||||
cursor: AggregationCursor<T>
|
||||
): AggregationCursor<T> {
|
||||
const assertCursorPipelineSafe = (): void => {
|
||||
if (Array.isArray(cursor.pipeline)) {
|
||||
this.assertReadOnlyPipeline(cursor.pipeline);
|
||||
}
|
||||
};
|
||||
|
||||
const wrapped: Record<PropertyKey, unknown> = {
|
||||
get id() {
|
||||
return cursor.id;
|
||||
},
|
||||
get namespace() {
|
||||
return cursor.namespace;
|
||||
},
|
||||
get readPreference() {
|
||||
return cursor.readPreference;
|
||||
},
|
||||
get readConcern() {
|
||||
return cursor.readConcern;
|
||||
},
|
||||
get closed() {
|
||||
return cursor.closed;
|
||||
},
|
||||
get killed() {
|
||||
return cursor.killed;
|
||||
},
|
||||
get loadBalanced() {
|
||||
return cursor.loadBalanced;
|
||||
},
|
||||
get pipeline() {
|
||||
return Object.freeze(Array.isArray(cursor.pipeline) ? [...cursor.pipeline] : []);
|
||||
},
|
||||
// Read / iteration operations
|
||||
toArray: () => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.toArray();
|
||||
},
|
||||
next: () => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.next();
|
||||
},
|
||||
tryNext: () => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.tryNext();
|
||||
},
|
||||
hasNext: () => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.hasNext();
|
||||
},
|
||||
forEach: (iterator: (doc: T) => boolean | void) => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.forEach(iterator);
|
||||
},
|
||||
stream: (...args: Parameters<AggregationCursor<T>['stream']>) => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.stream(...args);
|
||||
},
|
||||
explain: (...args: Parameters<AggregationCursor<T>['explain']>) => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.explain(...args);
|
||||
},
|
||||
close: (...args: Parameters<AggregationCursor<T>['close']>) => cursor.close(...args),
|
||||
rewind: () => cursor.rewind(),
|
||||
bufferedCount: () => cursor.bufferedCount(),
|
||||
readBufferedDocuments: (count?: number) => cursor.readBufferedDocuments(count),
|
||||
[Symbol.asyncIterator]: () => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor[Symbol.asyncIterator]();
|
||||
},
|
||||
[Symbol.asyncDispose]: () => {
|
||||
const disposable = cursor as unknown as Partial<Record<symbol, () => Promise<void>>>;
|
||||
const dispose = disposable[Symbol.asyncDispose];
|
||||
return typeof dispose === 'function' ? dispose.call(cursor) : cursor.close();
|
||||
},
|
||||
// Fluent stage builders (read-only stages validated and wrapped)
|
||||
out: () => {
|
||||
throw new Error(
|
||||
'MongoDB client is configured in read-only mode; aggregation cursor operation "out" is forbidden'
|
||||
);
|
||||
},
|
||||
addStage: (stage: Document) => {
|
||||
this.assertReadOnlyPipeline([stage]);
|
||||
cursor.addStage(stage);
|
||||
return wrapped;
|
||||
},
|
||||
lookup: ($lookup: Document) => {
|
||||
this.assertReadOnlyPipeline([{ $lookup }]);
|
||||
cursor.lookup($lookup);
|
||||
return wrapped;
|
||||
},
|
||||
match: ($match: Document) => {
|
||||
cursor.match($match);
|
||||
return wrapped;
|
||||
},
|
||||
project: ($project: Document) => {
|
||||
cursor.project($project);
|
||||
return wrapped;
|
||||
},
|
||||
group: ($group: Document) => {
|
||||
cursor.group($group);
|
||||
return wrapped;
|
||||
},
|
||||
sort: ($sort: Parameters<AggregationCursor<T>['sort']>[0]) => {
|
||||
cursor.sort($sort);
|
||||
return wrapped;
|
||||
},
|
||||
limit: ($limit: number) => {
|
||||
cursor.limit($limit);
|
||||
return wrapped;
|
||||
},
|
||||
skip: ($skip: number) => {
|
||||
cursor.skip($skip);
|
||||
return wrapped;
|
||||
},
|
||||
unwind: ($unwind: Document | string) => {
|
||||
cursor.unwind($unwind);
|
||||
return wrapped;
|
||||
},
|
||||
redact: ($redact: Document) => {
|
||||
cursor.redact($redact);
|
||||
return wrapped;
|
||||
},
|
||||
geoNear: ($geoNear: Document) => {
|
||||
cursor.geoNear($geoNear);
|
||||
return wrapped;
|
||||
},
|
||||
// Fluent cursor modifiers
|
||||
batchSize: (value: number) => {
|
||||
cursor.batchSize(value);
|
||||
return wrapped;
|
||||
},
|
||||
maxTimeMS: (value: number) => {
|
||||
cursor.maxTimeMS(value);
|
||||
return wrapped;
|
||||
},
|
||||
withReadPreference: (value: Parameters<AggregationCursor<T>['withReadPreference']>[0]) => {
|
||||
cursor.withReadPreference(value);
|
||||
return wrapped;
|
||||
},
|
||||
withReadConcern: (value: Parameters<AggregationCursor<T>['withReadConcern']>[0]) => {
|
||||
cursor.withReadConcern(value);
|
||||
return wrapped;
|
||||
},
|
||||
addCursorFlag: (
|
||||
flag: Parameters<AggregationCursor<T>['addCursorFlag']>[0],
|
||||
value: boolean
|
||||
) => {
|
||||
cursor.addCursorFlag(flag, value);
|
||||
return wrapped;
|
||||
},
|
||||
map: <TMapped>(transform: (doc: T) => TMapped) => {
|
||||
const mapped = cursor.map(transform);
|
||||
return mapped === (cursor as unknown)
|
||||
? wrapped
|
||||
: this.wrapReadOnlyAggregationCursor(mapped as unknown as AggregationCursor<Document>);
|
||||
},
|
||||
clone: () => this.wrapReadOnlyAggregationCursor(cursor.clone()),
|
||||
};
|
||||
|
||||
return wrapped as unknown as AggregationCursor<T>;
|
||||
}
|
||||
|
||||
private wrapReadOnlyCollection<T extends Document>(collection: Collection<T>): Collection<T> {
|
||||
return new Proxy(collection, {
|
||||
get: (target, prop, receiver) => {
|
||||
|
|
@ -192,9 +357,15 @@ export class MongoDBClient {
|
|||
return Reflect.get(target, prop, receiver);
|
||||
}
|
||||
if (prop === 'aggregate') {
|
||||
return (pipeline: Document[] = [], options?: AggregateOptions) => {
|
||||
this.assertReadOnlyPipeline(pipeline);
|
||||
return target.aggregate(pipeline, options);
|
||||
return <TDoc extends Document = Document>(
|
||||
pipeline: Document[] = [],
|
||||
options?: AggregateOptions
|
||||
) => {
|
||||
const safePipeline = [...pipeline];
|
||||
this.assertReadOnlyPipeline(safePipeline);
|
||||
return this.wrapReadOnlyAggregationCursor(
|
||||
target.aggregate<TDoc>(safePipeline, options)
|
||||
);
|
||||
};
|
||||
}
|
||||
if (READ_ONLY_COLLECTION_METHODS.has(prop)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue