T-13: Dev QA-Mongo guard misses percent-encoded loopback hostnames (confirmed reachable)
This commit is contained in:
parent
2dbecd7c95
commit
8eddb28935
2 changed files with 153 additions and 18 deletions
|
|
@ -162,15 +162,38 @@ 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}".`);
|
||||
const MAX_MONGO_HOST_DECODE_PASSES = 8;
|
||||
|
||||
function decodeMongoHost(rawHost: string, isBracketedIpv6 = false): string {
|
||||
// MongoDB driver 6.22.0 decodes seed hostnames once in HostAddress.fromString during
|
||||
// URI parsing and a second time when ServerDescription.hostAddress reparses the
|
||||
// serialized address for ConnectionPool/Monitor. Decode iteratively to a fixed point
|
||||
// so double- or multi-encoded loopback hosts cannot bypass the guard.
|
||||
let current = rawHost;
|
||||
for (let pass = 0; pass < MAX_MONGO_HOST_DECODE_PASSES; pass++) {
|
||||
let decoded: string;
|
||||
try {
|
||||
decoded = decodeURIComponent(current);
|
||||
} catch (error) {
|
||||
if (error instanceof URIError) {
|
||||
// After the first decode of a bracketed IPv6 literal, a literal '%' marks
|
||||
// an IPv6 zone ID (e.g., "::1%25lo" -> "::1%lo") rather than another escape.
|
||||
if (isBracketedIpv6 && pass > 0) {
|
||||
break;
|
||||
}
|
||||
throw new Error(`Safety guard: invalid percent-encoding in MongoDB host "${rawHost}".`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
throw error;
|
||||
if (decoded === current) {
|
||||
break;
|
||||
}
|
||||
current = decoded;
|
||||
}
|
||||
if (!isBracketedIpv6 && current.includes('%')) {
|
||||
throw new Error(`Safety guard: invalid percent-encoding in MongoDB host "${rawHost}".`);
|
||||
}
|
||||
return current.toLowerCase();
|
||||
}
|
||||
|
||||
function parseMongoHostPort(
|
||||
|
|
@ -191,7 +214,7 @@ function parseMongoHostPort(
|
|||
if (!rawHost) {
|
||||
return null;
|
||||
}
|
||||
const host = decodeMongoHost(rawHost).trim();
|
||||
const host = decodeMongoHost(rawHost, true).trim();
|
||||
if (!host) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue