diff --git a/test/unit/environment-safety-authority-split.test.ts b/test/unit/environment-safety-authority-split.test.ts index 76f90202..b23f17c0 100644 --- a/test/unit/environment-safety-authority-split.test.ts +++ b/test/unit/environment-safety-authority-split.test.ts @@ -322,6 +322,55 @@ describe('T-28: guard and driver agree on where the Mongo URI authority splits', } }); + test('IPv6 loopback behind a raw "#" (driver dials [::1]:27017) fails closed in schema and guard', () => { + const uri = 'mongodb://u:p@[::1]#:27018/stock'; + const hosts = driverDialHosts(uri) ?? []; + expect(driverDialsQaMongo(hosts)).toBe(true); + + expect( + stockAppSchema.shape.database.shape.mongodb.safeParse({ + enabled: true, + uri, + database: 'stock_dev', + }).success + ).toBe(false); + expect(() => assertEnvironmentSafety(makeDevConfig({ uri }), 'dataIngestion', {})).toThrow( + /unencoded "#"/ + ); + }); + + test('seeded fuzz through the real config path: no schema-accepted URI that dials QA loopback:27017 passes the guard', () => { + // Arrange: different seed from the schema-bypassed fuzz above. Only the bypass direction is + // asserted: the guard deliberately also blocks some non-QA URIs (percent-encoded hosts, + // trailing-dot loopback, srv to loopback), so "allowed" is not expected to mirror the driver. + const corpus = buildFuzzCorpus(0x28e7, 30000); + const mongoSchema = stockAppSchema.shape.database.shape.mongodb; + const bypasses: string[] = []; + let schemaAcceptedQa = 0; + + // Act + for (const uri of corpus) { + if (!mongoSchema.safeParse({ enabled: true, uri, database: 'stock_dev' }).success) { + continue; + } + const hosts = driverDialHosts(uri); + if (!hosts || !driverDialsQaMongo(hosts)) { + continue; + } + schemaAcceptedQa++; + if (guardAllows(makeDevConfig({ uri }), 'dataIngestion')) { + bypasses.push(`dataIngestion ${JSON.stringify(uri)}`); + } + if (guardAllows(makeDevConfig({ readUri: uri, readOnly: false }), 'dataPipeline')) { + bypasses.push(`dataPipeline ${JSON.stringify(uri)}`); + } + } + + // Assert: the schema-valid QA path is exercised, and nothing slips through + expect(schemaAcceptedQa).toBeGreaterThan(50); + expect(bypasses).toEqual([]); + }); + test('disabled MongoDB in development is not parsed, so a placeholder URI never blocks startup', () => { const config = makeDevConfig({ uri: 'mongodb://disabled' }); config.database.mongodb.enabled = false;