# 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 ([[raw/medium.com/jwks-and-kid-explained-how-to-find-the-key-that-signed-your-token-and-15966|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 ([[raw/robinx0.github.io/jwt-algorithm-confusion-none-hs256-rs256-mix-ups-and-kid-injection-15974|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 ([[raw/medium.com/jwks-and-kid-explained-how-to-find-the-key-that-signed-your-token-and-15966|JWKS and kid]]).
## Related
- [[JSON Web Tokens]]
- [[JWT Algorithm Confusion]]
- [[JWT Empty Key Forgery]]
- [[JWT None Algorithm]]
- [[Server-Side Request Forgery|SSRF]]
- [[URL Parser Differentials]]
## Sources
- [[raw/xalgorix.com/jwt-libraries-keep-trusting-the-key-an-attacker-puts-in-the-header-jwk-15782|JWT libraries keep trusting the key an attacker puts in the header (jwk/jku/kid)]]
- [[raw/portswigger.net/jwt-attacks-web-security-academy-3850|JWT attacks (PortSwigger Web Security Academy)]]
- [[raw/portswigger.net/lab-jwt-authentication-bypass-via-jku-header-injection-4126|Lab: JWT authentication bypass via jku header injection]]
- [[raw/invicti.com/jwt-signature-bypass-via-kid-path-traversal-4111|JWT Signature Bypass via kid Path Traversal]]
- [[raw/invicti.com/jwt-signature-bypass-via-kid-sql-injection-4112|JWT Signature Bypass via kid SQL injection]]
- [[raw/invicti.com/jwt-signature-bypass-via-unvalidated-jku-parameter-4110|JWT Signature Bypass via unvalidated jku parameter]]
- [[raw/invicti.com/jwt-forgery-via-unvalidated-jku-parameter-invicti-4556|JWT Forgery via unvalidated jku parameter (Invicti)]]
- [[raw/dev.to/bug-bounty-tip-exploiting-jwt-kid-header-injection-for-auth-bypass-14091|Bug bounty tip: exploiting JWT kid header injection for auth bypass]]
- [[raw/siunam321.github.io/jwt-authentication-bypass-via-kid-header-path-traversal-siunam-4564|JWT authentication bypass via kid header path traversal (siunam)]]
- [[raw/medium.com/hammer-tryhackme-from-a-4-digit-lottery-to-rce-via-jwt-kid-abuse-13773|Hammer (TryHackMe): From a 4-Digit Lottery to RCE via JWT kid Abuse]]
- [[raw/c0dn.dev/off-the-curve-jwt-forgery-ctf-writeup-14022|Off the Curve: JWT forgery CTF writeup]]
- [[raw/medium.com/jwt-header-parameter-injections-4364|JWT Header Parameter Injections]]
- [[raw/github.com/known-exploits-and-attacks-jwt-tool-wiki-4560|Known Exploits and Attacks (jwt_tool Wiki)]]
- [[raw/medium.com/jwks-and-kid-explained-how-to-find-the-key-that-signed-your-token-and-15966|JWKS and kid Explained: How to Find the Key That Signed Your Token (and Spot a Leaked Private Key)]]
- [[raw/robinx0.github.io/jwt-algorithm-confusion-none-hs256-rs256-mix-ups-and-kid-injection-15974|JWT algorithm confusion: none, HS256/RS256 mix-ups, and kid injection]]