T-9: Multi-host MongoDB URI bypasses the dev QA-Mongo guard
This commit is contained in:
commit
87e83117dd
7 changed files with 1004 additions and 39 deletions
|
|
@ -2,7 +2,19 @@ import type { StockAppConfig } from './schemas';
|
|||
|
||||
export type StockServiceName = 'dataIngestion' | 'dataPipeline' | 'webApi' | 'orchestrator';
|
||||
|
||||
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '0.0.0.0']);
|
||||
interface MongoTargetHost {
|
||||
host: string;
|
||||
port?: number;
|
||||
}
|
||||
|
||||
interface ParsedMongoTarget {
|
||||
hosts: MongoTargetHost[];
|
||||
host?: string;
|
||||
database?: string;
|
||||
username?: string;
|
||||
}
|
||||
|
||||
const LOOPBACK_HOSTS = new Set(['localhost', '0.0.0.0']);
|
||||
const FORBIDDEN_PROD_SECRETS = new Set([
|
||||
'trading_pass_dev',
|
||||
'trading_mongo_dev',
|
||||
|
|
@ -12,41 +24,271 @@ const FORBIDDEN_PROD_SECRETS = new Set([
|
|||
'admin123',
|
||||
]);
|
||||
|
||||
function parseIpv4Octets(host: string): [number, number, number, number] | null {
|
||||
const octets = host.split('.');
|
||||
if (octets.length !== 4) {
|
||||
return null;
|
||||
}
|
||||
const values: number[] = [];
|
||||
for (const part of octets) {
|
||||
if (!/^\d{1,3}$/.test(part)) {
|
||||
return null;
|
||||
}
|
||||
const value = Number(part);
|
||||
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 {
|
||||
if (!host) {
|
||||
return false;
|
||||
}
|
||||
const normalized = host.replace(/^\[|\]$/g, '').toLowerCase();
|
||||
return LOOPBACK_HOSTS.has(normalized);
|
||||
const normalized = host
|
||||
.trim()
|
||||
.replace(/^\[|\]$/g, '')
|
||||
.replace(/\.+$/, '')
|
||||
.toLowerCase();
|
||||
return (
|
||||
LOOPBACK_HOSTS.has(normalized) ||
|
||||
isIpv4Loopback(normalized) ||
|
||||
isIpv6Loopback(normalized)
|
||||
);
|
||||
}
|
||||
|
||||
function parseMongoTarget(uri?: string): {
|
||||
host?: string;
|
||||
port?: number;
|
||||
database?: string;
|
||||
username?: string;
|
||||
} {
|
||||
if (!uri) {
|
||||
return {};
|
||||
function parseMongoHostPort(
|
||||
segment: string,
|
||||
defaultPort: number | undefined
|
||||
): MongoTargetHost | null {
|
||||
const trimmed = segment.trim();
|
||||
if (!trimmed) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const parsed = new URL(uri);
|
||||
const database = parsed.pathname.replace(/^\/+/, '').split('/')[0] || undefined;
|
||||
const port = parsed.port ? Number(parsed.port) : parsed.protocol === 'mongodb:' ? 27017 : undefined;
|
||||
return {
|
||||
host: parsed.hostname || undefined,
|
||||
port: Number.isFinite(port) ? port : undefined,
|
||||
database,
|
||||
username: parsed.username ? decodeURIComponent(parsed.username) : undefined,
|
||||
};
|
||||
} catch {
|
||||
return {};
|
||||
|
||||
if (trimmed.startsWith('[')) {
|
||||
const closeIdx = trimmed.indexOf(']');
|
||||
if (closeIdx <= 1) {
|
||||
return null;
|
||||
}
|
||||
const host = trimmed.slice(1, closeIdx).trim();
|
||||
if (!host) {
|
||||
return null;
|
||||
}
|
||||
const after = trimmed.slice(closeIdx + 1).trim();
|
||||
if (after === '' || after === ':') {
|
||||
return { host, port: defaultPort };
|
||||
}
|
||||
if (!after.startsWith(':')) {
|
||||
return null;
|
||||
}
|
||||
const portStr = after.slice(1).trim();
|
||||
if (!/^\d+$/.test(portStr)) {
|
||||
return null;
|
||||
}
|
||||
const port = Number(portStr);
|
||||
if (port < 1 || port > 65535) {
|
||||
return null;
|
||||
}
|
||||
return { host, port };
|
||||
}
|
||||
|
||||
const colonIdx = trimmed.indexOf(':');
|
||||
if (colonIdx === -1) {
|
||||
return { host: trimmed, port: defaultPort };
|
||||
}
|
||||
if (trimmed.indexOf(':', colonIdx + 1) !== -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const host = trimmed.slice(0, colonIdx).trim();
|
||||
const portStr = trimmed.slice(colonIdx + 1).trim();
|
||||
if (!host) {
|
||||
return null;
|
||||
}
|
||||
if (portStr === '') {
|
||||
return { host, port: defaultPort };
|
||||
}
|
||||
if (!/^\d+$/.test(portStr)) {
|
||||
return null;
|
||||
}
|
||||
const port = Number(portStr);
|
||||
if (port < 1 || port > 65535) {
|
||||
return null;
|
||||
}
|
||||
return { host, port };
|
||||
}
|
||||
|
||||
function parseMongoTarget(uri?: string): ParsedMongoTarget {
|
||||
const trimmed = uri?.trim();
|
||||
if (!trimmed) {
|
||||
return { hosts: [] };
|
||||
}
|
||||
|
||||
const schemeMatch = /^(mongodb(?:\+srv)?):\/\/(.*)$/i.exec(trimmed);
|
||||
const rawScheme = schemeMatch?.[1];
|
||||
const rest = schemeMatch?.[2];
|
||||
if (!rawScheme || rest === undefined) {
|
||||
return { hosts: [] };
|
||||
}
|
||||
|
||||
const scheme = rawScheme.toLowerCase();
|
||||
const defaultPort = scheme === 'mongodb' ? 27017 : undefined;
|
||||
|
||||
const delimiterMatch = /[/?#]/.exec(rest);
|
||||
const authority = delimiterMatch ? rest.slice(0, delimiterMatch.index) : rest;
|
||||
const remainder = delimiterMatch ? rest.slice(delimiterMatch.index) : '';
|
||||
|
||||
let database: string | undefined;
|
||||
if (remainder.startsWith('/')) {
|
||||
const pathPart = remainder.split(/[?#]/, 1)[0] ?? '';
|
||||
const firstSegment = pathPart.replace(/^\/+/, '').split('/')[0];
|
||||
database = firstSegment || undefined;
|
||||
}
|
||||
|
||||
const atIndex = authority.lastIndexOf('@');
|
||||
const userinfo = atIndex >= 0 ? authority.slice(0, atIndex) : '';
|
||||
const hostList = atIndex >= 0 ? authority.slice(atIndex + 1) : authority;
|
||||
|
||||
let username: string | undefined;
|
||||
if (userinfo) {
|
||||
const colonIndex = userinfo.indexOf(':');
|
||||
const rawUsername = colonIndex >= 0 ? userinfo.slice(0, colonIndex) : userinfo;
|
||||
if (rawUsername) {
|
||||
try {
|
||||
username = decodeURIComponent(rawUsername);
|
||||
} catch {
|
||||
username = rawUsername;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const hosts: MongoTargetHost[] = [];
|
||||
for (const segment of hostList.split(',')) {
|
||||
const parsedHost = parseMongoHostPort(segment, defaultPort);
|
||||
if (parsedHost) {
|
||||
hosts.push(parsedHost);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
hosts,
|
||||
host: hosts[0]?.host,
|
||||
database,
|
||||
username,
|
||||
};
|
||||
}
|
||||
|
||||
function isQaMongoTarget(uri: string | undefined): boolean {
|
||||
const target = parseMongoTarget(uri);
|
||||
return isLoopbackHost(target.host) && target.port === 27017;
|
||||
return target.hosts.some(
|
||||
entry => isLoopbackHost(entry.host) && (entry.port === undefined || entry.port === 27017)
|
||||
);
|
||||
}
|
||||
|
||||
function isQaPostgresTarget(pg: StockAppConfig['database']['postgres']): boolean {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue