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 multi audience check
Thiago Araujo da Silva4c1a3b1c3976
diff --git a/backend/src/services/oauth-client/oauth-token-exchange-fns.test.ts b/backend/src/services/oauth-client/oauth-token-exchange-fns.test.tsindex 676ea0fe0d..468e19ecb7 100644--- a/backend/src/services/oauth-client/oauth-token-exchange-fns.test.ts+++ b/backend/src/services/oauth-client/oauth-token-exchange-fns.test.ts@@ -86,6 +86,7 @@ type TSignOptions = {notBefore?: string | number;subject?: string | null;keyId?: string | null;+ authorizedParty?: string;};const signSubjectToken = ({@@ -96,16 +97,21 @@ const signSubjectToken = ({expiresIn = "10m",notBefore,subject = SUBJECT,- keyId = "adfs-key-1"+ keyId = "adfs-key-1",+ authorizedParty}: TSignOptions = {}) =>- jwt.sign({ ...(subject ? { sub: subject } : {}) }, privateKeyPem, {- algorithm,- audience,- issuer,- ...(expiresIn !== null ? { expiresIn } : {}),- ...(notBefore !== undefined ? { notBefore } : {}),- ...(keyId ? { keyid: keyId } : {})- });+ jwt.sign(+ { ...(subject ? { sub: subject } : {}), ...(authorizedParty ? { azp: authorizedParty } : {}) },+ privateKeyPem,+ {+ algorithm,+ audience,+ issuer,+ ...(expiresIn !== null ? { expiresIn } : {}),+ ...(notBefore !== undefined ? { notBefore } : {}),+ ...(keyId ? { keyid: keyId } : {})+ }+ );// jsonwebtoken can't sign EdDSA, so those cases mint their tokens with jose.const signEddsaSubjectToken = async ({@@ -310,9 +316,32 @@ describe("verifySubjectToken", () => {await expect(verify(signSubjectToken())).resolves.toEqual({ subject: SUBJECT, issuer: ISSUER });});- test("accepts a token whose audience list contains the expected audience", async () => {+ test("accepts a multi-audience token whose authorized party is this application", async () => {+ const token = signSubjectToken({ audience: ["api://other-app", AUDIENCE], authorizedParty: AUDIENCE });++ await expect(verify(token)).resolves.toMatchObject({ subject: SUBJECT });+ });++ // Without this, an audience mapper that adds a shared entry to every client's tokens would make any+ // of them exchangeable here.+ test("rejects a multi-audience token issued to a different application", async () => {+ const token = signSubjectToken({+ audience: ["api://expenses-app", AUDIENCE],+ authorizedParty: "api://expenses-app"+ });++ await expect(verify(token)).rejects.toThrow(/was issued to 'api:\/\/expenses-app'/);+ });++ test("rejects a multi-audience token with no authorized party", async () => {const token = signSubjectToken({ audience: ["api://other-app", AUDIENCE] });+ await expect(verify(token)).rejects.toThrow(/addressed to several audiences/);+ });++ test("accepts a single-audience token that names a different authorized party", async () => {+ const token = signSubjectToken({ audience: AUDIENCE, authorizedParty: "some-client-id" });+await expect(verify(token)).resolves.toMatchObject({ subject: SUBJECT });});diff --git a/backend/src/services/oauth-client/oauth-token-exchange-fns.ts b/backend/src/services/oauth-client/oauth-token-exchange-fns.tsindex e5b700f10f..07f4783bb3 100644--- a/backend/src/services/oauth-client/oauth-token-exchange-fns.ts+++ b/backend/src/services/oauth-client/oauth-token-exchange-fns.ts@@ -292,6 +292,23 @@ export const verifySubjectToken = async ({ subjectToken, oidcConfig, expectedAudthrow toSubjectTokenError(error, anchor, expectedAudience) ?? error;}+ const audiences = Array.isArray(claims.aud) ? claims.aud : [claims.aud];+ if (audiences.length > 1) {+ const authorizedParty = typeof claims.azp === "string" ? claims.azp : undefined;++ if (!authorizedParty) {+ throw new UnauthorizedError({+ message: `The subject token is addressed to several audiences (${audiences.join(", ")}) and carries no 'azp' claim, so there is no way to tell it was issued to this application rather than another one. Configure your identity provider to issue tokens addressed only to '${expectedAudience}', or to include an 'azp' claim.`+ });+ }++ if (authorizedParty !== expectedAudience) {+ throw new UnauthorizedError({+ message: `The subject token was issued to '${authorizedParty}', not to this application's configured token exchange audience ('${expectedAudience}'). It is addressed to this application only as an additional audience, which is not enough to act on the user's behalf.`+ });+ }+ }+if (!claims.sub) {throw new UnauthorizedError({message: "The subject token has no 'sub' claim, so it does not identify a user."diff --git a/docs/documentation/platform/oauth-token-exchange.mdx b/docs/documentation/platform/oauth-token-exchange.mdxindex 555a18a840..6f7f319439 100644--- a/docs/documentation/platform/oauth-token-exchange.mdx+++ b/docs/documentation/platform/oauth-token-exchange.mdx@@ -72,6 +72,7 @@ Two things it deliberately cannot do:- **Its tokens carry an expiry (`exp`).** Infisical rejects a token without one, since it would be replayable indefinitely.- **Each user has signed in to Infisical through OIDC SSO at least once.** Infisical matches the token's subject to the account created on that sign-in, so treat one browser sign-in as an onboarding step.- **Your middleware has its own registration in your identity provider**, so its tokens carry an audience distinct from Infisical's.+- **Its tokens are addressed to that registration alone**, or carry an `azp` claim naming it. See [Why the audience matters](#why-the-audience-matters).## Registering your middleware@@ -107,6 +108,8 @@ Your identity provider signs tokens for every application in your estate. WithouSet it to your middleware's own registration in your identity provider, never to Infisical's.+Some providers can be configured to address one token to several audiences at once, which would let a token minted for another application list yours alongside its own. When a subject token carries more than one audience, Infisical requires an `azp` (authorized party) claim naming your configured audience, and rejects the token otherwise. A token addressed to a single audience needs no `azp`.+## Exchanging a tokenToken exchange follows [RFC 8693](https://datatracker.ietf.org/doc/html/rfc8693) on the existing token endpoint, so any RFC 8693-aware client library works. Client credentials may be sent as HTTP Basic auth or in the request body.diff --git a/sso.md b/sso.mdindex 09178ce6b1..2a9472aad3 100644--- a/sso.md+++ b/sso.md@@ -245,6 +245,7 @@ looks up.| Case | How | Expect || --- | --- | --- || Cross-application replay | subject token from `infisical-dev` | 401, audience does not match |+| Shared extra audience | add an audience mapper putting `infisical-mcp` on `infisical-dev`'s tokens, then send one | 401, issued to `infisical-dev`, not this application || Access token instead of ID token | send `access_token` | 401, audience does not match || Tampered signature | change a character in the third JWT segment | 401, could not be verified || `scope` supplied | add `-d scope=secrets:read` | 400, scope not supported |