|
| 1 | +import { http, HttpResponse } from 'msw'; |
| 2 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { mockJwks, mockRsaJwkKid, signingJwks } from '../../fixtures'; |
| 5 | +import { signJwt } from '../../jwt/signJwt'; |
| 6 | +import { server, validateHeaders } from '../../mock-server'; |
| 7 | +import { verifyHandshakeToken } from '../handshake'; |
| 8 | +import { |
| 9 | + JWT_CATEGORY_IGNORE, |
| 10 | + JWT_CATEGORY_JWT_TEMPLATE, |
| 11 | + JWT_CATEGORY_M2M_TOKEN, |
| 12 | + JWT_CATEGORY_SESSION_TOKEN, |
| 13 | +} from '../jwtCategories'; |
| 14 | + |
| 15 | +const directives = ['__session=foo; Path=/']; |
| 16 | + |
| 17 | +async function createHandshakeJwt(cat: string | undefined, handshake = directives) { |
| 18 | + const { data } = await signJwt({ handshake }, signingJwks, { |
| 19 | + algorithm: 'RS256', |
| 20 | + header: { typ: 'JWT', kid: mockRsaJwkKid, ...(cat !== undefined ? { cat } : {}) }, |
| 21 | + }); |
| 22 | + return data!; |
| 23 | +} |
| 24 | + |
| 25 | +function verify(token: string) { |
| 26 | + return verifyHandshakeToken(token, { |
| 27 | + apiUrl: 'https://api.clerk.test', |
| 28 | + secretKey: 'a-valid-key', |
| 29 | + skipJwksCache: true, |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +describe('tokens.verifyHandshakeToken(token, options)', () => { |
| 34 | + beforeEach(() => { |
| 35 | + server.use( |
| 36 | + http.get( |
| 37 | + 'https://api.clerk.test/v1/jwks', |
| 38 | + validateHeaders(() => HttpResponse.json(mockJwks)), |
| 39 | + ), |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it.each([ |
| 44 | + ['the session-token category, which is what the handshake minter stamps', JWT_CATEGORY_SESSION_TOKEN], |
| 45 | + ['the ignore category, used by instances that opt out of category tagging', JWT_CATEGORY_IGNORE], |
| 46 | + ['no category, for tokens minted before the category rollout', undefined], |
| 47 | + ])('verifies a handshake token with %s', async (_label, cat) => { |
| 48 | + await expect(verify(await createHandshakeJwt(cat))).resolves.toMatchObject({ handshake: directives }); |
| 49 | + }); |
| 50 | + |
| 51 | + // Regression test for AISEC-85. A JWT template is the one customer-authorable producer of a |
| 52 | + // token carrying a top-level `handshake[]` claim, and resolveHandshake emits those entries |
| 53 | + // verbatim as Set-Cookie. |
| 54 | + it('rejects a JWT-template token presented as a handshake token', async () => { |
| 55 | + const token = await createHandshakeJwt(JWT_CATEGORY_JWT_TEMPLATE, ['ATTACKER_INJECTED=pwned; Path=/']); |
| 56 | + |
| 57 | + await expect(verify(token)).rejects.toThrowError('Invalid handshake token category.'); |
| 58 | + }); |
| 59 | + |
| 60 | + it.each([ |
| 61 | + ['m2m', JWT_CATEGORY_M2M_TOKEN], |
| 62 | + ['unknown', 'cl_some_future_unknown_cat'], |
| 63 | + ])('rejects a handshake token with a %s category', async (_label, cat) => { |
| 64 | + await expect(verify(await createHandshakeJwt(cat))).rejects.toThrowError('Invalid handshake token category.'); |
| 65 | + }); |
| 66 | +}); |
0 commit comments