Summary
Eclipse Jetty's JASPIAuthenticator can leave authentication data in ThreadLocal variables when it exits early, causing subsequent requests on the same thread to inherit stale security context.
Under these conditions, a user may gain access with another user's privileges, resulting in broken access control and potential privilege escalation.
Affected context
This vulnerability appears in Jetty deployments using the JASPIAuthenticator where authentication state is stored in ThreadLocal variables.
It manifests when certain authentication flows cause JASPIAuthenticator to return early, skipping the cleanup of those ThreadLocals.
Environments that reuse worker threads heavily (such as typical servlet containers under load) are more likely to encounter cross-request leakage of authentication state.
Exploit preconditions
- Attack surface: HTTP endpoints handled by Jetty where JASPIAuthenticator performs authentication.
- Required access: Ability to send crafted or timed HTTP requests to the affected Jetty server.
- Trigger point: An initial request that partially triggers JASPIAuthenticator, sets ThreadLocal authentication values, and then exits via a code path that omits cleanup.
- Why exploitation is possible: ThreadLocal variables are bound to threads, not requests. When cleanup does not occur on early return, the next request reusing that thread can see the previous authentication state, causing the server to treat the new request as if it were authenticated as the prior user.
Why this happens
The core issue is incomplete lifecycle management of per-request security state stored in ThreadLocal variables.
JASPIAuthenticator sets two ThreadLocal values during authentication checks but relies on certain control-flow paths to clear them.
When early-return code paths bypass the cleanup logic, the ThreadLocals remain populated and leak into subsequent requests that reuse the same thread.
This maps to CWE-226: Sensitive Information Uncleared Before Release, where resources (here, threads and their ThreadLocal state) are returned to a pool while still holding sensitive or security-relevant data.
The resulting behavior also relates to CWE-287: Improper Authentication, because a request can be treated as authenticated based on stale state rather than its own credentials.
Potential impact
- Requests may be processed under another user's identity and privileges.
- Unauthorized access to protected resources that the current caller should not reach.
- Escalation from low-privileged to high-privileged roles in multi-user systems.
- Violation of tenant isolation in multi-tenant applications.
- Audit and logging inconsistencies, misattributing actions to the wrong user.
General mitigation guidance
Mitigation focuses on robust cleanup of per-request authentication state and defensive handling of ThreadLocals:
- Ensure all code paths in authentication filters/handlers, including early returns and error paths, explicitly clear ThreadLocal security context.
- Prefer request-scoped or context objects passed through the call chain instead of ThreadLocal for security-sensitive data where feasible.
- Use well-defined lifecycle hooks (e.g., finally blocks or container callbacks) to guarantee cleanup, independent of authentication outcome.
- Minimize the amount of authentication and authorization state stored in thread-bound structures, favoring immutable, per-request tokens.
- Apply least privilege defaults so that in ambiguous or inconsistent state, access is denied rather than granted.
How teams detect issues like this
Detection commonly involves a combination of code-level and behavior-level checks:
- Static analysis rules that flag use of ThreadLocal for authentication or authorization data without corresponding cleanup in all control flows.
- Code review guidelines requiring that any ThreadLocal used in security code be set and cleared symmetrically, ideally within try/finally constructs.
- Dynamic tests that simulate concurrent and sequential requests on the same connection pool, checking for cross-request leakage of user identity or roles.
- Security testing that repeatedly alternates authenticated and unauthenticated requests, inspecting whether the server ever treats unauthenticated requests as authenticated.
- Application-level monitoring for anomalous access patterns, such as sudden privilege jumps or actions attributed to users without proper login events.

