Skip to content
Myr

fix(jwt): throw JwtTokenInvalid when the signature is not valid base64url (#5379)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jwt): throw JwtTokenInvalid when the signature is not valid base64url verify() decoded the signature part with decodeBase64Url outside any try/catch, so a token whose third part contains characters outside the base64url alphabet rejected with a raw InvalidCharacterError DOMException instead of JwtTokenInvalid, unlike the header and payload parts. Wrap the decode so the typed error is thrown consistently. * test(jwt): assert the error class for invalid base64url signatures Replace the uninitialized-variable and @ts-ignore pattern with a type-safe rejection assertion that checks the JwtTokenInvalid class and its documented message, so the test also fails if verification resolves or rejects with the raw decoding error. Add a control asserting an empty signature still reports JwtTokenSignatureMismatched. ---------

authored by Neil Daquioag · committed by GitHub · parent f950277 · Browse files · 2 files+21 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: ndycode <ndycode@users.noreply.github.com>

Diff stats

2 files+21 added−6 removed

  • tests:+14 added−0 removed
  • other:+7 added−6 removed

2 files+21 added−6 removed

modified:src/utils/jwt/jwt.test.ts+14 added
90 unmodified lines
91 expect(err).toEqual(new JwtTokenInvalid(tok))92 expect(authorized).toBeUndefined()93 })9495 it('JwtTokenInvalid for a signature that is not valid base64url', async () => {96 const tok = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.@@@@'97 const err = await JWT.verify(tok, 'a-secret', AlgorithmTypes.HS256).catch((e: unknown) => e)98 expect(err).toBeInstanceOf(JwtTokenInvalid)99 expect(err).toHaveProperty('message', `invalid JWT token: ${tok}`)100 })101102 it('JwtTokenSignatureMismatched for an empty signature', async () => {103 const tok = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.'104 const err = await JWT.verify(tok, 'a-secret', AlgorithmTypes.HS256).catch((e: unknown) => e)105 expect(err).toBeInstanceOf(JwtTokenSignatureMismatched)106 expect(err).toHaveProperty('message', `token(${tok}) signature mismatched`)107 })108109 it('JwtTokenNotBefore', async () => {110 const tok =111 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NjQ2MDYzMzQsImV4cCI6MTY2NDYwOTkzNCwibmJmIjoiMzEwNDYwNjI2NCJ9.hpSDT_cfkxeiLWEpWVT8TDxFP3dFi27q1K7CcMcLXHc'112 const secret = 'a-secret'
1676 unmodified lines
modified:src/utils/jwt/jwt.ts+7 added-6 removed
172 unmodified lines
173 }174 }175176 const headerPayload = token.substring(0, token.lastIndexOf('.'))177 const verified = await verifying(178 publicKey,179 alg,180 decodeBase64Url(tokenParts[2]),181 utf8Encoder.encode(headerPayload)182 )177 let signature: Uint8Array<ArrayBuffer>178 try {179 signature = decodeBase64Url(tokenParts[2])180 } catch {181 throw new JwtTokenInvalid(token)182 }183 const verified = await verifying(publicKey, alg, signature, utf8Encoder.encode(headerPayload))184 if (!verified) {185 throw new JwtTokenSignatureMismatched(token)186 }187
105 unmodified lines