Skip to content
Myr

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

2 files+44 added−2 removed

modified:src/utils/jwt/jwt.test.ts+43 added-1 removed
1/* eslint-disable @typescript-eslint/ban-ts-comment */2import { vi } from 'vitest'3import { encodeBase64 } from '../encode'3import { encodeBase64, encodeBase64Url } from '../encode'4import { AlgorithmTypes } from './jwa'5import { signing } from './jws'6import * as JWT from './jwt'7import { verifyFromJwks } from './jwt'8import {9 JwtAlgorithmNotImplemented,10 JwtTokenExpired,11 JwtTokenInvalid,12 JwtTokenIssuedAt,13 JwtTokenNotBefore,14 JwtTokenSignatureMismatched,15} from './types'16import { utf8Encoder } from './utf8'1718describe('isTokenHeader', () => {19 it('should return true for valid TokenHeader', () => {20 const validTokenHeader: JWT.TokenHeader = {
455 unmodified lines
476 expect(await JWT.verify(tok, keyPair.publicKey, alg)).toEqual(payload)477 })478})479480describe('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'487488 // 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}`494495 // Use signing function from jws.ts instead of createHmac directly496 const signatureBuffer = await signing(secret, headerAlg, utf8Encoder.encode(signingInput))497 const signature = encodeBase64Url(signatureBuffer)498499 const token = `${encodedHeader}.${encodedPayload}.${signature}`500501 // 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 ]511512 // Execute: Verify the JWT token signed with HS384513 const result = await verifyFromJwks(token, { keys })514515 // If verification succeeds, it means header.alg was used516 expect(result).toEqual(payload)517 })518})519async 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 pem
59 unmodified lines
modified:src/utils/jwt/jwt.ts+1 added-1 removed