Skip to content

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

Merge remote-tracking branch 'origin/main' into feat/external-ca-issued-record

Carlos Monastyrskiaa76f263b17d
diff --cc backend/src/services/certificate/certificate-fns.test.ts
index 2078a416ae,28c4bbab89..b74dd52627
--- a/backend/src/services/certificate/certificate-fns.test.ts
+++ b/backend/src/services/certificate/certificate-fns.test.ts
@@@ -8,10 -9,11 +9,12 @@@ import
buildCertificateBundle,
CertificateThumbprintAlgorithm,
extractCertificateFields,
+ extractExternallyIssuedCertificateFields,
normalizeThumbprint,
- parseCertificateBody
+ parseCertificateBody,
+ resolveCertificateDeletionEligibility
} from "./certificate-fns";
+ import { CertificateDeletionEligibility, CertStatus } from "./certificate-types";
describe("normalizeThumbprint", () => {
const sha1Hex = "a".repeat(40);
@@@ -147,107 -149,75 +150,180 @@@ describe("parseCertificateBody usages"
});
});
+describe("extractExternallyIssuedCertificateFields", () => {
+ x509.cryptoProvider.set(webcrypto as unknown as Crypto);
+
+ const SCT_LIST_OID = "1.3.6.1.4.1.11129.2.4.2";
+ const TEMPLATE_NAME_OID = "1.3.6.1.4.1.311.20.2";
+
+ const buildIssuedCert = async (extensions: x509.Extension[], name = "CN=issued.example.com,O=Issued Corp") => {
+ const keys = await webcrypto.subtle.generateKey(
+ {
+ name: "RSASSA-PKCS1-v1_5",
+ hash: "SHA-384",
+ publicExponent: new Uint8Array([1, 0, 1]),
+ modulusLength: 3072
+ },
+ true,
+ ["sign", "verify"]
+ );
+
+ const cert = await x509.X509CertificateGenerator.createSelfSigned({
+ serialNumber: "02",
+ name,
+ notBefore: new Date("2026-01-01"),
+ notAfter: new Date("2027-01-01"),
+ keys: keys as CryptoKeyPair,
+ signingAlgorithm: { name: "RSASSA-PKCS1-v1_5", hash: "SHA-384" },
+ extensions
+ });
+
+ return cert;
+ };
+
+ test("records every extension the certificate carries, flagging the ones the request never asked for", async () => {
+ const requestedValue = Buffer.from([0x0c, 0x07, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65]);
+ const issued = await buildIssuedCert([
+ new x509.Extension(TEMPLATE_NAME_OID, false, requestedValue),
+ new x509.Extension(SCT_LIST_OID, false, Buffer.from([0x04, 0x02, 0x00, 0x42]))
+ ]);
+
+ const fields = extractExternallyIssuedCertificateFields(issued, [
+ { oid: TEMPLATE_NAME_OID, critical: false, value: requestedValue.toString("base64") }
+ ]);
+
+ expect(JSON.parse(fields.customExtensions as string)).toEqual([
+ { oid: SCT_LIST_OID, critical: false, value: "BAIAQg==", issuerAdded: true },
+ { oid: TEMPLATE_NAME_OID, critical: false, value: requestedValue.toString("base64") }
+ ]);
+ });
+
+ test("reads the subject, algorithms and subject alternative names the CA actually issued", async () => {
+ const issued = await buildIssuedCert([
+ new x509.SubjectAlternativeNameExtension([
+ { type: "dns", value: "issued.example.com" },
+ { type: "dns", value: "added-by-ca.example.com" }
+ ])
+ ]);
+
+ const fields = extractExternallyIssuedCertificateFields(issued);
+
+ expect(fields.commonName).toBe("issued.example.com");
+ expect(fields.subjectOrganization).toBe("Issued Corp");
+ expect(fields.altNames).toBe("issued.example.com,added-by-ca.example.com");
+ expect(fields.keyAlgorithm).toBe("RSA_3072");
+ expect(fields.signatureAlgorithm).toBe("RSA-SHA384");
+ });
+
+ // Otherwise each renewal asks for the duplicate again and the list grows until it overruns 4096.
+ test("collapses a repeated subject alternative name so renewals cannot accumulate copies", async () => {
+ const issued = await buildIssuedCert([
+ new x509.SubjectAlternativeNameExtension([
+ { type: "dns", value: "issued.example.com" },
+ { type: "dns", value: "added.example.com" },
+ { type: "dns", value: "issued.example.com" }
+ ])
+ ]);
+
+ const fields = extractExternallyIssuedCertificateFields(issued);
+
+ expect(fields.altNames).toBe("issued.example.com,added.example.com");
+ });
+
+ // The column is text, so a long list from a public CA is recorded in full rather than trimmed to
+ // whatever fit. Requests stay bounded at 4096 characters by the policy schema.
+ test("records every subject alternative name, past the old column width", async () => {
+ const many = Array.from({ length: 200 }, (_, index) => ({
+ type: "dns" as const,
+ value: `host-${String(index).padStart(3, "0")}.${"padding".repeat(4)}.example.com`
+ }));
+ const issued = await buildIssuedCert([new x509.SubjectAlternativeNameExtension(many)]);
+
+ const fields = extractExternallyIssuedCertificateFields(issued);
+
+ expect(many.map((san) => san.value).join(",").length).toBeGreaterThan(4096);
+ expect(fields.altNames).toBe(many.map((san) => san.value).join(","));
+ });
+
+ test("leaves the requested subject alternative names in place when the certificate carries none", async () => {
+ const issued = await buildIssuedCert([new x509.BasicConstraintsExtension(false)]);
+
+ const fields = extractExternallyIssuedCertificateFields(issued);
+
+ expect(fields).not.toHaveProperty("altNames");
+ expect({ altNames: "requested.example.com", ...fields }.altNames).toBe("requested.example.com");
+ });
+});
++
+ describe("resolveCertificateDeletionEligibility", () => {
+ const future = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
+ const past = new Date(Date.now() - 24 * 60 * 60 * 1000);
+
+ it("allows an expired certificate", () => {
+ expect(resolveCertificateDeletionEligibility({ status: CertStatus.ACTIVE, notAfter: past })).toBe(
+ CertificateDeletionEligibility.Expired
+ );
+ });
+
+ it("allows a discovered certificate that has not expired", () => {
+ expect(
+ resolveCertificateDeletionEligibility({
+ status: CertStatus.ACTIVE,
+ notAfter: future,
+ source: CertificateSource.Discovered
+ })
+ ).toBe(CertificateDeletionEligibility.Discovered);
+ });
+
+ it("allows an imported certificate that has not expired", () => {
+ expect(
+ resolveCertificateDeletionEligibility({
+ status: CertStatus.ACTIVE,
+ notAfter: future,
+ source: CertificateSource.Imported
+ })
+ ).toBe(CertificateDeletionEligibility.Imported);
+ });
+
+ it("blocks an issued certificate that has not expired", () => {
+ expect(
+ resolveCertificateDeletionEligibility({
+ status: CertStatus.ACTIVE,
+ notAfter: future,
+ source: CertificateSource.Issued
+ })
+ ).toBeNull();
+ });
+
+ it("blocks a certificate with no source, because issuance never records one", () => {
+ expect(resolveCertificateDeletionEligibility({ status: CertStatus.ACTIVE, notAfter: future })).toBeNull();
+ });
+
+ it("blocks a revoked certificate until it expires, so its serial stays on the CRL", () => {
+ expect(resolveCertificateDeletionEligibility({ status: CertStatus.REVOKED, notAfter: future })).toBeNull();
+ });
+
+ it("blocks a revoked certificate even when its source would exempt it", () => {
+ expect(
+ resolveCertificateDeletionEligibility({
+ status: CertStatus.REVOKED,
+ notAfter: future,
+ source: CertificateSource.Imported
+ })
+ ).toBeNull();
+
+ expect(
+ resolveCertificateDeletionEligibility({
+ status: CertStatus.REVOKED,
+ notAfter: future,
+ source: CertificateSource.Discovered
+ })
+ ).toBeNull();
+ });
+
+ it("allows a revoked certificate once it has expired", () => {
+ expect(resolveCertificateDeletionEligibility({ status: CertStatus.REVOKED, notAfter: past })).toBe(
+ CertificateDeletionEligibility.Expired
+ );
+ });
+ });
diff --cc backend/src/services/certificate/certificate-fns.ts
index 0b294733dc,1f36551c74..ad0ed1073f
--- a/backend/src/services/certificate/certificate-fns.ts
+++ b/backend/src/services/certificate/certificate-fns.ts
@@@ -3,12 -3,10 +3,13 @@@ import { Knex } from "knex"
import forge from "node-forge";
import RE2 from "re2";
+ import { CertificateSource } from "@app/ee/services/pki-discovery/pki-discovery-types";
import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
+import { logger } from "@app/lib/logger";
+import { SUPPORTED_GENERAL_NAME_TYPES } from "@app/services/certificate-common/certificate-constants";
import {
+ parseExternallyIssuedCustomExtensions,
parseIssuedCustomExtensions,
TResolvedCustomExtension
} from "@app/services/certificate-common/certificate-extension-fns";