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
validate every SAN
Thiago Araujo da Silvad6fda906590e
diff --git a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.test.ts b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.test.tsindex 88d279f34b..8ddf7ed964 100644--- a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.test.ts+++ b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.test.ts@@ -478,6 +478,30 @@ describe("verifyDirectlyIssuedClientCertificate", () => {return { cert, keys };};+ // Each group becomes its own subject alternative name extension, so a caller can build both the+ // conforming shape (one group) and a certificate that splits its names across two extensions.+ const makeLeafWithSans = async (+ issuer: { cert: x509.X509Certificate; keys: CryptoKeyPair },+ ...sanGroups: x509.JsonGeneralName[][]+ ) => {+ const keys = await crypto.webcrypto.subtle.generateKey(alg, true, ["sign", "verify"]);+ const cert = await x509.X509CertificateGenerator.create({+ serialNumber: "02",+ subject: "CN=workload",+ issuer: issuer.cert.subject,+ notBefore: NOT_BEFORE,+ notAfter: FAR_FUTURE,+ signingKey: issuer.keys.privateKey,+ publicKey: keys.publicKey,+ signingAlgorithm: alg,+ extensions: [+ new x509.BasicConstraintsExtension(false),+ ...sanGroups.map((sans) => new x509.SubjectAlternativeNameExtension(sans))+ ]+ });+ return { cert, keys };+ };+const permittedDnsConstraint = (permitted: string[]) =>new x509.Extension("2.5.29.30",@@ -548,6 +572,45 @@ describe("verifyDirectlyIssuedClientCertificate", () => {).toEqual({ ok: false, reasonCode: "name_constraint_violation" });});+ test("rejects a leaf pairing a permitted name with one outside the CA's namespace", async () => {+ const ca = await makeCa("Constrained CA", { extensions: [permittedDnsConstraint(["allowed.example.com"])] });+ const leaf = await makeLeafWithSans(ca, [+ { type: "dns", value: "workload.allowed.example.com" },+ { type: "dns", value: "victim.elsewhere.com" }+ ]);++ expect(+ await verifyDirectlyIssuedClientCertificate({ leaf: toNative(leaf.cert), ca: toNative(ca.cert), now: NOW })+ ).toEqual({ ok: false, reasonCode: "name_constraint_violation" });+ });++ test("accepts a leaf whose every name is inside the CA's namespace", async () => {+ const ca = await makeCa("Constrained CA", { extensions: [permittedDnsConstraint(["allowed.example.com"])] });+ const leaf = await makeLeafWithSans(ca, [+ { type: "dns", value: "one.allowed.example.com" },+ { type: "dns", value: "two.allowed.example.com" }+ ]);++ expect(+ await verifyDirectlyIssuedClientCertificate({ leaf: toNative(leaf.cert), ca: toNative(ca.cert), now: NOW })+ ).toEqual({ ok: true });+ });++ test("rejects an out-of-namespace name carried in a second subject alternative name extension", async () => {+ // A second extension is not conforming, but its names count all the same, so a leaf that keeps+ // the out-of-scope name out of the first extension must not slip past.+ const ca = await makeCa("Constrained CA", { extensions: [permittedDnsConstraint(["allowed.example.com"])] });+ const leaf = await makeLeafWithSans(+ ca,+ [{ type: "dns", value: "workload.allowed.example.com" }],+ [{ type: "dns", value: "victim.elsewhere.com" }]+ );++ expect(+ await verifyDirectlyIssuedClientCertificate({ leaf: toNative(leaf.cert), ca: toNative(ca.cert), now: NOW })+ ).toEqual({ ok: false, reasonCode: "name_constraint_violation" });+ });+test("rejects a leaf that is not yet valid", async () => {const ca = await makeCa("Root CA");const leaf = await makeLeaf(ca, "workload.example.com");@@ -942,9 +1005,10 @@ describe("verifyClientCertificateChain", () => {const makeLeaf = async (name: string,issuer: TIssued,- opts?: { notBefore?: Date; notAfter?: Date; dnsName?: string }+ opts?: { notBefore?: Date; notAfter?: Date; dnsName?: string; sans?: x509.JsonGeneralName[] }) => {const keys = await crypto.webcrypto.subtle.generateKey(alg, true, ["sign", "verify"]);+ const sans: x509.JsonGeneralName[] = opts?.sans ?? (opts?.dnsName ? [{ type: "dns", value: opts.dnsName }] : []);const cert = await x509.X509CertificateGenerator.create({serialNumber: "03",subject: `CN=${name}`,@@ -956,7 +1020,7 @@ describe("verifyClientCertificateChain", () => {signingAlgorithm: alg,extensions: [new x509.BasicConstraintsExtension(false),- ...(opts?.dnsName ? [new x509.SubjectAlternativeNameExtension([{ type: "dns", value: opts.dnsName }])] : [])+ ...(sans.length ? [new x509.SubjectAlternativeNameExtension(sans)] : [])]});return { cert, keys };@@ -1202,6 +1266,99 @@ describe("verifyClientCertificateChain", () => {expect(result).toEqual({ ok: true });});+ test("rejects a leaf pairing a permitted name with one outside its issuing CA's namespace", async () => {+ // The constrained sub-CA mints a leaf whose first name is inside team-a and whose second names a+ // workload it was never authorised to certify. Deciding the certificate on whichever name+ // happens to match would let the second one through, and an identity that pins it would then+ // authenticate the holder of the sub-CA.+ const constrained = await makeIntermediate("Constrained Intermediate CA", root, {+ serialNumber: "23",+ extraExtensions: [permittedDnsConstraint(["team-a.example.com"])]+ });+ const leaf = await makeLeaf("svc.team-a.example.com", constrained, {+ sans: [+ { type: "dns", value: "svc.team-a.example.com" },+ { type: "dns", value: "victim.team-b.example.com" }+ ]+ });++ const result = await verifyClientCertificateChain({+ leaf: toNative(leaf.cert),+ presentedChain: [toNative(constrained.cert)],+ trustAnchor: toNative(root.cert),+ now: NOW+ });+ expect(result).toEqual({ ok: false, reasonCode: "name_constraint_violation" });+ });++ test("rejects an out-of-namespace name that trails several permitted ones", async () => {+ const constrained = await makeIntermediate("Constrained Intermediate CA", root, {+ serialNumber: "24",+ extraExtensions: [permittedDnsConstraint(["team-a.example.com"])]+ });+ const leaf = await makeLeaf("svc.team-a.example.com", constrained, {+ sans: [+ { type: "dns", value: "one.team-a.example.com" },+ { type: "dns", value: "two.team-a.example.com" },+ { type: "dns", value: "victim.team-b.example.com" }+ ]+ });++ const result = await verifyClientCertificateChain({+ leaf: toNative(leaf.cert),+ presentedChain: [toNative(constrained.cert)],+ trustAnchor: toNative(root.cert),+ now: NOW+ });+ expect(result).toEqual({ ok: false, reasonCode: "name_constraint_violation" });+ });++ test("accepts a leaf whose every name is inside the permitted namespace", async () => {+ const constrained = await makeIntermediate("Constrained Intermediate CA", root, {+ serialNumber: "25",+ extraExtensions: [permittedDnsConstraint(["team-a.example.com"])]+ });+ const leaf = await makeLeaf("svc.team-a.example.com", constrained, {+ sans: [+ { type: "dns", value: "one.team-a.example.com" },+ { type: "dns", value: "two.team-a.example.com" },+ { type: "dns", value: "three.team-a.example.com" }+ ]+ });++ const result = await verifyClientCertificateChain({+ leaf: toNative(leaf.cert),+ presentedChain: [toNative(constrained.cert)],+ trustAnchor: toNative(root.cert),+ now: NOW+ });+ expect(result).toEqual({ ok: true });+ });++ test("leaves names of an unconstrained type alone", async () => {+ // Only DNS is constrained, so the URI names are outside what the CA restricted and must not be+ // held to the DNS subtrees.+ const constrained = await makeIntermediate("Constrained Intermediate CA", root, {+ serialNumber: "26",+ extraExtensions: [permittedDnsConstraint(["team-a.example.com"])]+ });+ const leaf = await makeLeaf("svc.team-a.example.com", constrained, {+ sans: [+ { type: "dns", value: "svc.team-a.example.com" },+ { type: "url", value: "spiffe://elsewhere.org/svc" },+ { type: "url", value: "spiffe://elsewhere.org/other" }+ ]+ });++ const result = await verifyClientCertificateChain({+ leaf: toNative(leaf.cert),+ presentedChain: [toNative(constrained.cert)],+ trustAnchor: toNative(root.cert),+ now: NOW+ });+ expect(result).toEqual({ ok: true });+ });+test("enforces name constraints carried by the configured anchor itself", async () => {const constrained = await makeIntermediate("Constrained Anchor CA", root, {serialNumber: "22",diff --git a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.ts b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.tsindex 0f516d7d2c..f7541baab9 100644--- a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.ts+++ b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.ts@@ -1,7 +1,14 @@import { webcrypto } from "node:crypto";import * as x509 from "@peculiar/x509";-import { Certificate, CertificateChainValidationEngine, CryptoEngine, GeneralSubtree, NameConstraints } from "pkijs";+import {+ AltName,+ Certificate,+ CertificateChainValidationEngine,+ CryptoEngine,+ GeneralSubtree,+ NameConstraints+} from "pkijs";import { crypto } from "@app/lib/crypto/cryptography";import { logger } from "@app/lib/logger";@@ -31,6 +38,7 @@ export type TVerifyClientCertificateChainResult =type TVerificationFailure = Extract<TVerifyClientCertificateChainResult, { ok: false }>;const NAME_CONSTRAINTS_EXTENSION_OID = "2.5.29.30";+const SUBJECT_ALT_NAME_EXTENSION_OID = "2.5.29.17";const CLIENT_AUTH_EKU_OID = CERT_EXTENDED_KEY_USAGES[CertExtendedKeyUsageType.CLIENT_AUTH].oid;const ANY_PURPOSE_EKU_OID = CERT_EXTENDED_KEY_USAGES[CertExtendedKeyUsageType.ANY_PURPOSE].oid;@@ -270,6 +278,77 @@ export const findNameConstraintsProblem = (cert: TNativeX509): TNameConstraintsPreturn constraint === null ? null : { kind: "unsupported_uri_subtree", constraint };};+/**+ * Every readable subject alternative name extension on the certificate, in the order pkijs reads+ * them. A conforming certificate carries at most one, but pkijs concatenates the names of all of+ * them, so a second one carries names the constraint evaluation sees and cannot be left out here.+ */+const subjectAltNamesOf = (cert: Certificate): AltName[] =>+ (cert.extensions ?? []).flatMap((ext) =>+ ext.extnID === SUBJECT_ALT_NAME_EXTENSION_OID && ext.parsedValue instanceof AltName ? [ext.parsedValue] : []+ );++const permittedSubtreeTypes = (states: TNameConstraintsState[]): Set<number> =>+ new Set(+ states.flatMap((state) =>+ state.status === "present" ? (state.constraints.permittedSubtrees ?? []).map((subtree) => subtree.base.type) : []+ )+ );++/**+ * How many rounds it takes to give every name a round of its own where it is the only name of its+ * type: the number of names carried by whichever constrained type is repeated the most.+ */+const roundsToCoverEveryName = (types: number[]): number => {+ const counts = new Map<number, number>();+ types.forEach((type) => counts.set(type, (counts.get(type) ?? 0) + 1));+ return Math.max(0, ...counts.values());+};++type TPerNameEvaluation = { certificate: Certificate; rounds: number; selectRound: (round: number) => void };++/**+ * A private copy of a certificate that carries more than one name of a constrained type, whose name+ * list can be rewritten to hold a single name of each type so pkijs evaluates that one name rather+ * than the certificate's names as a set. Null when the certificate repeats no constrained type, so+ * pkijs's own pass already decided each of its names on its own.+ *+ * The copy is parsed once and rewritten per round rather than reparsed per round, and it is+ * deliberately not published to the parse caches: every other reader of the certificate, the+ * identity's allow-list included, still has to see all of its names. It is also only made once the+ * shared parse has shown a copy is needed, so the ordinary certificate naming one workload does not+ * pay for a second decode.+ */+const perNameEvaluation = (+ cert: TNativeX509,+ parsed: Certificate,+ constrainedTypes: Set<number>+): TPerNameEvaluation | null => {+ const shared = subjectAltNamesOf(parsed).flatMap((altName) => altName.altNames);+ const rounds = roundsToCoverEveryName(shared.map((name) => name.type).filter((type) => constrainedTypes.has(type)));+ if (rounds < 2) return null;++ const certificate = Certificate.fromBER(cert.raw);+ const extensions = subjectAltNamesOf(certificate);+ if (!extensions.length) return null;+ const names = extensions.map((altName) => altName.altNames);++ return {+ certificate,+ rounds,+ selectRound: (round) => {+ const taken = new Map<number, number>();+ names.forEach((extensionNames, idx) => {+ extensions[idx].altNames = extensionNames.filter((name) => {+ const position = taken.get(name.type) ?? 0;+ taken.set(name.type, position + 1);+ return position === round;+ });+ });+ }+ };+};+/*** RFC 5280 6.1.4 (g): a CA may restrict the namespace its subordinates can certify. Without this,* the holder of a constrained sub-CA under the configured anchor could mint a leaf for any name and@@ -282,6 +361,16 @@ export const findNameConstraintsProblem = (cert: TNativeX509): TNameConstraintsP* supplied separately as the RFC's initial-permitted/excluded-subtrees inputs. That matters because* operators may pin a constrained sub-CA directly rather than the root above it.*+ * What is not delegated is how the names of one certificate combine. pkijs decides a permitted+ * subtree group by OR-ing every name of that type, so one name inside the permitted subtree carries+ * the rest, whereas 6.1.4 (g) requires every name of a constrained type to be within a permitted+ * subtree. Left alone, the holder of a constrained sub-CA could mint a leaf pairing one in-scope+ * name with any name it liked, and `isSubjectAltNameAllowed` would then authenticate the identity+ * that pins the out-of-scope one. So a certificate repeating a constrained type is also evaluated+ * one name at a time, by the same matcher: see `perNameEvaluation`. Excluded subtrees need no such+ * pass, because there OR-ing the names is what the RFC asks for: any name inside an excluded+ * subtree denies.+ ** Only runs when some certificate on the path actually asserts constraints, so a chain that has* none is validated exactly as before. A CA asserting constraints that cannot be read denies before* that point; see `nameConstraintsOf`.@@ -315,31 +404,58 @@ const enforceNameConstraints = async (orderedPath: TNativeX509[]): Promise<TVerireturn { ok: false, reasonCode: "unsupported_name_constraint" };}- // The gate above admits a certificate whose extensions could not be read, so a path pkijs finds- // no constraints on still reaches here. Nothing on it restricts anything, and running the engine- // anyway would subject the path to its unrelated policy processing.if (!pathConstraints.some((state) => state.status === "present")) return null;const anchorState = pathConstraints[pathConstraints.length - 1];const anchorConstraints = anchorState.status === "present" ? anchorState.constraints : null;- const { result, resultCode, resultMessage } = await new NameConstraintsEngine(parsedPath).verify(- {- initialPermittedSubtreesSet: anchorConstraints?.permittedSubtrees ?? [],- initialExcludedSubtreesSet: anchorConstraints?.excludedSubtrees ?? []- },- pkiCryptoEngine- );+ const evaluate = async (path: Certificate[]): Promise<TVerificationFailure | null> => {+ const { result, resultCode, resultMessage } = await new NameConstraintsEngine(path).verify(+ {+ initialPermittedSubtreesSet: anchorConstraints?.permittedSubtrees ?? [],+ initialExcludedSubtreesSet: anchorConstraints?.excludedSubtrees ?? []+ },+ pkiCryptoEngine+ );++ if (result) return null;+ if (resultCode === PERMITTED_SUBTREE_VIOLATION_CODE || resultCode === EXCLUDED_SUBTREE_VIOLATION_CODE) {+ return { ok: false, reasonCode: "name_constraint_violation" };+ }- if (result) return null;- if (resultCode === PERMITTED_SUBTREE_VIOLATION_CODE || resultCode === EXCLUDED_SUBTREE_VIOLATION_CODE) {- return { ok: false, reasonCode: "name_constraint_violation" };+ logger.warn(+ `TLS certificate auth: name constraint validation could not confirm the chain [resultCode=${resultCode}] [resultMessage=${resultMessage}] [leafSubject=${orderedPath[0].subject}] [anchorSubject=${orderedPath[orderedPath.length - 1].subject}]`+ );+ return { ok: false, reasonCode: "ca_verification_failed" };+ };++ const failure = await evaluate(parsedPath);+ if (failure) return failure;++ const constrainedTypes = permittedSubtreeTypes(pathConstraints);+ if (!constrainedTypes.size) return null;++ const perName = orderedPath.slice(0, -1).flatMap((cert, idx) => {+ const evaluation = perNameEvaluation(cert, parsedPath[idx], constrainedTypes);+ return evaluation ? [{ idx, evaluation }] : [];+ });++ const rounds = Math.max(0, ...perName.map(({ evaluation }) => evaluation.rounds));++ for (let round = 0; round < rounds; round += 1) {+ const path = [...parsedPath];+ perName.forEach(({ idx, evaluation }) => {+ if (evaluation.rounds <= round) return;+ evaluation.selectRound(round);+ path[idx] = evaluation.certificate;+ });++ // eslint-disable-next-line no-await-in-loop -- stop at the first name the path does not permit+ const roundFailure = await evaluate(path);+ if (roundFailure) return roundFailure;}- logger.warn(- `TLS certificate auth: name constraint validation could not confirm the chain [resultCode=${resultCode}] [resultMessage=${resultMessage}] [leafSubject=${orderedPath[0].subject}] [anchorSubject=${orderedPath[orderedPath.length - 1].subject}]`- );- return { ok: false, reasonCode: "ca_verification_failed" };+ return null;};/**diff --git a/docs/documentation/platform/identities/tls-cert-auth.mdx b/docs/documentation/platform/identities/tls-cert-auth.mdxindex 50f5ad7bd6..46e23dc7bb 100644--- a/docs/documentation/platform/identities/tls-cert-auth.mdx+++ b/docs/documentation/platform/identities/tls-cert-auth.mdx@@ -114,6 +114,10 @@ Here's some information about each field:`spiffe://example.org`, or writing one that carries a path, is not a hostname, so no clientcertificate can satisfy it and every login is denied. Infisical rejects such a CA Certificate when you save it.+ A client certificate that carries several names of a constrained type has to keep **every** one of+ them inside the permitted namespace. Pairing an in-scope name with an out-of-scope one does not+ make the out-of-scope name acceptable; the login is denied.+Name constraints cannot scope individual workloads, because a URI constraint applies to the hostand ignores the path. Use **Allowed Subject Alternative Names** for that.</Note>