DEV Community

Ship Stack
Ship Stack

Posted on Originally published at youtu.be

Myth: If a JWT Decodes, It Is Safe

A JWT is three base64 parts, and decoding it is trivial. That convenience creates a dangerous shortcut: if the token decodes, treat it as valid. I tested that belief with a real run, and it failed twice. Full run in this Short: https://youtu.be/1aLXVqGiOZk

What decode-only accepted

I built a decode-only check in Node and fed it 6 tokens. It accepted an expired token and a token signed with the wrong key. Decoding only reads the payload; it never asks who signed it or whether it is still valid. To a decode-only check, both of those tokens look perfectly fine.

What verify does instead

Verification is ordered. First the signature: recompute it from the header and payload with your key and compare. A token signed with the wrong key fails here and nothing else about it matters. Then the time claims: exp (not after) and nbf (not before), checked against your clock. The expired token fails here. Only after both pass is the payload yours to trust.

The test used HS256 with Node's crypto module, a fixed demo clock, and 6 tokens. Verify rejected exactly the tokens decode accepted.

The rule

Decode is for reading; verify is for trusting. Never branch on a payload you have not verified. Not tested here: RS256, key rotation, clock skew, and revocation, all of which matter in production. Use a maintained JWT library.

Watch the run

The real terminal output is in the Short: https://youtu.be/1aLXVqGiOZk (narration is AI-generated; the test output is from a real run). The full build, a 17-line demo file, is here: https://youtu.be/OGD-QY_qzy0

Subscribe to Ship Stack AI on YouTube and tell me in the comments what to build and break next.

Top comments (0)