fix(mongodb): prevent double encoding of credentials in connection string

Fixes #15397
This commit is contained in:
Aman Jha 2026-03-09 19:40:04 +05:30
parent 13bddce444
commit 4494bfee5e

View file

@ -316,9 +316,19 @@ async getConnection(sourceOptions: SourceOptions): Promise<any> {
const finalHosts = hostsList.join(",");
const finalDb = explicitDb || dbNameFromConn || "";
const authSection = needsAuth
? `${encodeURIComponent(finalUser)}:${encodeURIComponent(finalPass)}@`
: "";
const safeEncode = (value: string) => {
try {
const decoded = decodeURIComponent(value);
if (decoded !== value) {
return value;
}
} catch (e) {}
return encodeURIComponent(value);
};
const authSection = needsAuth
? `${safeEncode(finalUser)}:${safeEncode(finalPass)}@`
: "";
let finalUri = `${protocol}://${authSection}${finalHosts}`;