fix(jwt): use `header.alg` as fallback in `verifyFromJwks` (#4144)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent e0f8dd8 · Browse files · 2 files·+44 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+44 added−2 removed
- tests:+43 added−1 removed
- other:+1 added−1 removed
Provenance
- Commit
5facfad- Tree
f26fdb0- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
e0f8dd8
2 files changed+44 added−2 removed
modified:src/utils/jwt/jwt.test.ts+43 added-1 removed
1 /* eslint-disable @typescript-eslint/ban-ts-comment */2 import { vi } from 'vitest'3-import { encodeBase64 } from '../encode'3+import { encodeBase64, encodeBase64Url } from '../encode'4 import { AlgorithmTypes } from './jwa'5+import { signing } from './jws'6 import * as JWT from './jwt'7+import { verifyFromJwks } from './jwt'8 import {9 JwtAlgorithmNotImplemented,10 JwtTokenExpired,11 JwtTokenInvalid,12 JwtTokenIssuedAt,13 JwtTokenNotBefore,14 JwtTokenSignatureMismatched,15 } from './types'16+import { utf8Encoder } from './utf8'17 18 describe('isTokenHeader', () => {19 it('should return true for valid TokenHeader', () => {20 const validTokenHeader: JWT.TokenHeader = {455 unmodified lines476 expect(await JWT.verify(tok, keyPair.publicKey, alg)).toEqual(payload)477 })478 })479 480+describe('verifyFromJwks header.alg fallback', () => {481+ it('Should use header.alg as fallback when matchingKey.alg is missing', async () => {482+ // Setup: Create a JWT signed with HS384 (different from default HS256)483+ const payload = { message: 'hello world' }484+ const headerAlg = 'HS384' // Non-default value485+ const secret = 'secret'486+ const kid = 'dummy'487+488+ // Create JWT (signed with HS384)489+ const header = { alg: headerAlg, typ: 'JWT', kid }490+ const encode = (obj: object) => encodeBase64Url(utf8Encoder.encode(JSON.stringify(obj)).buffer)491+ const encodedHeader = encode(header)492+ const encodedPayload = encode(payload)493+ const signingInput = `${encodedHeader}.${encodedPayload}`494+495+ // Use signing function from jws.ts instead of createHmac directly496+ const signatureBuffer = await signing(secret, headerAlg, utf8Encoder.encode(signingInput))497+ const signature = encodeBase64Url(signatureBuffer)498+499+ const token = `${encodedHeader}.${encodedPayload}.${signature}`500+501+ // Create a key without alg property502+ const keys = [503+ {504+ kty: 'oct',505+ kid,506+ k: encodeBase64Url(utf8Encoder.encode(secret).buffer),507+ use: 'sig',508+ // alg is intentionally omitted509+ },510+ ]511+512+ // Execute: Verify the JWT token signed with HS384513+ const result = await verifyFromJwks(token, { keys })514+515+ // If verification succeeds, it means header.alg was used516+ expect(result).toEqual(payload)517+ })518+})519 async function exportPEMPrivateKey(key: CryptoKey): Promise<string> {520 const exported = await crypto.subtle.exportKey('pkcs8', key)521 const pem = `-----BEGIN PRIVATE KEY-----\n${encodeBase64(exported)}\n-----END PRIVATE KEY-----`522 return pem59 unmodified linesmodified:src/utils/jwt/jwt.ts+1 added-1 removed
151 unmodified linesExpand all15215315415515615615715815916020 unmodified lines151 unmodified linesExpand all if (!matchingKey) { throw new JwtTokenInvalid(token) }
return await verify(token, matchingKey, matchingKey.alg as SignatureAlgorithm) return await verify(token, matchingKey, (matchingKey.alg as SignatureAlgorithm) || header.alg)}
export const decode = (token: string): { header: TokenHeader; payload: JWTPayload } => { try {20 unmodified lines