mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-02 18:25:45 +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:
+9
-4
@@ -1,5 +1,6 @@
|
|||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
import { MongoClient } from "mongodb";
|
import { MongoClient } from "mongodb";
|
||||||
|
import RE2 from "re2";
|
||||||
|
|
||||||
import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";
|
import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";
|
||||||
import {
|
import {
|
||||||
@@ -49,11 +50,14 @@ export const mongodbCredentialsRotationFactory: TRotationFactory<
|
|||||||
options?: { validateConnection?: boolean; requireTlsForSrv?: boolean }
|
options?: { validateConnection?: boolean; requireTlsForSrv?: boolean }
|
||||||
): Promise<MongoClient> => {
|
): Promise<MongoClient> => {
|
||||||
let normalizedHost = connection.credentials.host.trim();
|
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) {
|
if (isSrvFromHost) {
|
||||||
normalizedHost = normalizedHost.replace(/^mongodb\+srv:\/\//, "");
|
normalizedHost = normalizedHost.replace(srvRegex, "");
|
||||||
} else if (normalizedHost.startsWith("mongodb://")) {
|
} else if (protocolRegex.test(normalizedHost)) {
|
||||||
normalizedHost = normalizedHost.replace(/^mongodb:\/\//, "");
|
normalizedHost = normalizedHost.replace(protocolRegex, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
const [hostIp] = await verifyHostInputValidity(normalizedHost);
|
const [hostIp] = await verifyHostInputValidity(normalizedHost);
|
||||||
@@ -73,6 +77,7 @@ export const mongodbCredentialsRotationFactory: TRotationFactory<
|
|||||||
username: authCredentials.username,
|
username: authCredentials.username,
|
||||||
password: authCredentials.password
|
password: authCredentials.password
|
||||||
},
|
},
|
||||||
|
authSource: connection.credentials.database,
|
||||||
directConnection: !isSrv
|
directConnection: !isSrv
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { MongoClient } from "mongodb";
|
import { MongoClient } from "mongodb";
|
||||||
|
import RE2 from "re2";
|
||||||
|
|
||||||
import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";
|
import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";
|
||||||
import { BadRequestError } from "@app/lib/errors";
|
import { BadRequestError } from "@app/lib/errors";
|
||||||
@@ -17,12 +18,15 @@ export const getMongoDBConnectionListItem = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const validateMongoDBConnectionCredentials = async (config: TMongoDBConnectionConfig) => {
|
export const validateMongoDBConnectionCredentials = async (config: TMongoDBConnectionConfig) => {
|
||||||
|
const srvRegex = new RE2("^mongodb\\+srv:\\/\\/");
|
||||||
|
const protocolRegex = new RE2("^mongodb:\\/\\/");
|
||||||
|
|
||||||
let normalizedHost = config.credentials.host.trim();
|
let normalizedHost = config.credentials.host.trim();
|
||||||
const isSrvFromHost = normalizedHost.startsWith("mongodb+srv://");
|
const isSrvFromHost = srvRegex.test(normalizedHost);
|
||||||
if (isSrvFromHost) {
|
if (isSrvFromHost) {
|
||||||
normalizedHost = normalizedHost.replace(/^mongodb\+srv:\/\//, "");
|
normalizedHost = normalizedHost.replace(srvRegex, "");
|
||||||
} else if (normalizedHost.startsWith("mongodb://")) {
|
} else if (protocolRegex.test(normalizedHost)) {
|
||||||
normalizedHost = normalizedHost.replace(/^mongodb:\/\//, "");
|
normalizedHost = normalizedHost.replace(protocolRegex, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
const [hostIp] = await verifyHostInputValidity(normalizedHost);
|
const [hostIp] = await verifyHostInputValidity(normalizedHost);
|
||||||
|
|||||||
Reference in New Issue
Block a user