Skip to content

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

validate on save time

Thiago Araujo da Silva659d6085898d
diff --git a/backend/e2e-test/routes/v1/identity-tls-cert-auth.spec.ts b/backend/e2e-test/routes/v1/identity-tls-cert-auth.spec.ts
index 4f943b56a3..9342d8043b 100644
--- a/backend/e2e-test/routes/v1/identity-tls-cert-auth.spec.ts
+++ b/backend/e2e-test/routes/v1/identity-tls-cert-auth.spec.ts
@@ -118,15 +118,13 @@ const deleteIdentity = async (identityId: string) => {
expect(res.statusCode).toBe(200);
};
-const attachTlsCertAuth = async (identityId: string, caCertificate: string, verifyClientCertificateChain: boolean) => {
- const res = await testServer.inject({
+const attachTlsCertAuth = (identityId: string, caCertificate: string, verifyClientCertificateChain: boolean) =>
+ testServer.inject({
method: "POST",
url: `/api/v1/auth/tls-cert-auth/identities/${identityId}`,
headers: { authorization: `Bearer ${jwtAuthToken}` },
body: { caCertificate, verifyClientCertificateChain }
});
- expect(res.statusCode).toBe(200);
-};
const login = (identityId: string, chain: x509.X509Certificate[]) =>
testServer.inject({
@@ -146,7 +144,8 @@ const withIdentity = async (
) => {
const identity = await createIdentity(`tls-cert-auth-${nextSerial()}`);
try {
- await attachTlsCertAuth(identity.id, toPem(opts.caCertificate), opts.verifyChain);
+ const res = await attachTlsCertAuth(identity.id, toPem(opts.caCertificate), opts.verifyChain);
+ expect(res.statusCode).toBe(200);
await run(identity.id);
} finally {
await deleteIdentity(identity.id);
@@ -178,15 +177,19 @@ describe("Identity TLS certificate auth v1", async () => {
});
});
- test("denies a chain whose configured CA may not issue for client authentication", async () => {
+ // A CA that cannot delegate client authentication is refused when it is configured, so the
+ // operator finds out then rather than through a login failure they have to trace back to it.
+ test("refuses to attach a CA that may not issue for client authentication", async () => {
const ca = await makeRoot("ServerAuth Only CA", [ekuExtension([SERVER_AUTH_EKU])]);
- const leaf = await makeLeaf("workload", ca, { usages: [CLIENT_AUTH_EKU] });
-
- await withIdentity({ caCertificate: ca.cert, verifyChain: false }, async (identityId) => {
- const res = await login(identityId, [leaf.cert]);
- expect(res.statusCode).toBe(401);
- expect(res.json().message).toContain("A CA in the certificate chain is not permitted");
- });
+ const identity = await createIdentity(`tls-cert-auth-${nextSerial()}`);
+
+ try {
+ const res = await attachTlsCertAuth(identity.id, toPem(ca.cert), false);
+ expect(res.statusCode).toBe(400);
+ expect(res.json().message).toContain("does not include client authentication");
+ } finally {
+ await deleteIdentity(identity.id);
+ }
});
});
diff --git a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
index d0caac9c11..12158e4bf6 100644
--- a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
+++ b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
@@ -70,6 +70,40 @@ const CHAIN_FAILURE_MESSAGES: Record<
path_length_exceeded: "Access denied: The certificate chain has more intermediate CAs than a CA in it permits."
};
+const normalizeCaCertificate = (caCertificate: string) => caCertificate.replace(/\s+/g, "");
+
+/**
+ * Reject a CA certificate the moment it is configured, rather than at every login it would deny.
+ *
+ * Only conditions that can never authenticate a client are rejected here. A certificate that is not
+ * yet valid is deliberately allowed through, since an operator may be pre-provisioning the next CA
+ * in a rotation.
+ */
+const validateCaCertificateUsable = (caCertificate: string) => {
+ let caCertificateX509: InstanceType<typeof crypto.nativeCrypto.X509Certificate>;
+ try {
+ caCertificateX509 = new crypto.nativeCrypto.X509Certificate(caCertificate);
+ } catch {
+ throw new BadRequestError({
+ message: "CA certificate could not be read. Provide a PEM-encoded X.509 certificate."
+ });
+ }
+
+ const validTo = new Date(caCertificateX509.validTo);
+ if (validTo < new Date()) {
+ throw new BadRequestError({
+ message: `CA certificate expired on ${validTo.toISOString()}. Provide a CA certificate that is still valid.`
+ });
+ }
+
+ if (!permitsClientAuth(caCertificateX509)) {
+ throw new BadRequestError({
+ message:
+ "CA certificate's extended key usage does not include client authentication, so no certificate it issues could be used to log in. Provide a CA certificate that permits client authentication."
+ });
+ }
+};
+
type TIdentityTlsCertAuthServiceFactoryDep = {
identityDAL: Pick<TIdentityDALFactory, "findById">;
identityAccessTokenDAL: Pick<TIdentityAccessTokenDALFactory, "delete">;
@@ -485,6 +519,8 @@ export const identityTlsCertAuthServiceFactory = ({
return extractIPDetails(accessTokenTrustedIp.ipAddress);
});
+ validateCaCertificateUsable(caCertificate);
+
const { encryptor } = await kmsService.createCipherPairWithDataKey({
type: KmsDataKey.Organization,
orgId: identityMembershipOrg.scopeOrgId
@@ -603,11 +639,18 @@ export const identityTlsCertAuthServiceFactory = ({
});
return extractIPDetails(accessTokenTrustedIp.ipAddress);
});
- const { encryptor } = await kmsService.createCipherPairWithDataKey({
+ const { encryptor, decryptor } = await kmsService.createCipherPairWithDataKey({
type: KmsDataKey.Organization,
orgId: identityMembershipOrg.scopeOrgId
});
+ const storedCaCertificate = decryptor({
+ cipherTextBlob: identityTlsCertAuth.encryptedCaCertificate
+ }).toString();
+ if (caCertificate && normalizeCaCertificate(caCertificate) !== normalizeCaCertificate(storedCaCertificate)) {
+ validateCaCertificateUsable(caCertificate);
+ }
+
const updatedTlsCertAuth = await identityTlsCertAuthDAL.updateById(identityTlsCertAuth.id, {
allowedCommonNames,
allowedSubjectAltNames: serializeAllowedSubjectAltNames(allowedSubjectAltNames),