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 malformedif it doesn't have exactly 3 parts, or the header or payload isn't base64url-encoded JSON objects;REJECT algifalgisn't exactly one ofHS256,RS256,ES256(case-sensitive —none,None,HS512are rejected);REJECT signatureif the signature part is empty;- otherwise
OK <alg> <sub>, where<sub>is the payload'ssubstring, or-if missing.
You only pre-screen here — real code must still verify the signature with a key chosen by the server.
Example 1 input
2 eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhbGljZSJ9.c2ln eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiJ9.
Expected output
OK HS256 alice REJECT alg
main.py
Ctrl+Enter runs · Ctrl+Shift+Enter submits