I threat-model the JSON Web Token. Not the crypto, which is fine. The string. It’s a bearer credential that looks like line noise, so people treat it like line noise: they paste it into Slack to ask “why is this 401ing”, drop it in a ticket, and feed it to whichever website ranks first for “jwt decoder.” Every convenience is an attack surface with better marketing, and “paste your token here to see what’s inside” is a text box asking for your house keys so it can tell you what colour they are.
SEVERITY: whoever reads the ticket
ATTACK VECTOR: Authorization: Bearer eyJ... pasted into a support thread
EXPLOIT: copy, paste, you are now them until "exp"
You don’t need the website. A JWT is three base64url segments joined by dots: header, payload, signature. The shell can read the first two with cut and base64. The tip comes from an it-journey.dev Software Developer quest report; this is the paranoid half of it: how to read a token without handing it to anyone, and why reading it tells you much less than it seems to.
Everything in the console blocks below ran on my machine: bash 5.3.9, OpenSSL 3.5.5, and a base64 that turned out to be uutils coreutils 0.10.0 rather than GNU (more on why that matters in a minute). I also ran the decode under GNU coreutils 9.7 and 9.1 and busybox in Docker containers (debian:stable-slim, debian:bookworm-slim, alpine:3). The token is one I minted myself with openssl dgst -hmac and the secret correct-horse-battery-staple, so nothing real was harmed.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMDQyIiwibmFtZSI6IkNhc3M_Iiwicm9sZSI6InJlYWRlciIsImV4cCI6MTc2NDU1MDgwMH0.l2irzCTsCWNgmlnxTCppU8L81HXSnGCz0Cpasw3-VCE
The one-liner everyone tries first
Field 1 is the header. Decode it the obvious way and it works, which is how the trap gets set:
$ cut -d. -f1 <<<"$JWT" | base64 -d
{"alg":"HS256","typ":"JWT"}
Same one-liner, field 2:
$ cut -d. -f2 <<<"$JWT" | base64 -d
base64: error: invalid input
The header was lucky. It’s 36 characters, a multiple of 4, with no - or _ in it, so it happens to be valid plain base64 too. The payload isn’t. JWTs use base64url (RFC 7515 points at the URL-safe alphabet from RFC 4648 §5 with the trailing = padding removed). Two differences from the base64 your shell speaks:
- The alphabet swaps
+for-and/for_. Look at the payload above:IkNhc3M_Iiw. That_is a/in disguise, and plain base64 rejects it. - The
=padding is dropped, so the length is whatever it is. This one is 83 characters, which is 3 mod 4.
The part where it broke (four decoders, four opinions)
The brief I worked from said: swap the characters back with tr, then pad to a multiple of 4, or base64 -d will choke. So I tested the swap without the padding first, expecting the choke. On my machine there wasn’t one:
$ cut -d. -f2 <<<"$JWT" | tr '_-' '/+' | base64 -d
{"sub":"1042","name":"Cass?","role":"reader","exp":1764550800}
Clean output, exit 0. Then I fed the same 83 unpadded characters to every decoder I could get my hands on, and they disagreed:
| decoder | output on the unpadded payload | exit |
|---|---|---|
| uutils coreutils 0.10.0 | the whole payload | 0 |
| GNU coreutils 9.7 | the whole payload | 0 |
| GNU coreutils 9.1 | the whole payload, then base64: invalid input |
1 |
busybox (alpine:3) |
…"exp":176455080 then base64: truncated input |
1 |
openssl base64 -d -A |
{"sub":"1042","name":"Cass?","role":"reader","ex |
0 |
Python base64.b64decode |
binascii.Error: Incorrect padding |
1 |
Read the busybox row again: it printed an exp with the last digit missing. Read the OpenSSL row twice: it dropped the tail of the payload and exited 0. A script that pipes that into jq gets a parse error if it’s lucky and a silently wrong claim if it isn’t. And for the record, the naive one-liner on GNU 9.7, before any tr, printed {"sub":"1042","name":"Cass and then the error, so you get half a token and a complaint, glued together on one line.
So the padding isn’t superstition. Some decoders don’t need it, and the ones that do fail in three different ways. Pad it anyway and every one of them agrees.
The function
Source this from your shell rc, or keep it in a file you . when you need it:
# jwt.sh: read and check JWTs without a library. Source it: . ./jwt.sh
# base64url -> bytes: swap the alphabet back, re-pad to a multiple of 4
b64url_decode() {
local s
s=$(tr '_-' '/+' <<<"$1")
while (( ${#s} % 4 )); do s+='='; done
base64 -d <<<"$s"
}
# bytes -> base64url: the reverse, used to rebuild a signature
b64url_encode() { base64 -w0 | tr '+/' '-_' | tr -d '='; }
# Print header and payload. This READS the token. It checks nothing.
jwt_decode() {
local h p _
IFS=. read -r h p _ <<<"$1"
b64url_decode "$h"; echo
b64url_decode "$p"; echo
}
($(…) strips the trailing newline that <<< adds, so ${#s} counts only the token’s own characters.)
$ . ./jwt.sh
$ jwt_decode "$JWT"
{"alg":"HS256","typ":"JWT"}
{"sub":"1042","name":"Cass?","role":"reader","exp":1764550800}
I ran jwt_decode under GNU 9.1 and busybox too, the two that refused the unpadded input. Both printed both lines and exited 0. Pipe either line to jq if you want it pretty. If you’re on a Mac: I didn’t test this on BSD base64, which historically spelled decode -D and doesn’t know -w0, so expect to adjust both.
Now the thriller
You decoded the token. It says "role":"reader". Here’s what I did next, with nothing but the functions above and sed:
$ IFS=. read -r h p s <<<"$JWT"
$ P2=$(b64url_decode "$p" | sed 's/"reader"/"admin"/' | b64url_encode)
$ FORGED="$h.$P2.$s"
$ jwt_decode "$FORGED"
{"alg":"HS256","typ":"JWT"}
{"sub":"1042","name":"Cass?","role":"admin","exp":1764550800}
That decodes perfectly. It’s a promotion with no paperwork. Anyone can write any claim into a payload; the only thing they can’t produce is a signature that matches it without the key. Decoding reads the claims. It doesn’t check the signature, so it can’t tell you who wrote them. The token you’re squinting at in a log could say "role":"admin", "exp" in the year 3000, and "sub" of the CEO, and jwt_decode will print all of it with a straight face, because that is its entire job.
Escalate properly. Somewhere a service “validates” tokens by base64-decoding the middle and reading role. Somewhere an intern, a rogue smart fridge, and a three-letter agency are all, independently, editing one word with sed. The fridge is admin now. The fridge always wanted to be admin.
Walk it back. Real auth libraries verify the signature; the risk is your code. That’s the debug script, the middleware someone “simplified,” the shell glue that reads exp to decide whether to refresh. Decoding is for reading. Anything that decides has to verify first.
Three mitigations, ranked
1. Verify the signature before you believe a single claim. For an HS256 token you need the shared secret and OpenSSL. Recompute the HMAC over header.payload and compare. Append this to jwt.sh:
# Verify an HS256 token against a shared secret. Exit 0 only if the
# header says HS256 and the signature matches.
jwt_verify_hs256() {
local token=$1 secret=$2 h p sig expected
IFS=. read -r h p sig <<<"$token"
if [[ $(b64url_decode "$h") != *'"alg":"HS256"'* ]]; then
echo "REJECT: alg is not HS256: $(b64url_decode "$h")" >&2; return 1
fi
expected=$(printf '%s.%s' "$h" "$p" | openssl dgst -sha256 -hmac "$secret" -binary | b64url_encode)
if [[ $sig != "$expected" ]]; then
echo "REJECT: bad signature" >&2; return 1
fi
echo "OK: signature valid"
}
$ jwt_verify_hs256 "$JWT" correct-horse-battery-staple; echo "exit=$?"
OK: signature valid
exit=0
$ jwt_verify_hs256 "$JWT" hunter2; echo "exit=$?"
REJECT: bad signature
exit=1
$ jwt_verify_hs256 "$FORGED" correct-horse-battery-staple; echo "exit=$?"
REJECT: bad signature
exit=1
The fridge is demoted. For RS256 you verify with the issuer’s public key instead, and openssl dgst does it directly. I signed the same payload under an RS256 header with a throwaway 2048-bit key (openssl genpkey), put the result in $RS, and checked it against pub.pem:
$ IFS=. read -r rh rp rsig <<<"$RS"
$ b64url_decode "$rsig" > sig.bin
$ printf '%s.%s' "$rh" "$rp" | openssl dgst -sha256 -verify pub.pem -signature sig.bin; echo "exit=$?"
Verified OK
exit=0
$ printf '%s.%s' "$rh" "$P2" | openssl dgst -sha256 -verify pub.pem -signature sig.bin; echo "exit=$?"
4007EC7E9F720000:error:02000068:rsa routines:ossl_rsa_verify:bad signature:../crypto/rsa/rsa_sign.c:441:
4007EC7E9F720000:error:1C880004:Provider routines:rsa_verify_directly:RSA lib:../providers/implementations/signature/rsa_sig.c:1035:
Verification failure
exit=1
The second check is the forged admin payload under the real signature. Know the limits: this is a tool for checking a token on your own laptop. The [[ != ]] comparison isn’t constant-time and the alg check is a substring match that fails closed, not a JSON parser. A server should use a maintained JWT library and never this function.
2. Pin the algorithm. Never let the token pick it. The header is attacker-controlled too. A token with "alg":"none" and an empty signature is valid by spec (RFC 7518 §3.6 defines “none” as unsecured), and it decodes as nicely as any other:
$ NONE="$(printf '%s' '{"alg":"none","typ":"JWT"}' | b64url_encode).$P2."
$ jwt_decode "$NONE"
{"alg":"none","typ":"JWT"}
{"sub":"1042","name":"Cass?","role":"admin","exp":1764550800}
$ jwt_verify_hs256 "$NONE" correct-horse-battery-staple; echo "exit=$?"
REJECT: alg is not HS256: {"alg":"none","typ":"JWT"}
exit=1
($P2 is the forged admin payload from the thriller.) The verifier decides the algorithm before it looks at the token, and refuses everything else. In a library, that’s the algorithms=[...] allowlist argument. Pass it, every time, even when it’s optional.
3. Decode locally, and check the clock yourself. The function above never touches the network, which is the point: a live token pasted into a web page is a credential that page now has, along with its logs, its analytics, and its browser extensions. Paste a revoked or expired one if you must use a website. And look at exp before you trust anything else about the token. It’s seconds since the epoch, so GNU date reads it:
$ date -u -d @1764550800
Mon Dec 1 01:00:00 UTC 2025
$ date -u
Tue Dec 1 22:44:32 UTC 2026
My sample token expired a year ago, which is exactly the kind of token I’m comfortable printing on a public website. (On a Mac it’s date -u -r 1764550800; I didn’t run that one.) An expired, signature-checked token is history. An unexpired one is a key, so treat it like one.
What a normal person should do
Keep jwt_decode for reading: debugging a 401, checking which aud or scope a token carries, seeing when it dies. Always pad, because your base64 might not need it but your coworker’s does. Never let a decoded claim make a decision; that’s what jwt_verify_hs256 or your auth library is for, with the algorithm pinned. And keep live tokens out of tickets, chat and decoder websites, because whoever holds a bearer token is the bearer.
Same genre, other files: threat-model your dotfiles covers where tokens like this end up living on your laptop, and rotate the secret you already deleted covers the one you committed.