Skip to content

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

Address PR comments

Carlos Monastyrski1fa630794baa
diff --git a/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.ts b/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.ts
index 3c2a8c1feb..23ea52d169 100644
--- a/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.ts
@@ -7,7 +7,6 @@ import {
SecretsManagerClient,
UpdateSecretCommand
} from "@aws-sdk/client-secrets-manager";
-import RE2 from "re2";
import { TCertificateSyncs } from "@app/db/schemas";
import { CustomAWSHasher } from "@app/lib/aws/hashing";
@@ -20,11 +19,7 @@ import { TCertificateDALFactory } from "@app/services/certificate/certificate-da
import { TCertificateSyncDALFactory } from "@app/services/certificate-sync/certificate-sync-dal";
import { CertificateSyncStatus } from "@app/services/certificate-sync/certificate-sync-enums";
import { createConnectionQueue, RateLimitConfig } from "@app/services/connection-queue";
-import {
- certificateNameSchemaHasFreeTextPlaceholder,
- sanitizeCertificateNameValue
-} from "@app/services/pki-sync/pki-sync-certificate-name-fns";
-import { PkiSync } from "@app/services/pki-sync/pki-sync-enums";
+import { certificateNameSchemaHasFreeTextPlaceholder } from "@app/services/pki-sync/pki-sync-certificate-name-fns";
import { matchesCertificateNameSchema } from "@app/services/pki-sync/pki-sync-fns";
import { TCertificateMap, TPkiSyncWithCredentials } from "@app/services/pki-sync/pki-sync-types";
@@ -274,24 +269,7 @@ export const awsSecretsManagerPkiSyncFactory = ({
}
let targetSecretName = certName;
- if (syncOptions?.certificateNameSchema) {
- const extendedCertData = certData as Record<string, unknown>;
- const safeCommonName = sanitizeCertificateNameValue(
- typeof extendedCertData.commonName === "string" ? extendedCertData.commonName : "",
- PkiSync.AwsSecretsManager
- );
- const profileId =
- typeof extendedCertData.profileId === "string" && extendedCertData.profileId
- ? extendedCertData.profileId
- : certificateId;
- const applicationId = typeof pkiSync.applicationId === "string" ? pkiSync.applicationId : "";
-
- targetSecretName = syncOptions.certificateNameSchema
- .replace(new RE2("\\{\\{certificateId\\}\\}", "g"), certificateId)
- .replace(new RE2("\\{\\{profileId\\}\\}", "g"), profileId)
- .replace(new RE2("\\{\\{applicationId\\}\\}", "g"), applicationId)
- .replace(new RE2("\\{\\{commonName\\}\\}", "g"), safeCommonName);
- } else {
+ if (!syncOptions?.certificateNameSchema) {
targetSecretName = `${AWS_SECRETS_MANAGER_PKI_SYNC_DEFAULTS.INFISICAL_PREFIX}${certificateId}`;
}
diff --git a/backend/src/services/pki-sync/f5-big-ip/f5-big-ip-pki-sync-fns.ts b/backend/src/services/pki-sync/f5-big-ip/f5-big-ip-pki-sync-fns.ts
index ee10bd96e8..bc910bfa04 100644
--- a/backend/src/services/pki-sync/f5-big-ip/f5-big-ip-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/f5-big-ip/f5-big-ip-pki-sync-fns.ts
@@ -18,6 +18,7 @@ import { CertificateSyncStatus } from "@app/services/certificate-sync/certificat
import { TCertificateMap } from "@app/services/pki-sync/pki-sync-types";
import {
+ buildManagedCertificateNameRegexSource,
certificateNameSchemaHasFreeTextPlaceholder,
compileCertificateNameSchema
} from "../pki-sync-certificate-name-fns";
@@ -92,31 +93,17 @@ const buildPartitionedPath = (partition: string, name: string) => `/${partition}
const buildEncodedObjectId = (partition: string, name: string) => encodeURIComponent(`~${partition}~${name}`);
-const escapeRegex = (s: string) => s.replace(new RE2("[\\\\^$.*+?()\\[\\]{}|/]", "g"), "\\$&");
-
export const buildManagedCertNamePattern = (certificateNameSchema: string | undefined): RE2 => {
if (!certificateNameSchema) {
return new RE2(`^${F5_BIG_IP_DEFAULT_CERT_NAME_PREFIX}[0-9a-f]{32}(-[a-zA-Z0-9]*)?$`);
}
- const CERT_ID_TOKEN = "__INFISICAL_CERT_ID_PLACEHOLDER__";
- const PROFILE_ID_TOKEN = "__INFISICAL_PROFILE_ID_PLACEHOLDER__";
- const APP_ID_TOKEN = "__INFISICAL_APP_ID_PLACEHOLDER__";
- const COMMON_NAME_TOKEN = "__INFISICAL_COMMON_NAME_PLACEHOLDER__";
-
- const tokenized = certificateNameSchema
- .replace(new RE2("\\{\\{certificateId\\}\\}", "g"), CERT_ID_TOKEN)
- .replace(new RE2("\\{\\{profileId\\}\\}", "g"), PROFILE_ID_TOKEN)
- .replace(new RE2("\\{\\{applicationId\\}\\}", "g"), APP_ID_TOKEN)
- .replace(new RE2("\\{\\{commonName\\}\\}", "g"), COMMON_NAME_TOKEN);
-
// {{certificateId}}, {{profileId}}, and {{applicationId}} resolve to dash-stripped UUIDs (32 hex chars).
// {{commonName}} is arbitrary, so match any run of BIG-IP-safe characters.
- const pattern = escapeRegex(tokenized)
- .replace(new RE2(CERT_ID_TOKEN, "g"), "[0-9a-f]{32}")
- .replace(new RE2(PROFILE_ID_TOKEN, "g"), "[0-9a-f]{32}")
- .replace(new RE2(APP_ID_TOKEN, "g"), "[0-9a-f]{32}")
- .replace(new RE2(COMMON_NAME_TOKEN, "g"), "[a-zA-Z0-9._-]*");
+ const pattern = buildManagedCertificateNameRegexSource(certificateNameSchema, {
+ uuid: "[0-9a-f]{32}",
+ commonName: "[a-zA-Z0-9._-]*"
+ });
return new RE2(`^${pattern}$`);
};
diff --git a/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts b/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts
index 349705be17..7befc9fabb 100644
--- a/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts
@@ -14,6 +14,7 @@ import { CertificateSyncStatus } from "@app/services/certificate-sync/certificat
import { TCertificateMap } from "@app/services/pki-sync/pki-sync-types";
import {
+ buildManagedCertificateNameRegexSource,
certificateNameSchemaHasFreeTextPlaceholder,
compileCertificateNameSchema
} from "../pki-sync-certificate-name-fns";
@@ -284,31 +285,17 @@ const removeNetScalerCertificate = async (
await deleteCertKey(session, certKeyName, true);
};
-const escapeRegex = (s: string) => s.replace(new RE2("[\\\\^$.*+?()\\[\\]{}|/]", "g"), "\\$&");
-
export const buildManagedCertNamePattern = (certificateNameSchema: string | undefined): RE2 => {
if (!certificateNameSchema) {
return new RE2("^Infisical-[0-9a-f]{32}(-[a-zA-Z0-9]*)?$");
}
- const CERT_ID_TOKEN = "__INFISICAL_CERT_ID_PLACEHOLDER__";
- const PROFILE_ID_TOKEN = "__INFISICAL_PROFILE_ID_PLACEHOLDER__";
- const APP_ID_TOKEN = "__INFISICAL_APP_ID_PLACEHOLDER__";
- const COMMON_NAME_TOKEN = "__INFISICAL_COMMON_NAME_PLACEHOLDER__";
-
- const tokenized = certificateNameSchema
- .replace(new RE2("\\{\\{certificateId\\}\\}", "g"), CERT_ID_TOKEN)
- .replace(new RE2("\\{\\{profileId\\}\\}", "g"), PROFILE_ID_TOKEN)
- .replace(new RE2("\\{\\{applicationId\\}\\}", "g"), APP_ID_TOKEN)
- .replace(new RE2("\\{\\{commonName\\}\\}", "g"), COMMON_NAME_TOKEN);
-
// {{certificateId}}, {{profileId}}, and {{applicationId}} resolve to dash-stripped UUIDs (32 hex chars).
// {{commonName}} is arbitrary, so match any run of NetScaler-safe characters.
- const pattern = escapeRegex(tokenized)
- .replace(new RE2(CERT_ID_TOKEN, "g"), "[0-9a-f]{32}")
- .replace(new RE2(PROFILE_ID_TOKEN, "g"), "[0-9a-f]{32}")
- .replace(new RE2(APP_ID_TOKEN, "g"), "[0-9a-f]{32}")
- .replace(new RE2(COMMON_NAME_TOKEN, "g"), "[a-zA-Z0-9._-]*");
+ const pattern = buildManagedCertificateNameRegexSource(certificateNameSchema, {
+ uuid: "[0-9a-f]{32}",
+ commonName: "[a-zA-Z0-9._-]*"
+ });
return new RE2(`^${pattern}$`);
};
diff --git a/backend/src/services/pki-sync/pki-sync-certificate-name-fns.test.ts b/backend/src/services/pki-sync/pki-sync-certificate-name-fns.test.ts
index c317867b9d..20dd139843 100644
--- a/backend/src/services/pki-sync/pki-sync-certificate-name-fns.test.ts
+++ b/backend/src/services/pki-sync/pki-sync-certificate-name-fns.test.ts
@@ -1,5 +1,6 @@
import {
buildCertificateNameSchemaTestName,
+ buildManagedCertificateNameRegexSource,
certificateNameSchemaHasFreeTextPlaceholder,
compileCertificateNameSchema,
sanitizeCertificateNameValue
@@ -147,6 +148,26 @@ describe("sanitizeCertificateNameValue", () => {
});
});
+describe("buildManagedCertificateNameRegexSource", () => {
+ const fragments = { uuid: "[0-9a-f]{32}", commonName: "[a-zA-Z0-9._-]*" };
+
+ test("substitutes placeholders and escapes literal regex characters", () => {
+ expect(buildManagedCertificateNameRegexSource("cert.{{certificateId}}", fragments)).toBe("cert\\.[0-9a-f]{32}");
+ expect(buildManagedCertificateNameRegexSource("{{commonName}}-{{certificateId}}", fragments)).toBe(
+ "[a-zA-Z0-9._-]*-[0-9a-f]{32}"
+ );
+ });
+
+ test("treats a sentinel-like literal as a literal, not a wildcard (no token-collision injection)", () => {
+ const source = buildManagedCertificateNameRegexSource(
+ "__INFISICAL_COMMON_NAME_PLACEHOLDER__-{{certificateId}}",
+ fragments
+ );
+ expect(source).toBe("__INFISICAL_COMMON_NAME_PLACEHOLDER__-[0-9a-f]{32}");
+ expect(source).not.toContain("[a-zA-Z0-9._-]*");
+ });
+});
+
describe("certificateNameSchemaHasFreeTextPlaceholder", () => {
test("true only for schemas containing the free-text {{commonName}} placeholder", () => {
expect(certificateNameSchemaHasFreeTextPlaceholder("{{commonName}}-{{certificateId}}")).toBe(true);
diff --git a/backend/src/services/pki-sync/pki-sync-certificate-name-fns.ts b/backend/src/services/pki-sync/pki-sync-certificate-name-fns.ts
index ffc7b8a56a..fc1e43ab05 100644
--- a/backend/src/services/pki-sync/pki-sync-certificate-name-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-certificate-name-fns.ts
@@ -36,6 +36,28 @@ export const UUID_NAME_REGEX_FRAGMENT = "[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?
export const certificateNameSchemaHasFreeTextPlaceholder = (schema?: string): boolean =>
Boolean(schema && schema.includes("{{commonName}}"));
+const PLACEHOLDER_OR_CHAR_REGEX = new RE2(
+ "\\{\\{(certificateId|profileId|applicationId|commonName)\\}\\}|[\\s\\S]",
+ "g"
+);
+const REGEX_SPECIAL_CHARS = new RE2("[\\\\^$.*+?()\\[\\]{}|/]", "g");
+
+export const buildManagedCertificateNameRegexSource = (
+ schema: string,
+ fragments: { uuid: string; commonName: string }
+): string => {
+ const fragmentByPlaceholder: Record<string, string> = {
+ certificateId: fragments.uuid,
+ profileId: fragments.uuid,
+ applicationId: fragments.uuid,
+ commonName: fragments.commonName
+ };
+
+ return schema.replace(PLACEHOLDER_OR_CHAR_REGEX, (match: string, placeholder?: string) =>
+ placeholder ? fragmentByPlaceholder[placeholder] : match.replace(REGEX_SPECIAL_CHARS, "\\$&")
+ );
+};
+
export type TCertificateNameSchemaData = {
certificateId: string;
profileId?: string | null;
diff --git a/backend/src/services/pki-sync/pki-sync-fns.ts b/backend/src/services/pki-sync/pki-sync-fns.ts
index 04d029fb1d..ea90633fcb 100644
--- a/backend/src/services/pki-sync/pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-fns.ts
@@ -26,7 +26,7 @@ import { F5_BIG_IP_PKI_SYNC_LIST_OPTION } from "./f5-big-ip/f5-big-ip-pki-sync-c
import { f5BigIpPkiSyncFactory } from "./f5-big-ip/f5-big-ip-pki-sync-fns";
import { NETSCALER_PKI_SYNC_LIST_OPTION } from "./netscaler/netscaler-pki-sync-constants";
import { netScalerPkiSyncFactory } from "./netscaler/netscaler-pki-sync-fns";
-import { UUID_NAME_REGEX_FRAGMENT } from "./pki-sync-certificate-name-fns";
+import { buildManagedCertificateNameRegexSource, UUID_NAME_REGEX_FRAGMENT } from "./pki-sync-certificate-name-fns";
import { PkiSync } from "./pki-sync-enums";
import { TCertificateMap, TPkiSyncWithCredentials } from "./pki-sync-types";
@@ -90,28 +90,13 @@ export const parsePkiSyncErrorMessage = (error: unknown): string => {
return "An unknown error occurred during PKI sync operation";
};
-const escapeNameSchemaRegex = (value: string): string =>
- value.replace(new RE2("[\\\\^$.*+?()\\[\\]{}|/]", "g"), "\\$&");
-
export const matchesCertificateNameSchema = (name: string, schema?: string): boolean => {
if (!schema) return true;
- const CERT_ID_TOKEN = "__INFISICAL_CERT_ID_PLACEHOLDER__";
- const PROFILE_ID_TOKEN = "__INFISICAL_PROFILE_ID_PLACEHOLDER__";
- const APP_ID_TOKEN = "__INFISICAL_APP_ID_PLACEHOLDER__";
- const COMMON_NAME_TOKEN = "__INFISICAL_COMMON_NAME_PLACEHOLDER__";
-
- const tokenized = schema
- .replace(new RE2("\\{\\{certificateId\\}\\}", "g"), CERT_ID_TOKEN)
- .replace(new RE2("\\{\\{profileId\\}\\}", "g"), PROFILE_ID_TOKEN)
- .replace(new RE2("\\{\\{applicationId\\}\\}", "g"), APP_ID_TOKEN)
- .replace(new RE2("\\{\\{commonName\\}\\}", "g"), COMMON_NAME_TOKEN);
-
- const pattern = escapeNameSchemaRegex(tokenized)
- .replace(new RE2(CERT_ID_TOKEN, "g"), UUID_NAME_REGEX_FRAGMENT)
- .replace(new RE2(PROFILE_ID_TOKEN, "g"), UUID_NAME_REGEX_FRAGMENT)
- .replace(new RE2(APP_ID_TOKEN, "g"), UUID_NAME_REGEX_FRAGMENT)
- .replace(new RE2(COMMON_NAME_TOKEN, "g"), "[a-zA-Z0-9._-]*");
+ const pattern = buildManagedCertificateNameRegexSource(schema, {
+ uuid: UUID_NAME_REGEX_FRAGMENT,
+ commonName: "[a-zA-Z0-9._-]*"
+ });
return new RE2(`^${pattern}$`).test(name);
};