Companion code for the follow-up article. The repository now holds two Maven
projects sharing one docs/ tree:
jwt-authentication/ the hand-written filter application (unchanged, moved)
oauth2-resource-server/ a resource server, a Keycloak compose, and a stub
issuer whose JWK Set can be mutated on command
The stub exists because Keycloak will not rotate a signing key at a chosen
second, report how many times its JWKS endpoint was fetched, or drop a key from
the published set on request - and the caching and rotation measurements need
all three. The Keycloak run confirms the same code path against a real issuer.
Findings captured under docs/output/, all from real runs:
* The default validator stack does not check aud. A token minted for another
service in the same realm is accepted.
* Spring Security builds its JWKSource with refreshAheadCache(false) and
rateLimited(false), overriding two of Nimbus's protective defaults, and
enables Nimbus caching only when NO Spring cache was supplied - so
supplying one removes the five-minute expiry.
* A key retired from the JWK Set stops being accepted at t+300s with the
default cache, and never with a Spring cache that has no TTL.
* 25 tokens carrying an unknown kid produce 25 JWKS fetches at the issuer,
through permitAll() endpoints included.
* A hyphenated client id in an authorities-claim-expression parses as
subtraction; the SpelEvaluationException is swallowed and logged at TRACE.
* A clientScopes key in a Keycloak realm import replaces the built-in scopes
rather than adding to them.
New docs chapters 12-18. README covers both projects. Existing docs and scripts
updated for the new paths; no docs/output/ file from the first article moved, so
links in the published article still resolve.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013f7f2XZXrQ6gW3RtZE187t
57 lines
2.5 KiB
Bash
Executable File
57 lines
2.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# How long a key stays trusted after the issuer removes it from the JWK Set.
|
|
#
|
|
# The only traffic after the retirement is the leaked token itself. That is the point:
|
|
# a token whose kid IS in the cached set never triggers the unknown-kid refresh, so the
|
|
# only thing that can dislodge the stale JWK Set is the cache expiring on its own.
|
|
#
|
|
# Run under both cache configurations and diff the transcripts:
|
|
# ./scripts/run-rs.sh stub,roles && ./scripts/retired-key-demo.sh "stub,roles"
|
|
# ./scripts/run-rs.sh stub,roles,nottlcache && ./scripts/retired-key-demo.sh "stub,roles,nottlcache"
|
|
set -eu
|
|
. "$(dirname "$0")/lib.sh"
|
|
PROFILES="${1:-unknown}"
|
|
PROBES="${2:-16}"
|
|
INTERVAL="${3:-30}"
|
|
|
|
probe() { curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $1" "$RS/api/me"; }
|
|
|
|
head1 "resource server profiles: $PROFILES"
|
|
curl -s -X POST "$STUB/admin/reset-counter" >/dev/null
|
|
|
|
echo "Scenario: a signing key is compromised. The issuer publishes a replacement and"
|
|
echo "removes the compromised key from the JWK Set immediately. Tokens it signed are"
|
|
echo "already out there with an hour left to run."
|
|
echo
|
|
|
|
VICTIM_KID=$(curl -s "$STUB/admin/state" | python3 -c 'import json,sys;print(json.load(sys.stdin)["activeKid"])')
|
|
LEAKED=$(stub_token "sub=attacker&aud=reports-api&expiresInSeconds=3600")
|
|
echo "1. A token signed with $VICTIM_KID, one hour to live: GET /api/me -> $(probe "$LEAKED")"
|
|
echo " jwks fetches: $(stub_fetches)"
|
|
echo
|
|
|
|
NEW=$(curl -s -X POST "$STUB/admin/publish" | python3 -c 'import json,sys;print(json.load(sys.stdin)["publishedKids"][-1])')
|
|
curl -s -X POST "$STUB/admin/activate?kid=$NEW" >/dev/null
|
|
curl -s -X POST "$STUB/admin/retire?kid=$VICTIM_KID" >/dev/null
|
|
echo "2. Issuer rotates to $NEW and retires $VICTIM_KID."
|
|
stub_state
|
|
echo
|
|
echo " Anyone fetching /jwks.json from this moment sees only $NEW."
|
|
echo
|
|
|
|
echo "3. From here the ONLY traffic is the leaked token. Nothing carries an unknown kid,"
|
|
echo " so nothing forces a refresh. Whether the token keeps working is decided purely"
|
|
echo " by whether the cached JWK Set expires."
|
|
echo
|
|
printf ' %-10s %-8s %s\n' "elapsed" "leaked" "jwksFetches"
|
|
START=$(date +%s)
|
|
for i in $(seq 1 "$PROBES"); do
|
|
ELAPSED=$(( $(date +%s) - START ))
|
|
printf ' t+%-8s %-8s %s\n' "${ELAPSED}s" "$(probe "$LEAKED")" "$(stub_fetches)"
|
|
sleep "$INTERVAL"
|
|
done
|
|
echo
|
|
echo "A row that flips to 401 is the cache expiring and the retired key going away."
|
|
echo "A column of 200s is a resource server that has not noticed, and will not, until"
|
|
echo "something happens to bring it a token it cannot verify."
|