Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(pki): enforce CA policy checks on certificate approval path (#6924)
Saif Ur Rahman0cb2864f1d39
diff --git a/backend/src/services/certificate-common/certificate-csr-utils.ts b/backend/src/services/certificate-common/certificate-csr-utils.tsindex 204a741954..63c2ae6d3d 100644--- a/backend/src/services/certificate-common/certificate-csr-utils.ts+++ b/backend/src/services/certificate-common/certificate-csr-utils.ts@@ -84,9 +84,10 @@ export const extractCertificateRequestFromCSR = (csr: string): TCertificateRequeconst basicConstraintsExtension = csrObj.getExtension("2.5.29.19") as x509.BasicConstraintsExtension;if (basicConstraintsExtension) {+ const parsedPathLength = basicConstraintsExtension.pathLength;certificateRequest.basicConstraints = {isCA: basicConstraintsExtension.ca,- pathLength: basicConstraintsExtension.pathLength+ pathLength: parsedPathLength !== undefined && parsedPathLength >= 0 ? parsedPathLength : undefined};}diff --git a/backend/src/services/certificate-v3/certificate-approval-fns.ts b/backend/src/services/certificate-v3/certificate-approval-fns.tsindex b99717787a..a0b7227ca1 100644--- a/backend/src/services/certificate-v3/certificate-approval-fns.ts+++ b/backend/src/services/certificate-v3/certificate-approval-fns.ts@@ -36,7 +36,11 @@ import { getProjectKmsCertificateKeyId } from "@app/services/project/project-fnsimport { TResourceMetadataDALFactory } from "@app/services/resource-metadata/resource-metadata-dal";import { copyMetadataFromRequestToCertificate } from "@app/services/resource-metadata/resource-metadata-fns";-import { CertExtendedKeyUsageType, CertKeyUsageType } from "../certificate-common/certificate-constants";+import {+ CertExtendedKeyUsageType,+ CertKeyUsageType,+ CertPolicyState+} from "../certificate-common/certificate-constants";import {calculateFinalRenewBeforeDays,extractCertificateFromBuffer,@@ -367,10 +371,54 @@ export const certificateApprovalServiceFactory = (validateCaSupport(ca, "CSR signing");+ const certPolicy = await certificatePolicyService.getPolicyById({+ actor: undefined,+ actorId: undefined,+ actorAuthMethod: undefined,+ actorOrgId: undefined,+ policyId: profile.certificatePolicyId,+ internal: true+ });++ if (!certPolicy) {+ throw new NotFoundError({ message: "Certificate policy not found for this profile" });+ }++ validateAlgorithmCompatibility(ca, certPolicy);++ const csrBasicConstraints = certRequest.basicConstraints as { isCA: boolean; pathLength?: number } | undefined;+ const policyIsCAState: CertPolicyState =+ (certPolicy.basicConstraints?.isCA as CertPolicyState) || CertPolicyState.DENIED;++ if (csrBasicConstraints?.isCA && policyIsCAState === CertPolicyState.DENIED) {+ throw new BadRequestError({+ message:+ "CA certificate issuance is not allowed by the current policy. The policy's CA:true basicConstraints must be set to 'allowed' or 'required'."+ });+ }++ let effectiveBasicConstraints = csrBasicConstraints;+ const storedPathLength = csrBasicConstraints?.pathLength;+ let effectivePathLength = storedPathLength !== undefined && storedPathLength >= 0 ? storedPathLength : undefined;++ if (csrBasicConstraints?.isCA && policyIsCAState !== CertPolicyState.DENIED) {+ const policyMaxPathLength = certPolicy.basicConstraints?.maxPathLength;+ effectiveBasicConstraints = {+ isCA: true,+ pathLength: policyMaxPathLength+ };+ if (+ policyMaxPathLength !== undefined &&+ policyMaxPathLength !== null &&+ policyMaxPathLength !== -1 &&+ (csrBasicConstraints.pathLength === undefined || csrBasicConstraints.pathLength === null)+ ) {+ effectivePathLength = policyMaxPathLength;+ }+ }+const { certificate, certificateChain, issuingCaCertificate, serialNumber, cert } =await certificateDAL.transaction(async (tx) => {- const csrBasicConstraints = certRequest.basicConstraints as { isCA: boolean; pathLength?: number } | undefined;-const certResult = await internalCaService.signCertFromCa({isInternal: true,caId: ca.id,@@ -382,8 +430,8 @@ export const certificateApprovalServiceFactory = (signatureAlgorithm: certRequest.signatureAlgorithm || undefined,keyAlgorithm: certRequest.keyAlgorithm || undefined,isFromProfile: true,- basicConstraints: csrBasicConstraints,- pathLength: csrBasicConstraints?.pathLength,+ basicConstraints: effectiveBasicConstraints,+ pathLength: effectivePathLength,tx});diff --git a/backend/src/services/certificate-v3/certificate-v3-service.ts b/backend/src/services/certificate-v3/certificate-v3-service.tsindex a9384a7f8a..56191e26b2 100644--- a/backend/src/services/certificate-v3/certificate-v3-service.ts+++ b/backend/src/services/certificate-v3/certificate-v3-service.ts@@ -1585,6 +1585,10 @@ export const certificateV3ServiceFactory = ({? { isCA: true, pathLength: effectiveBasicConstraints?.pathLength }: effectiveBasicConstraints;+ if (resolvedBasicConstraints) {+ mappedCertificateRequest.basicConstraints = resolvedBasicConstraints;+ }+const validationResult = await certificatePolicyService.validateCertificateRequest(profile.certificatePolicyId,mappedCertificateRequest@@ -1596,6 +1600,18 @@ export const certificateV3ServiceFactory = ({});}+ validateAlgorithmCompatibility(ca, policy);++ const policyIsCAState: CertPolicyState =+ (policy.basicConstraints?.isCA as CertPolicyState) || CertPolicyState.DENIED;++ if (shouldIssueAsCA && policyIsCAState === CertPolicyState.DENIED) {+ throw new BadRequestError({+ message:+ "CA certificate issuance is not allowed by this policy. The policy's CA:true basicConstraints must be set to 'allowed' or 'required'."+ });+ }+const csrApprovalFactory = APPROVAL_POLICY_FACTORY_MAP[ApprovalPolicyType.CertRequest](ApprovalPolicyType.CertRequest);@@ -1722,21 +1738,9 @@ export const certificateV3ServiceFactory = ({};}- validateAlgorithmCompatibility(ca, policy);-const effectiveSignatureAlgorithm = extractedSignatureAlgorithm;const effectiveKeyAlgorithm = extractedKeyAlgorithm;- const policyIsCAState: CertPolicyState =- (policy.basicConstraints?.isCA as CertPolicyState) || CertPolicyState.DENIED;-- if (shouldIssueAsCA && policyIsCAState === CertPolicyState.DENIED) {- throw new BadRequestError({- message:- "CA certificate issuance is not allowed by this policy. The policy's CA:true basicConstraints must be set to 'allowed' or 'required'."- });- }-// Transform policy basicConstraints to the format expected by the CA serviceconst caBasicConstraints = shouldIssueAsCA? { isCA: true, pathLength: policy.basicConstraints?.maxPathLength }