How to Decode a JWT Without Verifying It in Rust with insecure_decode
The jsonwebtoken crate's decode always verifies a token. To only read one, it has decode_header for the header and dangerous::insecure_decode for the claims: no key, and no checks. That's right for reading a token and wrong for trusting one.
Syntax
use jsonwebtoken::dangerous::insecure_decode;
use jsonwebtoken::decode_header;
let header = decode_header(token)?;
let claims = insecure_decode::<Claims>(token)?.claims;
| Part | What it does |
|---|---|
token | The JWT as text. |
decode_header(token) | Reads the header, with fields such as alg and kid. Returns a Result with a Header. |
insecure_decode::<Claims>(token) | Reads the header and the claims, the claims into Claims. Returns a Result with a TokenData. |
Claims | Your struct for the claims. It derives serde's Deserialize. |
.claims | The claims part of the TokenData. |
? | Passes the error on when the text isn't a well-formed token. |
The crate documents insecure_decode as "Decode a JWT with NO VALIDATION", and warns: "DANGER: This performs zero validation on the JWT". decode_header: "Decode a JWT without any signature verification/validations and return its Header." The function sits in a module named dangerous, so the word shows up in every use of it.
The Example
The example reads a token that another service signed with a key this program doesn't have. The token expired on 1 December 2025, and its header names the key that signed it, key-2025-12, in a kid.
Prerequisites
- Rust, installed with rustup, which comes with Cargo. This guide uses Rust 1.100.1.
Step 1: Create the Project
cargo new demo
cd demo
cargo add jsonwebtoken --features rust_crypto
cargo add serde --features derive
Creating binary (application) `demo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Updating crates.io index
Adding jsonwebtoken v12.0.0 to dependencies
Features:
+ rust_crypto
+ use_pem
- aws_lc_rs
Updating crates.io index
Locking 91 packages to highest Rust 1.100.1 compatible versions
Updating crates.io index
Adding serde v1.0.229 to dependencies
Features:
+ derive
+ serde_derive
+ std
- alloc
- rc
- unstable
cargo new demo creates the project, and cd demo moves into it. The two cargo add commands add jsonwebtoken 12.0.0 with its rust_crypto cryptography backend, and serde with its derive macros, which let the Claims struct derive Deserialize.
Step 2: Write the Code
Replace the contents of src/main.rs with:

use jsonwebtoken::dangerous::insecure_decode;
use jsonwebtoken::decode_header;
use serde::Deserialize;
/// The claims this program reads from the token.
#[derive(Deserialize)]
struct Claims {
sub: String,
role: String,
exp: u64,
}
fn main() -> Result<(), jsonwebtoken::errors::Error> {
// A token from another service, signed with a key this program doesn't have.
// It expired on 1 December 2025.
let token = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtleS0yMDI1LTEyIiwidHlwIjoiSldUIn0.eyJzdWIiOiJhZGEiLCJyb2xlIjoicmVhZGVyIiwiaWF0IjoxNzY0NTQ3MjAwLCJleHAiOjE3NjQ1NTA4MDB9.2EjUXD-_uc78GAdKpsWk7GOlMQztlsdWKQkT-RdKFKE";
let header = decode_header(token)?;
println!("{:?} {:?}", header.alg, header.kid);
let claims = insecure_decode::<Claims>(token)?.claims;
println!("{} {} {}", claims.sub, claims.role, claims.exp);
Ok(())
}
Claimslists the three claims the program reads. serde fills them in from the token's payload.decode_header(token)?returns the header.{:?}prints itsalgand itskid, which is anOptionbecause a header may have nokid.insecure_decode::<Claims>(token)?.claimsreturns the claims.mainreturns aResult, so?can pass on an error from either function.
Step 3: Run It
cargo run -q
HS256 Some("key-2025-12")
ada reader 1764550800

Both functions read the token without the key, and insecure_decode didn't mind that exp, 1764550800, is 1 December 2025 at 01:00 UTC.
Decoding Is Not Verifying
Anyone can write a token with any claims, so these claims prove nothing. Use them to read, never to decide who someone is or what they may do. To trust a token, verify it with decode, the key and the algorithm you expect:
let data = decode::<Claims>(token, &DecodingKey::from_base64_secret(secret)?, &Validation::new(Algorithm::HS256))?;
That call checks the signature and the expiry, and returns an Err when either fails.
Reading the header first is the one common server-side use: a service with several keys reads kid and picks that key from its own list. The header is just as forgeable as the claims, so look it up only among keys you already trust.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Trusting the claims from insecure_decode | Anyone can send a token with any claims | Verify with decode, the key and a Validation |
| Expecting the expiry to be checked | Nothing is checked: a token that expired in December 2025 decoded without an error | Verify; decode checks exp |
No claims type, as in let claims = insecure_decode(token)?.claims; | error[E0282]: type annotations needed | Name it: insecure_decode::<Claims>(token) |
Passing the whole Authorization header value | decode_header("Bearer a.b.c") failed with Base64 error: Invalid symbol 32, offset 6.: 32 is a space | Strip the Bearer prefix first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
InvalidToken | The text isn't three parts separated by dots; "not-a-jwt" gave this from both functions | Check what you passed |
Base64 error: Invalid symbol | Other text around the token, such as Bearer | Pass only the token |
FAQ
Can I get only the claims?
Yes. dangerous::insecure_decode_claims, added in jsonwebtoken 11.1.0, returns just the claims: let claims: Claims = insecure_decode_claims(token)?; read ada and 1764550800 from the same token.
My older code turns off signature validation instead. What changed?
jsonwebtoken 11.0.0 removed that setting. Its changelog: "BREAKING: Validation.insecure_disable_signature_validation has been removed, use dangerous::insecure_decode instead".
Is it safe to decode tokens this way in a client?
For reading, yes, for example to show the user's name or to refresh before exp. The server that receives the token still verifies it.
Conclusion
decode_header(token) and dangerous::insecure_decode::<Claims>(token) return a token's header and claims without a key and without any checks. Use them to look inside a token; verify it with decode, the key and a Validation before you trust it.
Additional Resources
- jsonwebtoken 12.0.0 on docs.rs, with insecure_decode and decode_header.
- RFC 8725, section 3.10: Do Not Trust Received Claims.
