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:
Studio 2026-11-27 02:26:02 -05:00
parent 91e087c42d
commit a8153e116a
2 changed files with 79 additions and 26 deletions

View file

@ -776,10 +776,10 @@ export class MongoDBClient {
}
private wrapReadOnlyCollection<T extends Document>(collection: Collection<T>): Collection<T> {
return new Proxy(collection, {
get: (target, prop, receiver) => {
return new Proxy({} as Collection<T>, {
get: (_target, prop) => {
if (typeof prop === 'symbol' || READ_ONLY_COLLECTION_PROPS.has(prop)) {
return Reflect.get(target, prop, receiver);
return Reflect.get(collection, prop, collection);
}
if (prop === 'aggregate') {
return <TDoc extends Document = Document>(
@ -789,15 +789,15 @@ export class MongoDBClient {
const safePipeline = [...pipeline];
this.assertReadOnlyPipeline(safePipeline);
return this.wrapReadOnlyAggregationCursor(
target.aggregate<TDoc>(safePipeline, options)
collection.aggregate<TDoc>(safePipeline, options)
);
};
}
if (prop === 'find' || prop === 'listIndexes') {
const fn: unknown = Reflect.get(target, prop, target);
const fn: unknown = Reflect.get(collection, prop, collection);
if (typeof fn === 'function') {
return (...args: unknown[]) => {
const cursor = (fn as (...methodArgs: unknown[]) => unknown).apply(target, args);
const cursor = (fn as (...methodArgs: unknown[]) => unknown).apply(collection, args);
return cursor && typeof cursor === 'object'
? this.wrapReadOnlyCursor(cursor as object)
: cursor;
@ -806,8 +806,8 @@ export class MongoDBClient {
return fn;
}
if (READ_ONLY_COLLECTION_METHODS.has(prop)) {
const fn = Reflect.get(target, prop, target);
return typeof fn === 'function' ? fn.bind(target) : fn;
const fn = Reflect.get(collection, prop, collection);
return typeof fn === 'function' ? fn.bind(collection) : fn;
}
return () => {
throw new Error(
@ -819,20 +819,20 @@ export class MongoDBClient {
}
private wrapReadOnlyDatabase(db: Db): Db {
return new Proxy(db, {
get: (target, prop, receiver) => {
return new Proxy({} as Db, {
get: (_target, prop) => {
if (typeof prop === 'symbol' || READ_ONLY_DB_PROPS.has(prop)) {
return Reflect.get(target, prop, receiver);
return Reflect.get(db, prop, db);
}
if (prop === 'collection') {
return <T extends Document = Document>(name: string) =>
this.wrapReadOnlyCollection(target.collection<T>(name));
this.wrapReadOnlyCollection(db.collection<T>(name));
}
if (prop === 'listCollections') {
const fn: unknown = Reflect.get(target, prop, target);
const fn: unknown = Reflect.get(db, prop, db);
if (typeof fn === 'function') {
return (...args: unknown[]) => {
const cursor = (fn as (...methodArgs: unknown[]) => unknown).apply(target, args);
const cursor = (fn as (...methodArgs: unknown[]) => unknown).apply(db, args);
return cursor && typeof cursor === 'object'
? this.wrapReadOnlyCursor(cursor as object)
: cursor;
@ -841,8 +841,8 @@ export class MongoDBClient {
return fn;
}
if (READ_ONLY_DB_METHODS.has(prop)) {
const fn = Reflect.get(target, prop, target);
return typeof fn === 'function' ? fn.bind(target) : fn;
const fn = Reflect.get(db, prop, db);
return typeof fn === 'function' ? fn.bind(db) : fn;
}
return () => {
throw new Error(