diff --git a/test/unit/environment-safety-encoded-host.test.ts b/test/unit/environment-safety-encoded-host.test.ts index 334ea552..a7db34e8 100644 --- a/test/unit/environment-safety-encoded-host.test.ts +++ b/test/unit/environment-safety-encoded-host.test.ts @@ -500,6 +500,116 @@ describe('Environment safety — percent-encoded MongoDB seed hosts', () => { ); }); + test('blocks seeds whose decoded host smuggles a URL delimiter that ServerDescription reparses to QA loopback', () => { + // QA regression: HostAddress decodes "localhost%2f" to host "localhost/" and keeps the explicit + // port, so the seed serializes as "localhost/:27018". ServerDescription.hostAddress then reparses + // that string with WHATWG URL, which stops the authority at "/" and dials localhost:27017 (QA). + const delimiterSmuggledUris = [ + 'mongodb://u:p@localhost%2f:27018/stock_dev', + 'mongodb://u:p@%6cocalhost%2f:27018/stock_dev', + 'mongodb://u:p@localhost%3f:27018/stock_dev', + 'mongodb://u:p@localhost%23:27018/stock_dev', + 'mongodb://u:p@%6cocalhost%3a27017%2f:27018/stock_dev', + 'mongodb://u:p@x%40localhost%3a27017%2f:27018/stock_dev', + 'mongodb://u:p@%5b%3a%3a1%5d%3a27017%2f:27018/stock_dev', + 'mongodb://u:p@localhost:27018,localhost%2f:27019/stock_dev', + ]; + + for (const mongoUri of delimiterSmuggledUris) { + // Arrange: prove offline that the driver's final dial target is QA MongoDB. + const dialTargets = new MongoClient(mongoUri).options.hosts.map(seed => + getDriverDialHostAddress(seed) + ); + expect( + dialTargets.some( + target => + typeof target.host === 'string' && isDriverHostLoopback(target.host) && target.port === 27017 + ) + ).toBe(true); + + const ingestionCfg = makeConfig({ + environment: 'development', + mongoUri, + mongoDatabase: 'stock_dev', + bypassSchemaValidation: true, + }); + const pipelineCfg = makeConfig({ + environment: 'development', + mongoReadUri: mongoUri, + mongoReadOnly: false, + mongoDatabase: 'stock_dev', + bypassSchemaValidation: true, + }); + + // Act + assert: the guard must not allow either service to start. + expect(() => assertEnvironmentSafety(ingestionCfg, 'dataIngestion', {})).toThrow( + /Safety guard/ + ); + expect(() => assertEnvironmentSafety(pipelineCfg, 'dataPipeline', {})).toThrow(/Safety guard/); + } + }); + + test('never allows a seed whose driver dial target is QA loopback:27017 (offline parity sweep)', () => { + // Every case is built so the guard sees a non-QA seed unless it follows the driver's reparse. + const hostSpellings = [ + 'localhost', + '%6cocalhost', + '%256cocalhost', + '127.0.0.1', + '%31%32%37.0.0.1', + '[::1]', + 'localhost%2f', + 'localhost%3f', + 'localhost%23', + 'localhost%3a27017%2f', + 'localhost%3a27017', + 'localhost%2c127.0.0.1', + 'mongo.internal', + 'mongo.internal%2f', + ]; + const ports = [':27017', ':27018', '']; + + const bypasses: string[] = []; + for (const host of hostSpellings) { + for (const port of ports) { + const uri = `mongodb://u:p@${host}${port}/stock_dev`; + let dialsQa = false; + try { + dialsQa = new MongoClient(uri).options.hosts.some(seed => { + const target = getDriverDialHostAddress(seed); + return ( + typeof target.host === 'string' && + isDriverHostLoopback(target.host) && + target.port === 27017 + ); + }); + } catch { + // The driver refuses this URI before dialing, so no QA connection is possible. + dialsQa = false; + } + + const cfg = makeConfig({ + environment: 'development', + mongoUri: uri, + mongoDatabase: 'stock_dev', + bypassSchemaValidation: true, + }); + let guardBlocked = false; + try { + assertEnvironmentSafety(cfg, 'dataIngestion', {}); + } catch { + guardBlocked = true; + } + + if (dialsQa && !guardBlocked) { + bypasses.push(uri); + } + } + } + + expect(bypasses).toEqual([]); + }); + test('does not throw on literal IPv6 zone ID in raw PostgreSQL host', () => { const cfg = makeConfig({ environment: 'development',