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
abbab4b5a8
commit
0e5ebbd62f
9 changed files with 425 additions and 29 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { Readable } from 'node:stream';
|
||||
import type {
|
||||
AggregateOptions,
|
||||
AggregationCursor,
|
||||
|
|
@ -241,9 +242,75 @@ export class MongoDBClient {
|
|||
assertCursorPipelineSafe();
|
||||
return cursor.forEach(iterator);
|
||||
},
|
||||
stream: (...args: Parameters<AggregationCursor<T>['stream']>) => {
|
||||
stream: (options?: Parameters<AggregationCursor<T>['stream']>[0]) => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.stream(...args);
|
||||
const transform = options?.transform;
|
||||
let readInProgress = false;
|
||||
|
||||
const readable = new Readable({
|
||||
objectMode: true,
|
||||
autoDestroy: false,
|
||||
highWaterMark: 1,
|
||||
read() {
|
||||
if (readInProgress) {
|
||||
return;
|
||||
}
|
||||
readInProgress = true;
|
||||
const readNext = (): void => {
|
||||
try {
|
||||
assertCursorPipelineSafe();
|
||||
} catch (err) {
|
||||
readInProgress = false;
|
||||
this.destroy(err instanceof Error ? err : new Error(String(err)));
|
||||
return;
|
||||
}
|
||||
|
||||
cursor.next().then(
|
||||
doc => {
|
||||
if (doc == null) {
|
||||
readInProgress = false;
|
||||
this.push(null);
|
||||
return;
|
||||
}
|
||||
if (this.destroyed) {
|
||||
readInProgress = false;
|
||||
cursor.close().catch(() => {});
|
||||
return;
|
||||
}
|
||||
let output: unknown = doc;
|
||||
if (transform) {
|
||||
try {
|
||||
output = transform(doc);
|
||||
} catch (err) {
|
||||
readInProgress = false;
|
||||
this.destroy(err instanceof Error ? err : new Error(String(err)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (this.push(output)) {
|
||||
readNext();
|
||||
} else {
|
||||
readInProgress = false;
|
||||
}
|
||||
},
|
||||
err => {
|
||||
readInProgress = false;
|
||||
this.destroy(err instanceof Error ? err : new Error(String(err)));
|
||||
}
|
||||
);
|
||||
};
|
||||
readNext();
|
||||
},
|
||||
destroy(error, callback) {
|
||||
cursor.close().then(
|
||||
() => callback(error),
|
||||
closeError =>
|
||||
callback(closeError instanceof Error ? closeError : new Error(String(closeError)))
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
return readable as ReturnType<AggregationCursor<T>['stream']>;
|
||||
},
|
||||
explain: (...args: Parameters<AggregationCursor<T>['explain']>) => {
|
||||
assertCursorPipelineSafe();
|
||||
|
|
@ -345,6 +412,69 @@ export class MongoDBClient {
|
|||
: this.wrapReadOnlyAggregationCursor(mapped as unknown as AggregationCursor<Document>);
|
||||
},
|
||||
clone: () => this.wrapReadOnlyAggregationCursor(cursor.clone()),
|
||||
// Safe EventEmitter delegation without exposing the raw driver cursor
|
||||
on: (event: string | symbol, listener: (...args: unknown[]) => void) => {
|
||||
if (typeof cursor.on === 'function') {
|
||||
cursor.on(event as never, listener as never);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
once: (event: string | symbol, listener: (...args: unknown[]) => void) => {
|
||||
if (typeof cursor.once === 'function') {
|
||||
cursor.once(event as never, listener as never);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
off: (event: string | symbol, listener: (...args: unknown[]) => void) => {
|
||||
if (typeof cursor.off === 'function') {
|
||||
cursor.off(event as never, listener as never);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
addListener: (event: string | symbol, listener: (...args: unknown[]) => void) => {
|
||||
if (typeof cursor.addListener === 'function') {
|
||||
cursor.addListener(event as never, listener as never);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
removeListener: (event: string | symbol, listener: (...args: unknown[]) => void) => {
|
||||
if (typeof cursor.removeListener === 'function') {
|
||||
cursor.removeListener(event as never, listener as never);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
removeAllListeners: (event?: string | symbol) => {
|
||||
if (typeof cursor.removeAllListeners === 'function') {
|
||||
cursor.removeAllListeners(event as never);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
prependListener: (event: string | symbol, listener: (...args: unknown[]) => void) => {
|
||||
if (typeof cursor.prependListener === 'function') {
|
||||
cursor.prependListener(event as never, listener as never);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
prependOnceListener: (event: string | symbol, listener: (...args: unknown[]) => void) => {
|
||||
if (typeof cursor.prependOnceListener === 'function') {
|
||||
cursor.prependOnceListener(event as never, listener as never);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
setMaxListeners: (n: number) => {
|
||||
if (typeof cursor.setMaxListeners === 'function') {
|
||||
cursor.setMaxListeners(n);
|
||||
}
|
||||
return wrapped;
|
||||
},
|
||||
getMaxListeners: () => (typeof cursor.getMaxListeners === 'function' ? cursor.getMaxListeners() : 10),
|
||||
listeners: (event: string | symbol) =>
|
||||
typeof cursor.listeners === 'function' ? cursor.listeners(event as never) : [],
|
||||
rawListeners: (event: string | symbol) =>
|
||||
typeof cursor.rawListeners === 'function' ? cursor.rawListeners(event as never) : [],
|
||||
listenerCount: (event: string | symbol) =>
|
||||
typeof cursor.listenerCount === 'function' ? cursor.listenerCount(event as never) : 0,
|
||||
eventNames: () => (typeof cursor.eventNames === 'function' ? cursor.eventNames() : []),
|
||||
};
|
||||
|
||||
return wrapped as unknown as AggregationCursor<T>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue