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 hasClientAuthorityChanged
Thiago Araujo da Silvac07af10b8679
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.mdindex fc215db05d..fa5cbc1338 100644--- a/backend/CLAUDE.md+++ b/backend/CLAUDE.md@@ -241,7 +241,10 @@ its own.** `exchangeSubjectToken` authenticates the client, then spends providerverification and several DB reads before inserting a session — long enough for a revocation to land inbetween and delete nothing. So after the session exists it re-reads the client via`oauthClientDAL.findByIdOnPrimary` and rejects (re-running the sweep) if the secret hash changed, the-grant is gone, or the row is. Two things make that sufficient and are load-bearing: each withdrawal path+grant is gone, the row is, or either of the two fields carrying the exchange's federation trust has been+narrowed — `tokenExchangeAudience` and `tokenExchangeIdpSatisfiesMfa`, which the exchange itself read off+the replica. Anything a future field grants the exchange belongs in `hasClientAuthorityChanged` for the+same reason. Two things make that sufficient and are load-bearing: each withdrawal pathcommits its write to the client *before* deleting the sessions, so a session that survived the deletebelongs to a request whose client row is already stale; and the re-read is on the **primary**, because thereplica that `authenticateClient` reads can still be serving the pre-revocation row. Only the exchangediff --git a/backend/src/services/oauth-client/oauth-client-fns.test.ts b/backend/src/services/oauth-client/oauth-client-fns.test.tsindex cfa4e0aacf..482d23f2de 100644--- a/backend/src/services/oauth-client/oauth-client-fns.test.ts+++ b/backend/src/services/oauth-client/oauth-client-fns.test.ts@@ -126,7 +126,9 @@ describe("isRegisteredRedirectUri", () => {describe("hasClientAuthorityChanged", () => {const authenticated = {clientSecretHash: "$2b$10$oldhasholdhasholdhashol",- grantTypes: [OauthGrantType.TokenExchange]+ grantTypes: [OauthGrantType.TokenExchange],+ tokenExchangeAudience: "https://coder.example.com",+ tokenExchangeIdpSatisfiesMfa: true};test("reports no change when the client is untouched", () => {@@ -166,6 +168,37 @@ describe("hasClientAuthorityChanged", () => {)).toBe(false);});++ test("reports a change when the token exchange audience was narrowed", () => {+ expect(+ hasClientAuthorityChanged(+ authenticated,+ { ...authenticated, tokenExchangeAudience: "https://other.example.com" },+ OauthGrantType.TokenExchange+ )+ ).toBe(true);+ });++ test("reports a change when the IdP MFA declaration was withdrawn", () => {+ expect(+ hasClientAuthorityChanged(+ authenticated,+ { ...authenticated, tokenExchangeIdpSatisfiesMfa: false },+ OauthGrantType.TokenExchange+ )+ ).toBe(true);+ });++ test("ignores the token exchange fields for other grants", () => {+ const redirectClient = { ...authenticated, grantTypes: [OauthGrantType.AuthorizationCode] };+ expect(+ hasClientAuthorityChanged(+ redirectClient,+ { ...redirectClient, tokenExchangeAudience: null, tokenExchangeIdpSatisfiesMfa: false },+ OauthGrantType.AuthorizationCode+ )+ ).toBe(false);+ });});describe("assertValidOauthClientGrantConfig", () => {diff --git a/backend/src/services/oauth-client/oauth-client-fns.ts b/backend/src/services/oauth-client/oauth-client-fns.tsindex 6f3f4d64ee..48d9f7555f 100644--- a/backend/src/services/oauth-client/oauth-client-fns.ts+++ b/backend/src/services/oauth-client/oauth-client-fns.ts@@ -71,14 +71,38 @@ export const isAllowedRedirectUri = (uri: string) => {// so the format must stay in sync between session creation and revocation.export const getOauthClientSessionUserAgent = (clientId: string) => `Infisical OAuth - ${clientId}`;+type TOauthClientAuthority = Pick<+ TOauthClients,+ "clientSecretHash" | "grantTypes" | "tokenExchangeAudience" | "tokenExchangeIdpSatisfiesMfa"+>;+// Whether the client an exchange authenticated against has since lost the authority it was granted:// deleted, secret rotated, or the grant withdrawn. Comparing the stored hashes is enough, because a// rotation writes a fresh bcrypt hash with a new salt, so no second compareHash is needed.+//+// Token exchange also compares the two fields that carry its federation trust. The audience is the+// only thing standing between the caller and any token the issuer signs, and the IdP-MFA declaration+// is what lets an exchange skip an MFA requirement, so an admin narrowing either one has withdrawn+// authority just as much as one rotating the secret. The exchange reads them off a replica, so+// without this they would survive the change for the length of the replication lag.export const hasClientAuthorityChanged = (- authenticated: Pick<TOauthClients, "clientSecretHash" | "grantTypes">,- current: Pick<TOauthClients, "clientSecretHash" | "grantTypes"> | undefined,+ authenticated: TOauthClientAuthority,+ current: TOauthClientAuthority | undefined,grantType: OauthGrantType-) => !current || current.clientSecretHash !== authenticated.clientSecretHash || !current.grantTypes.includes(grantType);+) => {+ if (!current) return true;+ if (current.clientSecretHash !== authenticated.clientSecretHash) return true;+ if (!current.grantTypes.includes(grantType)) return true;++ if (grantType === OauthGrantType.TokenExchange) {+ return (+ current.tokenExchangeAudience !== authenticated.tokenExchangeAudience ||+ Boolean(current.tokenExchangeIdpSatisfiesMfa) !== Boolean(authenticated.tokenExchangeIdpSatisfiesMfa)+ );+ }++ return false;+};export const isRegisteredRedirectUri = (registeredUris: string[], redirectUri: string) =>registeredUris.some((uri) => {