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(

View file

@ -825,32 +825,50 @@ describe('MongoDBClient read-only mode (offline)', () => {
expect(firstReceivers.length).toBe(docs.length);
expect(secondReceivers.length).toBe(docs.length);
// clone() after map() stays sealed and a map() on the clone binds `this` to the cloned wrapper
// clone() after map() calls the pinned driver's real clone(), stays sealed, leaves the
// original transform uncalled when iterating the clone, and binds a new map() on the clone
// to the cloned wrapper
const rawOriginal = createRaw();
const wrappedOriginal = wrapRawCursor(kind, rawOriginal) as {
map: (fn: (doc: unknown) => unknown) => {
clone: () => {
toArray: () => Promise<unknown[]>;
map: (fn: (doc: unknown) => unknown) => { toArray: () => Promise<unknown[]> };
};
};
};
const originalTransformCalls: unknown[] = [];
const mappedOriginal = wrappedOriginal.map(function (this: unknown, doc: unknown) {
return doc;
originalTransformCalls.push(this);
assertGuardedReceiver(this, mappedOriginal, rawOriginal, kind === 'aggregate');
return { original: true, doc };
});
const rawClone = createRaw();
seedCursorDocuments(rawClone, docs);
(rawOriginal as Record<string, unknown>).clone = mock(() => rawClone);
const cloned = mappedOriginal.clone();
expect((cloned as unknown as Record<string, unknown>).client).toBeUndefined();
expect((cloned as unknown as Record<string, unknown>).parent).toBeUndefined();
const realCloneFn = (rawOriginal as { clone: () => object }).clone;
let capturedRawClone: object | undefined;
(rawOriginal as Record<string, unknown>).clone = () => {
const realCloned = realCloneFn.call(rawOriginal);
capturedRawClone = realCloned;
seedCursorDocuments(realCloned, docs);
return realCloned;
};
const clonedForRead = mappedOriginal.clone();
expect(capturedRawClone).toBeDefined();
expect((clonedForRead as unknown as Record<string, unknown>).client).toBeUndefined();
expect((clonedForRead as unknown as Record<string, unknown>).parent).toBeUndefined();
expect(await clonedForRead.toArray()).toEqual(docs);
expect(originalTransformCalls.length).toBe(0);
const clonedForMap = mappedOriginal.clone();
const rawCloneForMap = capturedRawClone!;
const cloneReceivers: unknown[] = [];
const mappedClone = cloned.map(function (this: unknown, doc: unknown) {
const mappedClone = clonedForMap.map(function (this: unknown, doc: unknown) {
cloneReceivers.push(this);
assertGuardedReceiver(this, mappedClone, rawClone, kind === 'aggregate');
assertGuardedReceiver(this, mappedClone, rawCloneForMap, kind === 'aggregate');
return { cloned: true, doc };
});
expect(await mappedClone.toArray()).toEqual(docs.map(doc => ({ cloned: true, doc })));
expect(cloneReceivers.length).toBe(docs.length);
expect(originalTransformCalls.length).toBe(0);
// forEach() also binds `this` to the wrapped cursor (never rawCursor)
const rawForEach = createRaw();
@ -1024,6 +1042,41 @@ describe('MongoDBClient read-only mode (offline)', () => {
}
});
test('hides raw MongoClient/Db/Collection via property descriptor reflection on read-only proxies', () => {
const logger = getLogger('mongodb-read-only-reflection-test');
const realMongoClient = new MongoClient('mongodb://127.0.0.1:1/stock');
const realDb = realMongoClient.db('stock');
const wrappedClient = new MongoDBClient(
{ host: '127.0.0.1', port: 1, database: 'stock', readOnly: true },
logger
);
Object.assign(wrappedClient, {
client: realMongoClient,
db: realDb,
isConnected: true,
});
const roDb = wrappedClient.getDatabase();
const roCollection = wrappedClient.getCollection('eodPrices');
for (const key of ['client', 's'] as const) {
expect(Object.getOwnPropertyDescriptor(roDb, key)).toBeUndefined();
}
for (const key of ['client', 'db', 's'] as const) {
expect(Object.getOwnPropertyDescriptor(roCollection, key)).toBeUndefined();
}
expect(Reflect.ownKeys(roDb)).toEqual([]);
expect(Reflect.ownKeys(roCollection)).toEqual([]);
// Safe read-only metadata getters still resolve properly on the pinned driver
expect(roDb.databaseName).toBe('stock');
expect(roDb.namespace).toBe('stock');
expect(roCollection.collectionName).toBe('eodPrices');
expect(roCollection.dbName).toBe('stock');
expect(roCollection.namespace).toBe('stock.eodPrices');
});
test('passes through safe read operations on client and proxies', async () => {
const { client, dbStub, collectionStub } = createStubbedClient(true);