-
Notifications
You must be signed in to change notification settings - Fork 474
fix(backend)!: require matching OAuth token audiences #9724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ecfb738
fix(backend)!: require matching OAuth token audiences
thiskevinwang e1773cc
fix(backend): unify OAuth audience verification
thiskevinwang 6520cef
fix(backend): expose OAuth audience verification options
thiskevinwang e171727
fix(backend): clarify OAuth audience mismatch
thiskevinwang 838e1b8
clean up
thiskevinwang 2b28f05
soften types
thiskevinwang 7c5c279
pr feedback:
thiskevinwang fcfd5d6
JSdoc comments
thiskevinwang bc26815
fix tests
thiskevinwang 7401092
changeset
thiskevinwang 6d522c6
bump changeset down to minor
thiskevinwang f26735e
update changeset
thiskevinwang d295eb8
update changeset
thiskevinwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@clerk/backend': minor | ||
| --- | ||
|
|
||
| - Fixes an issue where OAuth token validation did not correctly validate audience (`aud`) claims. Previous usages that specified `audience` were falsely passing. This upgrade will cause those to start rejecting if the `audience` indeed does not match the OAuth token's `aud` claim, including cases where the `aud` claim is omitted. | ||
| - Adds an optional `audience` parameter to `idPOAuthAccessToken.verify()` | ||
65 changes: 65 additions & 0 deletions
65
packages/backend/src/api/__tests__/IdPOAuthAccessTokenApi.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { http, HttpResponse } from 'msw'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { server, validateHeaders } from '../../mock-server'; | ||
| import { createBackendApiClient } from '../factory'; | ||
|
|
||
| describe('IdPOAuthAccessToken', () => { | ||
| const accessToken = 'oat_xxxxx'; | ||
| const audience = 'https://resource.example.com'; | ||
| const tokenResponse = { | ||
| object: 'clerk_idp_oauth_access_token', | ||
| id: accessToken, | ||
| client_id: 'client_xxxxx', | ||
| type: 'oauth:access_token', | ||
| subject: 'user_xxxxx', | ||
| scopes: ['read:foo'], | ||
| revoked: false, | ||
| revocation_reason: null, | ||
| expired: false, | ||
| expiration: null, | ||
| created_at: 1753743316590, | ||
| updated_at: 1753743316590, | ||
| }; | ||
|
|
||
| it('verifies an opaque OAuth token with a matching audience', async () => { | ||
| const apiClient = createBackendApiClient({ | ||
| apiUrl: 'https://api.clerk.test', | ||
| secretKey: 'sk_xxxxx', | ||
| }); | ||
|
|
||
| server.use( | ||
| http.post( | ||
| 'https://api.clerk.test/oauth_applications/access_tokens/verify', | ||
| validateHeaders(async ({ request }) => { | ||
| expect(request.headers.get('Authorization')).toBe('Bearer sk_xxxxx'); | ||
| const body = (await request.json()) as Record<string, unknown>; | ||
| expect(body.access_token).toBe(accessToken); | ||
| return HttpResponse.json({ ...tokenResponse, aud: [audience] }); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const response = await apiClient.idPOAuthAccessToken.verify(accessToken, { audience }); | ||
|
|
||
| expect(response.id).toBe(accessToken); | ||
| expect(response.aud).toEqual([audience]); | ||
| }); | ||
|
|
||
| it('rejects an opaque OAuth token with a mismatched audience', async () => { | ||
| const apiClient = createBackendApiClient({ | ||
| apiUrl: 'https://api.clerk.test', | ||
| secretKey: 'sk_xxxxx', | ||
| }); | ||
|
|
||
| server.use( | ||
| http.post('https://api.clerk.test/oauth_applications/access_tokens/verify', () => | ||
| HttpResponse.json({ ...tokenResponse, aud: ['https://other.example.com'] }), | ||
| ), | ||
| ); | ||
|
|
||
| await expect(apiClient.idPOAuthAccessToken.verify(accessToken, { audience })).rejects.toThrow( | ||
| 'OAuth audience mismatch. Verification expected audience ["https://resource.example.com"], but incoming token has aud ["https://other.example.com"].', | ||
| ); | ||
| }); | ||
| }); |
26 changes: 24 additions & 2 deletions
26
packages/backend/src/api/endpoints/IdPOAuthAccessTokenApi.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,37 @@ | ||
| import { assertOAuthAudienceClaim } from '../../jwt/assertions'; | ||
| import { joinPaths } from '../../util/path'; | ||
| import type { IdPOAuthAccessToken } from '../resources'; | ||
| import { AbstractAPI } from './AbstractApi'; | ||
|
|
||
| const basePath = '/oauth_applications/access_tokens'; | ||
|
|
||
| export class IdPOAuthAccessTokenApi extends AbstractAPI { | ||
| async verify(accessToken: string) { | ||
| return this.request<IdPOAuthAccessToken>({ | ||
| /** | ||
| * Verifies an OAuth access token with the Clerk Backend API. If an audience is provided, at least one expected audience must match the token's `aud` claim. Without an audience, the SDK does not check the `aud` claim. | ||
| * | ||
| * @param accessToken - The OAuth access token to verify. | ||
| * @param options - Optional verification options. `audience` can be one expected audience or an array of acceptable audiences. | ||
| * @returns The verified `IdPOAuthAccessToken` resource, including its audience when present. | ||
| * @throws `ClerkAPIResponseError` if the Backend API rejects the token or request. | ||
| * @throws `TokenVerificationError` if an expected audience is provided but the token has no `aud` claim or none of its audiences match. | ||
| * | ||
| * @example | ||
| * ### Verify a token for an API | ||
| * | ||
| * ```ts | ||
| * const token = await clerkClient.idPOAuthAccessToken.verify(accessToken, { | ||
| * audience: 'https://api.example.com', | ||
| * }); | ||
| * ``` | ||
| */ | ||
| async verify(accessToken: string, options: { audience?: string | string[] } = {}): Promise<IdPOAuthAccessToken> { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const verifiedToken = await this.request<IdPOAuthAccessToken>({ | ||
| method: 'POST', | ||
| path: joinPaths(basePath, 'verify'), | ||
| bodyParams: { access_token: accessToken }, | ||
| }); | ||
|
|
||
| assertOAuthAudienceClaim(verifiedToken.aud, options.audience); | ||
| return verifiedToken; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we explicitly mention the upgrade impact here that when
audienceis configured, missing or malformed OAuth tokens are now rejected? Thanks!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wobsoriano added additional context in d295eb8