Skip to content

@clerk/backend: decodeJwt throws a raw SyntaxError for a malformed token #9263

Description

@ollipa

Preliminary Checks

Reproduction

Publishable key

Description

decodeJwt throws a plain SyntaxError if a token has three segments but they're not valid base64url. It should throw a TokenVerificationError like it does for other bad tokens.

curl -i -H 'Authorization: Bearer aaa.bbb.ccc' https://example.com/

Steps to reproduce:

const { decodeJwt } = require('@clerk/backend/jwt')

decodeJwt('aaa.bbb.ccc')

Expected behavior:

TokenVerificationError, which is what you get from decodeJwt('aaaa.bbbb') or decodeJwt('notajwt').

Actual behavior:

Throws SyntaxError: Unexpected end of data.

decodeJwt checks that there are three parts, but the base64url.parse and JSON.parse calls right after that check aren't wrapped in anything. decodeJwt('YWJj.YWJj.YWJj') hits the JSON.parse one and throws SyntaxError: Unexpected token 'a', "abc" is not valid JSON.

Environment

- @clerk/backend 3.13.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageA ticket that needs to be triaged by a team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions