Skip to content
ecdsa.com
jsonwebtoken (Node.js)JWT libraries

Error message

jwt malformed

What it means

jsonwebtoken's jwt.verify() could not even parse the token: the string does not split into exactly three segments separated by dots. The signature was never checked — the string you passed is not structurally a JWT. (A string that does have three segments but whose header is not base64url JSON fails one step later, with "invalid token".)

Why it happens

How to fix it

  1. 1.

    Strip the scheme prefix before verifying

    Take only the token part of the Authorization header, and guard against a missing header so you fail with a clear 401 instead of "jwt malformed".

    js
    const auth = req.headers.authorization ?? "";
    const [scheme, token] = auth.split(" ");
    if (scheme !== "Bearer" || !token) {
      return res.status(401).json({ error: "missing bearer token" });
    }
    jwt.verify(token, publicKey, { algorithms: ["ES256"] });
  2. 2.

    Log the raw value and count its segments

    One log line tells you whether you are dealing with a prefix, a truncation or the wrong variable. A JWS has exactly 3 segments; a JWE has 5; anything else is not a token.

    js
    console.log(JSON.stringify(token).slice(0, 80));
    console.log("segments:", String(token).split(".").length); // must be 3
  3. 3.

    Check what the token actually contains

    Paste the string into the JWT debugger below: it decodes the header and claims locally and tells you precisely which segment is broken — or that the string is not a JWT at all.

Related errors

Browse the full signature error database