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(identity-tls-cert-auth): memoize certificate chain traversal
Alexander Sinnott269ea7547141
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 26d9f97877..c1b3b114a5 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@@ -368,6 +368,47 @@ describe("verifyClientCertificateChain", () => {expect(result).toEqual({ ok: true });});+ test("does not re-explore shared dead-end issuer paths", () => {+ let verificationCount = 0;+ const validFrom = NOT_BEFORE.toString();+ const validTo = FAR_FUTURE.toString();+ const toMockNative = (id: string, subject: string, issuer: string, ca: boolean) =>+ ({+ raw: Buffer.from(id),+ subject,+ issuer,+ ca,+ validFrom,+ validTo,+ publicKey: id,+ verify: () => {+ verificationCount += 1;+ return true;+ }+ }) as unknown as InstanceType<typeof crypto.X509Certificate>;++ const width = 4;+ const levels = 4;+ const deadEndChain = Array.from({ length: levels }, (_, level) =>+ Array.from({ length: width }, (_, index) =>+ toMockNative(`dead-${level}-${index}`, `CN=level-${level}`, `CN=level-${level + 1}`, true)+ )+ );+ const leaf = toMockNative("leaf", "CN=leaf", "CN=level-0", false);+ const successfulIssuer = toMockNative("success", "CN=level-0", "CN=anchor", true);+ const trustAnchor = toMockNative("anchor", "CN=anchor", "CN=anchor", true);++ const result = verifyClientCertificateChain({+ leaf,+ presentedChain: [...deadEndChain.flat(), successfulIssuer],+ trustAnchor,+ now: NOW+ });++ expect(result).toEqual({ ok: true });+ expect(verificationCount).toBeLessThan(150);+ });+test("rejects an expired leaf", async () => {const leaf = await makeLeaf("workload", intermediate, {notBefore: NOT_BEFORE,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 8bcba1f41d..0168542bd1 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@@ -72,59 +72,70 @@ export const verifyClientCertificateChain = ({}};- // Depth-first path build from the leaf up to the anchor. Bounded by the number of presented- // intermediates to prevent cycles; each intermediate may be used at most once on a path.- const maxDepth = presentedChain.length + 1;+ // Cache results by DER bytes, so a shared issuer is explored once regardless of how many paths+ // reach it. This bounds signature verification to O(n^2) and avoids factorial work for crafted+ // chains with many overlapping paths.+ const results = new Map<string, TVerifyClientCertificateChainResult>();+ const inProgress = new Set<string>();+ const certificateId = (cert: TNativeX509) => cert.raw.toString("base64");++ const walk = (current: TNativeX509): TVerifyClientCertificateChainResult => {+ const currentId = certificateId(current);+ const cachedResult = results.get(currentId);+ if (cachedResult) return cachedResult;+ if (inProgress.has(currentId)) return { ok: false, reasonCode: "ca_verification_failed" };++ inProgress.add(currentId);+ let result: TVerifyClientCertificateChainResult;- const walk = (current: TNativeX509, used: Set<number>, depth: number): TVerifyClientCertificateChainResult => {if (!isWithinValidityWindow(current, now)) {- return {+ result = {ok: false,reasonCode: now < new Date(current.validFrom) ? "certificate_not_yet_valid" : "certificate_expired"};- }-- // Reached the trust anchor: the path is complete and valid.- if (isAnchor(current)) return { ok: true };-- // The configured anchor directly issued the current certificate. The anchor must itself be a- // CA to sign certificates: presented intermediates are gated on `candidate.ca` below, and the- // trust anchor is held to the same bar so a non-CA cert can't anchor a path. Without this, a- // configured end-entity (CA:FALSE) certificate would still be accepted as the leaf's issuer.- if (trustAnchor.ca && issuedBy(current, trustAnchor)) {+ } else if (isAnchor(current)) {+ // Reached the trust anchor: the path is complete and valid.+ result = { ok: true };+ } else if (trustAnchor.ca && issuedBy(current, trustAnchor)) {+ // The configured anchor directly issued the current certificate. The anchor must itself be a+ // CA to sign certificates: presented intermediates are gated on `candidate.ca` below, and the+ // trust anchor is held to the same bar so a non-CA cert can't anchor a path. Without this, a+ // configured end-entity (CA:FALSE) certificate would still be accepted as the leaf's issuer.if (!isWithinValidityWindow(trustAnchor, now)) {- return {+ result = {ok: false,reasonCode: now < new Date(trustAnchor.validFrom) ? "certificate_not_yet_valid" : "certificate_expired"};+ } else {+ result = { ok: true };}- return { ok: true };- }-- if (depth >= maxDepth) return { ok: false, reasonCode: "ca_verification_failed" };-- // No path to the anchor was found through this node. Default to the generic reason, but- // surface a more specific validity failure if the only viable issuer was rejected for being- // outside its validity window (so an expired/not-yet-valid intermediate is reported as such).- let bestFailure: TVerifyClientCertificateChainResult = { ok: false, reasonCode: "ca_verification_failed" };-- for (let i = 0; i < presentedChain.length; i += 1) {- const candidate = presentedChain[i];- // An issuer on the path must be unused on this path, be a CA, and have signed the current- // certificate.- if (!used.has(i) && candidate.ca && issuedBy(current, candidate)) {- const result = walk(candidate, new Set(used).add(i), depth + 1);- if (result.ok) return result;- if (result.reasonCode !== "ca_verification_failed") {- bestFailure = result;+ } else {+ // No path to the anchor was found through this node. Default to the generic reason, but+ // surface a more specific validity failure if the only viable issuer was rejected for being+ // outside its validity window (so an expired/not-yet-valid intermediate is reported as such).+ result = { ok: false, reasonCode: "ca_verification_failed" };++ for (const candidate of presentedChain) {+ // An issuer on the path must be a CA and have signed the current certificate.+ if (candidate.ca && issuedBy(current, candidate)) {+ const candidateResult = walk(candidate);+ if (candidateResult.ok) {+ result = candidateResult;+ break;+ }+ if (candidateResult.reasonCode !== "ca_verification_failed") {+ result = candidateResult;+ }}}}- return bestFailure;+ inProgress.delete(currentId);+ results.set(currentId, result);+ return result;};- return walk(leaf, new Set<number>(), 0);+ return walk(leaf);};export const parseSubjectDetails = (data?: string | null) => {