Skip to content

fix(auth): extract identity even when auth is bypassed (CIDR / AUTH_ENABLED=false)#68

Merged
aron-muon merged 1 commit into
mainfrom
hotfix/identity-extraction-on-bypass
May 22, 2026
Merged

fix(auth): extract identity even when auth is bypassed (CIDR / AUTH_ENABLED=false)#68
aron-muon merged 1 commit into
mainfrom
hotfix/identity-extraction-on-bypass

Conversation

@aron-muon
Copy link
Copy Markdown
Owner

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:

  1. LC web BE uploads file → `/upload` with `User-Id: ` header. Middleware bypasses on CIDR but our `/upload` consumes the `User-Id` header — session is created with `metadata.user_id = `. ✓
  2. LC agents lib (`@librechat/agents` `CodeExecutor.ts`) POSTs `/exec` with the file refs as `files: [...]` and an `Authorization: Bearer ` header but no `User-Id` header.
  3. Middleware bypasses on CIDR, leaves `request.state.user_id` unset.
  4. `exec.py` finds no `request.state.user_id` and (because agents lib doesn't send User-Id) no header fallback.
  5. Orchestrator has `request.user_id = None` → can't match the upload session's `metadata.user_id` → creates a fresh execution session.
  6. `_mount_files` denies the foreign-session file refs (user_id mismatch) and auto-rehydrate finds nothing in the new session.
  7. Pod boots with only `code.sh` in `/mnt/data`.

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:

  1. Verify the Bearer JWT if `CODEAPI_JWT_ENABLED` — sets `scope.state.user_id = claims.sub`. JWT verification failure in bypass mode is non-fatal (the bypass already allowed the request); just log and continue.
  2. `User-Id` / `X-User-Id` header — trusted because the bypass already trusted the boundary delivering it.
  3. Neither — leaves `user_id` unset (genuine anonymous).

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

  • `just test-unit` — 1,511 tests pass (+5 new bypass-with-identity tests)
  • `just lint` / `just format-check` / `just typecheck` — all clean

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

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>
@aron-muon aron-muon merged commit b13fa9e into main May 22, 2026
32 checks passed
@aron-muon aron-muon deleted the hotfix/identity-extraction-on-bypass branch May 22, 2026 11:40
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 3.5.3 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant