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
feat(identity-access-token): enhance org membership revocation handling
Victor Hugo dos Santosa954142d364b
diff --git a/backend/src/ee/services/group/group-service.ts b/backend/src/ee/services/group/group-service.tsindex 48b9a31c43..fcecefe8ce 100644--- a/backend/src/ee/services/group/group-service.ts+++ b/backend/src/ee/services/group/group-service.ts@@ -17,6 +17,7 @@ import { requestMemoKeys } from "@app/lib/request-context/memo-keys";import { requestMemoize } from "@app/lib/request-context/request-memoizer";import { TGenericPermission } from "@app/lib/types";import { TIdentityDALFactory } from "@app/services/identity/identity-dal";+import { TIdentityAccessTokenServiceFactory } from "@app/services/identity-access-token/identity-access-token-service";import { PamIdentities, SecretIdentities } from "@app/services/license-client";import { TUsageMeteringServiceFactory } from "@app/services/license-client/usage";import { TMembershipDALFactory } from "@app/services/membership/membership-dal";@@ -93,6 +94,7 @@ type TGroupServiceFactoryDep = {licenseService: Pick<TLicenseServiceFactory, "getPlan">;oidcConfigDAL: Pick<TOidcConfigDALFactory, "findOne">;usageMeteringService: Pick<TUsageMeteringServiceFactory, "emit">;+ identityAccessTokenService: Pick<TIdentityAccessTokenServiceFactory, "bumpIdentityRevocationVersion">;};export type TGroupServiceFactory = ReturnType<typeof groupServiceFactory>;@@ -113,7 +115,8 @@ export const groupServiceFactory = ({oidcConfigDAL,membershipGroupDAL,membershipRoleDAL,- usageMeteringService+ usageMeteringService,+ identityAccessTokenService}: TGroupServiceFactoryDep) => {const createGroup = async ({ name, slug, role, actor, actorId, actorAuthMethod, actorOrgId }: TCreateGroupDTO) => {if (!actorOrgId) throw new UnauthorizedError({ message: "No organization ID provided in request" });@@ -1228,6 +1231,9 @@ export const groupServiceFactory = ({identityIds: [identityId]});+ // Losing a group can remove the identity's effective org access.+ await identityAccessTokenService.bumpIdentityRevocationVersion({ identityId });+// The identity may have left a secret-manager or PAM project it only reached through this group.usageMeteringService.emit(actorOrgId, SecretIdentities.key);usageMeteringService.emit(actorOrgId, PamIdentities.key);diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex 0591525ebf..6517500fd7 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -1086,7 +1086,8 @@ export const registerRoutes = async (oidcConfigDAL,membershipGroupDAL,membershipRoleDAL,- usageMeteringService+ usageMeteringService,+ identityAccessTokenService});const groupProjectService = groupProjectServiceFactory({groupDAL,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 385b5013e6..d9e7c6e948 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@@ -1,3 +1,5 @@+import { Knex } from "knex";+import { TDbClient } from "@app/db";import { TableName, TIdentityAccessTokenRevocations } from "@app/db/schemas";import { DatabaseError } from "@app/lib/errors";@@ -27,9 +29,9 @@ const REVOCATION_PRUNE_BATCH_SIZE = 10000;const MAX_RETRY_ON_FAILURE = 3;export const identityAccessTokenRevocationDALFactory = (db: TDbClient) => {- const insertRevocation = async (row: TInsertRevocationInput) => {+ const insertRevocation = async (row: TInsertRevocationInput, tx?: Knex) => {try {- await db(TableName.IdentityAccessTokenRevocation)+ await (tx ?? db)(TableName.IdentityAccessTokenRevocation).insert(row).onConflict(["id"]).merge({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 8dd5b2955e..b56ecc663a 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@@ -811,18 +811,32 @@ describe("identityAccessTokenServiceFactory", () => {expect(orgDAL.findEffectiveOrgMembership).not.toHaveBeenCalled();});- test("revokeTokensForIdentityOrgMembership inserts an org-scoped marker and bumps version", async () => {+ test("insertOrgMembershipRevocationMarker writes an org-scoped marker in the caller tx without bumping", async () => {const { service, identityAccessTokenRevocationDAL, keyStore } = createService();+ const tx = {} as unknown as Parameters<typeof service.insertOrgMembershipRevocationMarker>[0]["tx"];- await service.revokeTokensForIdentityOrgMembership({ identityId: "identity-id", orgId: "org-id" });+ await service.insertOrgMembershipRevocationMarker({ identityId: "identity-id", orgId: "org-id", tx });- expect(identityAccessTokenRevocationDAL.insertRevocation).toHaveBeenCalledWith({- id: expect.any(String) as unknown as string,- identityId: "identity-id",- scope: "org-id",- revokedAt: new Date(NOW_SECONDS * 1000),- expiresAt: new Date((NOW_SECONDS + MAX_AGE) * 1000)- });+ expect(identityAccessTokenRevocationDAL.insertRevocation).toHaveBeenCalledWith(+ {+ id: expect.any(String) as unknown as string,+ identityId: "identity-id",+ scope: "org-id",+ revokedAt: new Date(NOW_SECONDS * 1000),+ expiresAt: new Date((NOW_SECONDS + MAX_AGE) * 1000)+ },+ tx+ );+ // The bump is a separate post-commit step, never triggered by the marker insert.+ expect(keyStore.incrementSeededWithExpiry).not.toHaveBeenCalled();+ });++ test("bumpIdentityRevocationVersion bumps the identity version without writing a marker", async () => {+ const { service, identityAccessTokenRevocationDAL, keyStore } = createService();++ await service.bumpIdentityRevocationVersion({ identityId: "identity-id" });++ expect(identityAccessTokenRevocationDAL.insertRevocation).not.toHaveBeenCalled();expect(keyStore.incrementSeededWithExpiry).toHaveBeenCalledWith("identity-revocation-version:identity-id",expect.any(Number),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 6ef34fb478..dd4be9f43b 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@@ -734,21 +734,39 @@ export const identityAccessTokenServiceFactory = ({await bumpRevocationVersion(identityId);};- // Org-scoped revoke for membership deactivate / remove-from-org: rejects every- // JWT for this identity whose orgId claim matches, with iat < revokedAt.- // Tokens scoped to other orgs (sub-org hierarchy) stay valid. Bumps the global- // version so cached allows are rechecked once; only this org's tokens deny.- const revokeTokensForIdentityOrgMembership = async ({ identityId, orgId }: { identityId: string; orgId: string }) => {+ // Org-scoped revoke marker for membership deactivate / remove-from-org: rejects+ // every JWT for this identity whose orgId claim matches, with iat < revokedAt.+ // Tokens scoped to other orgs (sub-org hierarchy) stay valid. Accepts the caller's+ // tx so the marker commits atomically with the membership mutation, making it the+ // durable source of truth for the deny (see bumpIdentityRevocationVersion).+ const insertOrgMembershipRevocationMarker = async ({+ identityId,+ orgId,+ tx+ }: {+ identityId: string;+ orgId: string;+ tx?: Knex;+ }) => {const appCfg = getConfig();const revokedAt = new Date();- await identityAccessTokenRevocationDAL.insertRevocation({- id: crypto.nativeCrypto.randomUUID(),- identityId,- scope: orgId,- revokedAt,- expiresAt: new Date(revokedAt.getTime() + appCfg.MAX_MACHINE_IDENTITY_TOKEN_AGE * 1000)- });+ await identityAccessTokenRevocationDAL.insertRevocation(+ {+ id: crypto.nativeCrypto.randomUUID(),+ identityId,+ scope: orgId,+ revokedAt,+ expiresAt: new Date(revokedAt.getTime() + appCfg.MAX_MACHINE_IDENTITY_TOKEN_AGE * 1000)+ },+ tx+ );+ };++ // Best-effort cache accelerator: bumps the version so cached allow verdicts are+ // rechecked once. A failed bump only defers enforcement to the verdict TTL, since the committed+ // membership row (and marker) remain the durable deny that the cold path reads.+ const bumpIdentityRevocationVersion = async ({ identityId }: { identityId: string }) => {await bumpRevocationVersion(identityId);};@@ -759,7 +777,8 @@ export const identityAccessTokenServiceFactory = ({revokeAllTokensForIdentity,revokeAllTokensForClientSecret,revokeTokensForIdentityAuthMethod,- revokeTokensForIdentityOrgMembership,+ insertOrgMembershipRevocationMarker,+ bumpIdentityRevocationVersion,markPerTokenRevocation,fnValidateIdentityAccessTokenFast};diff --git a/backend/src/services/membership-identity/membership-identity-service.ts b/backend/src/services/membership-identity/membership-identity-service.tsindex c5d4c8541b..973e799b0e 100644--- a/backend/src/services/membership-identity/membership-identity-service.ts+++ b/backend/src/services/membership-identity/membership-identity-service.ts@@ -52,7 +52,10 @@ type TMembershipIdentityServiceFactoryDep = {projectDAL: Pick<TProjectDALFactory, "findById">;keyStore: Pick<TKeyStoreFactory, "getKeysByPattern" | "getItem">;usageMeteringService: Pick<TUsageMeteringServiceFactory, "emit" | "emitForProject">;- identityAccessTokenService: Pick<TIdentityAccessTokenServiceFactory, "revokeTokensForIdentityOrgMembership">;+ identityAccessTokenService: Pick<+ TIdentityAccessTokenServiceFactory,+ "insertOrgMembershipRevocationMarker" | "bumpIdentityRevocationVersion"+ >;};export type TMembershipIdentityServiceFactory = ReturnType<typeof membershipIdentityServiceFactory>;@@ -281,6 +284,9 @@ export const membershipIdentityServiceFactory = ({const customRolesGroupBySlug = groupBy(customRoles, ({ slug }) => slug);+ const shouldRevokeOrgTokens =+ scopeData.scope === AccessScope.Organization && data.isActive === false && existingMembership.isActive !== false;+const membershipDoc = await membershipIdentityDAL.transaction(async (tx) => {const doc =typeof data.isActive === "undefined"@@ -329,18 +335,20 @@ export const membershipIdentityServiceFactory = ({tx);const insertedRoleDocs = await membershipRoleDAL.insertMany(roleDocs, tx);++ if (shouldRevokeOrgTokens) {+ await identityAccessTokenService.insertOrgMembershipRevocationMarker({+ identityId: dto.selector.identityId,+ orgId: scopeData.orgId,+ tx+ });+ }+return { ...doc, roles: insertedRoleDocs };});- if (- scopeData.scope === AccessScope.Organization &&- data.isActive === false &&- existingMembership.isActive !== false- ) {- await identityAccessTokenService.revokeTokensForIdentityOrgMembership({- identityId: dto.selector.identityId,- orgId: scopeData.orgId- });+ if (shouldRevokeOrgTokens) {+ await identityAccessTokenService.bumpIdentityRevocationVersion({ identityId: dto.selector.identityId });}return { membership: membershipDoc };@@ -394,6 +402,15 @@ export const membershipIdentityServiceFactory = ({}}+ // Durable deny atomic with the org-membership removal.+ if (scopeData.scope === AccessScope.Organization) {+ await identityAccessTokenService.insertOrgMembershipRevocationMarker({+ identityId: dto.selector.identityId,+ orgId: scopeData.orgId,+ tx+ });+ }+return doc;};@@ -402,10 +419,7 @@ export const membershipIdentityServiceFactory = ({: await membershipIdentityDAL.transaction(performDelete);if (scopeData.scope === AccessScope.Organization) {- await identityAccessTokenService.revokeTokensForIdentityOrgMembership({- identityId: dto.selector.identityId,- orgId: scopeData.orgId- });+ await identityAccessTokenService.bumpIdentityRevocationVersion({ identityId: dto.selector.identityId });}// Removing an identity from a project drops a direct member; removing it from the org cascades its