Skip to content

Reject JWT tokens with alg:none to prevent signature bypass#3

Open
emrazik wants to merge 1 commit into
masterfrom
fix/jwt-algorithm-none
Open

Reject JWT tokens with alg:none to prevent signature bypass#3
emrazik wants to merge 1 commit into
masterfrom
fix/jwt-algorithm-none

Conversation

@emrazik

@emrazik emrazik commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Finding

NetSPI Finding #1048219779 — JWT Weak Signature Validation | High

The JWT library accepted tokens whose header declared "alg": "none". When a token has no algorithm, the signature field is ignored and the library treats the payload as trusted without any cryptographic check. An unauthenticated attacker can craft a token with any email address, set alg to none (or case variants like None, nOnE), omit the signature, and be accepted as that user by every protected endpoint.

What changed

lib/insecurity.tsisAuthorized()

The function previously returned expressJwt(...) directly. It now wraps that middleware: before delegating to expressJwt, it decodes the token header with jws.decode() and returns 401 immediately if the alg field is missing or equals none (case-insensitive). expressJwt only receives tokens with a real algorithm.

lib/insecurity.tsupdateAuthenticatedUsers()

Before calling jwt.verify() to cache the decoded user, the middleware now checks the token header via jws.decode() and skips verification entirely for alg:none tokens, so they are never stored as authenticated sessions.

Test plan

  • Send a forged alg:none token (e.g. header.payload.) to GET /rest/basket/1 — expect 401
  • Log in normally (RS256 token) and verify protected endpoints still work
  • Confirm alg:none tokens with a forged admin email are rejected

🤖 Generated with Claude Code

The expressJwt middleware and jwt.verify() accepted tokens whose header
declared alg:none, which skips signature verification entirely and lets an
attacker forge arbitrary claims without knowing the private key.

- isAuthorized(): wraps expressJwt in a guard that decodes the token header
  first and returns 401 if the algorithm is absent or 'none'
- updateAuthenticatedUsers(): skips jwt.verify() for alg:none tokens so
  they are never cached as authenticated sessions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant