
JSON Web Tokens (JWTs) | HTTP Security Headers | Email address lists
In Part 1, Part 2, and Part 3 we used jc to pick apart IP addresses, certificates, URLs, paths, git logs, calendars, SSH keys, and headerless CSV files. This time we put on the security hat.
I’ve spent many years in security, and a lot of security work is just looking at strings: a token someone pasted into a ticket, the headers a web server sends back, a list of email addresses from a sign-up form. All three are easy to read by eye and annoying to read in a script. Here are three ways jc helps:
- Decoding and verifying JSON Web Tokens (JWTs)
- Auditing HTTP security headers
- Cleaning up email address lists
All examples use jc v1.26.3. Let’s get started!
Decoding JWTs
JWTs are everywhere: API keys, session cookies, CI tokens, cloud identity. A JWT is three base64url chunks joined with dots: a header, a payload of claims, and a signature. The usual trick is cut and base64 -d, but base64url drops the padding, so that often fails:
% cut -d. -f2 token.txt | base64 -d
{"iss":"https://auth.example.com","sub":"deploy-bot","aud":"api.example.com","iat":1795985059,"exp":1795992259,"scope":"deploy:write"}
base64: invalid input
The --jwt parser does the decoding for you and gives you the header, the payload, and the signature in hex:
% cat token.txt | jc --jwt -p
{
"header": {
"alg": "HS256",
"typ": "JWT"
},
"payload": {
"iss": "https://auth.example.com",
"sub": "deploy-bot",
"aud": "api.example.com",
"iat": 1795985059,
"exp": 1795992259,
"scope": "deploy:write"
},
"signature": "fa:08:4e:40:0b:88:3b:73:72:48:fc:d3:1c:99:d6:f5:25:a6:45:55:15:68:56:25:5d:30:fe:71:27:7e:53:8e"
}
Note:
jcdecodes the token. It does not verify the signature. We’ll go over that in the next section.
The iat (issued at) and exp (expires) claims are epoch seconds, so jq‘s todate makes them readable:
% cat token.txt | jc --jwt |
jq '.payload |
{sub, issued: (.iat | todate),
expires: (.exp | todate)}'
{
"sub": "deploy-bot",
"issued": "2026-11-29T20:44:19Z",
"expires": "2026-11-29T22:44:19Z"
}
Now let’s audit a file of service tokens, one per line. With --slurp we get one array. Since exp is a number, we compare it with now directly. No date strings to parse:
% cat tokens.txt | jc --jwt --slurp | jq -r '.[] | .payload |
"\(.sub): " +
(if .exp == null then "NO EXPIRY"
elif .exp < now then "expired \((now - .exp) / 60 | floor) min ago"
else "valid for \((.exp - now) / 60 | floor) min" end)'
deploy-bot: valid for 59 min
ci-runner: expired 60 min ago
backup-job: NO EXPIRY
legacy-sync: valid for 9 min
I’d check a token with no expiry first. You can also check the issuer… here is a token that came from a retired identity provider:
% cat tokens.txt | jc --jwt --slurp |
jq -r '.[] | select(.payload.iss != "https://auth.example.com") |
"\(.payload.sub) \(.payload.iss)"'
legacy-sync https://login.old-idp.net
Verifying JWTs
jc decodes a JWT, but it does not check the signature. A decoded token tells you what it claims, not whether those claims are true. Anyone can change "scope":"read" to "scope":"admin" and base64 it again.
The good news is that the signature jc gives you is exactly what you need to check it with openssl. A JWT signature covers the first two parts of the token, header.payload, which in bash is just ${token%.*} (everything before the last dot).
For HS256 tokens, the signature is an HMAC-SHA256 with a shared secret. Remove the colons from the jc signature and compare it with the HMAC that openssl calculates:
% sig=$(cat token.txt | jc --jwt |
jq -r '.signature | gsub(":"; "")')
% calc=$(printf '%s' "$(cut -d. -f1,2 token.txt)" |
openssl dgst -sha256 -hmac "$JWT_SECRET" -r |
cut -d' ' -f1)
% [ "$sig" = "$calc" ] && echo VALID || echo INVALID
VALID
For RS256 tokens (what most identity providers use), you need the provider’s public key as a PEM file. openssl wants the signature as binary, so we convert the hex from jc with perl (or xxd -r -p if you have it):
% cat rs256-token.txt | jc --jwt |
jq -r '.signature | gsub(":"; "")' |
perl -ne 'chomp; print pack("H*", $_)' > sig.bin
% printf '%s' "$(cut -d. -f1,2 rs256-token.txt)" |
openssl dgst -sha256 \
-verify pub.pem \
-signature sig.bin
Verified OK
A good signature is only half of the job – you also have to check the claims: the token must not be expired or not valid yet, and it must come from the issuer and be meant for the audience you expect. Here is a small script that puts it all together. Note the first check: the script decides which algorithm to use. It never trusts the alg in the header, because an attacker controls the header too (look up “JWT algorithm confusion” and "alg": "none" attacks).
#!/usr/bin/env bash
# Usage: jwt-verify.sh TOKEN (HS256 secret in $JWT_SECRET)
set -o pipefail
token=$1
expect_iss="https://auth.example.com"
expect_aud="api.example.com"
expect_alg="HS256" # the signature check below is HS256 only
jwt=$(printf '%s\n' "$token" | jc --jwt) || { echo "INVALID: cannot decode"; exit 1; }
# 1. We choose the algorithm. Never trust the alg in the header.
alg=$(jq -r '.header.alg' <<< "$jwt")
[ "$alg" = "$expect_alg" ] || { echo "INVALID: unexpected alg $alg"; exit 1; }
# 2. Check the signature over "header.payload".
sig=$(jq -r '.signature | gsub(":"; "")' <<< "$jwt")
calc=$(printf '%s' "${token%.*}" | openssl dgst -sha256 -hmac "$JWT_SECRET" -r | cut -d' ' -f1)
[ "$sig" = "$calc" ] || { echo "INVALID: bad signature"; exit 1; }
# 3. Check the claims.
result=$(jq -r --arg iss "$expect_iss" --arg aud "$expect_aud" '.payload
| if .exp == null then "INVALID: no exp claim"
elif .exp <= now then "INVALID: expired"
elif (.nbf // 0) > now then "INVALID: not valid yet"
elif .iss != $iss then "INVALID: wrong issuer \(.iss)"
elif ([.aud] | flatten | index($aud)) == null then "INVALID: wrong audience"
else "VALID: \(.sub)" end' <<< "$jwt")
echo "$result"
# 4. Exit 0 only for a valid token, 1 for all others.
[[ $result == VALID* ]] && exit 0 || exit 1
The aud claim can be a string or an array, so [.aud] | flatten handles both. Here it is against a set of test tokens, plus a token where I changed the payload, a token signed with a different secret, and an "alg": "none" token:
% while read -r t; do ./jwt-verify.sh "$t"; done < test-tokens.txt VALID: deploy-bot INVALID: expired INVALID: no exp claim INVALID: wrong issuer https://login.old-idp.net INVALID: wrong audience VALID: multi-aud INVALID: not valid yet % ./jwt-verify.sh "$TAMPERED" INVALID: bad signature % JWT_SECRET=wrong ./jwt-verify.sh "$TOKEN" INVALID: bad signature % ./jwt-verify.sh "$ALG_NONE" INVALID: unexpected alg none
The script exits with 0 only for a valid token, so you can use it in an if statement or a CI step.
Warning: this is great for debugging, audits, and learning how JWTs work. Do not use it to protect a production service. Use a maintained library, such as PyJWT for Python. A shell string comparison is not constant-time, leaving the script open to timing attacks. Also, two common cases are harder in a shell: most providers publish their keys as a JWKS document, and
openssl3.0 cannot read that format directly. AndES256signatures must be converted to DER beforeopensslcan check them.
Auditing HTTP Security Headers
A few response headers do a lot of security work: Strict-Transport-Security (HSTS), Content-Security-Policy, X-Content-Type-Options, X-Frame-Options, and Referrer-Policy. The --curl-head parser turns curl --head output into JSON. Header names are lowercased, so you don’t have to worry about X-Frame-Options vs. x-frame-options:
% curl -sI https://legacy.example.com | jc --curl-head -p
[
{
"_type": "response",
"_response_version": "HTTP/1.1",
"_response_status": 200,
<snip>
"server": [
"nginx/1.18.0"
],
"content-type": "text/html",
"x-powered-by": "PHP/7.4.3",
"set-cookie": [
"session=abc123; path=/"
],
"date_epoch_utc": 1795987801
}
]
The output is an array with one object for each response. If you follow redirects with curl -L, you get one object for each hop, so .[-1] is the final response.
Here’s the audit. jq can subtract arrays, so the list of required headers minus the keys of the response is the list of missing headers:
% for site in www.example.com shop.example.com legacy.example.com; do
curl -sI "https://$site" |
jc --curl-head |
jq -c --arg site "$site" '.[-1]|
{
site: $site, status: ._response_status, missing:
(
[
"strict-transport-security", "content-security-policy",
"x-content-type-options", "x-frame-options", "referrer-policy"
] - keys
)
}'
done
{"site":"www.example.com","status":200,"missing":[]}
{"site":"shop.example.com","status":301,"missing":["content-security-policy","x-content-type-options","referrer-policy"]}
{"site":"legacy.example.com","status":200,"missing":["strict-transport-security","content-security-policy","x-content-type-options","x-frame-options","referrer-policy"]}
Having the header is good, but an HSTS max-age of a few minutes doesn’t help much. Let’s pull out the value and flag anything under 180 days (15552000 seconds):
% for site in www.example.com shop.example.com legacy.example.com; do
curl -sI "https://$site" | jc --curl-head |
jq -r --arg site "$site" '.[-1] |
(
[
."strict-transport-security" // "" |
capture("max-age=(?<s>[0-9]+)").s |
tonumber
] | first
) as $age |
if $age == null then "\($site): no HSTS"
elif $age < 15552000 then "\($site): HSTS max-age too short (\($age)s)"
else "\($site): HSTS ok (\($age / 86400 | floor) days)" end'
done
www.example.com: HSTS ok (730 days)
shop.example.com: HSTS max-age too short (300s)
legacy.example.com: no HSTS
The [...] | first trick gives null when there is no match, instead of no output at all.
Finally, let’s find servers that tell the world which software versions they run:
% for site in www.example.com shop.example.com legacy.example.com; do
curl -sI "https://$site" | jc --curl-head |
jq -r --arg site "$site" '.[-1] |
[(.server // [] | .[]), (."x-powered-by" // empty)] |
map(select(test("[0-9]"))) |
select(length > 0) |
"\($site): \(join(", "))"'
done
legacy.example.com: nginx/1.18.0, PHP/7.4.3
If you already have headers saved in a file (from a proxy log or a browser’s dev tools), use the --http-headers parser instead. It gives the same output.
Cleaning Up Email Address Lists
Email addresses look simple until you try to de-duplicate a sign-up list. Alice+newsletter@Example.com and alice@example.com go to the same inbox. The --email-address parser splits an address into its parts, including the + suffix:
% echo 'Alice+newsletter@Example.com' | jc --email-address -p
{
"username": "Alice",
"domain": "Example.com",
"local": "Alice+newsletter",
"local_plus_suffix": "newsletter"
}
Here’s our list of sign-ups:
% cat signups.txt alice@example.com Alice+newsletter@Example.com bob.smith@gmail.com bob.smith+shop@gmail.com carol@corp.example.org dave@gmail.com erin+test1@corp.example.org erin+test2@corp.example.org frank@mailinator.com
With --slurp, each line becomes an object in one array. Let’s count sign-ups per domain:
% cat signups.txt | jc --email-address --slurp |
jq -r 'group_by(.domain |
ascii_downcase)[] |
"\(length) \(.[0].domain |
ascii_downcase)"' |
sort -rn
3 gmail.com
3 corp.example.org
2 example.com
1 mailinator.com
Now let’s find people who signed up more than once. We build a canonical address from the username (the part without the + suffix) and the domain:
% cat signups.txt | jc --email-address --slurp |
jq -r 'map(. + {canonical: ("\(.username)@\(.domain)" |
ascii_downcase)})| group_by(.canonical)[] |
select(length > 1) | "\(.[0].canonical): \(map(.local) |
join(", "))"'
alice@example.com: alice, Alice+newsletter
bob.smith@gmail.com: bob.smith, bob.smith+shop
erin@corp.example.org: erin+test1, erin+test2
Or just print the clean, unique list:
% cat signups.txt | jc --email-address --slurp |
jq -r 'map("\(.username)@\(.domain)" |
ascii_downcase) | unique[]'
alice@example.com
bob.smith@gmail.com
carol@corp.example.org
dave@gmail.com
erin@corp.example.org
frank@mailinator.com
Note: Strictly speaking, the part before the
@can be case-sensitive. In practice almost every mail provider ignores case, but keep that in mind before you merge accounts.
And to flag throwaway addresses, pass in a block list with --argjson:
% cat signups.txt | jc --email-address --slurp |
jq -r --argjson block '["mailinator.com","guerrillamail.com"]' \
'.[] | select(.domain as $d | $block | index($d)) |
"\(.username)@\(.domain)"'
frank@mailinator.com
Wrapping Up
Tokens, headers, and email addresses are all strings that hide a structure. Once jc turns them into JSON, the checks are a few lines of jq, and they are easy to drop into a cron job or a CI pipeline.
All of these parsers are in jc v1.26.3. Upgrade with pip install --upgrade jc, or get a binary or package from the releases page. Use jc -h --<parser-name> to see the full schema for any parser.
That’s it for this post – more to come! Do you have a security use case for jc? Let me know in the comments.