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: add legacy identity token expiration grace
Tre Dubrava213d78dd72c2
diff --git a/backend/src/services/identity-access-token/identity-access-token-fns.test.ts b/backend/src/services/identity-access-token/identity-access-token-fns.test.tsindex 05d8086b24..22176611d9 100644--- a/backend/src/services/identity-access-token/identity-access-token-fns.test.ts+++ b/backend/src/services/identity-access-token/identity-access-token-fns.test.ts@@ -1,6 +1,10 @@import { afterEach, describe, expect, test, vi } from "vitest";-import { computeIssuedTtl } from "./identity-access-token-fns";+import {+ computeIssuedTtl,+ hasLegacyTokenWithoutExpExceededMaxAge,+ LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT+} from "./identity-access-token-fns";const MAX_AGE = 7_776_000;const NOW = 1_700_000_000;@@ -112,3 +116,33 @@ describe("computeIssuedTtl", () => {});});});++describe("hasLegacyTokenWithoutExpExceededMaxAge", () => {+ test("does not apply to JWTs that already have exp", () => {+ expect(+ hasLegacyTokenWithoutExpExceededMaxAge({+ exp: NOW,+ maxAgeSeconds: MAX_AGE,+ nowMs: LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT.getTime() + MAX_AGE * 1000 + 1+ })+ ).toBe(false);+ });++ test("keeps no-exp legacy JWTs valid until deployment plus max age", () => {+ expect(+ hasLegacyTokenWithoutExpExceededMaxAge({+ maxAgeSeconds: MAX_AGE,+ nowMs: LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT.getTime() + MAX_AGE * 1000+ })+ ).toBe(false);+ });++ test("expires no-exp legacy JWTs after deployment plus max age", () => {+ expect(+ hasLegacyTokenWithoutExpExceededMaxAge({+ maxAgeSeconds: MAX_AGE,+ nowMs: LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT.getTime() + MAX_AGE * 1000 + 1+ })+ ).toBe(true);+ });+});diff --git a/backend/src/services/identity-access-token/identity-access-token-fns.ts b/backend/src/services/identity-access-token/identity-access-token-fns.tsindex c320a307c3..842f6e538d 100644--- a/backend/src/services/identity-access-token/identity-access-token-fns.ts+++ b/backend/src/services/identity-access-token/identity-access-token-fns.ts@@ -15,6 +15,8 @@ import {TSignIdentityAccessTokenOutput} from "./identity-access-token-types";+export const LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT = new Date("2026-05-04T00:00:00.000Z");+export const hasNonWildcardTrustedIps = (trustedIps: TIp[] | null | undefined): boolean => {if (!trustedIps || trustedIps.length === 0) {return false;@@ -87,6 +89,18 @@ export const assertRevocableClaims = assertMinimalRenewClaims as (decoded: TIdentityAccessTokenJwtPayload) => TRevocableClaims;+export const hasLegacyTokenWithoutExpExceededMaxAge = ({+ exp,+ maxAgeSeconds,+ nowMs = Date.now()+}: {+ exp?: number;+ maxAgeSeconds: number;+ nowMs?: number;+}) =>+ typeof exp !== "number" &&+ nowMs > LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT.getTime() + maxAgeSeconds * 1000;+// Compute the TTL (seconds) to sign the JWT with.//// Three caps:diff --git a/backend/src/services/identity-access-token/identity-access-token-service.test.ts b/backend/src/services/identity-access-token/identity-access-token-service.test.tsindex 410d19ffe9..b989ec8391 100644--- a/backend/src/services/identity-access-token/identity-access-token-service.test.ts+++ b/backend/src/services/identity-access-token/identity-access-token-service.test.ts@@ -5,7 +5,11 @@ import { crypto } from "@app/lib/crypto";import { IPType, TIp } from "@app/lib/ip";import { AuthTokenType } from "../auth/auth-type";-import { signIdentityAccessToken, verifyAccessTokenJwt } from "./identity-access-token-fns";+import {+ LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT,+ signIdentityAccessToken,+ verifyAccessTokenJwt+} from "./identity-access-token-fns";import { identityAccessTokenServiceFactory } from "./identity-access-token-service";import { TIdentityAccessTokenJwtPayload } from "./identity-access-token-types";@@ -515,6 +519,33 @@ describe("identityAccessTokenServiceFactory", () => {});});+ test("renews no-exp legacy tokens with old iat during the deployment grace window", async () => {+ const { service } = createService({ tokenRow: createLegacyTokenRow() });+ const legacyToken = signLegacyUniversalAuthAccessToken(+ { iat: NOW_SECONDS - MAX_AGE - 1 },+ { expiresIn: undefined }+ );++ const renewed = await service.renewAccessToken({ accessToken: legacyToken });+ const renewedClaims = verifyAccessTokenJwt(renewed.accessToken);++ expect(renewedClaims).toMatchObject({+ jti: "legacy-token-id",+ identityId: "identity-id",+ authMethod: IdentityAuthMethod.UNIVERSAL_AUTH+ });+ });++ test("rejects no-exp legacy renewal after deployment plus MAX_MACHINE_IDENTITY_TOKEN_AGE", async () => {+ const { service } = createService({ tokenRow: createLegacyTokenRow() });+ const legacyToken = signLegacyUniversalAuthAccessToken({}, { expiresIn: undefined });+ vi.setSystemTime(+ new Date(LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT.getTime() + MAX_AGE * 1000 + 1)+ );++ await expect(service.renewAccessToken({ accessToken: legacyToken })).rejects.toThrow("exceeded max age");+ });+test("rejects legacy renewal when the PG row is missing", async () => {const { service } = createService({ tokenRow: null });const legacyToken = signLegacyUniversalAuthAccessToken();@@ -548,7 +579,7 @@ describe("identityAccessTokenServiceFactory", () => {await expect(service.renewAccessToken({ accessToken: legacyToken })).rejects.toThrow("has reached its max TTL");});- test("rejects legacy JWTs without exp after MAX_MACHINE_IDENTITY_TOKEN_AGE", async () => {+ test("allows legacy JWTs without exp until deployment plus MAX_MACHINE_IDENTITY_TOKEN_AGE", async () => {const { service } = createService();await expect(@@ -556,6 +587,20 @@ describe("identityAccessTokenServiceFactory", () => {createTokenClaims({ exp: undefined, iat: NOW_SECONDS - MAX_AGE - 1 }),"10.0.0.1")+ ).resolves.toMatchObject({ identityId: "identity-id" });+ });++ test("rejects legacy JWTs without exp after deployment plus MAX_MACHINE_IDENTITY_TOKEN_AGE", async () => {+ const { service } = createService();+ vi.setSystemTime(+ new Date(LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT.getTime() + MAX_AGE * 1000 + 1)+ );++ await expect(+ service.fnValidateIdentityAccessTokenFast(+ createTokenClaims({ exp: undefined, iat: NOW_SECONDS }),+ "10.0.0.1"+ )).rejects.toThrow("exceeded max age");});diff --git a/backend/src/services/identity-access-token/identity-access-token-service.ts b/backend/src/services/identity-access-token/identity-access-token-service.tsindex b6aa7a7c77..6b0cb30e8d 100644--- a/backend/src/services/identity-access-token/identity-access-token-service.ts+++ b/backend/src/services/identity-access-token/identity-access-token-service.ts@@ -16,6 +16,7 @@ import {assertRevocableClaims,computeIssuedTtl,hasFullRenewClaims,+ hasLegacyTokenWithoutExpExceededMaxAge,hasNonWildcardTrustedIps,parseUsesRemaining,resolveTtlInputs,@@ -256,11 +257,16 @@ export const identityAccessTokenServiceFactory = ({}: await loadLegacyTokenSource(token);- // Legacy tokens (pre-redesign) were signed without `expiresIn` so the JWT- // has no `exp`. For those, enforce the `iat + MAX_AGE` ceiling here.+ // Legacy tokens (pre-redesign) may have no `exp`. Start the max-age clock+ // at the enforcement deployment date so old tokens get a communication window.// New tokens always have `exp`; jwt.verify already rejected expired ones.const issuedAtMs = token.iat * 1000;- if (typeof token.exp !== "number" && Date.now() > issuedAtMs + appCfg.MAX_MACHINE_IDENTITY_TOKEN_AGE * 1000) {+ if (+ hasLegacyTokenWithoutExpExceededMaxAge({+ exp: token.exp,+ maxAgeSeconds: appCfg.MAX_MACHINE_IDENTITY_TOKEN_AGE+ })+ ) {throw new UnauthorizedError({ message: "Identity access token exceeded max age, please re-authenticate" });}@@ -336,10 +342,14 @@ export const identityAccessTokenServiceFactory = ({const appCfg = getConfig();const decodedToken = assertMinimalRenewClaims(verifyAccessTokenJwt(accessToken));- // Single-JWT max age. New-format validation enforces this on the hot path- // via the same check; mirror it here so legacy JWTs (which were signed- // with arbitrarily long expiresIn) can't renew themselves indefinitely.- if (decodedToken.iat * 1000 + appCfg.MAX_MACHINE_IDENTITY_TOKEN_AGE * 1000 < Date.now()) {+ // Single-JWT max age for legacy tokens without exp. New-format validation+ // uses JWT exp; no-exp legacy tokens get a deployment-anchored grace window.+ if (+ hasLegacyTokenWithoutExpExceededMaxAge({+ exp: decodedToken.exp,+ maxAgeSeconds: appCfg.MAX_MACHINE_IDENTITY_TOKEN_AGE+ })+ ) {throw new UnauthorizedError({ message: "Identity access token exceeded max age, please re-authenticate" });}