T-9: Multi-host MongoDB URI bypasses the dev QA-Mongo guard

This commit is contained in:
Studio 2026-11-27 11:13:53 -05:00
parent 2a28c402e8
commit 677658a32f
4 changed files with 348 additions and 33 deletions

View file

@ -14,13 +14,7 @@ interface ParsedMongoTarget {
username?: string;
}
const LOOPBACK_HOSTS = new Set([
'localhost',
'0.0.0.0',
'::1',
'0:0:0:0:0:0:0:1',
'::ffff:127.0.0.1',
]);
const LOOPBACK_HOSTS = new Set(['localhost', '0.0.0.0']);
const FORBIDDEN_PROD_SECRETS = new Set([
'trading_pass_dev',
'trading_mongo_dev',
@ -30,18 +24,126 @@ const FORBIDDEN_PROD_SECRETS = new Set([
'admin123',
]);
function isIpv4Loopback(host: string): boolean {
function parseIpv4Octets(host: string): [number, number, number, number] | null {
const octets = host.split('.');
if (octets.length !== 4 || octets[0] !== '127') {
return false;
if (octets.length !== 4) {
return null;
}
return octets.every(part => {
const values: number[] = [];
for (const part of octets) {
if (!/^\d{1,3}$/.test(part)) {
return false;
return null;
}
const value = Number(part);
return value >= 0 && value <= 255;
});
if (value < 0 || value > 255) {
return null;
}
values.push(value);
}
const [a, b, c, d] = values;
if (a === undefined || b === undefined || c === undefined || d === undefined) {
return null;
}
return [a, b, c, d];
}
function isIpv4Loopback(host: string): boolean {
const octets = parseIpv4Octets(host);
return octets !== null && octets[0] === 127;
}
function parseIpv6Side(side: string, allowIpv4Tail: boolean): number[] | null {
if (side === '') {
return [];
}
const parts = side.split(':');
const groups: number[] = [];
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (part === undefined) {
return null;
}
if (i === parts.length - 1 && allowIpv4Tail && part.includes('.')) {
const octets = parseIpv4Octets(part);
if (!octets) {
return null;
}
groups.push((octets[0] << 8) | octets[1], (octets[2] << 8) | octets[3]);
continue;
}
if (!/^[0-9a-f]{1,4}$/i.test(part)) {
return null;
}
groups.push(parseInt(part, 16));
}
return groups;
}
function parseIpv6Hextets(host: string): number[] | null {
if (!host.includes(':')) {
return null;
}
const zoneIdx = host.indexOf('%');
const addr = zoneIdx >= 0 ? host.slice(0, zoneIdx) : host;
const halves = addr.split('::');
if (halves.length > 2) {
return null;
}
const firstHalf = halves[0];
if (firstHalf === undefined) {
return null;
}
if (halves.length === 1) {
const groups = parseIpv6Side(firstHalf, true);
return groups && groups.length === 8 ? groups : null;
}
const secondHalf = halves[1];
if (secondHalf === undefined) {
return null;
}
const left = parseIpv6Side(firstHalf, false);
const right = parseIpv6Side(secondHalf, true);
if (!left || !right) {
return null;
}
const missing = 8 - left.length - right.length;
if (missing < 1) {
return null;
}
return [...left, ...Array<number>(missing).fill(0), ...right];
}
function isIpv6Loopback(host: string): boolean {
const hextets = parseIpv6Hextets(host);
if (!hextets) {
return false;
}
const [h0, h1, h2, h3, h4, h5, h6, h7] = hextets;
if (
h0 === undefined ||
h1 === undefined ||
h2 === undefined ||
h3 === undefined ||
h4 === undefined ||
h5 === undefined ||
h6 === undefined ||
h7 === undefined
) {
return false;
}
if (h0 === 0 && h1 === 0 && h2 === 0 && h3 === 0 && h4 === 0 && h5 === 0 && h6 === 0) {
return h7 === 0 || h7 === 1;
}
// IPv4-mapped IPv6 (::ffff:0:0/96): match 127.0.0.0/8 and 0.0.0.0 in both dotted and hex forms
if (h0 === 0 && h1 === 0 && h2 === 0 && h3 === 0 && h4 === 0 && h5 === 0xffff) {
const firstOctet = (h6 >> 8) & 0xff;
return firstOctet === 127 || (h6 === 0 && h7 === 0);
}
return false;
}
function isLoopbackHost(host?: string): boolean {
@ -53,10 +155,11 @@ function isLoopbackHost(host?: string): boolean {
.replace(/^\[|\]$/g, '')
.replace(/\.+$/, '')
.toLowerCase();
if (LOOPBACK_HOSTS.has(normalized) || isIpv4Loopback(normalized)) {
return true;
}
return normalized.startsWith('::ffff:') && isIpv4Loopback(normalized.slice(7));
return (
LOOPBACK_HOSTS.has(normalized) ||
isIpv4Loopback(normalized) ||
isIpv6Loopback(normalized)
);
}
function parseMongoHostPort(