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
3bbf25ca25
commit
91e087c42d
2 changed files with 456 additions and 15 deletions
|
|
@ -220,8 +220,13 @@ export class MongoDBClient {
|
|||
): Readable {
|
||||
beforeRead?.();
|
||||
const transform = options?.transform;
|
||||
const closeCursor = (): Promise<unknown> =>
|
||||
typeof cursor.close === 'function' ? cursor.close() : Promise.resolve();
|
||||
let closePromise: Promise<unknown> | null = null;
|
||||
const closeCursor = (): Promise<unknown> => {
|
||||
if (!closePromise) {
|
||||
closePromise = typeof cursor.close === 'function' ? cursor.close() : Promise.resolve();
|
||||
}
|
||||
return closePromise;
|
||||
};
|
||||
let readInProgress = false;
|
||||
|
||||
return new Readable({
|
||||
|
|
@ -246,7 +251,18 @@ export class MongoDBClient {
|
|||
doc => {
|
||||
if (doc == null) {
|
||||
readInProgress = false;
|
||||
this.push(null);
|
||||
closeCursor().then(
|
||||
() => {
|
||||
if (!this.destroyed) {
|
||||
this.push(null);
|
||||
}
|
||||
},
|
||||
closeError => {
|
||||
this.destroy(
|
||||
closeError instanceof Error ? closeError : new Error(String(closeError))
|
||||
);
|
||||
}
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (this.destroyed) {
|
||||
|
|
@ -263,6 +279,10 @@ export class MongoDBClient {
|
|||
this.destroy(err instanceof Error ? err : new Error(String(err)));
|
||||
return;
|
||||
}
|
||||
if (output == null) {
|
||||
readNext();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (this.push(output)) {
|
||||
readNext();
|
||||
|
|
@ -478,7 +498,16 @@ export class MongoDBClient {
|
|||
next: delegate('next'),
|
||||
tryNext: delegate('tryNext'),
|
||||
hasNext: delegate('hasNext'),
|
||||
forEach: delegate('forEach'),
|
||||
forEach: (iterator: (doc: unknown) => boolean | void) => {
|
||||
const fn = rawCursor.forEach;
|
||||
if (typeof fn !== 'function') {
|
||||
throw new TypeError('Cursor does not implement "forEach"');
|
||||
}
|
||||
if (typeof iterator !== 'function') {
|
||||
return fn.call(cursor, iterator);
|
||||
}
|
||||
return fn.call(cursor, (doc: unknown) => iterator.call(wrapped, doc));
|
||||
},
|
||||
count: delegate('count'),
|
||||
explain: delegate('explain'),
|
||||
close: delegate('close'),
|
||||
|
|
@ -513,12 +542,20 @@ export class MongoDBClient {
|
|||
if (typeof fn !== 'function') {
|
||||
throw new TypeError('Cursor does not implement "map"');
|
||||
}
|
||||
const mapped = fn.call(cursor, transform);
|
||||
return mapped === cursor
|
||||
? wrapped
|
||||
: mapped && typeof mapped === 'object'
|
||||
? this.wrapReadOnlyCursor(mapped as object)
|
||||
: mapped;
|
||||
let receiver: unknown;
|
||||
const guardedTransform =
|
||||
typeof transform === 'function'
|
||||
? (doc: unknown) => transform.call(receiver, doc)
|
||||
: transform;
|
||||
const mapped = fn.call(cursor, guardedTransform);
|
||||
const result =
|
||||
mapped === cursor
|
||||
? wrapped
|
||||
: mapped && typeof mapped === 'object'
|
||||
? this.wrapReadOnlyCursor(mapped as object)
|
||||
: mapped;
|
||||
receiver = result;
|
||||
return result;
|
||||
},
|
||||
clone: () => {
|
||||
const fn = rawCursor.clone;
|
||||
|
|
@ -606,7 +643,10 @@ export class MongoDBClient {
|
|||
},
|
||||
forEach: (iterator: (doc: T) => boolean | void) => {
|
||||
assertCursorPipelineSafe();
|
||||
return cursor.forEach(iterator);
|
||||
if (typeof iterator !== 'function') {
|
||||
return cursor.forEach(iterator);
|
||||
}
|
||||
return cursor.forEach(doc => iterator.call(wrapped, doc));
|
||||
},
|
||||
stream: (options?: Parameters<AggregationCursor<T>['stream']>[0]) =>
|
||||
this.createGuardedCursorStream(
|
||||
|
|
@ -711,10 +751,18 @@ export class MongoDBClient {
|
|||
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>);
|
||||
let receiver: unknown;
|
||||
const guardedTransform =
|
||||
typeof transform === 'function'
|
||||
? (doc: T) => transform.call(receiver, doc)
|
||||
: transform;
|
||||
const mapped = cursor.map(guardedTransform);
|
||||
const result =
|
||||
mapped === (cursor as unknown)
|
||||
? wrapped
|
||||
: this.wrapReadOnlyAggregationCursor(mapped as unknown as AggregationCursor<Document>);
|
||||
receiver = result;
|
||||
return result;
|
||||
},
|
||||
clone: () => this.wrapReadOnlyAggregationCursor(cursor.clone()),
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue