Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

move request to saferequest and add validation on schema

adilson994a8a71660c
diff --git a/backend/src/lib/validator/validate-url.ts b/backend/src/lib/validator/validate-url.ts
index b73a08d737..33db37c713 100644
--- a/backend/src/lib/validator/validate-url.ts
+++ b/backend/src/lib/validator/validate-url.ts
@@ -258,6 +258,17 @@ type TBuildAgentOptions = {
* addresses or non-DNS subjects (e.g. AD CS web enrollment).
*/
checkServerIdentity?: https.AgentOptions["checkServerIdentity"];
+ /**
+ * mTLS client private key (PEM). Forwarded to https.Agent. Only applied to
+ * HTTPS. Pair with `cert`. Required for endpoints that authenticate the
+ * client via certificate (e.g. OVH OKMS).
+ */
+ key?: string | Buffer;
+ /**
+ * mTLS client certificate (PEM). Forwarded to https.Agent. Only applied to
+ * HTTPS. Pair with `key`.
+ */
+ cert?: string | Buffer;
};
const hasAgentCustomization = (opts: TBuildAgentOptions): boolean =>
@@ -266,7 +277,9 @@ const hasAgentCustomization = (opts: TBuildAgentOptions): boolean =>
opts.ca !== undefined ||
opts.servername !== undefined ||
opts.keepAlive !== undefined ||
- opts.checkServerIdentity !== undefined;
+ opts.checkServerIdentity !== undefined ||
+ opts.key !== undefined ||
+ opts.cert !== undefined;
const buildPinnedAgent = (
validated: TValidatedHost | undefined,
@@ -291,7 +304,9 @@ const buildPinnedAgent = (
...(opts.ca !== undefined && { ca: opts.ca }),
...(opts.rejectUnauthorized !== undefined && { rejectUnauthorized: opts.rejectUnauthorized }),
...(opts.servername !== undefined && { servername: opts.servername }),
- ...(opts.checkServerIdentity !== undefined && { checkServerIdentity: opts.checkServerIdentity })
+ ...(opts.checkServerIdentity !== undefined && { checkServerIdentity: opts.checkServerIdentity }),
+ ...(opts.key !== undefined && { key: opts.key }),
+ ...(opts.cert !== undefined && { cert: opts.cert })
};
return new https.Agent(httpsOpts);
}
@@ -334,6 +349,8 @@ type TBuildSsrfSafeAgentOptions = {
servername?: string;
keepAlive?: boolean;
checkServerIdentity?: https.AgentOptions["checkServerIdentity"];
+ key?: string | Buffer;
+ cert?: string | Buffer;
};
/**
@@ -354,8 +371,17 @@ export const buildSsrfSafeAgent = async (
url: string,
options: TBuildSsrfSafeAgentOptions = {}
): Promise<http.Agent | https.Agent | undefined> => {
- const { allowPrivateIps, addressFamily, ca, rejectUnauthorized, servername, keepAlive, checkServerIdentity } =
- options;
+ const {
+ allowPrivateIps,
+ addressFamily,
+ ca,
+ rejectUnauthorized,
+ servername,
+ keepAlive,
+ checkServerIdentity,
+ key,
+ cert
+ } = options;
const validated = await validateSsrfUrl(url, { allowPrivateIps });
const { protocol } = new URL(url);
return buildPinnedAgent(validated, protocol, {
@@ -364,7 +390,9 @@ export const buildSsrfSafeAgent = async (
rejectUnauthorized,
servername,
keepAlive,
- checkServerIdentity
+ checkServerIdentity,
+ key,
+ cert
});
};
@@ -393,6 +421,18 @@ type TSafeRequestExtras = {
* (e.g. Kubernetes API servers). Defaults to the URL hostname if unset.
*/
servername?: string;
+ /**
+ * mTLS client private key (PEM). Forwarded to the per-request https.Agent
+ * so it composes with the pinned lookup. Only applied to HTTPS URLs. Pair
+ * with `cert`. Required for endpoints that authenticate the client via
+ * certificate (e.g. OVH OKMS).
+ */
+ key?: string | Buffer;
+ /**
+ * mTLS client certificate (PEM). Forwarded to the per-request https.Agent.
+ * Only applied to HTTPS URLs. Pair with `key`.
+ */
+ cert?: string | Buffer;
};
type TSafeRequestConfig = Omit<AxiosRequestConfig, "httpAgent" | "httpsAgent" | "maxRedirects" | "url" | "method">;
@@ -426,11 +466,18 @@ const dispatch = async <T>(
data: unknown,
options: TSafeRequestConfig & TSafeRequestExtras = {}
) => {
- const { allowPrivateIps, addressFamily, ca, rejectUnauthorized, servername, ...axiosOpts } = options;
+ const { allowPrivateIps, addressFamily, ca, rejectUnauthorized, servername, key, cert, ...axiosOpts } = options;
const effectiveUrl = resolveBaseUrl(url, axiosOpts.baseURL);
const validated = await validateSsrfUrl(effectiveUrl, { allowPrivateIps });
const { protocol } = new URL(effectiveUrl);
- const agent = buildPinnedAgent(validated, protocol, { addressFamily, ca, rejectUnauthorized, servername });
+ const agent = buildPinnedAgent(validated, protocol, {
+ addressFamily,
+ ca,
+ rejectUnauthorized,
+ servername,
+ key,
+ cert
+ });
return request.request<T>({
...axiosOpts,
@@ -444,11 +491,18 @@ const dispatch = async <T>(
};
const dispatchFull = async <T>(config: TSafeRequestFullConfig) => {
- const { allowPrivateIps, addressFamily, ca, rejectUnauthorized, servername, url, ...axiosOpts } = config;
+ const { allowPrivateIps, addressFamily, ca, rejectUnauthorized, servername, key, cert, url, ...axiosOpts } = config;
const effectiveUrl = resolveBaseUrl(url, axiosOpts.baseURL);
const validated = await validateSsrfUrl(effectiveUrl, { allowPrivateIps });
const { protocol } = new URL(effectiveUrl);
- const agent = buildPinnedAgent(validated, protocol, { addressFamily, ca, rejectUnauthorized, servername });
+ const agent = buildPinnedAgent(validated, protocol, {
+ addressFamily,
+ ca,
+ rejectUnauthorized,
+ servername,
+ key,
+ cert
+ });
return request.request<T>({
...axiosOpts,
diff --git a/backend/src/services/app-connection/ovh/ovh-connection-fns.ts b/backend/src/services/app-connection/ovh/ovh-connection-fns.ts
index 0ec5bc31c1..0ec930e69f 100644
--- a/backend/src/services/app-connection/ovh/ovh-connection-fns.ts
+++ b/backend/src/services/app-connection/ovh/ovh-connection-fns.ts
@@ -1,12 +1,9 @@
-import https from "https";
-
-import { request } from "@app/lib/config/request";
import { BadRequestError } from "@app/lib/errors";
-import { validateSsrfUrl } from "@app/lib/validator/validate-url";
+import { safeRequest } from "@app/lib/validator";
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
import { OVHConnectionMethod } from "./ovh-connection-enums";
-import { TOvhConnection, TOvhConnectionConfig } from "./ovh-connection-types";
+import { TOvhConnectionConfig } from "./ovh-connection-types";
export const getOvhConnectionListItem = () => {
return {
@@ -16,25 +13,13 @@ export const getOvhConnectionListItem = () => {
};
};
-export const getOvhHttpsAgent = (connection: Pick<TOvhConnection, "credentials"> | TOvhConnectionConfig) => {
- const { privateKey, certificate } = connection.credentials;
-
- return new https.Agent({
- key: privateKey,
- cert: certificate
- });
-};
-
export const validateOvhConnectionCredentials = async (config: TOvhConnectionConfig) => {
- const { okmsDomain, okmsId } = config.credentials;
-
- await validateSsrfUrl(okmsDomain);
-
- const httpsAgent = getOvhHttpsAgent(config);
+ const { okmsDomain, okmsId, privateKey, certificate } = config.credentials;
try {
- await request.get(`${okmsDomain}/api/${encodeURIComponent(okmsId)}/v1/servicekey`, {
- httpsAgent,
+ await safeRequest.get(`${okmsDomain}/api/${encodeURIComponent(okmsId)}/v1/servicekey`, {
+ key: privateKey,
+ cert: certificate,
timeout: 15000,
validateStatus: (status) => status === 200
});
diff --git a/backend/src/services/app-connection/ovh/ovh-connection-schemas.ts b/backend/src/services/app-connection/ovh/ovh-connection-schemas.ts
index 5ff0ca3ced..38e52c000c 100644
--- a/backend/src/services/app-connection/ovh/ovh-connection-schemas.ts
+++ b/backend/src/services/app-connection/ovh/ovh-connection-schemas.ts
@@ -14,7 +14,13 @@ import { OVHConnectionMethod } from "./ovh-connection-enums";
export const OvhConnectionCertificateCredentialsSchema = z.object({
privateKey: z.string().trim().min(1, "Private key required").describe(AppConnections.CREDENTIALS.OVH.privateKey),
certificate: z.string().trim().min(1, "Certificate required").describe(AppConnections.CREDENTIALS.OVH.certificate),
- okmsDomain: z.string().trim().min(1, "OKMS domain required").describe(AppConnections.CREDENTIALS.OVH.okmsDomain),
+ okmsDomain: z
+ .string()
+ .trim()
+ .min(1, "OKMS domain required")
+ .url("OKMS domain must be a valid URL (e.g. https://example.com)")
+ .refine((val) => new URL(val).protocol === "https:", { message: "OKMS domain must use https" })
+ .describe(AppConnections.CREDENTIALS.OVH.okmsDomain),
okmsId: z.string().trim().min(1, "OKMS ID required").describe(AppConnections.CREDENTIALS.OVH.okmsId)
});
diff --git a/backend/src/services/secret-sync/ovh/ovh-sync-fns.ts b/backend/src/services/secret-sync/ovh/ovh-sync-fns.ts
index 72892e89ed..eaf0053eda 100644
--- a/backend/src/services/secret-sync/ovh/ovh-sync-fns.ts
+++ b/backend/src/services/secret-sync/ovh/ovh-sync-fns.ts
@@ -1,9 +1,7 @@
import { isAxiosError } from "axios";
-import https from "https";
-import { request } from "@app/lib/config/request";
import { deepEqual } from "@app/lib/fn/object";
-import { getOvhHttpsAgent } from "@app/services/app-connection/ovh";
+import { safeRequest } from "@app/lib/validator";
import { SecretSyncError } from "@app/services/secret-sync/secret-sync-errors";
import { matchesSchema } from "@app/services/secret-sync/secret-sync-fns";
import { TSecretMap } from "@app/services/secret-sync/secret-sync-types";
@@ -61,13 +59,15 @@ const readSecret = async (
okmsDomain: string,
okmsId: string,
path: string,
- httpsAgent: https.Agent
+ privateKey: string,
+ certificate: string
): Promise<TOvhSecretRead> => {
try {
- const { data } = await request.get<TOvhGetSecretResponse>(
+ const { data } = await safeRequest.get<TOvhGetSecretResponse>(
`${getSecretUrl(okmsDomain, okmsId, path)}?includeData=true`,
{
- httpsAgent,
+ key: privateKey,
+ cert: certificate,
timeout: REQUEST_TIMEOUT_MS
}
);
@@ -89,13 +89,15 @@ const createSecret = async (
okmsId: string,
path: string,
data: Record<string, string>,
- httpsAgent: https.Agent
+ privateKey: string,
+ certificate: string
) =>
- request.post(
+ safeRequest.post(
`${okmsDomain}/api/${encodeURIComponent(okmsId)}/v2/secret`,
{ path, version: { data } },
{
- httpsAgent,
+ key: privateKey,
+ cert: certificate,
timeout: REQUEST_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
@@ -110,15 +112,17 @@ const updateSecret = async (
path: string,
data: Record<string, string>,
cas: number | null,
- httpsAgent: https.Agent
+ privateKey: string,
+ certificate: string
) => {
const base = getSecretUrl(okmsDomain, okmsId, path);
const url = cas !== null ? `${base}?cas=${cas}` : base;
- return request.put(
+ return safeRequest.put(
url,
{ version: { data } },
{
- httpsAgent,
+ key: privateKey,
+ cert: certificate,
timeout: REQUEST_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
@@ -138,19 +142,22 @@ const writeSecretBundle = async (
) => {
const { connection, destinationConfig } = secretSync;
const path = String(destinationConfig.path);
- const { okmsDomain, okmsId } = connection.credentials;
- const httpsAgent = getOvhHttpsAgent(connection);
+ const { okmsDomain, okmsId, privateKey, certificate } = connection.credentials;
try {
- const { exists, data: existingData, currentVersion } = await readSecret(okmsDomain, okmsId, path, httpsAgent);
+ const {
+ exists,
+ data: existingData,
+ currentVersion
+ } = await readSecret(okmsDomain, okmsId, path, privateKey, certificate);
const desiredData = buildDesiredBundle(existingData);
if (deepEqual(existingData, desiredData)) return;
if (exists) {
- await updateSecret(okmsDomain, okmsId, path, desiredData, currentVersion, httpsAgent);
+ await updateSecret(okmsDomain, okmsId, path, desiredData, currentVersion, privateKey, certificate);
} else {
- await createSecret(okmsDomain, okmsId, path, desiredData, httpsAgent);
+ await createSecret(okmsDomain, okmsId, path, desiredData, privateKey, certificate);
}
} catch (error) {
throw new SecretSyncError({ error: sanitizeOvhError(error) });
@@ -186,11 +193,10 @@ export const OvhSyncFns = {
getSecrets: async (secretSync: TOvhSyncWithCredentials): Promise<TSecretMap> => {
const { connection, destinationConfig } = secretSync;
const path = String(destinationConfig.path);
- const { okmsDomain, okmsId } = connection.credentials;
- const httpsAgent = getOvhHttpsAgent(connection);
+ const { okmsDomain, okmsId, privateKey, certificate } = connection.credentials;
try {
- const { data } = await readSecret(okmsDomain, okmsId, path, httpsAgent);
+ const { data } = await readSecret(okmsDomain, okmsId, path, privateKey, certificate);
return Object.fromEntries(Object.entries(data).map(([key, value]) => [key, { value }]));
} catch (error) {
throw new SecretSyncError({ error: sanitizeOvhError(error) });