mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
Refactor MongoDB host validation using RE2 regex
- Replaced string methods with RE2 regex for improved host validation in MongoDB connection functions. - Updated the `mongodbCredentialsRotationFactory` and `validateMongoDBConnectionCredentials` to utilize regex for parsing connection strings. - Enhanced code readability and maintainability by consolidating host normalization logic.
This commit is contained in:
@@ -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<MongoClient> => {
|
||||
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
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user