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
75e459f50b
commit
3bbf25ca25
3 changed files with 551 additions and 137 deletions
|
|
@ -139,6 +139,10 @@ describe('Environment safety & multi-env isolation', () => {
|
|||
'MONGODB_READ_URI',
|
||||
'MONGODB_DATABASE',
|
||||
'QUEUE_ENABLE_SCHEDULED_JOBS',
|
||||
'HTTP_PROXY',
|
||||
'HTTPS_PROXY',
|
||||
'http_proxy',
|
||||
'https_proxy',
|
||||
] as const;
|
||||
const savedEnv: Partial<Record<(typeof envKeys)[number], string | undefined>> = {};
|
||||
for (const key of envKeys) {
|
||||
|
|
@ -158,6 +162,10 @@ describe('Environment safety & multi-env isolation', () => {
|
|||
'mongodb://studio_stock_bot_read:secret@localhost:27017/stock?authSource=stock';
|
||||
process.env.MONGODB_DATABASE = 'stock_dev';
|
||||
delete process.env.QUEUE_ENABLE_SCHEDULED_JOBS;
|
||||
delete process.env.HTTP_PROXY;
|
||||
delete process.env.HTTPS_PROXY;
|
||||
delete process.env.http_proxy;
|
||||
delete process.env.https_proxy;
|
||||
|
||||
resetStockConfig();
|
||||
const cfg = initializeStockConfig('dataPipeline', { mongoReadOnly: true });
|
||||
|
|
|
|||
|
|
@ -414,6 +414,223 @@ describe('MongoDBClient read-only mode (offline)', () => {
|
|||
expect(closeMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('seals find(), listIndexes(), and listCollections() cursors against pinned-driver MongoClient/Db/Collection leaks', async () => {
|
||||
const logger = getLogger('mongodb-read-only-read-cursors-test');
|
||||
// Unconnected real driver client — zero sockets opened
|
||||
const realMongoClient = new MongoClient('mongodb://127.0.0.1:1/stock');
|
||||
const realDb = realMongoClient.db('stock');
|
||||
const realCollection = realDb.collection('eodPrices');
|
||||
|
||||
const roClient = new MongoDBClient(
|
||||
{ host: '127.0.0.1', port: 1, database: 'stock', readOnly: true },
|
||||
logger
|
||||
);
|
||||
Object.assign(roClient, {
|
||||
client: realMongoClient,
|
||||
db: realDb,
|
||||
isConnected: true,
|
||||
});
|
||||
|
||||
const assertCursorSealed = (
|
||||
cursorObj: unknown,
|
||||
forbiddenRefs: unknown[]
|
||||
): void => {
|
||||
const record = cursorObj as Record<PropertyKey, unknown>;
|
||||
expect(record.client).toBeUndefined();
|
||||
expect(record.cursorClient).toBeUndefined();
|
||||
expect(record.parent).toBeUndefined();
|
||||
expect(record.session).toBeUndefined();
|
||||
expect(record.cursorSession).toBeUndefined();
|
||||
expect(record.server).toBeUndefined();
|
||||
expect(record.selectedServer).toBeUndefined();
|
||||
expect(record.s).toBeUndefined();
|
||||
for (const key of Reflect.ownKeys(cursorObj as object)) {
|
||||
const value = record[key];
|
||||
for (const ref of forbiddenRefs) {
|
||||
expect(value).not.toBe(ref);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 1. Direct cursors from find(), listIndexes(), and listCollections() do not expose raw driver objects
|
||||
const findCursor = roClient.getCollection('eodPrices').find({});
|
||||
const listIndexesCursor = roClient.getCollection('eodPrices').listIndexes();
|
||||
const listCollectionsCursor = roClient.getDatabase().listCollections();
|
||||
|
||||
assertCursorSealed(findCursor, [realMongoClient, realDb, realCollection]);
|
||||
assertCursorSealed(listIndexesCursor, [realMongoClient, realDb, realCollection]);
|
||||
assertCursorSealed(listCollectionsCursor, [realMongoClient, realDb, realCollection]);
|
||||
|
||||
// 2. Fluent modifiers, clone(), and map() on find() remain sealed
|
||||
const chainedFind = findCursor.sort({ date: -1 }).limit(5).batchSize(10);
|
||||
expect(chainedFind).toBe(findCursor);
|
||||
assertCursorSealed(chainedFind, [realMongoClient, realDb, realCollection]);
|
||||
|
||||
const clonedFind = chainedFind.clone();
|
||||
expect(clonedFind).not.toBe(chainedFind);
|
||||
assertCursorSealed(clonedFind, [realMongoClient, realDb, realCollection]);
|
||||
|
||||
const mappedFind = chainedFind.map(doc => doc);
|
||||
assertCursorSealed(mappedFind, [realMongoClient, realDb, realCollection]);
|
||||
|
||||
// 3 & 4 & 5. Verify guarded stream(), EventEmitter `this` binding, and safe reads on real stubbed cursors
|
||||
const cursorCases = [
|
||||
{
|
||||
label: 'find',
|
||||
createRaw: () => realCollection.find({}),
|
||||
attachAndOpen: (raw: ReturnType<typeof realCollection.find>) => {
|
||||
const fakeDb = {
|
||||
databaseName: 'stock',
|
||||
collection: mock(() => ({
|
||||
collectionName: 'eodPrices',
|
||||
find: mock(() => raw),
|
||||
})),
|
||||
};
|
||||
const wrappedClient = new MongoDBClient(
|
||||
{ host: '127.0.0.1', port: 1, database: 'stock', readOnly: true },
|
||||
logger
|
||||
);
|
||||
Object.assign(wrappedClient, {
|
||||
client: { db: mock(() => fakeDb) },
|
||||
db: fakeDb,
|
||||
isConnected: true,
|
||||
});
|
||||
return wrappedClient.getCollection('eodPrices').find({});
|
||||
},
|
||||
docs: [
|
||||
{ _id: '1', symbol: 'AAPL', close: 200 },
|
||||
{ _id: '2', symbol: 'MSFT', close: 400 },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: 'listIndexes',
|
||||
createRaw: () => realCollection.listIndexes(),
|
||||
attachAndOpen: (raw: ReturnType<typeof realCollection.listIndexes>) => {
|
||||
const fakeDb = {
|
||||
databaseName: 'stock',
|
||||
collection: mock(() => ({
|
||||
collectionName: 'eodPrices',
|
||||
listIndexes: mock(() => raw),
|
||||
})),
|
||||
};
|
||||
const wrappedClient = new MongoDBClient(
|
||||
{ host: '127.0.0.1', port: 1, database: 'stock', readOnly: true },
|
||||
logger
|
||||
);
|
||||
Object.assign(wrappedClient, {
|
||||
client: { db: mock(() => fakeDb) },
|
||||
db: fakeDb,
|
||||
isConnected: true,
|
||||
});
|
||||
return wrappedClient.getCollection('eodPrices').listIndexes();
|
||||
},
|
||||
docs: [{ name: '_id_', key: { _id: 1 } }],
|
||||
},
|
||||
{
|
||||
label: 'listCollections',
|
||||
createRaw: () => realDb.listCollections(),
|
||||
attachAndOpen: (raw: ReturnType<typeof realDb.listCollections>) => {
|
||||
const fakeDb = {
|
||||
databaseName: 'stock',
|
||||
listCollections: mock(() => raw),
|
||||
};
|
||||
const wrappedClient = new MongoDBClient(
|
||||
{ host: '127.0.0.1', port: 1, database: 'stock', readOnly: true },
|
||||
logger
|
||||
);
|
||||
Object.assign(wrappedClient, {
|
||||
client: { db: mock(() => fakeDb) },
|
||||
db: fakeDb,
|
||||
isConnected: true,
|
||||
});
|
||||
return wrappedClient.getDatabase().listCollections();
|
||||
},
|
||||
docs: [{ name: 'eodPrices', type: 'collection' }],
|
||||
},
|
||||
];
|
||||
|
||||
for (const c of cursorCases) {
|
||||
const rawCursor = c.createRaw();
|
||||
// Confirm raw pinned driver cursor does expose the raw MongoClient
|
||||
expect((rawCursor as unknown as Record<string, unknown>).client).toBe(realMongoClient);
|
||||
|
||||
let nextIndex = 0;
|
||||
const closeMock = mock(async () => {});
|
||||
rawCursor.next = mock(async () =>
|
||||
nextIndex < c.docs.length ? c.docs[nextIndex++] : null
|
||||
) as never;
|
||||
rawCursor.toArray = mock(async () => [...c.docs]) as never;
|
||||
rawCursor.close = closeMock as never;
|
||||
(rawCursor as unknown as Record<symbol, unknown>)[Symbol.asyncIterator] = async function* () {
|
||||
for (const doc of c.docs) {
|
||||
yield doc;
|
||||
}
|
||||
};
|
||||
|
||||
const wrappedCursor = c.attachAndOpen(rawCursor as never);
|
||||
assertCursorSealed(wrappedCursor, [realMongoClient, realDb, realCollection, rawCursor]);
|
||||
|
||||
// Stream does not expose `_cursor`, raw cursor, or raw MongoClient
|
||||
const stream = wrappedCursor.stream();
|
||||
const streamRecord = stream as unknown as Record<PropertyKey, unknown>;
|
||||
expect(streamRecord._cursor).toBeUndefined();
|
||||
for (const key of Reflect.ownKeys(stream)) {
|
||||
expect(streamRecord[key]).not.toBe(rawCursor);
|
||||
expect(streamRecord[key]).not.toBe(realMongoClient);
|
||||
}
|
||||
const streamed: unknown[] = [];
|
||||
for await (const doc of stream) {
|
||||
streamed.push(doc);
|
||||
}
|
||||
expect(streamed).toEqual(c.docs);
|
||||
|
||||
// Destroying stream calls close()
|
||||
nextIndex = 0;
|
||||
const destroyable = wrappedCursor.stream();
|
||||
destroyable.destroy();
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
expect(closeMock).toHaveBeenCalledTimes(1);
|
||||
|
||||
// EventEmitter listeners receive wrappedCursor as `this` (never rawCursor)
|
||||
let onCalls = 0;
|
||||
let onceCalls = 0;
|
||||
function onClose(this: unknown) {
|
||||
onCalls++;
|
||||
expect(this).toBe(wrappedCursor);
|
||||
expect(this).not.toBe(rawCursor);
|
||||
expect((this as Record<string, unknown>).client).toBeUndefined();
|
||||
}
|
||||
function onceClose(this: unknown) {
|
||||
onceCalls++;
|
||||
expect(this).toBe(wrappedCursor);
|
||||
expect(this).not.toBe(rawCursor);
|
||||
expect((this as Record<string, unknown>).client).toBeUndefined();
|
||||
}
|
||||
wrappedCursor.on('close', onClose);
|
||||
wrappedCursor.once('close', onceClose);
|
||||
expect(wrappedCursor.listeners('close')).toEqual([onClose, onceClose]);
|
||||
|
||||
rawCursor.emit('close');
|
||||
expect(onCalls).toBe(1);
|
||||
expect(onceCalls).toBe(1);
|
||||
expect(wrappedCursor.listeners('close')).toEqual([onClose]);
|
||||
|
||||
wrappedCursor.off('close', onClose);
|
||||
rawCursor.emit('close');
|
||||
expect(onCalls).toBe(1);
|
||||
|
||||
// Safe read methods work on real stubbed cursors
|
||||
expect(await wrappedCursor.toArray()).toEqual(c.docs);
|
||||
nextIndex = 0;
|
||||
expect(await wrappedCursor.next()).toEqual(c.docs[0]);
|
||||
const iterated: unknown[] = [];
|
||||
for await (const doc of wrappedCursor) {
|
||||
iterated.push(doc);
|
||||
}
|
||||
expect(iterated).toEqual(c.docs);
|
||||
}
|
||||
});
|
||||
|
||||
test('passes through safe read operations on client and proxies', async () => {
|
||||
const { client, dbStub, collectionStub } = createStubbedClient(true);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue