-
Notifications
You must be signed in to change notification settings - Fork 422
feat(backend): Error if azp is missing on a cookie-based token #7332
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
Open
jescalan
wants to merge
7
commits into
main
Choose a base branch
from
je.error-on-missing-azp-in-cookie
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b7926d1
error if azp is missing on a cookie-based token
jescalan 808c933
Merge branch 'main' into je.error-on-missing-azp-in-cookie
jacekradko 3556290
Merge branch 'main' into je.error-on-missing-azp-in-cookie
jacekradko 5f85333
chore(repo): Add changeset for azp validation in cookie tokens
jacekradko b52ac81
fix(backend): Fix ESLint errors in request_azp test
jacekradko e504313
chore(repo): Bump @clerk/backend changeset to major
jacekradko 854fd49
fix(backend): Remove unnecessary @ts-expect-error directives in reque…
jacekradko 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,5 @@ | ||
| --- | ||
| '@clerk/backend': major | ||
| --- | ||
|
|
||
| Add validation to require `azp` claim in cookie-based session tokens. Tokens from cookies that are missing the `azp` (authorized party) claim will now return a signed-out state with reason `token-missing-azp`. |
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
132 changes: 132 additions & 0 deletions
132
packages/backend/src/tokens/__tests__/request_azp.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,132 @@ | ||
| import { describe, expect, test, vi } from 'vitest'; | ||
|
|
||
| import { TokenVerificationErrorReason } from '../../errors'; | ||
| import { decodeJwt } from '../../jwt/verifyJwt'; | ||
| import { authenticateRequest } from '../request'; | ||
| import { verifyToken } from '../verify'; | ||
|
|
||
| vi.mock('../verify', () => ({ | ||
| verifyToken: vi.fn(), | ||
| verifyMachineAuthToken: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('../../jwt/verifyJwt', () => ({ | ||
| decodeJwt: vi.fn(), | ||
| })); | ||
|
|
||
| describe('authenticateRequest with cookie token', () => { | ||
| test('throws TokenMissingAzp when azp claim is missing', async () => { | ||
| const payload = { | ||
| sub: 'user_123', | ||
| sid: 'sess_123', | ||
| iat: 1234567891, | ||
| exp: 1234567991, | ||
| // azp is missing | ||
| }; | ||
|
|
||
| // Mock verifyToken to return a payload without azp | ||
| vi.mocked(verifyToken).mockResolvedValue({ | ||
| data: payload as any, | ||
| errors: undefined, | ||
| }); | ||
|
|
||
| // Mock decodeJwt to return the same payload | ||
| vi.mocked(decodeJwt).mockReturnValue({ | ||
| data: { payload } as any, | ||
| errors: undefined, | ||
| }); | ||
|
|
||
| const request = new Request('http://localhost:3000', { | ||
| headers: { | ||
| cookie: '__session=mock_token; __client_uat=1234567890', | ||
| }, | ||
| }); | ||
|
|
||
| const options = { | ||
| publishableKey: 'pk_live_Y2xlcmsuaW5zcGlyZWQucHVtYS03NC5sY2wuZGV2JA', | ||
| secretKey: 'sk_live_deadbeef', | ||
| }; | ||
jacekradko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const result = await authenticateRequest(request, options); | ||
|
|
||
| expect(result.status).toBe('signed-out'); | ||
| expect(result.reason).toBe(TokenVerificationErrorReason.TokenMissingAzp); | ||
| expect(result.message).toBe( | ||
| 'Session tokens from cookies must have an azp claim. (reason=token-missing-azp, token-carrier=cookie)', | ||
| ); | ||
| }); | ||
|
|
||
| test('succeeds when azp claim is present', async () => { | ||
| const payload = { | ||
| sub: 'user_123', | ||
| sid: 'sess_123', | ||
| iat: 1234567891, | ||
| exp: 1234567991, | ||
| azp: 'http://localhost:3000', | ||
| }; | ||
|
|
||
| // Mock verifyToken to return a payload with azp | ||
| vi.mocked(verifyToken).mockResolvedValue({ | ||
| data: payload as any, | ||
| errors: undefined, | ||
| }); | ||
|
|
||
| // Mock decodeJwt to return the same payload | ||
| vi.mocked(decodeJwt).mockReturnValue({ | ||
| data: { payload } as any, | ||
| errors: undefined, | ||
| }); | ||
|
|
||
| const request = new Request('http://localhost:3000', { | ||
| headers: { | ||
| cookie: '__session=mock_token; __client_uat=1234567890', | ||
| }, | ||
| }); | ||
|
|
||
| const options = { | ||
| publishableKey: 'pk_live_Y2xlcmsuaW5zcGlyZWQucHVtYS03NC5sY2wuZGV2JA', | ||
| secretKey: 'sk_live_deadbeef', | ||
| }; | ||
|
|
||
| const result = await authenticateRequest(request, options); | ||
| expect(result.isSignedIn).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('authenticateRequest with header token', () => { | ||
| test('succeeds when azp claim is missing', async () => { | ||
| const payload = { | ||
| sub: 'user_123', | ||
| sid: 'sess_123', | ||
| iat: 1234567891, | ||
| exp: 1234567991, | ||
| // azp is missing | ||
| }; | ||
|
|
||
| // Mock verifyToken to return a payload without azp | ||
| vi.mocked(verifyToken).mockResolvedValue({ | ||
| data: payload as any, | ||
| errors: undefined, | ||
| }); | ||
|
|
||
| // Mock decodeJwt to return the same payload | ||
| vi.mocked(decodeJwt).mockReturnValue({ | ||
| data: { payload } as any, | ||
| errors: undefined, | ||
| }); | ||
|
|
||
| const request = new Request('http://localhost:3000', { | ||
| headers: { | ||
| authorization: 'Bearer mock_token', | ||
| }, | ||
| }); | ||
|
|
||
| const options = { | ||
| publishableKey: 'pk_live_Y2xlcmsuaW5zcGlyZWQucHVtYS03NC5sY2wuZGV2JA', | ||
| secretKey: 'sk_live_deadbeef', | ||
| }; | ||
|
|
||
| const result = await authenticateRequest(request, options); | ||
| expect(result.isSignedIn).toBe(true); | ||
| }); | ||
| }); | ||
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.
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.
Blocker: secret-looking test keys (Gitleaks) +
as anycasts may fail CIpk_live_...literal is being detected as an API key; this is likely to break security scanning (and shouldn’t be in tests).as anycan tripno-explicit-any(common in TS repos) and fail lint.Proposed fix
import { describe, expect, test, vi } from 'vitest'; import { TokenVerificationErrorReason } from '../../errors'; import { decodeJwt } from '../../jwt/verifyJwt'; import { authenticateRequest } from '../request'; import { verifyToken } from '../verify'; +type VerifyTokenResult = Awaited<ReturnType<typeof verifyToken>>; +type DecodeJwtResult = ReturnType<typeof decodeJwt>; + +const testKeys = { + // Avoid secret-scanner false-positives while still resembling expected formats. + publishableKey: 'pk_test_' + 'local_testing_key', + secretKey: 'sk_test_' + 'local_testing_key', +} as const; + vi.mock('../verify', () => ({ verifyToken: vi.fn(), verifyMachineAuthToken: vi.fn(), })); vi.mock('../../jwt/verifyJwt', () => ({ decodeJwt: vi.fn(), })); describe('authenticateRequest with cookie token', () => { test('throws TokenMissingAzp when azp claim is missing', async () => { const payload = { sub: 'user_123', sid: 'sess_123', iat: 1234567891, exp: 1234567991, // azp is missing }; // Mock verifyToken to return a payload without azp vi.mocked(verifyToken).mockResolvedValue({ - data: payload as any, + data: payload as unknown as VerifyTokenResult['data'], errors: undefined, }); // Mock decodeJwt to return the same payload vi.mocked(decodeJwt).mockReturnValue({ - data: { payload } as any, + data: { payload } as unknown as DecodeJwtResult extends { data: infer D } ? D : never, errors: undefined, }); const request = new Request('http://localhost:3000', { headers: { cookie: '__session=mock_token; __client_uat=1234567890', }, }); - const options = { - publishableKey: 'pk_live_Y2xlcmsuaW5zcGlyZWQucHVtYS03NC5sY2wuZGV2JA', - secretKey: 'sk_live_deadbeef', - }; + const options = testKeys; const result = await authenticateRequest(request, options); expect(result.status).toBe('signed-out'); expect(result.reason).toBe(TokenVerificationErrorReason.TokenMissingAzp); expect(result.message).toBe( 'Session tokens from cookies must have an azp claim. (reason=token-missing-azp, token-carrier=cookie)', ); }); test('succeeds when azp claim is present', async () => { const payload = { sub: 'user_123', sid: 'sess_123', iat: 1234567891, exp: 1234567991, azp: 'http://localhost:3000', }; // Mock verifyToken to return a payload with azp vi.mocked(verifyToken).mockResolvedValue({ - data: payload as any, + data: payload as unknown as VerifyTokenResult['data'], errors: undefined, }); // Mock decodeJwt to return the same payload vi.mocked(decodeJwt).mockReturnValue({ - data: { payload } as any, + data: { payload } as unknown as DecodeJwtResult extends { data: infer D } ? D : never, errors: undefined, }); const request = new Request('http://localhost:3000', { headers: { cookie: '__session=mock_token; __client_uat=1234567890', }, }); - const options = { - publishableKey: 'pk_live_Y2xlcmsuaW5zcGlyZWQucHVtYS03NC5sY2wuZGV2JA', - secretKey: 'sk_live_deadbeef', - }; + const options = testKeys; const result = await authenticateRequest(request, options); expect(result.isSignedIn).toBe(true); }); }); describe('authenticateRequest with header token', () => { test('succeeds when azp claim is missing', async () => { const payload = { sub: 'user_123', sid: 'sess_123', iat: 1234567891, exp: 1234567991, // azp is missing }; // Mock verifyToken to return a payload without azp vi.mocked(verifyToken).mockResolvedValue({ - data: payload as any, + data: payload as unknown as VerifyTokenResult['data'], errors: undefined, }); // Mock decodeJwt to return the same payload vi.mocked(decodeJwt).mockReturnValue({ - data: { payload } as any, + data: { payload } as unknown as DecodeJwtResult extends { data: infer D } ? D : never, errors: undefined, }); const request = new Request('http://localhost:3000', { headers: { authorization: 'Bearer mock_token', }, }); - const options = { - publishableKey: 'pk_live_Y2xlcmsuaW5zcGlyZWQucHVtYS03NC5sY2wuZGV2JA', - secretKey: 'sk_live_deadbeef', - }; + const options = testKeys; const result = await authenticateRequest(request, options); expect(result.isSignedIn).toBe(true); }); });Also applies to: 45-49, 60-79, 86-90, 98-117, 124-128
🤖 Prompt for AI Agents