Skip to content

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

withdraw tokens on change

Thiago Araujo da Silvaf1052cbb7221
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 5f59387cff..46e1bfc970 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -231,13 +231,18 @@ Without that, deactivating a member doesn't revoke their access. Any future non-
a user token needs the same check.
**Withdrawing an exchange application's authority has to revoke the tokens it already issued.** Deleting
-the client, rotating its secret, and removing the token-exchange grant all call
-`revokeSessionsByUserAgent`; sessions are tagged with `getOauthClientSessionUserAgent(clientId)`, the only
-handle we have on them. Rotation revokes only on exchange applications: there the secret alone mints
-tokens for any user, whereas in the redirect flow it has to be paired with an authorization code or
-refresh token and blanket revocation would just sign everyone out. The tag is per client, not per grant,
-so a client holding both grants loses its redirect-flow tokens too (the safe direction, and only
-reachable through the API).
+the client and rotating its secret both call `revokeSessionsByUserAgent`, and so does any update that
+narrows the exchange's trust: removing the grant, changing `tokenExchangeAudience`, or switching
+`tokenExchangeIdpSatisfiesMfa` off (`hasWithdrawnTokenExchangeTrust`). These are the same fields
+`hasClientAuthorityChanged` guards, and for the same reason, so a new field belongs in both. An admin
+narrowing any of them is responding to a problem, so it has to reach the tokens already out there rather
+than wait for them to expire. Widening does not revoke: turning the MFA declaration on grants more without
+invalidating anything already issued. Sessions are tagged with
+`getOauthClientSessionUserAgent(clientId)`, the only handle we have on them. Rotation revokes only on
+exchange applications: there the secret alone mints tokens for any user, whereas in the redirect flow it
+has to be paired with an authorization code or refresh token and blanket revocation would just sign
+everyone out. The tag is per client, not per grant, so a client holding both grants loses its
+redirect-flow tokens too (the safe direction, and only reachable through the API).
**That sweep only reaps sessions that already exist, so the exchange rechecks the client after creating
its own.** `exchangeSubjectToken` authenticates the client, then spends provider discovery, JWKS
diff --git a/backend/src/services/oauth-client/oauth-client-fns.test.ts b/backend/src/services/oauth-client/oauth-client-fns.test.ts
index 482d23f2de..6bf0e8ce9f 100644
--- a/backend/src/services/oauth-client/oauth-client-fns.test.ts
+++ b/backend/src/services/oauth-client/oauth-client-fns.test.ts
@@ -4,6 +4,7 @@ import {
assertValidOauthClientGrantConfig,
computePkceChallenge,
hasClientAuthorityChanged,
+ hasWithdrawnTokenExchangeTrust,
isAllowedRedirectUri,
isRegisteredRedirectUri,
parseBasicAuthHeader
@@ -201,6 +202,55 @@ describe("hasClientAuthorityChanged", () => {
});
});
+describe("hasWithdrawnTokenExchangeTrust", () => {
+ const enabled = { isEnabled: true, audience: "api://mcp", idpSatisfiesMfa: true };
+
+ test("reports nothing withdrawn when the configuration is unchanged", () => {
+ expect(hasWithdrawnTokenExchangeTrust(enabled, { ...enabled })).toBe(false);
+ });
+
+ test("reports a withdrawal when the token exchange grant is removed", () => {
+ expect(hasWithdrawnTokenExchangeTrust(enabled, { isEnabled: false, audience: null, idpSatisfiesMfa: false })).toBe(
+ true
+ );
+ });
+
+ // Tokens already issued were accepted against the old audience, which no longer describes what this
+ // application is allowed to exchange.
+ test("reports a withdrawal when the audience is narrowed", () => {
+ expect(hasWithdrawnTokenExchangeTrust(enabled, { ...enabled, audience: "api://mcp-prod" })).toBe(true);
+ });
+
+ test("reports a withdrawal when the audience is cleared", () => {
+ expect(hasWithdrawnTokenExchangeTrust(enabled, { ...enabled, audience: null })).toBe(true);
+ });
+
+ // The declaration is what let an MFA-required user be exchanged at all, so their live tokens go with it.
+ test("reports a withdrawal when the identity provider MFA declaration is turned off", () => {
+ expect(hasWithdrawnTokenExchangeTrust(enabled, { ...enabled, idpSatisfiesMfa: false })).toBe(true);
+ });
+
+ // Widening: nothing already issued was granted on a basis that stopped holding.
+ test("reports nothing withdrawn when the identity provider MFA declaration is turned on", () => {
+ const previous = { ...enabled, idpSatisfiesMfa: false };
+
+ expect(hasWithdrawnTokenExchangeTrust(previous, { ...previous, idpSatisfiesMfa: true })).toBe(false);
+ });
+
+ test("reports nothing withdrawn when the application never held the grant", () => {
+ const previous = { isEnabled: false, audience: null, idpSatisfiesMfa: false };
+
+ expect(hasWithdrawnTokenExchangeTrust(previous, enabled)).toBe(false);
+ });
+
+ // A redirect-flow application carries no audience at all, and updating it must not sign its users out.
+ test("treats an absent audience and a null audience as the same value", () => {
+ const previous = { isEnabled: true, audience: null, idpSatisfiesMfa: false };
+
+ expect(hasWithdrawnTokenExchangeTrust(previous, { isEnabled: true, idpSatisfiesMfa: false })).toBe(false);
+ });
+});
+
describe("assertValidOauthClientGrantConfig", () => {
const redirectFlow = {
grantTypes: [OauthGrantType.AuthorizationCode, OauthGrantType.RefreshToken],
diff --git a/backend/src/services/oauth-client/oauth-client-fns.ts b/backend/src/services/oauth-client/oauth-client-fns.ts
index 0f17132da0..13fff25c0b 100644
--- a/backend/src/services/oauth-client/oauth-client-fns.ts
+++ b/backend/src/services/oauth-client/oauth-client-fns.ts
@@ -104,6 +104,30 @@ export const hasClientAuthorityChanged = (
return false;
};
+type TTokenExchangeTrust = {
+ isEnabled: boolean;
+ audience?: string | null;
+ idpSatisfiesMfa?: boolean | null;
+};
+
+// Whether an update takes back federation trust the client already holds, in which case the tokens
+// issued under the old configuration have to go with it. Guards the same fields as
+// hasClientAuthorityChanged, for the same reason: the audience is what binds the exchange to one set of
+// subject tokens, and the IdP-MFA declaration is what lets an exchange skip an MFA requirement. An admin
+// narrowing either is responding to a problem, so it has to reach the tokens already out there instead
+// of waiting for them to expire. Rotating the secret revokes on the same grounds.
+//
+// Any audience change counts, in either direction, because tokens issued under the old one were accepted
+// on a basis that no longer holds. The MFA declaration only counts when switched off: turning it on
+// widens what is exchangeable without invalidating anything already issued.
+export const hasWithdrawnTokenExchangeTrust = (previous: TTokenExchangeTrust, next: TTokenExchangeTrust) => {
+ if (!previous.isEnabled) return false;
+ if (!next.isEnabled) return true;
+ if ((next.audience ?? null) !== (previous.audience ?? null)) return true;
+
+ return Boolean(previous.idpSatisfiesMfa) && !next.idpSatisfiesMfa;
+};
+
export const isRegisteredRedirectUri = (registeredUris: string[], redirectUri: string) =>
registeredUris.some((uri) => {
if (uri === redirectUri) return true;
diff --git a/backend/src/services/oauth-client/oauth-client-service.ts b/backend/src/services/oauth-client/oauth-client-service.ts
index 65e99d35c6..7cb980cb62 100644
--- a/backend/src/services/oauth-client/oauth-client-service.ts
+++ b/backend/src/services/oauth-client/oauth-client-service.ts
@@ -30,6 +30,7 @@ import {
computePkceChallenge,
getOauthClientSessionUserAgent,
hasClientAuthorityChanged,
+ hasWithdrawnTokenExchangeTrust,
isRegisteredRedirectUri,
PKCE_CODE_VERIFIER_REGEX
} from "./oauth-client-fns";
@@ -359,17 +360,33 @@ export const oauthClientServiceFactory = ({
ssoPermissionAction: "change the token exchange configuration of an OAuth application"
});
+ const nextTokenExchangeAudience = isTokenExchangeEnabled ? tokenExchangeAudience : null;
+ const nextTokenExchangeIdpSatisfiesMfa = isTokenExchangeEnabled ? tokenExchangeIdpSatisfiesMfa : false;
+
const updatedClient = await oauthClientDAL.updateById(client.id, {
name: dto.name,
description: dto.description,
grantTypes: dto.grantTypes ? grantTypes : undefined,
redirectUris: isRedirectBased ? dto.redirectUris : [],
requirePkce: isRedirectBased ? dto.requirePkce : false,
- tokenExchangeAudience: isTokenExchangeEnabled ? tokenExchangeAudience : null,
- tokenExchangeIdpSatisfiesMfa: isTokenExchangeEnabled ? tokenExchangeIdpSatisfiesMfa : false
+ tokenExchangeAudience: nextTokenExchangeAudience,
+ tokenExchangeIdpSatisfiesMfa: nextTokenExchangeIdpSatisfiesMfa
});
- if (wasTokenExchangeEnabled && !isTokenExchangeEnabled) {
+ const isTrustWithdrawn = hasWithdrawnTokenExchangeTrust(
+ {
+ isEnabled: wasTokenExchangeEnabled,
+ audience: client.tokenExchangeAudience,
+ idpSatisfiesMfa: client.tokenExchangeIdpSatisfiesMfa
+ },
+ {
+ isEnabled: isTokenExchangeEnabled,
+ audience: nextTokenExchangeAudience,
+ idpSatisfiesMfa: nextTokenExchangeIdpSatisfiesMfa
+ }
+ );
+
+ if (isTrustWithdrawn) {
await tokenService.revokeSessionsByUserAgent(getOauthClientSessionUserAgent(client.clientId));
}