# JWT algorithm confusion via key sniffing

Verifier picks HMAC vs asymmetric from key bytes or unprotected header, not a pinned algorithm

_Area: Web authentication and account takeover. Seen in 8 disclosed reports; one confirmed bounty: $1,337; latest 2026-10-27. Updated 2026-12-05._

## How it works

A JWT verifier must decide which algorithm to use. When it decides by sniffing the key's byte format ("looks like PEM, so RSA; otherwise treat as HMAC secret") or by trusting the token's `alg` header, the attacker controls the decision. The public key is public; if the verifier can be steered into the HMAC path, that public key becomes the shared secret and the attacker signs their own tokens. The broken assumption is that "key format" and "algorithm" are the same fact, and that a format recogniser accepts exactly what the downstream parser accepts. Any gap between the two — a leading byte, a BOM, a tab, DER instead of PEM, a JSON JWK — reopens the confusion [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md)[E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md)[E3](https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md)[E6](https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md)[E7](https://bugbountylibrary.com/r/ghsa%3AGHSA-r6x4-923q-g947.md)[E8](https://bugbountylibrary.com/r/ghsa%3AGHSA-xgmm-8j9v-c9wx.md). The degenerate case is no pinning at all: the verifier simply honours whatever `alg` the token names [E4](https://bugbountylibrary.com/r/h1%3A3800870.md). Adjacent shape: `alg` read from the unprotected header, which is not integrity-checked [E4](https://bugbountylibrary.com/r/h1%3A3800870.md).

## Where it shows up

- Key-type detection by `startswith`, anchored regex, `trim()`/`lstrip()` before a `^`-anchored match, or marker text-matching (`-----BEGIN`, `ssh-`) instead of parsing [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md)[E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md)[E3](https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md)[E5](https://bugbountylibrary.com/r/ghsa%3AGHSA-mvf2-f6gm-w987.md)[E6](https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md)[E7](https://bugbountylibrary.com/r/ghsa%3AGHSA-r6x4-923q-g947.md)
- Verifier accepts multiple algorithms, or no pinning at all [E4](https://bugbountylibrary.com/r/h1%3A3800870.md)
- Header merge where the unprotected header is spread last (PHP `[...$protected, ...$header]`, `array_merge`) so its `alg` wins [E4](https://bugbountylibrary.com/r/h1%3A3800870.md)
- Guards that only cover PEM/SSH and ignore raw JWK JSON, dicts, or DER [E6](https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md)[E8](https://bugbountylibrary.com/r/ghsa%3AGHSA-xgmm-8j9v-c9wx.md)
- Fixes that are a single denylist byte check in front of a tolerant third-party parser [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md)[E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md)[E3](https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md)

## How to review

Find where the verifier chooses the algorithm. The invariant: the algorithm is fixed by server-side policy (pinned per key/endpoint), and the key is parsed by the same parser that will consume it — never classified by a hand-rolled prefix/regex check. Confirm `alg` is read only from the integrity-protected header, and that protected/unprotected merges cannot override it [E4](https://bugbountylibrary.com/r/h1%3A3800870.md). For every guard, enumerate the byte-forms the real loader accepts (leading whitespace, BOM, CR-only, comments, DER, JWK JSON) and check the guard rejects all of them [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md)[E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md)[E3](https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md)[E6](https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md)[E7](https://bugbountylibrary.com/r/ghsa%3AGHSA-r6x4-923q-g947.md)[E8](https://bugbountylibrary.com/r/ghsa%3AGHSA-xgmm-8j9v-c9wx.md). Reject any key whose family does not match the pinned algorithm.

## How finders spotted it

- Read the prior CVE fix, saw it was one anchored `startswith`, and diffed the guard's accepted byte set against what `cryptography`'s PEM loader tolerates [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md).
- Reviewed a patch, noticed `trim()` only strips ECMAScript whitespace while the regex stayed `^`-anchored, then built a bypass matrix of control bytes, zero-width unicode, comments and wrappers [E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md).
- Noticed `is_pem_format()` was the sole family guard, compared its regex against the tolerant loader, hypothesised a parser differential on whitespace/line endings [E3](https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md).
- Code-read the header merge, saw PHP spread last-wins put the unprotected header second, contrasted with the builder which spreads protected last [E4](https://bugbountylibrary.com/r/h1%3A3800870.md).
- Noticed the verifier did not pin the algorithm, then tested HS256 signed with the RSA public key [E4](https://bugbountylibrary.com/r/h1%3A3800870.md).
- Saw the public-key matcher used `^` with no `.trim()` while the sibling private-key check did trim — an asymmetry suggesting whitespace defeats the anchor [E5](https://bugbountylibrary.com/r/ghsa%3AGHSA-mvf2-f6gm-w987.md).
- Reasoned a DER key (0x30 0x82) matches no text marker, grepped for DER handling, tested all three encodings [E6](https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md).
- Reviewed the JWK guard, saw `lstrip()` + `startswith(b"{")`, reasoned a BOM defeats it [E7](https://bugbountylibrary.com/r/ghsa%3AGHSA-r6x4-923q-g947.md).
- Checked existing HMAC protections, saw they only cover PEM/SSH, hypothesised a JSON JWK slips through [E8](https://bugbountylibrary.com/r/ghsa%3AGHSA-xgmm-8j9v-c9wx.md).

## Variants seen

- Leading byte before PEM defeats anchored guard [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md)[E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md)
- Whitespace/line-ending mutation defeats PEM regex [E3](https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md)[E5](https://bugbountylibrary.com/r/ghsa%3AGHSA-mvf2-f6gm-w987.md)
- DER encoding bypasses text markers [E6](https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md)
- BOM defeats `lstrip()` JWK check [E7](https://bugbountylibrary.com/r/ghsa%3AGHSA-r6x4-923q-g947.md)
- Raw JWK JSON/dict bypasses PEM/SSH denylist [E8](https://bugbountylibrary.com/r/ghsa%3AGHSA-xgmm-8j9v-c9wx.md)
- `alg` taken from unprotected header [E4](https://bugbountylibrary.com/r/h1%3A3800870.md)
- No algorithm pinning at all [E4](https://bugbountylibrary.com/r/h1%3A3800870.md)

## Why fixes fail

Every fix here patched the reported trigger, not the class: a byte-prefix denylist, `trim()`, or a text matcher in front of a tolerant parser. Each was bypassed by a different encoding of the same key [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md)[E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md)[E3](https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md)[E6](https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md)[E7](https://bugbountylibrary.com/r/ghsa%3AGHSA-r6x4-923q-g947.md).

## Impact and severity

Forged tokens with arbitrary `sub` and `role=admin` yielded HTTP 200 and admin secrets [E1](https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md); superadmin under mixed allow-lists [E3](https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md); read access via forged tokens [E4](https://bugbountylibrary.com/r/h1%3A3800870.md). Severity is high: full authentication bypass, scaling to any account the attacker names. In one case impact was gated by whether read and write endpoints shared the verifier [E4](https://bugbountylibrary.com/r/h1%3A3800870.md).

## Seen in real software
Open-source packages and projects, and products, where this pattern was found. If the target runs one, compare the version it runs with the fix.
- **authlib (pip)** >= 1.3.1, <= 1.8.0, fixed in 1.9.0: Authlib incomplete algorithm-confusion fix permits HMAC forgery with prefixed public keys (CVE-2026-114615). Report: https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md. Fix: https://github.com/authlib/authlib/commit/e3bd41cb29dcfee434453b135ffb487884baeedf
- **fast-jwt (npm)** >= 6.2.0, <= 6.2.4, fixed in 6.3.0: fast-jwt: Incomplete patch of CVE-2026-34950: Non-whitespace key-prefix re-enables RSA→HS256 algorithm confusion (CVE-2026-107722). Report: https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md. Fix: https://github.com/nearform/fast-jwt/commit/d96bbc6c5336055a6dbfa318fdbab01197264cfa
- **PyJWT (pip)** <= 2.13.0, fixed in 2.14.0: PyJWT: Asymmetric-PEM detection bypass: whitespace/line-ending-mutated public keys skip the HS/asymmetric confusion guar (CVE-2026-102268). Report: https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md. Fix: https://github.com/jpadilla/pyjwt/commit/8b4e233a22206b34ec1186e912e75c0b2396ac07
- **pyjwt (pip)** >= 2.4.0, < 2.14.0, fixed in 2.14.0: PyJWT: Public keys in DER form are accepted as HMAC secrets, bypassing the CVE-2022-29217 guard (CVE-2026-102271). Report: https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md. Fix: https://github.com/jpadilla/pyjwt/commit/2798504fa2663364573cf2d1043d8d7fef389499
- **PyJWT (pip)** = 2.13.0, fixed in 2.14.0: PyJWT BOM Bypass (CVE-2026-102272). Report: https://bugbountylibrary.com/r/ghsa%3AGHSA-r6x4-923q-g947.md. Fix: https://github.com/jpadilla/pyjwt/commit/180783930de91876bc0d601f826a1f2956057291
- **pyjwt (pip)** < 2.13.0, fixed in 2.13.0: PyJWT: Public-key JWK accepted as HMAC secret enables forged HS256 tokens when mixed families are allowed (CVE-2026-48526). Report: https://bugbountylibrary.com/r/ghsa%3AGHSA-xgmm-8j9v-c9wx.md
- 1 more in [components.md](https://bugbountylibrary.com/skills/components.md).

## Evidence
- E1: https://bugbountylibrary.com/r/ghsa%3AGHSA-7vr2-2fgp-9w7p.md
- E2: https://bugbountylibrary.com/r/ghsa%3AGHSA-ww5h-9m49-7xx4.md
- E3: https://bugbountylibrary.com/r/ghsa%3AGHSA-ffc3-869f-jxw9.md
- E4: https://bugbountylibrary.com/r/h1%3A3800870.md
- E5: https://bugbountylibrary.com/r/ghsa%3AGHSA-mvf2-f6gm-w987.md
- E6: https://bugbountylibrary.com/r/ghsa%3AGHSA-p4g4-x82p-q773.md
- E7: https://bugbountylibrary.com/r/ghsa%3AGHSA-r6x4-923q-g947.md
- E8: https://bugbountylibrary.com/r/ghsa%3AGHSA-xgmm-8j9v-c9wx.md
