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
misc: addressed feedback
Sheen Capadngan45ddffd3ffc2
diff --git a/backend/src/ee/services/agent-proxy-ca/agent-proxy-ca-service.ts b/backend/src/ee/services/agent-proxy-ca/agent-proxy-ca-service.tsindex 61c5bc25c8..529e2f2d27 100644--- a/backend/src/ee/services/agent-proxy-ca/agent-proxy-ca-service.ts+++ b/backend/src/ee/services/agent-proxy-ca/agent-proxy-ca-service.ts@@ -8,6 +8,8 @@ import { OrgServiceActor } from "@app/lib/types";import { CertKeyAlgorithm } from "@app/services/certificate/certificate-types";import {createSerialNumber,+ getNotAfterWithClockSkew,+ getNotBeforeWithClockSkew,keyAlgorithmToAlgCfg} from "@app/services/certificate-authority/certificate-authority-fns";import { TKmsServiceFactory } from "@app/services/kms/kms-service";@@ -29,7 +31,6 @@ type TAgentProxyCaServiceFactoryDep = {const ROOT_CA_ALGORITHM = CertKeyAlgorithm.ECDSA_P256;const INTERMEDIATE_CA_TTL_MS = 7 * 24 * 60 * 60 * 1000;const ROOT_CA_VALIDITY_YEARS = 10;-const CLOCK_SKEW_MS = 5 * 60 * 1000;export const agentProxyCaServiceFactory = ({orgAgentProxyConfigDAL,@@ -77,14 +78,13 @@ export const agentProxyCaServiceFactory = ({const rootCaSerialNumber = createSerialNumber();const rootCaIssuedAt = new Date();- const rootCaNotBefore = new Date(rootCaIssuedAt.getTime() - CLOCK_SKEW_MS);const rootCaExpiration = new Date(new Date().setFullYear(new Date().getFullYear() + ROOT_CA_VALIDITY_YEARS));const rootCaCert = await x509.X509CertificateGenerator.createSelfSigned({name: `O=${orgId},CN=Infisical Agent Proxy Root CA`,serialNumber: rootCaSerialNumber,- notBefore: rootCaNotBefore,- notAfter: rootCaExpiration,+ notBefore: getNotBeforeWithClockSkew(rootCaIssuedAt),+ notAfter: getNotAfterWithClockSkew(rootCaExpiration),signingAlgorithm: alg,keys: rootCaKeys,extensions: [@@ -181,16 +181,16 @@ export const agentProxyCaServiceFactory = ({const serialNumber = createSerialNumber();const issuedAt = new Date();- const notBefore = new Date(issuedAt.getTime() - CLOCK_SKEW_MS);- // clamp so an intermediate can never outlive the root it chains to- const requestedExpiration = new Date(issuedAt.getTime() + INTERMEDIATE_CA_TTL_MS);+ // clamp so an intermediate can never outlive the root it chains to, tolerance included — a root+ // minted before we widened notAfter carries none of its own for the clamp to absorb+ const requestedExpiration = getNotAfterWithClockSkew(new Date(issuedAt.getTime() + INTERMEDIATE_CA_TTL_MS));const expiration = requestedExpiration < rootCaCert.notAfter ? requestedExpiration : rootCaCert.notAfter;const intermediateCert = await x509.X509CertificateGenerator.create({serialNumber,subject: `O=${actor.orgId},CN=Infisical Agent Proxy Intermediate CA`,issuer: rootCaCert.subject,- notBefore,+ notBefore: getNotBeforeWithClockSkew(issuedAt),notAfter: expiration,signingKey: importedRootCaPrivateKey,publicKey: intermediatePublicKey,diff --git a/backend/src/ee/services/dynamic-secret/providers/ssh.ts b/backend/src/ee/services/dynamic-secret/providers/ssh.tsindex f238f2141b..d535138531 100644--- a/backend/src/ee/services/dynamic-secret/providers/ssh.ts+++ b/backend/src/ee/services/dynamic-secret/providers/ssh.ts@@ -101,7 +101,10 @@ export const SshProvider = (): TDynamicProviderFns => {keyId,principals: requestedPrincipals,requestedTtl,- certType: SshCertType.USER+ certType: SshCertType.USER,+ // revoke is a no-op for this provider, so the certificate's expiry is the only thing ending+ // the lease and must not drift past it+ enforceExactExpiry: true});return {diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts b/backend/src/ee/services/gateway-v2/gateway-v2-constants.tsindex 227ffadf05..45e7f2d9ac 100644--- a/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts+++ b/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts@@ -1,15 +1,6 @@export const HEARTBEAT_BUFFER_SECONDS = 30;export const DEFAULT_HEARTBEAT_TTL = 1800; // 30 minutes — fallback for old gateways that don't report their interval-/**- * Gateway certificates are minted just-in-time and verified by gateways and relays on hosts whose- * clocks we do not control. A notBefore of "now" makes a fresh certificate look not-yet-valid to any- * host running even fractionally behind us, so backdate it by a tolerance instead.- */-export const CERT_CLOCK_SKEW_MS = 5 * 60 * 1000;--export const getNotBeforeWithClockSkew = (issuedAt: Date) => new Date(issuedAt.getTime() - CERT_CLOCK_SKEW_MS);-export const GATEWAY_ROUTING_INFO_OID = "1.3.6.1.4.1.12345.100.1";export const GATEWAY_ACTOR_OID = "1.3.6.1.4.1.12345.100.2";export const PAM_INFO_OID = "1.3.6.1.4.1.12345.100.3";diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.tsindex c211c9b9fb..c4ecf97e68 100644--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts@@ -19,6 +19,8 @@ import { constructPemChainFromCerts } from "@app/services/certificate/certificatimport { CertExtendedKeyUsage, CertKeyAlgorithm, CertKeyUsage } from "@app/services/certificate/certificate-types";import {createSerialNumber,+ getNotAfterWithClockSkew,+ getNotBeforeWithClockSkew,keyAlgorithmToAlgCfg} from "@app/services/certificate-authority/certificate-authority-fns";import { TIdentityKubernetesAuthDALFactory } from "@app/services/identity-kubernetes-auth/identity-kubernetes-auth-dal";@@ -42,7 +44,6 @@ import {DEFAULT_HEARTBEAT_TTL,GATEWAY_ACTOR_OID,GATEWAY_ROUTING_INFO_OID,- getNotBeforeWithClockSkew,PAM_INFO_OID} from "./gateway-v2-constants";import { TGatewayV2DALFactory } from "./gateway-v2-dal";@@ -131,7 +132,7 @@ export const gatewayV2ServiceFactory = ({name: `O=${orgId},CN=Infisical Gateway Root CA`,serialNumber: rootCaSerialNumber,notBefore: getNotBeforeWithClockSkew(rootCaIssuedAt),- notAfter: rootCaExpiration,+ notAfter: getNotAfterWithClockSkew(rootCaExpiration),signingAlgorithm: alg,keys: rootCaKeys,extensions: [@@ -152,7 +153,7 @@ export const gatewayV2ServiceFactory = ({subject: `O=${orgId},CN=Infisical Gateway Server CA`,issuer: rootCaCert.subject,notBefore: getNotBeforeWithClockSkew(serverCaIssuedAt),- notAfter: serverCaExpiration,+ notAfter: getNotAfterWithClockSkew(serverCaExpiration),signingKey: rootCaKeys.privateKey,publicKey: serverCaKeys.publicKey,signingAlgorithm: alg,@@ -182,7 +183,7 @@ export const gatewayV2ServiceFactory = ({subject: `O=${orgId},CN=Infisical Gateway Client CA`,issuer: rootCaCert.subject,notBefore: getNotBeforeWithClockSkew(clientCaIssuedAt),- notAfter: clientCaExpiration,+ notAfter: getNotAfterWithClockSkew(clientCaExpiration),signingKey: rootCaKeys.privateKey,publicKey: clientCaKeys.publicKey,signingAlgorithm: alg,@@ -425,7 +426,7 @@ export const gatewayV2ServiceFactory = ({serialNumber: clientCertSerialNumber,subject: `O=${orgGatewayConfig.orgId},OU=gateway-client,CN=${ActorType.PLATFORM}:${gatewayId}`,issuer: gatewayClientCaCert.subject,- notAfter: clientCertExpiration,+ notAfter: getNotAfterWithClockSkew(clientCertExpiration),notBefore: getNotBeforeWithClockSkew(clientCertIssuedAt),signingKey: importedGatewayClientCaPrivateKey,publicKey: clientKeys.publicKey,@@ -586,7 +587,7 @@ export const gatewayV2ServiceFactory = ({serialNumber: clientCertSerialNumber,subject: `O=${orgGatewayConfig.orgId},OU=gateway-client,CN=${actorMetadata.type}:${gatewayId}`,issuer: gatewayClientCaCert.subject,- notAfter: clientCertExpiration,+ notAfter: getNotAfterWithClockSkew(clientCertExpiration),notBefore: getNotBeforeWithClockSkew(clientCertIssuedAt),signingKey: importedGatewayClientCaPrivateKey,publicKey: clientKeys.publicKey,@@ -694,7 +695,7 @@ export const gatewayV2ServiceFactory = ({subject: `O=${orgId},CN=Gateway`,issuer: gatewayServerCaCert.subject,notBefore: getNotBeforeWithClockSkew(gatewayServerCertIssuedAt),- notAfter: gatewayServerCertExpireAt,+ notAfter: getNotAfterWithClockSkew(gatewayServerCertExpireAt),signingKey: gatewayServerCaPrivateKey,publicKey: gatewayServerKeys.publicKey,signingAlgorithm: alg,diff --git a/backend/src/ee/services/relay/relay-service.ts b/backend/src/ee/services/relay/relay-service.tsindex 30bb068944..90fa50937f 100644--- a/backend/src/ee/services/relay/relay-service.ts+++ b/backend/src/ee/services/relay/relay-service.ts@@ -17,6 +17,8 @@ import { constructPemChainFromCerts, prependCertToPemChain } from "@app/servicesimport { CertExtendedKeyUsage, CertKeyAlgorithm, CertKeyUsage } from "@app/services/certificate/certificate-types";import {createSerialNumber,+ getNotAfterWithClockSkew,+ getNotBeforeWithClockSkew,keyAlgorithmToAlgCfg} from "@app/services/certificate-authority/certificate-authority-fns";import { TKmsServiceFactory } from "@app/services/kms/kms-service";@@ -28,7 +30,6 @@ import { SmtpTemplates, TSmtpService } from "@app/services/smtp/smtp-service";import { TUserDALFactory } from "@app/services/user/user-dal";import { verifyHostInputValidity } from "../dynamic-secret/dynamic-secret-fns";-import { CERT_CLOCK_SKEW_MS, getNotBeforeWithClockSkew } from "../gateway-v2/gateway-v2-constants";import { TGatewayV2DALFactory } from "../gateway-v2/gateway-v2-dal";import { OrgPermissionRelayActions, OrgPermissionSubjects } from "../permission/org-permission";import { TPermissionServiceFactory } from "../permission/permission-service-types";@@ -86,7 +87,7 @@ export const relayServiceFactory = ({name: `O=Infisical,CN=Infisical Instance Root Relay CA`,serialNumber: rootCaSerialNumber,notBefore: getNotBeforeWithClockSkew(rootCaIssuedAt),- notAfter: rootCaExpiration,+ notAfter: getNotAfterWithClockSkew(rootCaExpiration),signingAlgorithm: alg,keys: rootCaKeys,extensions: [@@ -107,7 +108,7 @@ export const relayServiceFactory = ({subject: `O=Infisical,CN=Infisical Organization Relay CA`,issuer: rootCaCert.subject,notBefore: getNotBeforeWithClockSkew(orgRelayCaIssuedAt),- notAfter: orgRelayCaExpiration,+ notAfter: getNotAfterWithClockSkew(orgRelayCaExpiration),signingKey: rootCaKeys.privateKey,publicKey: orgRelayCaKeys.publicKey,signingAlgorithm: alg,@@ -138,7 +139,7 @@ export const relayServiceFactory = ({subject: `O=Infisical,CN=Infisical Instance Relay CA`,issuer: rootCaCert.subject,notBefore: getNotBeforeWithClockSkew(instanceRelayCaIssuedAt),- notAfter: instanceRelayCaExpiration,+ notAfter: getNotAfterWithClockSkew(instanceRelayCaExpiration),signingKey: rootCaKeys.privateKey,publicKey: instanceRelayCaKeys.publicKey,signingAlgorithm: alg,@@ -169,7 +170,7 @@ export const relayServiceFactory = ({subject: `O=Infisical,CN=Infisical Instance Relay Client CA`,issuer: instanceRelayCaCert.subject,notBefore: getNotBeforeWithClockSkew(instanceRelayClientCaIssuedAt),- notAfter: instanceRelayClientCaExpiration,+ notAfter: getNotAfterWithClockSkew(instanceRelayClientCaExpiration),signingKey: instanceRelayCaKeys.privateKey,publicKey: instanceRelayClientCaKeys.publicKey,signingAlgorithm: alg,@@ -200,7 +201,7 @@ export const relayServiceFactory = ({subject: `O=Infisical,CN=Infisical Instance Relay Server CA`,issuer: instanceRelayCaCert.subject,notBefore: getNotBeforeWithClockSkew(instanceRelayServerCaIssuedAt),- notAfter: instanceRelayServerCaExpiration,+ notAfter: getNotAfterWithClockSkew(instanceRelayServerCaExpiration),signingKey: instanceRelayCaKeys.privateKey,publicKey: instanceRelayServerCaKeys.publicKey,signingAlgorithm: alg,@@ -455,7 +456,7 @@ export const relayServiceFactory = ({subject: `O=${orgId},CN=Infisical Org Relay Client CA`,issuer: orgRelayCaCert.subject,notBefore: getNotBeforeWithClockSkew(orgRelayClientCaIssuedAt),- notAfter: orgRelayClientCaExpiration,+ notAfter: getNotAfterWithClockSkew(orgRelayClientCaExpiration),signingKey: orgRelayCaPrivateKey,publicKey: orgRelayClientCaKeys.publicKey,signingAlgorithm: alg,@@ -490,7 +491,7 @@ export const relayServiceFactory = ({subject: `O=${orgId},CN=Infisical Org Relay Server CA`,issuer: orgRelayCaCert.subject,notBefore: getNotBeforeWithClockSkew(orgRelayServerCaIssuedAt),- notAfter: orgRelayServerCaExpiration,+ notAfter: getNotAfterWithClockSkew(orgRelayServerCaExpiration),signingKey: orgRelayCaPrivateKey,publicKey: orgRelayServerCaKeys.publicKey,signingAlgorithm: alg,@@ -681,7 +682,7 @@ export const relayServiceFactory = ({subject: `CN=${host},O=${orgId ?? "Infisical"},OU=Relay`,issuer: relayServerCaCert.subject,notBefore: getNotBeforeWithClockSkew(relayServerCertIssuedAt),- notAfter: relayServerCertExpireAt,+ notAfter: getNotAfterWithClockSkew(relayServerCertExpireAt),signingKey: relayServerCaPrivateKey,publicKey: relayServerKeys.publicKey,signingAlgorithm: alg,@@ -699,8 +700,7 @@ export const relayServiceFactory = ({keyId: "relay-server",principals: [`${host}:2222`],certType: SshCertType.HOST,- requestedTtl: "30d",- clockSkewSeconds: CERT_CLOCK_SKEW_MS / 1000+ requestedTtl: "30d"});return {@@ -795,7 +795,7 @@ export const relayServiceFactory = ({serialNumber: clientCertSerialNumber,subject: `O=${orgName}-${orgId},OU=relay-client,CN=${gatewayId}`,issuer: relayClientCaCert.subject,- notAfter: clientCertExpiration,+ notAfter: getNotAfterWithClockSkew(clientCertExpiration),notBefore: getNotBeforeWithClockSkew(clientCertIssuedAt),signingKey: importedRelayClientCaPrivateKey,publicKey: clientKeys.publicKey,@@ -854,8 +854,7 @@ export const relayServiceFactory = ({keyId: `client-${relayName}`,principals: [gatewayId],certType: SshCertType.USER,- requestedTtl: "1d",- clockSkewSeconds: CERT_CLOCK_SKEW_MS / 1000+ requestedTtl: "1d"});return {@@ -873,8 +872,7 @@ export const relayServiceFactory = ({keyId: `client-${relayName}`,principals: [gatewayId, gatewayName],certType: SshCertType.USER,- requestedTtl: "1d",- clockSkewSeconds: CERT_CLOCK_SKEW_MS / 1000+ requestedTtl: "1d"});return {diff --git a/backend/src/lib/ssh/index.ts b/backend/src/lib/ssh/index.tsindex ffa9d30667..b37dc93fa6 100644--- a/backend/src/lib/ssh/index.ts+++ b/backend/src/lib/ssh/index.ts@@ -8,11 +8,14 @@ import { crypto } from "@app/lib/crypto/cryptography";import { BadRequestError } from "@app/lib/errors";import { ms } from "@app/lib/ms";import { CharacterType, characterValidator } from "@app/lib/validator/validate-string";+import { CERT_CLOCK_SKEW_MS } from "@app/services/certificate-common/certificate-constants";const execFileAsync = promisify(execFile);const EXEC_TIMEOUT_MS = 10000; // 10 seconds+const DEFAULT_CLOCK_SKEW_SECONDS = CERT_CLOCK_SKEW_MS / 1000;+export enum SshCertKeyAlgorithm {RSA_2048 = "RSA_2048",RSA_4096 = "RSA_4096",@@ -33,9 +36,13 @@ export type TCreateSshCertDTO = {principals: string[];requestedTtl?: string;certType: SshCertType;- // backdates the certificate's valid-from so a verifying host running behind us does not reject it- // as not-yet-valid; the expiry stays at issuance + ttl either way+ // Widens the validity window by this many seconds at both ends, so a verifying host whose clock+ // runs behind or ahead of ours rejects the certificate as neither not-yet-valid nor expired.clockSkewSeconds?: number;+ // Set when the requested TTL is the only thing enforcing an access boundary, in which case the+ // expiry has to stay at issuance + ttl. The start is still backdated, since moving that end+ // cannot extend access past the boundary.+ enforceExactExpiry?: boolean;};/* eslint-disable no-bitwise */@@ -192,7 +199,8 @@ export const createSshCert = async ({principals,requestedTtl, // in ms lib formatcertType,- clockSkewSeconds+ clockSkewSeconds = DEFAULT_CLOCK_SKEW_SECONDS,+ enforceExactExpiry = false}: TCreateSshCertDTO) => {let ttl: number | undefined;@@ -207,6 +215,14 @@ export const createSshCert = async ({});}+ // the skew is interpolated into ssh-keygen's -V argument, where a negative value reads as another+ // flag and a fractional one is rejected outright, both surfacing as an opaque non-zero exit+ if (!Number.isInteger(clockSkewSeconds) || clockSkewSeconds <= 0) {+ throw new BadRequestError({+ message: "Failed to create SSH certificate due to a clock skew that is not a whole number of seconds above zero"+ });+ }+validateSshCertificateKeyId(keyId);await validateSshPublicKey(clientPublicKey);@@ -217,6 +233,7 @@ export const createSshCert = async ({const signedPublicKeyFile = path.join(tempDir, "user_key-cert.pub");const serialNumber = createSshCertSerialNumber();+ const validitySeconds = enforceExactExpiry ? ttl : ttl + clockSkewSeconds;// Build `ssh-keygen` arguments for signing// Using an array avoids shell injection issues@@ -229,7 +246,7 @@ export const createSshCert = async ({"-n",principals.join(","), // principals"-V",- clockSkewSeconds ? `-${clockSkewSeconds}s:+${ttl}s` : `+${ttl}s`, // validity (TTL in seconds)+ `-${clockSkewSeconds}s:+${validitySeconds}s`, // validity, widened for clock skew at both ends"-z",serialNumber, // serial numberpublicKeyFile // public key file to signdiff --git a/backend/src/services/certificate-authority/certificate-authority-fns.ts b/backend/src/services/certificate-authority/certificate-authority-fns.tsindex 38e53678f0..f9e60d359b 100644--- a/backend/src/services/certificate-authority/certificate-authority-fns.ts+++ b/backend/src/services/certificate-authority/certificate-authority-fns.ts@@ -16,7 +16,7 @@ import {CertStatus,TAltNameType} from "../certificate/certificate-types";-import { DEFAULT_CRL_VALIDITY_DAYS } from "../certificate-common/certificate-constants";+import { CERT_CLOCK_SKEW_MS, DEFAULT_CRL_VALIDITY_DAYS } from "../certificate-common/certificate-constants";import { buildHsmCaSigner, buildLocalCaSigner, caKeyAlgorithmToHsmShape, TCaSigner } from "./ca-signer";import { TCertificateAuthorityDALFactory } from "./certificate-authority-dal";import {@@ -35,6 +35,12 @@ export const createSerialNumber = () => {return randomBytes.toString("hex");};+// on anything short-lived these belong together: backdating the start without pushing out the+// expiry leaves the certificate rejected as expired by a host whose clock runs ahead of ours+export const getNotBeforeWithClockSkew = (issuedAt: Date) => new Date(issuedAt.getTime() - CERT_CLOCK_SKEW_MS);++export const getNotAfterWithClockSkew = (expiresAt: Date) => new Date(expiresAt.getTime() + CERT_CLOCK_SKEW_MS);+export const assertCaInProfileProject = (ca: { projectId: string }, profile: { projectId: string }) => {if (ca.projectId !== profile.projectId) {throw new ForbiddenRequestError({diff --git a/backend/src/services/certificate-common/certificate-constants.ts b/backend/src/services/certificate-common/certificate-constants.tsindex a7e777cb05..4b35157192 100644--- a/backend/src/services/certificate-common/certificate-constants.ts+++ b/backend/src/services/certificate-common/certificate-constants.ts@@ -353,6 +353,17 @@ export const CERTIFICATE_RENEWAL_CONFIG = {export const DEFAULT_CRL_VALIDITY_DAYS = 7;+/**+ * Certificates we mint just-in-time are verified by gateways, relays, and agents on hosts whose+ * clocks we do not control. A notBefore of "now" makes a fresh certificate look not-yet-valid to a+ * host running behind us, and a notAfter of "issuance + ttl" makes a short-lived one look already+ * expired to a host running ahead of us, so widen the window by this tolerance at both ends.+ *+ * Lives here rather than next to the getNotBefore/getNotAfter helpers in certificate-authority-fns+ * so `@app/lib/ssh` can read it without importing that module and closing an import cycle.+ */+export const CERT_CLOCK_SKEW_MS = 5 * 60 * 1000;+export const ALGORITHM_FAMILIES = {ECDSA: {signature: ["SHA256-ECDSA", "SHA384-ECDSA", "SHA512-ECDSA"],