An AI Agent Wrote JWT Checks in Node. Decode-Only Accepted All 6 Fake Tokens
I asked an AI agent to write JWT expiry checks in Node.js and I ran them for real: HS256 tokens, Node's built-in crypto, a fixed demo clock. The full run is on video: https://youtu.be/OGD-QY_qzy0 The decode-onl
I asked an AI agent to write JWT expiry checks in Node.js and I ran them for real: HS256 tokens, Node's built-in crypto, a fixed demo clock. The full run is on video: https://youtu.be/OGD-QY_qzy0
The decode-only version
The tempting shortcut is to decode the payload and trust what it says:
const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64url').toString());
if (payload.exp < now) throw new Error('expired');
This reads the claims without ever checking the signature. I built 6 fake tokens to probe it: an expired token, a not-yet-valid token, a token signed with the wrong key, and a token whose payload I tampered with to claim admin.
The decode-only check accepted all 6 of 6.
That includes the expired one, but far worse: the wrong-key token and the tampered admin token also sailed through, because nothing ever proved the payload came from someone holding the key. exp is data, and unauthenticated data is whatever the attacker wants it to be.
The verify version
The agent then produced a verify function (the whole demo file is 17 lines). The order matters:
- Verify the signature first, with a constant-time comparison.
- Only then trust the claims: check expiry and not-before.
I ran the same 6 fake tokens against it. It rejected 5 of 6, each with the correct reason. The sixth case is the interesting one: a token that expires exactly now. The verify function counts it as expired, which is the correct reading of the spec (exp is the time at or after which the token must not be accepted).
What I did not test
RS256 and other algorithms, key rotation, clock skew leeway, and token revocation are all still open. In production, use a maintained library like jsonwebtoken or jose rather than hand-rolled crypto. But the lesson of the run stands: signature first, claims second, and never trust a decoded payload you have not verified.
Watch the run
The code, the terminal output, and every accept/reject are in the video: 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.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.