fjordtools / Errors / jwt malformed

JsonWebTokenError: jwt malformed

A JSON Web Token must have exactly three parts separated by dots: header, payload and signature. The library found something else, such as the text undefined, a token with its signature cut off, or a token that still has a Bearer prefix or quotes. Log the exact string you pass in.

Fjordtools team · Seen in Node (jsonwebtoken) · Published · 1 min read

How browsers word it

Node (jsonwebtoken)JsonWebTokenError: jwt malformed
Python (PyJWT)jwt.exceptions.DecodeError: Not enough segments
Java (jjwt)MalformedJwtException: JWT strings must contain exactly 2 period characters

Common causes

  1. The value is the text undefined

    A missing header, cookie or storage key gives undefined, and that is passed on as the token.

    Input

    undefined

    JWT Decoder says

    A token has three parts separated by dots, this has 1.
  2. The signature part was cut off

    A token that was shortened by a log, a form field or a copy and paste has only two parts.

    Input

    eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0

    JWT Decoder says

    A token has three parts separated by dots, this has 2.
  3. The Bearer prefix is still on the token

    The Authorization header holds Bearer followed by a space and the token. Libraries expect only the token. This tool removes the prefix for you, so it decodes, but your server code may not.

    Input

    Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

    JWT Decoder says

    Decodes (the tool strips the Bearer prefix)

How to fix it

  1. Log the exact string you pass to verify, and count the dots: a JWT has two.
  2. Remove the Bearer prefix, quotes and line breaks before you verify the token.
  3. Check that the header, cookie or storage key really exists before you read it.
  4. Paste the token into the JWT decoder to see how many parts it has.

Related errors

More error explainers are coming

See all errors.