T-13: Dev QA-Mongo guard misses percent-encoded loopback hostnames (confirmed reachable)

This commit is contained in:
Studio 2026-11-27 12:38:42 -05:00
parent f712c3619b
commit 2dbecd7c95
2 changed files with 426 additions and 3 deletions

View file

@ -162,6 +162,17 @@ function isLoopbackHost(host?: string): boolean {
);
}
function decodeMongoHost(rawHost: string): string {
try {
return decodeURIComponent(rawHost).toLowerCase();
} catch (error) {
if (error instanceof URIError) {
throw new Error(`Safety guard: invalid percent-encoding in MongoDB host "${rawHost}".`);
}
throw error;
}
}
function parseMongoHostPort(
segment: string,
defaultPort: number | undefined
@ -176,7 +187,11 @@ function parseMongoHostPort(
if (closeIdx <= 1) {
return null;
}
const host = trimmed.slice(1, closeIdx).trim();
const rawHost = trimmed.slice(1, closeIdx).trim();
if (!rawHost) {
return null;
}
const host = decodeMongoHost(rawHost).trim();
if (!host) {
return null;
}
@ -200,14 +215,22 @@ function parseMongoHostPort(
const colonIdx = trimmed.indexOf(':');
if (colonIdx === -1) {
return { host: trimmed, port: defaultPort };
const host = decodeMongoHost(trimmed).trim();
if (!host) {
return null;
}
return { host, port: defaultPort };
}
if (trimmed.indexOf(':', colonIdx + 1) !== -1) {
return null;
}
const host = trimmed.slice(0, colonIdx).trim();
const rawHost = trimmed.slice(0, colonIdx).trim();
const portStr = trimmed.slice(colonIdx + 1).trim();
if (!rawHost) {
return null;
}
const host = decodeMongoHost(rawHost).trim();
if (!host) {
return null;
}