Skip to content

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

return 401 on error

Thiago Araujo da Silvaa3b77b9721b7
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.ts
index 7f59f74bf7..2e94f6eec4 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
@@ -2,7 +2,7 @@ import crypto from "node:crypto";
import * as x509 from "@peculiar/x509";
import { GeneralName, GeneralSubtree, NameConstraints } from "pkijs";
-import { beforeAll, describe, expect, test } from "vitest";
+import { beforeAll, describe, expect, test, vi } from "vitest";
import {
isSubjectAltNameAllowed,
@@ -15,6 +15,13 @@ import {
verifyClientCertificateChain
} from "./identity-tls-cert-auth-fns";
+// The singleton logger is only initialized by the server bootstrap, so it is undefined under vitest.
+vi.mock("@app/lib/logger", () => ({
+ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
+ initLogger: vi.fn(),
+ sanitizeUrlForLog: (url: string) => url
+}));
+
describe("parseSubjectDetails", () => {
test("parses a single CN field", () => {
expect(parseSubjectDetails("CN=my-service")).toEqual({ CN: "my-service" });
@@ -290,6 +297,17 @@ describe("permitsClientAuth", () => {
test("rejects a codeSigning-only certificate", async () => {
expect(permitsClientAuth(await makeLeafWithUsages(["1.3.6.1.5.5.7.3.3"]))).toBe(false);
});
+
+ // Node's OpenSSL-backed parser accepts encodings @peculiar/x509 rejects, so this stands in for a
+ // certificate that reached the check and then fails to parse: it must deny, not throw.
+ test("rejects a certificate whose extensions cannot be parsed", () => {
+ const unparseable = {
+ raw: Buffer.from("not a certificate"),
+ subject: "CN=broken"
+ } as unknown as crypto.X509Certificate;
+
+ expect(permitsClientAuth(unparseable)).toBe(false);
+ });
});
describe("verifyClientCertificateChain", () => {
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.ts
index 3bb0d5f4fa..7ddd7c6e96 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
@@ -127,9 +127,20 @@ const enforceNameConstraints = async (orderedPath: TNativeX509[], now: Date): Pr
* A certificate that omits the extension is unconstrained and stays accepted, which is how OpenSSL
* and Go read a missing EKU and what keeps existing leaves that carry no EKU working. Only a
* certificate that explicitly enumerates purposes and leaves client authentication out is rejected.
+ *
+ * Node's X509Certificate is OpenSSL-backed and accepts DER that @peculiar/x509 rejects, so a
+ * certificate can parse at the edge and still fail here. Reading the extension is what establishes
+ * that client authentication is permitted, so a certificate whose extensions cannot be read has not
+ * established it and is denied rather than failing the request as an internal error.
*/
export const permitsClientAuth = (cert: TNativeX509): boolean => {
- const usages = new x509.X509Certificate(cert.raw).getExtension(x509.ExtendedKeyUsageExtension)?.usages;
+ let usages: readonly string[] | undefined;
+ try {
+ usages = new x509.X509Certificate(cert.raw).getExtension(x509.ExtendedKeyUsageExtension)?.usages;
+ } catch (err) {
+ logger.warn(err, `TLS certificate auth: could not read extended key usage [subject=${cert.subject}]`);
+ return false;
+ }
if (!usages) return true;
return usages.some((oid) => oid === CLIENT_AUTH_EKU_OID || oid === ANY_PURPOSE_EKU_OID);
};