Skip to content

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

misc: add clock skew handling

Sheen Capadngan680b1e9541ef
diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts b/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts
index 45e7f2d9ac..227ffadf05 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts
@@ -1,6 +1,15 @@
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.ts
index d5eb46c931..c211c9b9fb 100644
--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts
@@ -42,6 +42,7 @@ 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";
@@ -129,7 +130,7 @@ export const gatewayV2ServiceFactory = ({
const rootCaCert = await x509.X509CertificateGenerator.createSelfSigned({
name: `O=${orgId},CN=Infisical Gateway Root CA`,
serialNumber: rootCaSerialNumber,
- notBefore: rootCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(rootCaIssuedAt),
notAfter: rootCaExpiration,
signingAlgorithm: alg,
keys: rootCaKeys,
@@ -150,7 +151,7 @@ export const gatewayV2ServiceFactory = ({
serialNumber: serverCaSerialNumber,
subject: `O=${orgId},CN=Infisical Gateway Server CA`,
issuer: rootCaCert.subject,
- notBefore: serverCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(serverCaIssuedAt),
notAfter: serverCaExpiration,
signingKey: rootCaKeys.privateKey,
publicKey: serverCaKeys.publicKey,
@@ -180,7 +181,7 @@ export const gatewayV2ServiceFactory = ({
serialNumber: clientCaSerialNumber,
subject: `O=${orgId},CN=Infisical Gateway Client CA`,
issuer: rootCaCert.subject,
- notBefore: clientCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(clientCaIssuedAt),
notAfter: clientCaExpiration,
signingKey: rootCaKeys.privateKey,
publicKey: clientCaKeys.publicKey,
@@ -425,7 +426,7 @@ export const gatewayV2ServiceFactory = ({
subject: `O=${orgGatewayConfig.orgId},OU=gateway-client,CN=${ActorType.PLATFORM}:${gatewayId}`,
issuer: gatewayClientCaCert.subject,
notAfter: clientCertExpiration,
- notBefore: clientCertIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(clientCertIssuedAt),
signingKey: importedGatewayClientCaPrivateKey,
publicKey: clientKeys.publicKey,
signingAlgorithm: alg,
@@ -586,7 +587,7 @@ export const gatewayV2ServiceFactory = ({
subject: `O=${orgGatewayConfig.orgId},OU=gateway-client,CN=${actorMetadata.type}:${gatewayId}`,
issuer: gatewayClientCaCert.subject,
notAfter: clientCertExpiration,
- notBefore: clientCertIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(clientCertIssuedAt),
signingKey: importedGatewayClientCaPrivateKey,
publicKey: clientKeys.publicKey,
signingAlgorithm: alg,
@@ -692,7 +693,7 @@ export const gatewayV2ServiceFactory = ({
serialNumber: gatewayServerSerialNumber,
subject: `O=${orgId},CN=Gateway`,
issuer: gatewayServerCaCert.subject,
- notBefore: gatewayServerCertIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(gatewayServerCertIssuedAt),
notAfter: gatewayServerCertExpireAt,
signingKey: gatewayServerCaPrivateKey,
publicKey: gatewayServerKeys.publicKey,
diff --git a/backend/src/ee/services/relay/relay-service.ts b/backend/src/ee/services/relay/relay-service.ts
index 7a05911ec3..30bb068944 100644
--- a/backend/src/ee/services/relay/relay-service.ts
+++ b/backend/src/ee/services/relay/relay-service.ts
@@ -28,6 +28,7 @@ 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";
@@ -84,7 +85,7 @@ export const relayServiceFactory = ({
const rootCaCert = await x509.X509CertificateGenerator.createSelfSigned({
name: `O=Infisical,CN=Infisical Instance Root Relay CA`,
serialNumber: rootCaSerialNumber,
- notBefore: rootCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(rootCaIssuedAt),
notAfter: rootCaExpiration,
signingAlgorithm: alg,
keys: rootCaKeys,
@@ -105,7 +106,7 @@ export const relayServiceFactory = ({
serialNumber: orgRelayCaSerialNumber,
subject: `O=Infisical,CN=Infisical Organization Relay CA`,
issuer: rootCaCert.subject,
- notBefore: orgRelayCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(orgRelayCaIssuedAt),
notAfter: orgRelayCaExpiration,
signingKey: rootCaKeys.privateKey,
publicKey: orgRelayCaKeys.publicKey,
@@ -136,7 +137,7 @@ export const relayServiceFactory = ({
serialNumber: instanceRelayCaSerialNumber,
subject: `O=Infisical,CN=Infisical Instance Relay CA`,
issuer: rootCaCert.subject,
- notBefore: instanceRelayCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(instanceRelayCaIssuedAt),
notAfter: instanceRelayCaExpiration,
signingKey: rootCaKeys.privateKey,
publicKey: instanceRelayCaKeys.publicKey,
@@ -167,7 +168,7 @@ export const relayServiceFactory = ({
serialNumber: instanceRelayClientCaSerialNumber,
subject: `O=Infisical,CN=Infisical Instance Relay Client CA`,
issuer: instanceRelayCaCert.subject,
- notBefore: instanceRelayClientCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(instanceRelayClientCaIssuedAt),
notAfter: instanceRelayClientCaExpiration,
signingKey: instanceRelayCaKeys.privateKey,
publicKey: instanceRelayClientCaKeys.publicKey,
@@ -198,7 +199,7 @@ export const relayServiceFactory = ({
serialNumber: instanceRelayServerCaSerialNumber,
subject: `O=Infisical,CN=Infisical Instance Relay Server CA`,
issuer: instanceRelayCaCert.subject,
- notBefore: instanceRelayServerCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(instanceRelayServerCaIssuedAt),
notAfter: instanceRelayServerCaExpiration,
signingKey: instanceRelayCaKeys.privateKey,
publicKey: instanceRelayServerCaKeys.publicKey,
@@ -453,7 +454,7 @@ export const relayServiceFactory = ({
serialNumber: orgRelayClientCaSerialNumber,
subject: `O=${orgId},CN=Infisical Org Relay Client CA`,
issuer: orgRelayCaCert.subject,
- notBefore: orgRelayClientCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(orgRelayClientCaIssuedAt),
notAfter: orgRelayClientCaExpiration,
signingKey: orgRelayCaPrivateKey,
publicKey: orgRelayClientCaKeys.publicKey,
@@ -488,7 +489,7 @@ export const relayServiceFactory = ({
serialNumber: orgRelayServerCaSerialNumber,
subject: `O=${orgId},CN=Infisical Org Relay Server CA`,
issuer: orgRelayCaCert.subject,
- notBefore: orgRelayServerCaIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(orgRelayServerCaIssuedAt),
notAfter: orgRelayServerCaExpiration,
signingKey: orgRelayCaPrivateKey,
publicKey: orgRelayServerCaKeys.publicKey,
@@ -679,7 +680,7 @@ export const relayServiceFactory = ({
serialNumber: relayServerSerialNumber,
subject: `CN=${host},O=${orgId ?? "Infisical"},OU=Relay`,
issuer: relayServerCaCert.subject,
- notBefore: relayServerCertIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(relayServerCertIssuedAt),
notAfter: relayServerCertExpireAt,
signingKey: relayServerCaPrivateKey,
publicKey: relayServerKeys.publicKey,
@@ -698,7 +699,8 @@ export const relayServiceFactory = ({
keyId: "relay-server",
principals: [`${host}:2222`],
certType: SshCertType.HOST,
- requestedTtl: "30d"
+ requestedTtl: "30d",
+ clockSkewSeconds: CERT_CLOCK_SKEW_MS / 1000
});
return {
@@ -794,7 +796,7 @@ export const relayServiceFactory = ({
subject: `O=${orgName}-${orgId},OU=relay-client,CN=${gatewayId}`,
issuer: relayClientCaCert.subject,
notAfter: clientCertExpiration,
- notBefore: clientCertIssuedAt,
+ notBefore: getNotBeforeWithClockSkew(clientCertIssuedAt),
signingKey: importedRelayClientCaPrivateKey,
publicKey: clientKeys.publicKey,
signingAlgorithm: alg,
@@ -852,7 +854,8 @@ export const relayServiceFactory = ({
keyId: `client-${relayName}`,
principals: [gatewayId],
certType: SshCertType.USER,
- requestedTtl: "1d"
+ requestedTtl: "1d",
+ clockSkewSeconds: CERT_CLOCK_SKEW_MS / 1000
});
return {
@@ -870,7 +873,8 @@ export const relayServiceFactory = ({
keyId: `client-${relayName}`,
principals: [gatewayId, gatewayName],
certType: SshCertType.USER,
- requestedTtl: "1d"
+ requestedTtl: "1d",
+ clockSkewSeconds: CERT_CLOCK_SKEW_MS / 1000
});
return {
diff --git a/backend/src/lib/ssh/index.ts b/backend/src/lib/ssh/index.ts
index efc320336b..ffa9d30667 100644
--- a/backend/src/lib/ssh/index.ts
+++ b/backend/src/lib/ssh/index.ts
@@ -33,6 +33,9 @@ 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
+ clockSkewSeconds?: number;
};
/* eslint-disable no-bitwise */
@@ -188,7 +191,8 @@ export const createSshCert = async ({
keyId,
principals,
requestedTtl, // in ms lib format
- certType
+ certType,
+ clockSkewSeconds
}: TCreateSshCertDTO) => {
let ttl: number | undefined;
@@ -225,7 +229,7 @@ export const createSshCert = async ({
"-n",
principals.join(","), // principals
"-V",
- `+${ttl}s`, // validity (TTL in seconds)
+ clockSkewSeconds ? `-${clockSkewSeconds}s:+${ttl}s` : `+${ttl}s`, // validity (TTL in seconds)
"-z",
serialNumber, // serial number
publicKeyFile // public key file to sign