Clear remaining M0/M1 bugs on master (T-22, T-25, T-27, T-31, T-35)

This commit is contained in:
Boki 2026-11-27 16:19:09 -05:00
parent 9e259ca43b
commit b20fc28e45
8 changed files with 245 additions and 252 deletions

View file

@ -291,7 +291,22 @@ function isValidIpv6WithZoneId(host: string): boolean {
return parseIpv6Hextets(addr) !== null && VALID_IPV6_ZONE_ID.test(zone);
}
function decodeMongoHost(rawHost: string, isBracketedIpv6 = false): string {
const FORBIDDEN_MONGO_UNIX_SOCKET_CHARS = /[?#@:\\[\],\s]/;
function isUnixDomainSocketHost(decodedHost: string): boolean {
return (
decodedHost.startsWith('/') &&
decodedHost.endsWith('.sock') &&
!hasAsciiControlChar(decodedHost) &&
!FORBIDDEN_MONGO_UNIX_SOCKET_CHARS.test(decodedHost)
);
}
function decodeMongoHost(
rawHost: string,
isBracketedIpv6 = false,
allowUnixSocket = 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
@ -324,6 +339,9 @@ function decodeMongoHost(rawHost: string, isBracketedIpv6 = false): string {
if (current.includes('%') && !(isBracketedIpv6 && isValidIpv6WithZoneId(current))) {
throw new Error(`Safety guard: invalid percent-encoding in MongoDB host "${rawHost}".`);
}
if (!isBracketedIpv6 && allowUnixSocket && isUnixDomainSocketHost(current)) {
return current;
}
const forbiddenChars = isBracketedIpv6
? FORBIDDEN_MONGO_IPV6_HOST_CHARS
: FORBIDDEN_MONGO_HOST_CHARS;
@ -335,7 +353,8 @@ function decodeMongoHost(rawHost: string, isBracketedIpv6 = false): string {
function parseMongoHostPort(
segment: string,
defaultPort: number | undefined
defaultPort: number | undefined,
allowUnixSocket = false
): MongoTargetHost | null {
const trimmed = segment.trim();
if (!trimmed) {
@ -375,10 +394,13 @@ function parseMongoHostPort(
const colonIdx = trimmed.indexOf(':');
if (colonIdx === -1) {
const host = decodeMongoHost(trimmed).trim();
const host = decodeMongoHost(trimmed, false, allowUnixSocket).trim();
if (!host) {
return null;
}
if (allowUnixSocket && isUnixDomainSocketHost(host)) {
return { host, port: undefined };
}
return { host, port: defaultPort };
}
if (trimmed.indexOf(':', colonIdx + 1) !== -1) {
@ -491,8 +513,9 @@ function parseMongoTarget(uri?: string): ParsedMongoTarget {
}
const hosts: MongoTargetHost[] = [];
const allowUnixSocket = scheme === 'mongodb';
for (const segment of hostList.split(',')) {
const parsedHost = parseMongoHostPort(segment, defaultPort);
const parsedHost = parseMongoHostPort(segment, defaultPort, allowUnixSocket);
if (parsedHost) {
hosts.push(parsedHost);
}