From 5a23e862afce92f8fce83f1e54727ce897eb787d Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 27 Nov 2026 14:12:53 -0500 Subject: [PATCH] T-29: tests --- test/integration/research-agents-api.test.ts | 12 ++++ .../environment-safety-encoded-host.test.ts | 47 ++++++++++++++ test/unit/mongodb-read-only.test.ts | 64 +++++++++++++++++++ 3 files changed, 123 insertions(+) diff --git a/test/integration/research-agents-api.test.ts b/test/integration/research-agents-api.test.ts index da6089a6..f42cb576 100644 --- a/test/integration/research-agents-api.test.ts +++ b/test/integration/research-agents-api.test.ts @@ -336,6 +336,9 @@ test('resolver scopes GET and HEAD reads to granted projects, templates and owne `/api/v2/experiments/validation/${ownedExperiment}`, `/api/v2/experiments/walk-forward/${ownedWalkForward}`, `/api/v2/experiments/compare?leftId=${ownedExperiment}&rightId=${ownedExperiment2}`, + `/api/v2/experiments/compare?leftId=${ownedExperiment.toUpperCase()}&rightId=${ownedExperiment2.toUpperCase()}`, + `/api/v2/research-projects/${grantedProject.toUpperCase()}`, + `/api/v2/backtests/${grantedBacktest.toUpperCase()}`, `/api/v2/runs/${ownedRun}`, `/api/v2/runs/${ownedRun}/results`, '/api/v2/strategies', @@ -378,6 +381,15 @@ test('resolver scopes GET and HEAD reads to granted projects, templates and owne `/api/v2/experiments/compare?leftId=${ownedExperiment}`, `/api/v2/experiments/compare?leftId=${ownedExperiment}&leftId=${ownedExperiment2}&rightId=${ownedExperiment2}`, '/api/v2/experiments/compare?leftId=not-a-uuid&rightId=not-a-uuid', + '/api/v2/experiments/compare', + `/api/v2/experiments/compare?rightId=${ownedExperiment2}`, + `/api/v2/experiments/compare?leftId=&rightId=${ownedExperiment2}`, + `/api/v2/experiments/compare?leftId=${ownedExperiment}&rightId=`, + '/api/v2/experiments/compare?leftId=&rightId=', + `/api/v2/experiments/compare?leftId=${ownedExperiment}&rightId=${ownedExperiment2}&rightId=${ownedExperiment2}`, + `/api/v2/experiments/compare?leftId=${foreignExperiment}&rightId=${ownedExperiment2}`, + `/api/v2/research-projects/${ungrantedProject.toUpperCase()}`, + `/api/v2/backtests/${ungrantedBacktest.toUpperCase()}`, `/api/v2/runs/${foreignRun}`, `/api/v2/runs/${foreignRun}/results`, '/api/v2/runs/not-a-uuid', diff --git a/test/unit/environment-safety-encoded-host.test.ts b/test/unit/environment-safety-encoded-host.test.ts index 0cab3edc..7cf23e23 100644 --- a/test/unit/environment-safety-encoded-host.test.ts +++ b/test/unit/environment-safety-encoded-host.test.ts @@ -649,6 +649,53 @@ describe('Environment safety — percent-encoded MongoDB seed hosts', () => { } }); + test('fails closed with an explicit error on percent-encoded ASCII control characters (including non-whitespace NUL, 0x1f, DEL) in MongoDB host', () => { + // %00, %01, %1f and %7f are not matched by \s, so only the explicit control-char check + // in decodeMongoHost rejects them (T-29 moved that check out of the forbidden-char regex). + const smuggledUris = [ + 'mongodb://u:p@localhost%00:27018/stock_dev', + 'mongodb://u:p@loc%0aalhost:27018/stock_dev', + 'mongodb://u:p@localhost%1f:27018/stock_dev', + 'mongodb://u:p@localhost%7f:27018/stock_dev', + 'mongodb://u:p@localhost%7F:27018/stock_dev', + 'mongodb://u:p@local%01host:27018/stock_dev', + 'mongodb://u:p@localhost%2500:27018/stock_dev', + 'mongodb://u:p@localhost%257f:27018/stock_dev', + 'mongodb://u:p@mongo.internal:27018,localhost%1f:27018/stock_dev', + 'mongodb://u:p@[::1%00]:27018/stock_dev', + 'mongodb://u:p@[::1%7f]:27018/stock_dev', + ]; + + for (const mongoUri of smuggledUris) { + const cfg = makeConfig({ + environment: 'development', + mongoUri, + mongoDatabase: 'stock_dev', + bypassSchemaValidation: true, + }); + expect(() => assertEnvironmentSafety(cfg, 'dataIngestion', {}), mongoUri).toThrow( + /Safety guard: invalid characters in MongoDB host/ + ); + expect( + () => + assertEnvironmentSafety(cfg, 'dataIngestion', { + STOCK_BOT_ALLOW_DEV_QA_MONGO_WRITE: 'true', + }), + mongoUri + ).toThrow(/Safety guard: invalid characters in MongoDB host/); + } + }); + + test('still accepts printable non-delimiter hosts next to the control-character boundary (0x20 is whitespace, 0x7e is allowed)', () => { + const cfg = makeConfig({ + environment: 'development', + mongoUri: 'mongodb://u:p@mongo%7einternal:27018/stock_dev', + mongoDatabase: 'stock_dev', + bypassSchemaValidation: true, + }); + expect(() => assertEnvironmentSafety(cfg, 'dataIngestion', {})).not.toThrow(); + }); + test('seeded random percent-encoding sweep never allows a seed the driver dials as QA loopback:27017', () => { // Deterministic LCG (fixed seed) so the sweep is reproducible; strictly offline, no connect(). let state = 20261127; diff --git a/test/unit/mongodb-read-only.test.ts b/test/unit/mongodb-read-only.test.ts index ae9de363..c4e5c4df 100644 --- a/test/unit/mongodb-read-only.test.ts +++ b/test/unit/mongodb-read-only.test.ts @@ -1042,6 +1042,70 @@ describe('MongoDBClient read-only mode (offline)', () => { } }); + test('stream treats both null and undefined cursor.next() as EOF but passes falsy non-nullish transform results (0, false, empty string) through', async () => { + const logger = getLogger('mongodb-read-only-stream-nullish-test'); + const realMongoClient = new MongoClient('mongodb://127.0.0.1:1/stock'); + const realCollection = realMongoClient.db('stock').collection('eodPrices'); + + const rows = [ + { _id: '1', v: 0 }, + { _id: '2', v: false }, + { _id: '3', v: '' }, + { _id: '4', v: null }, + { _id: '5', v: 'tail' }, + ]; + + const openWrappedFind = (rawCursor: object) => { + const fakeDb = { + databaseName: 'stock', + collection: mock(() => ({ + collectionName: 'eodPrices', + find: mock(() => rawCursor), + })), + }; + 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({}); + }; + + for (const eofValue of [null, undefined] as const) { + const rawCursor = realCollection.find({}); + let index = 0; + let eofReads = 0; + const closeMock = mock(async () => {}); + rawCursor.next = mock(async () => { + if (index < rows.length) { + return rows[index++]; + } + eofReads++; + return eofValue; + }) as never; + rawCursor.close = closeMock as never; + + // Object mode: a raw `null` chunk would end a Readable, so falsy values are only safe + // because the guard skips strictly nullish outputs (row 4) and pushes 0/false/'' as-is. + const stream = openWrappedFind(rawCursor).stream({ + transform: (doc: Record) => doc.v, + }); + + const emitted: unknown[] = []; + for await (const chunk of stream) { + emitted.push(chunk); + } + + expect(emitted).toEqual([0, false, '', 'tail']); + expect(eofReads).toBe(1); + expect(closeMock).toHaveBeenCalledTimes(1); + } + }); + 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');