DEV Community

Ali Ahmed
Ali Ahmed

Posted on Edited on Originally published at aliahmed.si

Verifying JWTs in Node.js: alg none, algorithm confusion, audience checks and clock skew

A JWT is three base64url parts, header.payload.signature, and decoding it proves nothing: anyone can write a payload. Verification means checking the signature with an algorithm you chose, not the one the token names, then checking the claims: exp (with a small clock skew allowance), iss, and above all aud, so a token issued for one service can't be replayed against another. Below is a deliberately small HS256 verifier using only node:crypto, with a test for each attack. It's here to show what the checks are. In production, use a maintained library such as jose and set the same options explicitly.

JWT verification steps: split header.payload.signature, use your own algorithm and ignore the token's alg, compare the signature in constant time, then check exp, iss and aud. Attacks as tests, each rejected: alg none with the signature dropped throws unexpected alg none; a payload edited to sub admin throws bad signature; a valid token for billing-api throws wrong audience; exp 31 seconds ago with a 30 second skew throws expired. exp 29 seconds ago passes, inside the clock skew allowance.

Each attack in the table below has a failing token in the tests.

What goes wrong?

Mistake What an attacker does
Reading claims without verifying Edits the payload, e.g. "sub": "admin"
Trusting the token's alg header Sends "alg": "none" with no signature, or switches RS256 to HS256 (below)
Comparing signatures with === In principle, learns the signature byte by byte from timing
Skipping the audience check Replays a token issued for one of your services against another
No exp, or exp not required Uses a leaked token forever

Algorithm confusion deserves a sentence. If your service expects RS256 but lets the token pick the algorithm, an attacker can sign a token with HS256 using your public key as the HMAC secret. A verifier that passes "the key" to whatever algorithm the header names will accept it. The defence is the same as for none: the algorithm is configuration, never input.

The verifier

jwt.mjs

import { createHmac, timingSafeEqual } from "node:crypto";

const decodeJson = (part) => JSON.parse(Buffer.from(part, "base64url").toString("utf8"));

/** Verify an HS256 JWT. The algorithm, issuer and audience come from your config, never from the token. */
export function verifyHS256(token, secret, { issuer, audience, now = Date.now(), skewSeconds = 30 }) {
  const parts = token.split(".");
  if (parts.length !== 3) throw new Error("malformed token");
  const [head, body, sig] = parts;

  const header = decodeJson(head);
  if (header.alg !== "HS256") throw new Error(`unexpected alg ${header.alg}`); // rejects "none" and confusion

  // Compare the text, not decoded bytes: Buffer's base64url decoding is lenient, so sig + "==" would also pass.
  const expected = Buffer.from(createHmac("sha256", secret).update(`${head}.${body}`).digest("base64url"));
  const given = Buffer.from(sig);
  if (given.length !== expected.length || !timingSafeEqual(given, expected)) throw new Error("bad signature");

  const claims = decodeJson(body); // only trust the payload after the signature checks out
  const t = Math.floor(now / 1000);
  if (typeof claims.exp !== "number") throw new Error("missing exp");
  if (t > claims.exp + skewSeconds) throw new Error("expired");
  if (typeof claims.nbf === "number" && t + skewSeconds < claims.nbf) throw new Error("not yet valid");
  if (claims.iss !== issuer) throw new Error("wrong issuer");
  const aud = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
  if (!aud.includes(audience)) throw new Error("wrong audience");
  return claims;
}
Enter fullscreen mode Exit fullscreen mode

Notes on the details:

  • timingSafeEqual throws if the lengths differ, so the length check comes first. A truncated signature should be "bad signature", not a crash.
  • Compare the signature as text. My first version decoded sig with Buffer.from(sig, "base64url") and compared bytes. That decoder is lenient: the real signature, the same with == or ! appended, and three other final characters all decoded to the same bytes and passed. A reader on DEV spotted it. It matters once anything keys on the token string, such as a logout denylist: one token would have several spellings. Comparing against the canonical base64url text accepts exactly one.
  • aud can be a string or an array; both are allowed by the JWT standard (RFC 7519).
  • Clock skew: servers' clocks disagree by a few seconds, so allow a little, such as 30 seconds. Minutes of skew mean tokens live longer than you think.
  • The secret for HS256 should be at least 32 random bytes. A guessable secret can be brute-forced offline from any token you've issued.

The attacks, as tests

jwt.test.mjs

import assert from "node:assert/strict";

const secret = "test-secret-at-least-32-bytes-long!!";
const now = Date.UTC(2026, 10, 19, 12);
const t = now / 1000;
const opts = { issuer: "https://auth.example", audience: "kyc-api", now };
const good = { sub: "user-1", iss: "https://auth.example", aud: "kyc-api", exp: t + 300 };

// alg "none": header swapped, signature dropped.
const [, body] = signHS256(good, secret).split(".");
const none = `${Buffer.from(JSON.stringify({ alg: "none" })).toString("base64url")}.${body}.`;
assert.throws(() => verifyHS256(none, secret, opts), /unexpected alg none/);

// Payload edited after signing.
const [h, , s] = signHS256(good, secret).split(".");
const forged = `${h}.${Buffer.from(JSON.stringify({ ...good, sub: "admin" })).toString("base64url")}.${s}`;
assert.throws(() => verifyHS256(forged, secret, opts), /bad signature/);

// A valid token for another service, signed with the same key.
assert.throws(() => verifyHS256(signHS256({ ...good, aud: "billing-api" }, secret), secret, opts), /wrong audience/);

// Expired beyond the skew fails; inside the skew passes.
assert.throws(() => verifyHS256(signHS256({ ...good, exp: t - 31 }, secret), secret, opts), /expired/);
assert.ok(verifyHS256(signHS256({ ...good, exp: t - 29 }, secret), secret, opts));

// Alternative spellings of a valid signature are rejected.
const valid = signHS256(good, secret);
assert.throws(() => verifyHS256(`${valid}==`, secret, opts), /bad signature/);
assert.throws(() => verifyHS256(`${valid}!`, secret, opts), /bad signature/);
Enter fullscreen mode Exit fullscreen mode

signHS256 is the mirror image of the verifier, used only by the tests. All of these pass on Node.js 24, along with tests for a missing exp, a truncated signature and an array aud.

What a library adds

A small verifier is useful for understanding, and dangerous to maintain. jose and similar libraries handle RS256 and ES256 with key sets fetched from an issuer's JWKS endpoint, key rotation by kid, and the encoding edge cases. Using one doesn't remove the decisions, though. Always pass:

  • the allowed algorithms (algorithms: ["RS256"]),
  • the issuer and audience,
  • a maximum clock tolerance.

The audience check is the one I see skipped most, usually when several internal services share one identity provider. It's the same class of problem as an internal endpoint that trusts anything on localhost: something you assumed only one caller could reach. For tokens issued to AI tools, scoping matters even more; see adding OAuth to an MCP server.

Summary

  • Decoding isn't verifying. Verify the signature before reading any claim.
  • The algorithm comes from your config: reject none and anything unexpected.
  • Compare signatures in constant time, after checking the lengths.
  • Require exp, check iss and aud, and allow only seconds of skew.
  • Use a maintained library in production, with those options set explicitly.

Related: securing a voice AI agent's webhook.


Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore ยท Original article

Top comments (2)

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

Buffer.from(sig, "base64url") is lenient, so one signed token has several spellings. I ran your verifier and the original, sig + "==", sig + "!" and 3 other values of the last character all pass. That bites once a logout denylist keys on the token string. Comparing sig to expected.toString("base64url") closes it.

Collapse
 
quantumbreakz profile image
Ali Ahmed •

You're right, and thank you. I reproduced it: the real signature, sig + "==", sig + "!" and three other final characters all passed my verifier. I've changed it to compare against expected.toString("base64url") as text (still with timingSafeEqual), added tests for the alternative spellings, and credited the report in the post. The denylist point is the one that makes it matter.