Skip to content

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

Address greptile comments

Carlos Monastyrskia94879d54bd1
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 207665f072..3c2a8c1feb 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
@@ -20,6 +20,11 @@ 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 { matchesCertificateNameSchema } from "@app/services/pki-sync/pki-sync-fns";
import { TCertificateMap, TPkiSyncWithCredentials } from "@app/services/pki-sync/pki-sync-types";
@@ -271,10 +276,20 @@ export const awsSecretsManagerPkiSyncFactory = ({
let targetSecretName = certName;
if (syncOptions?.certificateNameSchema) {
const extendedCertData = certData as Record<string, unknown>;
- const safeCommonName = typeof extendedCertData.commonName === "string" ? extendedCertData.commonName : "";
+ 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 {
targetSecretName = `${AWS_SECRETS_MANAGER_PKI_SYNC_DEFAULTS.INFISICAL_PREFIX}${certificateId}`;
@@ -443,8 +458,17 @@ export const awsSecretsManagerPkiSyncFactory = ({
}
if (canRemoveCertificates) {
+ const trackedExternalIds = new Set(
+ existingSyncRecords.map((record) => record.externalIdentifier).filter((id): id is string => Boolean(id))
+ );
+ const allowPatternCleanup = !certificateNameSchemaHasFreeTextPlaceholder(syncOptions?.certificateNameSchema);
+
for (const [secretName] of Object.entries(existingSecrets)) {
if (!activeExternalIdentifiers.has(secretName)) {
+ if (!allowPatternCleanup && !trackedExternalIds.has(secretName)) {
+ // eslint-disable-next-line no-continue
+ continue;
+ }
try {
await withRateLimitRetry(
() =>
diff --git a/backend/src/services/pki-sync/azure-key-vault/azure-key-vault-pki-sync-fns.ts b/backend/src/services/pki-sync/azure-key-vault/azure-key-vault-pki-sync-fns.ts
index f2874f493c..28e1438c98 100644
--- a/backend/src/services/pki-sync/azure-key-vault/azure-key-vault-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/azure-key-vault/azure-key-vault-pki-sync-fns.ts
@@ -13,6 +13,7 @@ import { TCertificateSyncDALFactory } from "@app/services/certificate-sync/certi
import { CertificateSyncStatus } from "@app/services/certificate-sync/certificate-sync-enums";
import { createConnectionQueue, RateLimitConfig } from "@app/services/connection-queue";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
+import { certificateNameSchemaHasFreeTextPlaceholder } from "@app/services/pki-sync/pki-sync-certificate-name-fns";
import { matchesCertificateNameSchema } from "@app/services/pki-sync/pki-sync-fns";
import { TCertificateMap } from "@app/services/pki-sync/pki-sync-types";
@@ -447,21 +448,23 @@ export const azureKeyVaultPkiSyncFactory = ({
}
});
- Object.keys(vaultCertificates).forEach((certificateName) => {
- const isInfisicalManaged = isInfisicalManagedCertificate(certificateName, pkiSync);
+ if (!certificateNameSchemaHasFreeTextPlaceholder(syncOptions?.certificateNameSchema)) {
+ Object.keys(vaultCertificates).forEach((certificateName) => {
+ const isInfisicalManaged = isInfisicalManagedCertificate(certificateName, pkiSync);
- if (isInfisicalManaged) {
- const isTrackedInSyncRecords = existingSyncRecords.some(
- (record) => record.externalIdentifier === certificateName
- );
+ if (isInfisicalManaged) {
+ const isTrackedInSyncRecords = existingSyncRecords.some(
+ (record) => record.externalIdentifier === certificateName
+ );
- const isInActiveSet = activeExternalIdentifiers.has(certificateName);
+ const isInActiveSet = activeExternalIdentifiers.has(certificateName);
- if (!isTrackedInSyncRecords && !isInActiveSet && !certificatesToRemove.includes(certificateName)) {
- certificatesToRemove.push(certificateName);
+ if (!isTrackedInSyncRecords && !isInActiveSet && !certificatesToRemove.includes(certificateName)) {
+ certificatesToRemove.push(certificateName);
+ }
}
- }
- });
+ });
+ }
}
// Upload certificates to Azure Key Vault with rate limiting
diff --git a/backend/src/services/pki-sync/chef/chef-pki-sync-fns.ts b/backend/src/services/pki-sync/chef/chef-pki-sync-fns.ts
index 2c5cc14dd0..69c6cc2e0d 100644
--- a/backend/src/services/pki-sync/chef/chef-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/chef/chef-pki-sync-fns.ts
@@ -13,6 +13,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 } 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";
@@ -153,6 +154,7 @@ export const chefPkiSyncFactory = ({ certificateDAL, certificateSyncDAL }: TChef
| {
canRemoveCertificates?: boolean;
preserveItemOnRenewal?: boolean;
+ certificateNameSchema?: string;
fieldMappings?: {
certificate?: string;
privateKey?: string;
@@ -344,8 +346,15 @@ export const chefPkiSyncFactory = ({ certificateDAL, certificateSyncDAL }: TChef
if (canRemoveCertificates) {
const itemsToRemove: string[] = [];
+ const trackedExternalIds = new Set(
+ existingSyncRecords.map((record) => record.externalIdentifier).filter((id): id is string => Boolean(id))
+ );
+ const allowPatternCleanup = !certificateNameSchemaHasFreeTextPlaceholder(syncOptions?.certificateNameSchema);
+
Object.keys(chefDataBagItems).forEach((itemName) => {
- if (!activeExternalIdentifiers.has(itemName) && isInfisicalManagedCertificate(itemName, pkiSync)) {
+ if (activeExternalIdentifiers.has(itemName)) return;
+ const isTracked = trackedExternalIds.has(itemName);
+ if (isTracked || (allowPatternCleanup && isInfisicalManagedCertificate(itemName, pkiSync))) {
itemsToRemove.push(itemName);
}
});
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 8f3b464c9b..ee10bd96e8 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
@@ -17,7 +17,10 @@ import { TCertificateSyncDALFactory } from "@app/services/certificate-sync/certi
import { CertificateSyncStatus } from "@app/services/certificate-sync/certificate-sync-enums";
import { TCertificateMap } from "@app/services/pki-sync/pki-sync-types";
-import { compileCertificateNameSchema } from "../pki-sync-certificate-name-fns";
+import {
+ certificateNameSchemaHasFreeTextPlaceholder,
+ compileCertificateNameSchema
+} from "../pki-sync-certificate-name-fns";
import { PkiSync } from "../pki-sync-enums";
import { PkiSyncError } from "../pki-sync-errors";
import { TPkiSyncWithCredentials } from "../pki-sync-types";
@@ -1009,16 +1012,18 @@ export const f5BigIpPkiSyncFactory = ({
}
}
- const managedCertNamePattern = buildManagedCertNamePattern(certificateNameSchema);
const partitionCandidates: string[] = [];
- for (const certName of existingCertNames) {
- if (
- managedCertNamePattern.test(certName) &&
- !activeExternalIdentifiers.has(certName) &&
- !certNamesToRemove.has(certName) &&
- !certName.endsWith(F5_BIG_IP_CHAIN_SUFFIX)
- ) {
- partitionCandidates.push(certName);
+ if (!certificateNameSchemaHasFreeTextPlaceholder(certificateNameSchema)) {
+ const managedCertNamePattern = buildManagedCertNamePattern(certificateNameSchema);
+ for (const certName of existingCertNames) {
+ if (
+ managedCertNamePattern.test(certName) &&
+ !activeExternalIdentifiers.has(certName) &&
+ !certNamesToRemove.has(certName) &&
+ !certName.endsWith(F5_BIG_IP_CHAIN_SUFFIX)
+ ) {
+ partitionCandidates.push(certName);
+ }
}
}
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 243857ac6e..349705be17 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
@@ -13,7 +13,10 @@ import { TCertificateSyncDALFactory } from "@app/services/certificate-sync/certi
import { CertificateSyncStatus } from "@app/services/certificate-sync/certificate-sync-enums";
import { TCertificateMap } from "@app/services/pki-sync/pki-sync-types";
-import { compileCertificateNameSchema } from "../pki-sync-certificate-name-fns";
+import {
+ certificateNameSchemaHasFreeTextPlaceholder,
+ compileCertificateNameSchema
+} from "../pki-sync-certificate-name-fns";
import { PkiSync } from "../pki-sync-enums";
import { PkiSyncError } from "../pki-sync-errors";
import { TPkiSyncWithCredentials } from "../pki-sync-types";
@@ -585,11 +588,13 @@ export const netScalerPkiSyncFactory = ({
}
}
- const managedCertNamePattern = buildManagedCertNamePattern(certificateNameSchema);
+ if (!certificateNameSchemaHasFreeTextPlaceholder(certificateNameSchema)) {
+ const managedCertNamePattern = buildManagedCertNamePattern(certificateNameSchema);
- for (const certKeyName of existingCertKeyNames) {
- if (managedCertNamePattern.test(certKeyName) && !activeExternalIdentifiers.has(certKeyName)) {
- certKeysToRemove.add(certKeyName);
+ for (const certKeyName of existingCertKeyNames) {
+ if (managedCertNamePattern.test(certKeyName) && !activeExternalIdentifiers.has(certKeyName)) {
+ certKeysToRemove.add(certKeyName);
+ }
}
}
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 7940499f75..c317867b9d 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,4 +1,9 @@
-import { buildCertificateNameSchemaTestName, compileCertificateNameSchema } from "./pki-sync-certificate-name-fns";
+import {
+ buildCertificateNameSchemaTestName,
+ certificateNameSchemaHasFreeTextPlaceholder,
+ compileCertificateNameSchema,
+ sanitizeCertificateNameValue
+} from "./pki-sync-certificate-name-fns";
import { PkiSync } from "./pki-sync-enums";
const CERT_ID = "550e8400-e29b-41d4-a716-446655440000";
@@ -125,3 +130,30 @@ describe("buildCertificateNameSchemaTestName", () => {
expect(testName).toContain("{{friendlyName}}");
});
});
+
+describe("sanitizeCertificateNameValue", () => {
+ test("replaces destination-forbidden characters with hyphens", () => {
+ // AWS Secrets Manager forbids dots/spaces/wildcards in its charset
+ expect(sanitizeCertificateNameValue("app.example.com", PkiSync.AwsSecretsManager)).toBe("app-example-com");
+ expect(sanitizeCertificateNameValue("*.example.com", PkiSync.AwsSecretsManager)).toBe("--example-com");
+ });
+
+ test("preserves dots for destinations that allow them (NetScaler/F5)", () => {
+ expect(sanitizeCertificateNameValue("app.example.com", PkiSync.NetScaler)).toBe("app.example.com");
+ });
+
+ test("no-op without a destination", () => {
+ expect(sanitizeCertificateNameValue("app.example.com")).toBe("app.example.com");
+ });
+});
+
+describe("certificateNameSchemaHasFreeTextPlaceholder", () => {
+ test("true only for schemas containing the free-text {{commonName}} placeholder", () => {
+ expect(certificateNameSchemaHasFreeTextPlaceholder("{{commonName}}-{{certificateId}}")).toBe(true);
+ expect(certificateNameSchemaHasFreeTextPlaceholder("Infisical-{{certificateId}}")).toBe(false);
+ expect(certificateNameSchemaHasFreeTextPlaceholder("{{profileId}}-{{applicationId}}-{{certificateId}}")).toBe(
+ false
+ );
+ expect(certificateNameSchemaHasFreeTextPlaceholder(undefined)).toBe(false);
+ });
+});
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 dcce654aec..ffc7b8a56a 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
@@ -25,12 +25,17 @@ const NAME_VALUE_DISALLOWED_CHARS: Partial<Record<PkiSync, RE2>> = {
[PkiSync.F5BigIp]: new RE2("[^a-zA-Z0-9._-]", "g")
};
-const sanitizeNameValue = (value: string, destination?: PkiSync): string => {
+export const sanitizeCertificateNameValue = (value: string, destination?: PkiSync): string => {
if (!value || !destination) return value;
const disallowed = NAME_VALUE_DISALLOWED_CHARS[destination];
return disallowed ? value.replace(disallowed, "-") : value;
};
+export const UUID_NAME_REGEX_FRAGMENT = "[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}";
+
+export const certificateNameSchemaHasFreeTextPlaceholder = (schema?: string): boolean =>
+ Boolean(schema && schema.includes("{{commonName}}"));
+
export type TCertificateNameSchemaData = {
certificateId: string;
profileId?: string | null;
@@ -63,7 +68,7 @@ export const compileCertificateNameSchema = (
certificateId,
profileId,
applicationId,
- commonName: sanitizeNameValue(data.commonName || "", destination)
+ commonName: sanitizeCertificateNameValue(data.commonName || "", destination)
});
};
diff --git a/backend/src/services/pki-sync/pki-sync-fns.test.ts b/backend/src/services/pki-sync/pki-sync-fns.test.ts
index f74e64640d..e2bc3fe0a0 100644
--- a/backend/src/services/pki-sync/pki-sync-fns.test.ts
+++ b/backend/src/services/pki-sync/pki-sync-fns.test.ts
@@ -76,4 +76,17 @@ describe("matchesCertificateNameSchema (managed-certificate detection for cleanu
// {{environment}} is treated as a literal now, so a name where it was substituted with 'global' won't match.
expect(matchesCertificateNameSchema(`global-${HEX}`, "{{environment}}-{{certificateId}}")).toBe(false);
});
+
+ test("matches UUIDs whether dash-stripped or raw (AWS Secrets Manager stores the dashed form)", () => {
+ const dashed = "550e8400-e29b-41d4-a716-446655440000";
+ expect(matchesCertificateNameSchema(`infisical-${HEX}`, "infisical-{{certificateId}}")).toBe(true);
+ expect(matchesCertificateNameSchema(`infisical-${dashed}`, "infisical-{{certificateId}}")).toBe(true);
+ });
+
+ test("common-name slot uses a constrained charset, not a greedy .*", () => {
+ const schema = "{{commonName}}-{{certificateId}}";
+ // a value containing characters outside the sanitized set (e.g. a space) must not match
+ expect(matchesCertificateNameSchema(`weird name-${HEX}`, schema)).toBe(false);
+ expect(matchesCertificateNameSchema(`weird/name-${HEX}`, schema)).toBe(false);
+ });
});
diff --git a/backend/src/services/pki-sync/pki-sync-fns.ts b/backend/src/services/pki-sync/pki-sync-fns.ts
index 274a6abe2e..04d029fb1d 100644
--- a/backend/src/services/pki-sync/pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-fns.ts
@@ -26,6 +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 { PkiSync } from "./pki-sync-enums";
import { TCertificateMap, TPkiSyncWithCredentials } from "./pki-sync-types";
@@ -107,10 +108,10 @@ export const matchesCertificateNameSchema = (name: string, schema?: string): boo
.replace(new RE2("\\{\\{commonName\\}\\}", "g"), COMMON_NAME_TOKEN);
const pattern = escapeNameSchemaRegex(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"), ".*");
+ .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._-]*");
return new RE2(`^${pattern}$`).test(name);
};
diff --git a/frontend/src/components/pki-syncs/forms/PkiSyncOptionsFields/PkiSyncOptionsFields.tsx b/frontend/src/components/pki-syncs/forms/PkiSyncOptionsFields/PkiSyncOptionsFields.tsx
index f167aff235..2b46591fbf 100644
--- a/frontend/src/components/pki-syncs/forms/PkiSyncOptionsFields/PkiSyncOptionsFields.tsx
+++ b/frontend/src/components/pki-syncs/forms/PkiSyncOptionsFields/PkiSyncOptionsFields.tsx
@@ -448,8 +448,8 @@ export const PkiSyncOptionsFields = ({ destination }: Props) => {
</ul>
<span className="mt-1 text-xs text-bunker-300">
The schema must include <code>{"{{certificateId}}"}</code> to guarantee a unique
- name per certificate. Values that resolve to characters not allowed by the
- destination will be rejected at sync time.
+ name per certificate. Characters not allowed by the destination (e.g. dots in a
+ common name for Azure Key Vault) are automatically replaced with hyphens.
</span>
</div>
{syncOption?.forbiddenCharacters && syncOption.forbiddenCharacters.length > 0 && (