Skip to content

securityhard

JWT: Trust Nothing in the Header

225 points · 0 solved

Many JWT breaches come from trusting the token's own alg header (none, or an RS256→HS256 swap). The first line is T; each next line is a token header.payload.signature (base64url, no padding). For each token print:

  • REJECT malformed if it doesn't have exactly 3 parts, or the header or payload isn't base64url-encoded JSON objects;
  • REJECT alg if alg isn't exactly one of HS256, RS256, ES256 (case-sensitive — none, None, HS512 are rejected);
  • REJECT signature if the signature part is empty;
  • otherwise OK <alg> <sub>, where <sub> is the payload's sub string, or - if missing.

You only pre-screen here — real code must still verify the signature with a key chosen by the server.

2
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhbGljZSJ9.c2ln
eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiJ9.
OK HS256 alice
REJECT alg
Learn the technique/learn/secure-coding-in-practice

main.py