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(PLATFOR-358): revoke identity access tokens when client secret is deleted
Tre Dubrava2bc1d207938e
diff --git a/backend/src/db/migrations/20260511202224_add-scope-to-identity-access-token-revocations.ts b/backend/src/db/migrations/20260511202224_add-scope-to-identity-access-token-revocations.tsnew file mode 100644index 0000000000..e6b9019b79--- /dev/null+++ b/backend/src/db/migrations/20260511202224_add-scope-to-identity-access-token-revocations.ts@@ -0,0 +1,37 @@+import { Knex } from "knex";++import { TableName } from "../schemas";++const SCOPE_COLUMN = "scope";+const IDENTITY_EXPIRES_INDEX = "identity_access_token_revocations_identityid_expiresat_index";++export async function up(knex: Knex): Promise<void> {+ const hasScope = await knex.schema.hasColumn(TableName.IdentityAccessTokenRevocation, SCOPE_COLUMN);+ if (!hasScope) {+ await knex.schema.alterTable(TableName.IdentityAccessTokenRevocation, (t) => {+ // Holds the per-marker scope key: clientSecretId UUID for client-secret revokes,+ // IdentityAuthMethod enum string for auth-method revokes. Null for legacy markers+ // (per-token and identity-wide) which key off `id` instead.+ t.string(SCOPE_COLUMN).nullable();+ });+ }++ // Validator changes from `WHERE id IN (?, ?)` to `WHERE identityId = ?`, so this+ // index covers the new hot path.+ await knex.schema.alterTable(TableName.IdentityAccessTokenRevocation, (t) => {+ t.index(["identityId", "expiresAt"], IDENTITY_EXPIRES_INDEX);+ });+}++export async function down(knex: Knex): Promise<void> {+ await knex.schema.alterTable(TableName.IdentityAccessTokenRevocation, (t) => {+ t.dropIndex(["identityId", "expiresAt"], IDENTITY_EXPIRES_INDEX);+ });++ const hasScope = await knex.schema.hasColumn(TableName.IdentityAccessTokenRevocation, SCOPE_COLUMN);+ if (hasScope) {+ await knex.schema.alterTable(TableName.IdentityAccessTokenRevocation, (t) => {+ t.dropColumn(SCOPE_COLUMN);+ });+ }+}diff --git a/backend/src/db/schemas/identity-access-token-revocations.ts b/backend/src/db/schemas/identity-access-token-revocations.tsindex 583f70a418..9b46c81736 100644--- a/backend/src/db/schemas/identity-access-token-revocations.ts+++ b/backend/src/db/schemas/identity-access-token-revocations.ts@@ -13,7 +13,8 @@ export const IdentityAccessTokenRevocationsSchema = z.object({expiresAt: z.date(),revokedAt: z.date().nullable().optional(),createdAt: z.date(),- updatedAt: z.date()+ updatedAt: z.date(),+ scope: z.string().nullable().optional()});export type TIdentityAccessTokenRevocations = z.infer<typeof IdentityAccessTokenRevocationsSchema>;diff --git a/backend/src/services/identity-access-token/identity-access-token-revocation-dal.ts b/backend/src/services/identity-access-token/identity-access-token-revocation-dal.tsindex 8a3e6c62a2..69cc24b1d6 100644--- a/backend/src/services/identity-access-token/identity-access-token-revocation-dal.ts+++ b/backend/src/services/identity-access-token/identity-access-token-revocation-dal.ts@@ -2,18 +2,22 @@ import { TDbClient } from "@app/db";import { TableName, TIdentityAccessTokenRevocations } from "@app/db/schemas";import { DatabaseError } from "@app/lib/errors";-type TRevocationRow = Pick<TIdentityAccessTokenRevocations, "id" | "identityId" | "revokedAt" | "createdAt">;+type TRevocationRow = Pick<TIdentityAccessTokenRevocations, "id" | "identityId" | "revokedAt" | "createdAt" | "scope">;export type TIdentityAccessTokenRevocationDALFactory = ReturnType<typeof identityAccessTokenRevocationDALFactory>;// `id` is set explicitly: JWT jti for per-token revocations, identityId for-// revoke-all sentinels. `revokedAt` is populated only for sentinels so runtime-// validation can compare the JWT iat against the exact revocation time.+// revoke-all sentinels, random UUID for scoped markers. `revokedAt` is populated+// for every marker except per-token revocations so runtime validation can compare+// the JWT iat against the exact revocation time. `scope` is null for legacy+// (per-token / identity-wide) markers and holds the scope key (clientSecretId or+// IdentityAuthMethod string) for scoped markers.type TInsertRevocationInput = {id: string;identityId: string;expiresAt: Date;revokedAt?: Date | null;+ scope?: string | null;};export const identityAccessTokenRevocationDALFactory = (db: TDbClient) => {@@ -26,6 +30,7 @@ export const identityAccessTokenRevocationDALFactory = (db: TDbClient) => {identityId: row.identityId,expiresAt: row.expiresAt,revokedAt: row.revokedAt ?? null,+ scope: row.scope ?? null,updatedAt: db.fn.now()});} catch (error) {@@ -44,11 +49,14 @@ export const identityAccessTokenRevocationDALFactory = (db: TDbClient) => {return ((await db.replicaNode()(TableName.IdentityAccessTokenRevocation)- .select("id", "identityId", "revokedAt", "createdAt")+ .select("id", "identityId", "revokedAt", "createdAt", "scope").where("expiresAt", ">", db.fn.now()).where("identityId", identityId)- // Revoke-all uses identityId as id- .whereIn("id", [tokenId, identityId])) as TRevocationRow[]+ // Legacy markers key off id (tokenId or identityId); scoped markers always+ // come along so the in-memory filter can match by clientSecretId / authMethod.+ .andWhere((qb) => {+ void qb.whereIn("id", [tokenId, identityId]).orWhereNotNull("scope");+ })) as TRevocationRow[]);} catch (error) {throw new DatabaseError({ error, name: "IdentityAccessTokenRevocationFindActiveForToken" });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 5ff4e932f6..87c6e23096 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@@ -39,7 +39,13 @@ const createService = ({trustedIps?: TIp[] | null;membership?: { isActive: boolean } | null;org?: { id: string; rootOrgId?: string; parentOrgId?: string };- activeRevocations?: Array<{ id: string; identityId: string; revokedAt?: Date | null; createdAt: Date }>;+ activeRevocations?: Array<{+ id: string;+ identityId: string;+ revokedAt?: Date | null;+ createdAt: Date;+ scope?: string | null;+ }>;tokenRow?: Record<string, unknown> | null;} = {}) => {const keyStore = {@@ -69,7 +75,14 @@ const createService = ({keyStore: keyStore as never});- return { service, keyStore, identityDAL, orgDAL, identityAccessTokenDAL, identityAccessTokenRevocationDAL };+ return {+ service,+ keyStore,+ identityDAL,+ orgDAL,+ identityAccessTokenDAL,+ identityAccessTokenRevocationDAL+ };};const createTokenClaims = (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 a7a1833934..345727cd38 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@@ -93,11 +93,15 @@ export const identityAccessTokenServiceFactory = ({tokenId,identityId,issuedAtMs,+ clientSecretId,+ authMethod,messagePrefix = "Failed to authorize"}: {tokenId: string;identityId: string;issuedAtMs: number;+ clientSecretId?: string;+ authMethod?: string;messagePrefix?: "Failed to authorize" | "Cannot renew";}) => {const activeRevocations = await identityAccessTokenRevocationDAL.findActiveRevocationsForToken({@@ -106,14 +110,28 @@ export const identityAccessTokenServiceFactory = ({});for (const revocation of activeRevocations) {- if (revocation.id === tokenId) {- throw new UnauthorizedError({ message: `${messagePrefix}: token has been revoked` });- }-- if (revocation.id === identityId) {+ // Legacy markers have scope=null and key off polymorphic `id`.+ if (revocation.scope === null || revocation.scope === undefined) {+ if (revocation.id === tokenId) {+ throw new UnauthorizedError({ message: `${messagePrefix}: token has been revoked` });+ }+ if (revocation.id === identityId) {+ const revokedAtMs = (revocation.revokedAt ?? revocation.createdAt).getTime();+ if (issuedAtMs < revokedAtMs) {+ throw new UnauthorizedError({ message: `${messagePrefix}: identity tokens have been revoked` });+ }+ }+ } else {+ // Scoped marker: scope holds the value the JWT must NOT match+ // (clientSecretId for UA tokens, authMethod for any token).const revokedAtMs = (revocation.revokedAt ?? revocation.createdAt).getTime();if (issuedAtMs < revokedAtMs) {- throw new UnauthorizedError({ message: `${messagePrefix}: identity tokens have been revoked` });+ if (clientSecretId && revocation.scope === clientSecretId) {+ throw new UnauthorizedError({ message: `${messagePrefix}: client secret has been revoked` });+ }+ if (authMethod && revocation.scope === authMethod) {+ throw new UnauthorizedError({ message: `${messagePrefix}: auth method has been revoked` });+ }}}}@@ -281,7 +299,9 @@ export const identityAccessTokenServiceFactory = ({assertTokenIsNotRevoked({tokenId,identityId: token.identityId,- issuedAtMs+ issuedAtMs,+ clientSecretId: source.clientSecretId,+ authMethod: source.authMethod}),keyStore.getItem(KeyStorePrefixes.IdentityTokenUsesRemaining(token.identityId, tokenId))]);@@ -385,6 +405,8 @@ export const identityAccessTokenServiceFactory = ({tokenId,identityId: decodedToken.identityId,issuedAtMs: decodedToken.iat * 1000,+ clientSecretId: source.clientSecretId,+ authMethod: source.authMethod,messagePrefix: "Cannot renew"}),keyStore.getItem(KeyStorePrefixes.IdentityTokenUsesRemaining(decodedToken.identityId, tokenId))@@ -524,11 +546,57 @@ export const identityAccessTokenServiceFactory = ({});};+ // Scoped revoke for Universal Auth client secret deletion: rejects every JWT+ // carrying this clientSecretId with iat < revokedAt. Stored in the polymorphic+ // revocation table with a synthetic id and scope = clientSecretId.+ const revokeAllTokensForClientSecret = async ({+ identityId,+ clientSecretId+ }: {+ identityId: string;+ clientSecretId: string;+ }) => {+ const appCfg = getConfig();+ const revokedAt = new Date();++ await identityAccessTokenRevocationDAL.insertRevocation({+ id: crypto.nativeCrypto.randomUUID(),+ identityId,+ scope: clientSecretId,+ revokedAt,+ expiresAt: new Date(revokedAt.getTime() + appCfg.MAX_MACHINE_IDENTITY_TOKEN_AGE * 1000)+ });+ };++ // Scoped revoke for auth-method removal: rejects every JWT issued via the+ // removed method with iat < revokedAt. Tokens issued via other methods on+ // the same identity stay valid.+ const revokeTokensForIdentityAuthMethod = async ({+ identityId,+ authMethod+ }: {+ identityId: string;+ authMethod: IdentityAuthMethod;+ }) => {+ const appCfg = getConfig();+ const revokedAt = new Date();++ await identityAccessTokenRevocationDAL.insertRevocation({+ id: crypto.nativeCrypto.randomUUID(),+ identityId,+ scope: authMethod,+ revokedAt,+ expiresAt: new Date(revokedAt.getTime() + appCfg.MAX_MACHINE_IDENTITY_TOKEN_AGE * 1000)+ });+ };+return {issueIdentityAccessToken,renewAccessToken,revokeAccessToken,revokeAllTokensForIdentity,+ revokeAllTokensForClientSecret,+ revokeTokensForIdentityAuthMethod,markPerTokenRevocation,fnValidateIdentityAccessTokenFast};diff --git a/backend/src/services/identity-ua/identity-ua-service.ts b/backend/src/services/identity-ua/identity-ua-service.tsindex 6438f85db2..0a793ff573 100644--- a/backend/src/services/identity-ua/identity-ua-service.ts+++ b/backend/src/services/identity-ua/identity-ua-service.ts@@ -59,7 +59,7 @@ type TIdentityUaServiceFactoryDep = {orgDAL: Pick<TOrgDALFactory, "findById" | "findOne" | "findEffectiveOrgMembership">;identityAccessTokenService: Pick<TIdentityAccessTokenServiceFactory,- "issueIdentityAccessToken" | "revokeAllTokensForIdentity"+ "issueIdentityAccessToken" | "revokeAllTokensForIdentity" | "revokeAllTokensForClientSecret">;keyStore: Pick<TKeyStoreFactory,@@ -1169,6 +1169,14 @@ export const identityUaServiceFactory = ({});}}+ // Insert the revocation marker BEFORE flipping isClientSecretRevoked. If+ // the flip fails, tokens are already dead and a retry safely re-flips the+ // bit; flipping first would leave the secret flagged but tokens authentic.+ await identityAccessTokenService.revokeAllTokensForClientSecret({+ identityId,+ clientSecretId+ });+const updatedClientSecret = await identityUaClientSecretDAL.updateById(clientSecretId, {isClientSecretRevoked: true});