JWT Header Injection

Abuse of the five JOSE header parameters that point at key material (jwk, jku, x5c, x5u, kid) to make the verifier trust a key the attacker controls, or to turn key lookup into an injection sink.

How it works

A JWS header is attacker-controlled JSON, base64url-encoded (not encrypted), and read before the signature is checked because the verifier needs it to pick the key. RFC 7515 defines five parameters that tell the verifier where the key is, and §10.3 is explicit that none of them makes a key trustworthy — kid is only “a hint indicating which key was used”. A signature that verifies under a key the attacker chose proves only that the attacker signed it.

jwk — embedded public key

The header carries the verification key itself. A verifier that uses it accepts a token that vouches for itself: generate a key pair, write arbitrary claims, sign with the private key, embed the public half.

{ "alg":"RS256", "typ":"JWT", "jwk":{ "kty":"RSA", "e":"AQAB", "n":"<attacker modulus>" } }

Still shipping in 2026: node-jose fixed it in 2018 (CVE-2018-0114); Authlib (CVE-2026-27962, CVSS 9.1, fixed 1.6.9) and Dart jose (CVE-2026-34240) repeated it. Authlib’s subtle trigger was a key-resolver that returned None (e.g. a JWKS lookup that misses an unknown/rotated kid): instead of failing, _prepare_algorithm_key() fell through to the header jwk, so even an app that looked keys up correctly could be bypassed with an unseen kid plus an embedded key. Panva jose instead makes embedded-key use opt-in via an explicit EmbeddedJWK resolver (correct for DPoP, where trust comes from a separate binding).

jku / x5u — URL to the key set

The token says “my key is at this URL” and a naive verifier fetches it. Host your own JWKS, set kid to match, sign with your key. This is also a pre-auth SSRF primitive (RFC 8725 warns against blindly following jku/x5u). Allow-list failures to probe:

  • String startsWith/contains on the trusted host instead of parsing scheme+host+port for equality (see URL Parser Differentials).
  • Open redirect on the allow-listed host when the HTTP client follows redirects.
  • Attacker-writable content on the trusted origin (upload bucket, profile field, paste) serving a JWK Set.

x5c / x5u — certificate chain

Same as embedded jwk wearing a certificate. RFC 7515 requires RFC 5280 chain validation to a controlled trust anchor; a verifier that just reads the public key from the first cert accepts a self-signed cert minted a minute ago.

kid — key lookup becomes an injection sink

Legitimate kid semantics: verifiers look up the JWK whose kid equals the header’s, never assume the first key, and refresh the cached JWKS once on an unknown kid before rejecting — a bounded retry, not a loop (JWKS and kid).
kid cannot introduce a new key by itself, but it reaches whatever code turns the identifier into a key, often as a trusted string:

  • Path traversal: "kid":"../../../../../../dev/null". With HMAC, the file’s contents become the shared secret; /dev/null (empty) or any predictable static file lets the attacker sign with a known/empty secret. A null byte encodes as AA==.
  • SQL injection: "kid":"nonexistent' UNION SELECT 'attacker_secret'-- -" returns an attacker-known key from the key-lookup query.
  • Command injection / SSRF: kid passed to a shell or to a remote key-fetch endpoint.
  • Predictable-file HMAC: kid pointing at a web-served static file (e.g. ../../var/www/html/index.html) makes its fetchable contents the secret; with kid SQLi the injected UNION SELECT value becomes the key. Confirm jku fetching with an out-of-band callback before building a JWKS (robinx0).

Notable cases

  • Authlib (CVE-2026-27962) and Dart jose (CVE-2026-34240): embedded-jwk acceptance, March 2026, eight years after node-jose’s CVE-2018-0114.
  • PortSwigger Web Security Academy labs: jwk injection, jku injection (trusted-domain check bypass), kid path traversal; the canonical reproductions.
  • Invicti: forgery via unvalidated jku (also a blind SSRF), signature bypass via kid path traversal, signature bypass via kid SQL injection.
  • Hammer (TryHackMe): 4-digit lottery → JWT kid abuse → RCE, a worked chain from kid manipulation to code execution.
  • “Off the Curve” CTF: kid=/ made the HMAC key empty, forging a role=admin token (empty-key forgery meeting header injection) as stage one of a crypto chain.
  • xalgorix survey: catalogs the whole jwk/jku/x5c/x5u/kid class and the key-resolution rules that close it.

Detection and testing

  • Inspect the header for any of the five parameters; many servers process them even when the issuer never sets them.
  • jwk: embed your public key, sign with your private key, update kid to match (Burp JWT Editor “Embedded JWK” does both).
  • jku/x5u: point at a collaborator host; if fetched, test allow-list bypass (parser differential, open redirect, same-origin writable content) and watch for SSRF.
  • kid: try ../../../dev/null with an empty HMAC secret, SQL metacharacters, and command/SSRF payloads; observe differential responses.

Defenses

  • Resolve keys only from configuration (the IdP’s jwks_uri from metadata). Ignore jwk, jku, x5u, x5c entirely unless a protocol genuinely needs them.
  • If jku/x5u is required: exact scheme+host+port allow-list on the parsed URL, no redirects, no cookies, DNS-rebinding-safe client.
  • Validate x5c chains to a pinned application-specific root (not the OS CA store) plus expected subject.
  • Treat kid as untrusted: map it to keys via an allow-list or static table, never a filesystem path, SQL string, or shell argument.
  • Audit the published JWKS itself: no private (d, p, q, dp, dq, qi) or symmetric (oct/k) material, unique kid per key (JWKS and kid).

Sources