Skip to main content

How to Decode a JWT Without Verifying It in Rust with insecure_decode

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
use jsonwebtoken::dangerous::insecure_decode;
use jsonwebtoken::decode_header;

let header = decode_header(token)?;
let claims = insecure_decode::<Claims>(token)?.claims;
PartWhat it does
tokenThe 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.
ClaimsYour struct for the claims. It derives serde's Deserialize.
.claimsThe 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​

Terminal
cargo new demo
cd demo
cargo add jsonwebtoken --features rust_crypto
cargo add serde --features derive
Output
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:

src/main.rs, which defines Claims with sub, role and exp deriving Deserialize, holds a token from another service that expired in December 2025, prints the alg and kid from decode_header, and prints the claims from insecure_decode.

src/main.rs
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(())
}
  • Claims lists the three claims the program reads. serde fills them in from the token's payload.
  • decode_header(token)? returns the header. {:?} prints its alg and its kid, which is an Option because a header may have no kid.
  • insecure_decode::<Claims>(token)?.claims returns the claims.
  • main returns a Result, so ? can pass on an error from either function.

Step 3: Run It​

Terminal
cargo run -q
Output
HS256 Some("key-2025-12")
ada reader 1764550800

A zsh terminal where cargo run -q prints HS256 and Some key-2025-12 from the header, then ada, reader and the exp 1764550800 from the claims.

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:

src/main.rs
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​

MistakeWhat happensDo this instead
Trusting the claims from insecure_decodeAnyone can send a token with any claimsVerify with decode, the key and a Validation
Expecting the expiry to be checkedNothing is checked: a token that expired in December 2025 decoded without an errorVerify; decode checks exp
No claims type, as in let claims = insecure_decode(token)?.claims;error[E0282]: type annotations neededName it: insecure_decode::<Claims>(token)
Passing the whole Authorization header valuedecode_header("Bearer a.b.c") failed with Base64 error: Invalid symbol 32, offset 6.: 32 is a spaceStrip the Bearer prefix first

Troubleshooting​

SymptomLikely causeFix
InvalidTokenThe text isn't three parts separated by dots; "not-a-jwt" gave this from both functionsCheck what you passed
Base64 error: Invalid symbolOther 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​