Summary
NextAuth.js can raise an uncaught exception when its getToken() helper processes a malformed Authorization: Bearer header with invalid percent encoding and no session cookie is present. An unauthenticated request may therefore cause a per-request denial of service in API routes, middleware, or server-side request handlers, without bypassing authentication or exposing sensitive data.
Affected context
The issue occurs in applications using the next-auth/jwt or @auth/core/jwt getToken() helper to authenticate requests. It becomes risky when a request lacks a session cookie, supplies a malformed bearer value, and the authentication path decodes that value before safely handling validation errors.
Exploit preconditions
- Attack surface: HTTP endpoints, middleware, or server-side handlers that invoke getToken()
- Required access: Ability to send unauthenticated HTTP requests to an exposed application route
- Trigger point: Processing of an Authorization: Bearer header when no session cookie is available
- Why exploitation is possible: Malformed percent encoding causes URL decoding to throw an exception that is not caught and converted into an invalid-token result
Why this happens
The underlying bug is improper input validation combined with an uncaught exception. The authentication helper decodes attacker-controlled bearer input before validation, but does not safely handle decoding failures caused by malformed percent encoding. This maps primarily to CWE-20: Improper Input Validation and secondarily to CWE-248: Uncaught Exception, because invalid request data can escape normal authentication error handling and disrupt request processing.
Potential impact
- An attacker may cause individual authentication requests to fail
- Application routes or middleware may return unexpected server errors
- Repeated requests could consume resources or contribute to service degradation
- Availability of affected API or server-side request paths could be reduced
- Authentication may remain enforced, with no token or session disclosure expected
General mitigation guidance
Mitigation generally focuses on treating authorization headers as untrusted input and making token parsing failure-safe.
Common defensive patterns include:
- Validate bearer-header structure and encoding before decoding
- Catch decoding and parsing exceptions at the authentication boundary
- Convert malformed tokens into ordinary authentication failures
- Use secure-by-default authentication helpers that reject invalid input without propagating exceptions
- Apply request-size, rate, and resource-consumption limits to public authentication paths
- Run application components with only the privileges required for their responsibilities
- Keep authentication and error-handling behavior consistent across routes, middleware, and server-side handlers
How teams detect issues like this
Organizations can detect this class of issue through:
- Static analysis for URL-decoding operations on request-controlled values without adjacent exception handling
- Code review checks for authentication helpers that parse headers before validating syntax and encoding
- Tests covering malformed percent encoding, invalid bearer formats, missing cookies, and repeated requests
- Runtime monitoring for spikes in server errors associated with authentication middleware
- Policy enforcement requiring invalid credentials to produce controlled authentication failures rather than uncaught exceptions
- Automated security review tools that identify unsafe request parsing and missing error boundaries

