How to test JWT security in Python
Use Python to confirm your API rejects forged JWTs. Generate educational attack variants with the browser JWT Fuzzer (tampered payloads, expired exp, alg:none), then assert failures with PyJWT exception handling. Only test systems you own or have written authorization to assess — never attack third-party production.
Last updated August 26, 2026
Steps
- 1
Paste a development JWT you own into the JWT Fuzzer and generate test variants.
- 2
Note which variants you will try: invalid signature, expired exp, and algorithm confusion (alg:none).
- 3
In Python, call your verifier with PyJWT exception handling and assert each variant is rejected.
- 4
Pin an explicit algorithm allowlist in production so unexpected algs cannot slip through.
- 5
Document results in your security checklist; do not run these tests against unauthorized targets.
Code Example
import jwt
# AUTHORIZED / LAB USE ONLY — paste variants from the JWT Fuzzer
def expect_reject(tampered_token: str, secret: str) -> None:
try:
jwt.decode(tampered_token, secret, algorithms=["HS256"])
raise AssertionError("Expected verification to fail")
except (jwt.InvalidSignatureError, jwt.ExpiredSignatureError, jwt.InvalidAlgorithmError):
return
expect_reject(tampered_token, secret)
# Production rule: always pass algorithms= explicitlyOther languages
Related Articles
Frequently Asked Questions
Is JWT fuzzing legal?
Only test systems you own or have explicit written authorization to test. Unauthorized probing can violate laws and terms of service.
How should Python reject bad JWTs?
Use PyJWT exception handling with an algorithm allowlist, validate exp/nbf, and treat any verify failure as 401. Never fall back to decode-only when authentication fails.
What does the JWT Fuzzer simulate?
Common failure modes: tampered payloads, invalid signatures, expired tokens, and algorithm confusion such as alg:none. It is an educational helper for development labs.
Should my API accept alg:none?
Never. Always specify allowed algorithms in your JWT library and reject tokens that advertise none or any algorithm outside the allowlist.