fix(auth): extract identity even when auth is bypassed (CIDR / AUTH_ENABLED=false)#68
Merged
Merged
Conversation
When AUTH_TRUSTED_NETWORKS or AUTH_ENABLED=false short-circuits the
auth middleware, we were also losing the request's identity — every
bypassed call looked like a brand-new anonymous user.
That broke a real scenario users reported: deployment has CIDR bypass
on (interim fix for an earlier auth issue) AND now also enables
CODEAPI_JWT_*. LibreChat 0.8.5's bash_tool POSTs /exec with a valid
Bearer JWT, but the CIDR bypass fires first → middleware returns
early without ever parsing the JWT → request.state.user_id stays
unset → orchestrator falls through to "create new session" because
the cross-user isolation check needs a user_id to match against the
upload session → /mnt/data shows only code.sh and the uploaded file
is invisible.
Fix: in `_should_skip_auth`, both bypass branches now call a new
`_extract_best_effort_identity(request, scope)` helper that:
1. Tries to verify the Bearer JWT if codeapi_jwt_enabled is on
(so the JWT-signed identity is trusted), setting
scope.state.user_id = claims.sub. A failed JWT in bypass mode
does NOT 401 — the bypass already allowed the request; we just
log and continue without an identity.
2. Falls back to `User-Id` / `X-User-Id` header (unsigned, trusted
because the bypass already trusted the boundary that delivered
it).
3. If neither is present, leaves user_id unset (genuine anonymous).
This preserves the original bypass semantics (no auth-key check)
while restoring the file-ownership graph for the legitimate identity
already present in the request.
5 new tests in test_security_middleware.py cover: JWT-bypass identity
extraction (CIDR), JWT-bypass extraction (AUTH_ENABLED=false), User-Id
header fallback during bypass, invalid JWT during bypass (anonymous
fallback, no 401), and the truly-anonymous case.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
🎉 This PR is included in version 3.5.3 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Hotfix for user-reported issue: when running with both `AUTH_TRUSTED_NETWORKS` (CIDR bypass) AND `CODEAPI_JWT_*` enabled, LibreChat 0.8.5 `bash_tool` calls can't see uploaded files — `/mnt/data` shows only `code.sh`.
Root cause
`SecurityMiddleware._should_skip_auth` returns early on CIDR-trusted requests and seeds anonymous state. It never inspects the request's identity signals (JWT, `User-Id` header). For LC's bash_tool path:
Fix
`_should_skip_auth` now calls a new `_extract_best_effort_identity(request, scope)` on both bypass branches (CIDR + `AUTH_ENABLED=false`). It tries, in order:
The original bypass semantics are preserved (no api-key requirement). The orchestrator's cross-user isolation guard now has the identity it needs to recognize ownership and reuse the upload session.
Testing
New tests (`tests/unit/test_security_middleware.py`)
```
TestBypassWithIdentityExtraction
test_trusted_network_bypass_extracts_jwt_sub # CIDR + valid JWT → user_id set
test_trusted_network_bypass_falls_back_to_user_id_header # No JWT, User-Id header → user_id set
test_auth_disabled_bypass_extracts_jwt_sub # AUTH_ENABLED=false + JWT
test_bypass_with_invalid_jwt_falls_back_to_anonymous # Invalid JWT in bypass → no 401, no user_id
test_bypass_with_no_identity_signals_stays_anonymous # Documents genuine anonymous
```
User-facing answer for the AUTH_ENABLED question
Users asking "I set up CODEAPI_JWT_, do I still need AUTH_ENABLED?"*:
Yes — keep AUTH_ENABLED=true (the default). It's a global toggle for whether any auth (api-key OR JWT) is enforced on user paths. Setting AUTH_ENABLED=false would skip JWT verification too.
Operator note
If you have both `AUTH_TRUSTED_NETWORKS` AND `CODEAPI_JWT_*` set, this hotfix is what makes file uploads work end-to-end. If you only have JWT (no CIDR bypass), nothing changes for you.
🤖 Generated with Claude Code