T-13: tests

This commit is contained in:
Studio 2026-11-27 13:13:18 -05:00
parent 0236566e02
commit 4a6f75c232

View file

@ -648,4 +648,164 @@ describe('Environment safety — percent-encoded MongoDB seed hosts', () => {
).toThrow(/Safety guard: invalid characters in MongoDB host/);
}
});
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;
const nextRandom = (): number => {
state = (Math.imul(state, 1103515245) + 12345) & 0x7fffffff;
return state / 0x7fffffff;
};
const encodeChar = (ch: string): string => {
const hex = ch.charCodeAt(0).toString(16).padStart(2, '0');
return `%${nextRandom() < 0.5 ? hex.toUpperCase() : hex}`;
};
const encodeHost = (host: string, depth: number): string => {
let out = '';
for (const ch of host) {
out += nextRandom() < 0.4 ? encodeChar(ch) : ch;
}
for (let level = 1; level < depth; level++) {
out = out.replace(/%/g, '%25');
}
return out;
};
const baseHosts = [
'localhost',
'LocalHost',
'localhost.',
'127.0.0.1',
'127.1.2.3',
'0.0.0.0',
'[::1]',
'localhost/',
'localhost?',
'localhost#',
'localhost:27017',
'localhost:27017/',
'[::1]:27017/',
'localhost,localhost',
'loc%0aalhost',
'localhost%09',
'loc%00alhost',
'x%40localhost',
'%5b::1%5d',
'mongo.internal',
];
const portSuffixes = [':27017', ':27018', ''];
const bypasses: string[] = [];
let driverQaDialCount = 0;
for (let iteration = 0; iteration < 3000; iteration++) {
const base = baseHosts[Math.floor(nextRandom() * baseHosts.length)] ?? 'localhost';
const port = portSuffixes[Math.floor(nextRandom() * portSuffixes.length)] ?? '';
const depth = 1 + Math.floor(nextRandom() * 3);
const uri = `mongodb://u:p@${encodeHost(base, depth)}${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 {
// Driver refuses the URI or seed before dialing, so no QA socket can be opened.
dialsQa = false;
}
if (!dialsQa) {
continue;
}
driverQaDialCount++;
const cfg = makeConfig({
environment: 'development',
mongoUri: uri,
mongoDatabase: 'stock_dev',
bypassSchemaValidation: true,
});
let guardBlocked = false;
try {
assertEnvironmentSafety(cfg, 'dataIngestion', {});
} catch {
guardBlocked = true;
}
if (!guardBlocked) {
bypasses.push(uri);
}
}
// Guard against a vacuous sweep: plenty of generated URIs must really dial QA.
expect(driverQaDialCount).toBeGreaterThan(500);
expect(bypasses).toEqual([]);
});
test('allows encoded dev-port loopback seeds that the driver dials on 27018', () => {
const devUris = [
'mongodb://dev_admin:dev_mongo_pass@%6C%4F%63%61%6C%68%6F%73%74:27018/stock_dev',
'mongodb://dev_admin:dev_mongo_pass@%2531%2532%2537.0.0.1:27018/stock_dev',
'mongodb://dev_admin:dev_mongo_pass@[::1]:27018/stock_dev',
'mongodb://dev_admin:dev_mongo_pass@%6cocalhost:27018,127%2e0.0.1:27019/stock_dev',
];
for (const mongoUri of devUris) {
// Arrange: confirm offline the driver never dials QA for these seeds.
const dialTargets = new MongoClient(mongoUri).options.hosts.map(seed =>
getDriverDialHostAddress(seed)
);
expect(dialTargets.every(target => target.port !== 27017)).toBe(true);
const cfg = makeConfig({
environment: 'development',
mongoUri,
mongoDatabase: 'stock_dev',
bypassSchemaValidation: true,
});
expect(() => assertEnvironmentSafety(cfg, 'dataIngestion', {})).not.toThrow();
}
});
test('schema-validated config path blocks percent-encoded QA seed for dataIngestion', () => {
// No bypassSchemaValidation: the URI must survive stockAppSchema.parse and still be blocked.
const cfg = makeConfig({
environment: 'development',
mongoUri: 'mongodb://dev_admin:dev_mongo_pass@%6cocalhost:27017/stock?authSource=admin',
mongoDatabase: 'stock',
});
expect(() => assertEnvironmentSafety(cfg, 'dataIngestion', {})).toThrow(
/cannot open a writable MongoDB connection to QA MongoDB/
);
});
test('production and qa guards still accept ordinary TCP and SRV MongoDB hosts', () => {
const prodUris = [
'mongodb+srv://prod_user:Str0ngProdSecret@cluster0.prod-db.example.net/stock?retryWrites=true',
'mongodb://prod_user:Str0ngProdSecret@mongo-0.prod.internal:27017,mongo-1.prod.internal:27017/stock?replicaSet=rs0',
'mongodb://prod_user:Str0ngProdSecret@%6dongo-0.prod.internal:27017/stock',
];
for (const mongoUri of prodUris) {
const prodCfg = makeConfig({
environment: 'production',
pgPassword: 'Str0ngProdPgSecret',
mongoUri,
mongoDatabase: 'stock',
});
expect(() => assertEnvironmentSafety(prodCfg, 'dataIngestion', {})).not.toThrow();
const qaCfg = makeConfig({
environment: 'qa',
pgDatabase: 'trading_bot',
mongoUri,
mongoDatabase: 'stock',
});
expect(() => assertEnvironmentSafety(qaCfg, 'dataIngestion', {})).not.toThrow();
}
});
});