diff --git a/backend/src/ee/services/secret-rotation-v2/mongodb-credentials/mongodb-credentials-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/mongodb-credentials/mongodb-credentials-rotation-fns.ts index 5b66f13bc..ef5b67216 100644 --- a/backend/src/ee/services/secret-rotation-v2/mongodb-credentials/mongodb-credentials-rotation-fns.ts +++ b/backend/src/ee/services/secret-rotation-v2/mongodb-credentials/mongodb-credentials-rotation-fns.ts @@ -1,5 +1,6 @@ /* eslint-disable no-await-in-loop */ import { MongoClient } from "mongodb"; +import RE2 from "re2"; import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns"; import { @@ -49,11 +50,14 @@ export const mongodbCredentialsRotationFactory: TRotationFactory< options?: { validateConnection?: boolean; requireTlsForSrv?: boolean } ): Promise => { let normalizedHost = connection.credentials.host.trim(); - const isSrvFromHost = normalizedHost.startsWith("mongodb+srv://"); + const srvRegex = new RE2("^mongodb\\+srv:\\/\\/"); + const protocolRegex = new RE2("^mongodb:\\/\\/"); + + const isSrvFromHost = srvRegex.test(normalizedHost); if (isSrvFromHost) { - normalizedHost = normalizedHost.replace(/^mongodb\+srv:\/\//, ""); - } else if (normalizedHost.startsWith("mongodb://")) { - normalizedHost = normalizedHost.replace(/^mongodb:\/\//, ""); + normalizedHost = normalizedHost.replace(srvRegex, ""); + } else if (protocolRegex.test(normalizedHost)) { + normalizedHost = normalizedHost.replace(protocolRegex, ""); } const [hostIp] = await verifyHostInputValidity(normalizedHost); @@ -73,6 +77,7 @@ export const mongodbCredentialsRotationFactory: TRotationFactory< username: authCredentials.username, password: authCredentials.password }, + authSource: connection.credentials.database, directConnection: !isSrv }; diff --git a/backend/src/services/app-connection/mongodb/mongodb-connection-fns.ts b/backend/src/services/app-connection/mongodb/mongodb-connection-fns.ts index 78d8b5cff..84dfbcb66 100644 --- a/backend/src/services/app-connection/mongodb/mongodb-connection-fns.ts +++ b/backend/src/services/app-connection/mongodb/mongodb-connection-fns.ts @@ -1,4 +1,5 @@ import { MongoClient } from "mongodb"; +import RE2 from "re2"; import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns"; import { BadRequestError } from "@app/lib/errors"; @@ -17,12 +18,15 @@ export const getMongoDBConnectionListItem = () => { }; export const validateMongoDBConnectionCredentials = async (config: TMongoDBConnectionConfig) => { + const srvRegex = new RE2("^mongodb\\+srv:\\/\\/"); + const protocolRegex = new RE2("^mongodb:\\/\\/"); + let normalizedHost = config.credentials.host.trim(); - const isSrvFromHost = normalizedHost.startsWith("mongodb+srv://"); + const isSrvFromHost = srvRegex.test(normalizedHost); if (isSrvFromHost) { - normalizedHost = normalizedHost.replace(/^mongodb\+srv:\/\//, ""); - } else if (normalizedHost.startsWith("mongodb://")) { - normalizedHost = normalizedHost.replace(/^mongodb:\/\//, ""); + normalizedHost = normalizedHost.replace(srvRegex, ""); + } else if (protocolRegex.test(normalizedHost)) { + normalizedHost = normalizedHost.replace(protocolRegex, ""); } const [hostIp] = await verifyHostInputValidity(normalizedHost);