Skip to content
Myr

fix(jwt): validate token format in decode and decodeHeader functions (#4752)

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

* fix(jwt): validate token format in decode and decodeHeader functions * refactor: simplify the code ---------

authored by otoneko. · committed by GitHub · parent 0f49915 · Browse files · 2 files+56 added−5 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: Yusuke Wada <yusuke@kamawada.com>

Diff stats

2 files+56 added−5 removed

  • tests:+45 added−0 removed
  • other:+11 added−5 removed

2 files+56 added−5 removed

modified:src/utils/jwt/jwt.test.ts+45 added
1526 unmodified lines
1527 }1528 expect(err).toBeInstanceOf(JwtAlgorithmRequired)1529 })1530})15311532describe('JWT decode token format validation', () => {1533 it('decode should throw JwtTokenInvalid for token with 2 parts', () => {1534 const malformed = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8ifQ'1535 expect(() => JWT.decode(malformed)).toThrow(JwtTokenInvalid)1536 })15371538 it('decode should throw JwtTokenInvalid for token with 1 part', () => {1539 expect(() => JWT.decode('eyJhbGciOiJIUzI1NiJ9')).toThrow(JwtTokenInvalid)1540 })15411542 it('decode should throw JwtTokenInvalid for token with 4 parts', () => {1543 const fourParts = 'a.b.c.d'1544 expect(() => JWT.decode(fourParts)).toThrow(JwtTokenInvalid)1545 })15461547 it('decode should throw JwtTokenInvalid for empty string', () => {1548 expect(() => JWT.decode('')).toThrow(JwtTokenInvalid)1549 })15501551 it('decodeHeader should throw JwtTokenInvalid for token with 2 parts', () => {1552 const malformed = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8ifQ'1553 expect(() => JWT.decodeHeader(malformed)).toThrow(JwtTokenInvalid)1554 })15551556 it('decodeHeader should throw JwtTokenInvalid for empty string', () => {1557 expect(() => JWT.decodeHeader('')).toThrow(JwtTokenInvalid)1558 })15591560 it('decode should work for valid 3-part token', async () => {1561 const secret = 'a-secret'1562 const tok = await JWT.sign({ message: 'hello' }, secret, AlgorithmTypes.HS256)1563 const decoded = JWT.decode(tok)1564 expect(decoded.header.alg).toBe('HS256')1565 expect(decoded.payload).toEqual({ message: 'hello' })1566 })15671568 it('decodeHeader should work for valid 3-part token', async () => {1569 const secret = 'a-secret'1570 const tok = await JWT.sign({ message: 'hello' }, secret, AlgorithmTypes.HS256)1571 const header = JWT.decodeHeader(tok)1572 expect(header.alg).toBe('HS256')1573 expect(header.typ).toBe('JWT')1574 })1575})
modified:src/utils/jwt/jwt.ts+11 added-5 removed
254 unmodified lines
255 })256}257258export const decode = (token: string): { header: TokenHeader; payload: JWTPayload } => {259 const parts = token.split('.')260 if (parts.length !== 3) {261 throw new JwtTokenInvalid(token)262 }263 try {260 const [h, p] = token.split('.')261 const header = decodeJwtPart(h) as TokenHeader262 const payload = decodeJwtPart(p) as JWTPayload264 const header = decodeJwtPart(parts[0]) as TokenHeader265 const payload = decodeJwtPart(parts[1]) as JWTPayload266 return {267 header,268 payload,269 }
2 unmodified lines
272 }273}274275export const decodeHeader = (token: string): TokenHeader => {276 const parts = token.split('.')277 if (parts.length !== 3) {278 throw new JwtTokenInvalid(token)279 }280 try {274 const [h] = token.split('.')275 return decodeJwtPart(h) as TokenHeader281 return decodeJwtPart(parts[0]) as TokenHeader282 } catch {283 throw new JwtTokenInvalid(token)284 }285}