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

@ -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);