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(identity-access-token): cover legacy JWTs when reseeding a lost counter
Scott Wilsonc3874d6e04f5
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 dea6a71af1..c4f3177f64 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@@ -186,28 +186,78 @@ describe("computeUsesRemainingTtl", () => {});describe("computeReseededUsesRemainingTtl", () => {- test("lasts until the token's maxTTL budget runs out, covering any renewed JWT", () => {+ const ENFORCED_AT_SECONDS = Math.floor(ENFORCED_AT.getTime() / 1000);+ const THIRTY_DAYS = 30 * 24 * 60 * 60;++ test("lasts until the maxTTL budget runs out when a renewed JWT can outlive the presented one", () => {expect(- computeReseededUsesRemainingTtl({ accessTokenMaxTTL: 7200, creationEpoch: NOW - 1800, nowSeconds: NOW })+ computeReseededUsesRemainingTtl({+ exp: NOW + 600,+ accessTokenMaxTTL: 7200,+ creationEpoch: NOW - 1800,+ legacyEnforcedAt: ENFORCED_AT,+ nowSeconds: NOW+ })).toBe(5400 + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS);});test("falls back to the ceiling when the token has no maxTTL", () => {- expect(computeReseededUsesRemainingTtl({ accessTokenMaxTTL: 0, creationEpoch: NOW, nowSeconds: NOW })).toBe(- MAX_AGE + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS- );+ expect(+ computeReseededUsesRemainingTtl({+ exp: NOW + 3600,+ accessTokenMaxTTL: 0,+ creationEpoch: NOW,+ legacyEnforcedAt: ENFORCED_AT,+ nowSeconds: NOW+ })+ ).toBe(MAX_AGE + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS);});test("never exceeds the ceiling even with a larger maxTTL budget", () => {expect(- computeReseededUsesRemainingTtl({ accessTokenMaxTTL: MAX_AGE * 2, creationEpoch: NOW, nowSeconds: NOW })+ computeReseededUsesRemainingTtl({+ exp: NOW + 3600,+ accessTokenMaxTTL: MAX_AGE * 2,+ creationEpoch: NOW,+ legacyEnforcedAt: ENFORCED_AT,+ nowSeconds: NOW+ })).toBe(MAX_AGE + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS);});- test("stays positive once the maxTTL budget is spent", () => {- expect(computeReseededUsesRemainingTtl({ accessTokenMaxTTL: 600, creationEpoch: NOW - 900, nowSeconds: NOW })).toBe(- 1 + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS- );+ test("covers the presented JWT once the maxTTL budget is spent", () => {+ expect(+ computeReseededUsesRemainingTtl({+ exp: NOW + 60,+ accessTokenMaxTTL: 600,+ creationEpoch: NOW - 900,+ legacyEnforcedAt: ENFORCED_AT,+ nowSeconds: NOW+ })+ ).toBe(60 + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS);+ });++ test("stays positive when every JWT for the token is past its deadline", () => {+ expect(+ computeReseededUsesRemainingTtl({+ exp: NOW,+ accessTokenMaxTTL: 600,+ creationEpoch: NOW - 900,+ legacyEnforcedAt: ENFORCED_AT,+ nowSeconds: NOW+ })+ ).toBe(1 + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS);+ });++ test("covers the legacy window for a JWT with no exp, even after its maxTTL budget is spent", () => {+ expect(+ computeReseededUsesRemainingTtl({+ accessTokenMaxTTL: 7200,+ creationEpoch: ENFORCED_AT_SECONDS - 365 * 24 * 60 * 60,+ legacyEnforcedAt: ENFORCED_AT,+ nowSeconds: ENFORCED_AT_SECONDS + THIRTY_DAYS+ })+ ).toBe(MAX_AGE - THIRTY_DAYS + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS);});});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 eed598f6a1..2aa9ba4872 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@@ -171,22 +171,30 @@ export const computeUsesRemainingTtl = (jwtTtlSeconds: number): number =>jwtTtlSeconds + IDENTITY_REVOCATION_MARKER_SKEW_SECONDS;// A reseed rebuilds the one counter that every JWT for the token id shares (renewal keeps the id), so-// it has to last until the latest of them could expire, not just the one being presented. No JWT-// outlives creationEpoch + maxTTL, and none outlives now + ceiling since each is issued no later than-// now with at most the ceiling.+// it has to last until the latest of them could still be presented, not just the one in hand:+// - the presented JWT authenticates until its exp, or, for a legacy JWT with no exp, until the end of+// the legacy window (legacyEnforcedAt + ceiling). maxTTL does not bound that legacy window, it only+// bounds renewal.+// - a renewed JWT never outlives creationEpoch + maxTTL when there is one.+// - nothing outlives now + ceiling, since every JWT is issued no later than now with at most the ceiling.export const computeReseededUsesRemainingTtl = ({+ exp,accessTokenMaxTTL,creationEpoch,+ legacyEnforcedAt,nowSeconds = Math.floor(Date.now() / 1000)}: {+ exp?: number;accessTokenMaxTTL: number;creationEpoch: number;+ legacyEnforcedAt: Date;nowSeconds?: number;}): number => {const { MAX_MACHINE_IDENTITY_TOKEN_AGE: ceiling } = getConfig();- const latestJwtExpiry =- accessTokenMaxTTL > 0 ? Math.min(creationEpoch + accessTokenMaxTTL - nowSeconds, ceiling) : ceiling;- return computeUsesRemainingTtl(Math.max(latestJwtExpiry, 1));+ const presentedJwtDeadline = typeof exp === "number" ? exp : Math.floor(legacyEnforcedAt.getTime() / 1000) + ceiling;+ const renewedJwtDeadline = accessTokenMaxTTL > 0 ? creationEpoch + accessTokenMaxTTL : nowSeconds + ceiling;+ const latest = Math.min(Math.max(presentedJwtDeadline, renewedJwtDeadline) - nowSeconds, ceiling);+ return computeUsesRemainingTtl(Math.max(latest, 1));};// Longest lifetime (seconds) a single token can be handed out with thediff --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 ae2ce7e6ca..b002b77fed 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@@ -382,6 +382,27 @@ describe("identityAccessTokenServiceFactory", () => {);});+ test("reseeds a lost counter for a legacy JWT with no exp to last through the legacy window", async () => {+ // Thirty days into the legacy window, with the row's maxTTL budget long spent. The JWT still+ // authenticates, so the counter has to outlive it rather than the spent budget.+ const thirtyDays = 30 * 24 * 60 * 60;+ vi.setSystemTime(new Date(ENFORCED_AT.getTime() + thirtyDays * 1000));+ const { service, keyStore } = createService({+ tokenRow: createLegacyTokenRow({ accessTokenNumUsesLimit: 3 })+ });+ const legacyClaims = verifyAccessTokenJwt(signLegacyUniversalAuthAccessToken({}, { expiresIn: undefined }));+ expect(legacyClaims.exp).toBeUndefined();++ await expect(service.fnValidateIdentityAccessTokenFast(legacyClaims, "10.0.0.1")).resolves.toMatchObject({+ identityId: "identity-id"+ });+ expect(keyStore.setItemWithExpiry).toHaveBeenCalledWith(+ "identity-token-uses-remaining:identity-id:legacy-token-id",+ MAX_AGE - thirtyDays + MARKER_SKEW_SECONDS,+ 2+ );+ });+test("does not reseed a lost counter for a JWT that expired after it was verified", async () => {const { service, keyStore } = createService();keyStore.getItem.mockImplementation(async (key: string) => {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 442193f146..c3053222e0 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@@ -502,8 +502,10 @@ export const identityAccessTokenServiceFactory = ({tokenId,numUsesLimit - 1,computeReseededUsesRemainingTtl({+ exp: token.exp,accessTokenMaxTTL: source.accessTokenMaxTTL,- creationEpoch: source.creationEpoch+ creationEpoch: source.creationEpoch,+ legacyEnforcedAt: appCfg.LEGACY_IDENTITY_ACCESS_TOKEN_EXPIRATION_ENFORCED_AT}));} else {