LibreChat/packages/api/src
Danny Avila 78ec1940a2
🧹 fix: Clean Up MCP OAuth State Mappings on Uninstall + Reject Superseded Callbacks (#14549)
* 🧹 fix: Clean Up MCP OAuth State Mappings on Uninstall + Reject Superseded Callbacks

Disconnecting an OAuth MCP server deleted its mcp_oauth flows but left the
mcp_oauth_state:{state} mappings behind for the full TTL. Because flow ids
are deterministic (userId:serverName) and the CSRF token is HMAC(flowId), a
stale browser tab's callback could resolve its orphaned state to the NEXT
flow for the same server, pass CSRF, burn the fresh flow's one-shot CSRF
cookie, and fail the PKCE exchange, sabotaging the legitimate retry.

- Add MCPOAuthHandler.deleteFlowAndStateMapping: reads the flow's stored
  state and deletes the mapping before the flow (mapping-first so a crash
  between deletes fails closed instead of recreating the orphan)
- Route mcp_oauth deletions in clearStoredMCPOAuthState through the helper
  for both tenant-scoped and legacy flow ids
- Reject callbacks whose state does not match the resolved flow's stored
  state: the only control distinguishing a superseded attempt from the
  current one on a deterministic flow id

Fixes #14534

* fix: gate failFlow on state match in the OAuth error branch (Codex P1)

The provider-error branch failed the resolved flow on CSRF/session alone,
so a superseded error callback resolved through an orphaned mapping could
mark the current flow FAILED. Apply the same stored-state equality gate
before failFlow.

* fix: leave the flow in place when the state-mapping delete fails (Codex P2)

deleteFlow swallows storage errors and returns false, and
deleteStateMapping discarded that result, so a failed mapping delete
followed by a successful flow delete would silently recreate the orphan.
Surface the boolean from deleteStateMapping and throw from
deleteFlowAndStateMapping before touching the flow, so the caller's
allSettled warn branch fires and the next replacement retries both.

* fix: restore the state mapping when the flow delete fails (Codex P2)

The inverse partial failure of the round-3 fix: a successful mapping
delete followed by a silently failed flow delete left a PENDING flow
whose reused authorization URL could never resolve, dead-ending every
callback in invalid_state until the flow went stale. Check deleteFlow's
result, re-store the mapping on failure, and throw so the caller's
allSettled warn branch fires.

* fix: never leave a callback-capable flow behind on uninstall (Codex round 6)

Teardown runs after the server's tokens are deleted, so a preserved
flow+mapping pair (the round-3 early-throw path) let a lingering consent
tab complete the callback and recreate credentials post-uninstall. Now
that both callback branches gate on stored-state equality, an orphaned
mapping is the benign failure mode, so invert the order: delete the flow
first, attempt the mapping delete regardless, and reject when either
reports a storage failure. This supersedes the round-4 mapping restore,
which also preserved a callback-capable pair.

* fix: delete the flow even when its metadata read fails (Codex round 7)

A storage error on the initial getFlowState aborted teardown before any
delete ran, preserving the callback-capable flow after token deletion.
Tolerate the read failure, delete the flow blindly, skip the mapping it
could not identify (the callback gates neutralize the possible orphan),
and reject so the caller's warn branch fires.
2026-07-31 12:11:36 -04:00
..
acl 🔒 fix: Prevent Role Grants on Shared Links (#14137) 2026-07-07 07:04:52 -04:00
actions
admin 🪢 feat: Langfuse Fanout Connection Setting (#14108) 2026-07-29 18:33:10 -04:00
agents 🎯 fix: Exact Ask-Question Attribution via Interrupt tool_call_id (#14539) 2026-07-31 10:11:08 -04:00
apiKeys
app 🗝️ feat: Custom Endpoint API Key Encryption via Array Secret Registry (#14510) 2026-07-29 15:37:46 -04:00
artifacts 🧵 fix: Preserve Fenced Markdown Artifacts (#14121) 2026-07-05 12:04:59 -04:00
auth 📉 perf: cache OpenID JWT user documents (#14187) 2026-07-12 07:53:50 -04:00
cache feat: add low-noise Redis observability (#14309) 2026-07-16 10:22:03 -04:00
cdn 🔥 fix: Firebase CDN Initialization Under tsdown CJS Interop (#14046) 2026-07-01 08:20:40 -04:00
cluster feat: add low-noise Redis observability (#14309) 2026-07-16 10:22:03 -04:00
crypto
db
endpoints 🗝️ feat: Custom Endpoint API Key Encryption via Array Secret Registry (#14510) 2026-07-29 15:37:46 -04:00
favorites 🧰 feat: Redesign Agent Builder with Unified Tools Marketplace, Skills & Orchestration (#13952) 2026-07-05 11:30:12 -04:00
files 📄 fix: Filter Non-PDF Documents on the Anthropic Encode Path (#14535) 2026-07-31 09:57:02 -04:00
flow 🤫 refactor: Silent MCP OAuth Refresh on Mid-Session 401 (#13369) 2026-06-10 13:12:42 -04:00
html ⚙️ refactor: lazy-load React Query Devtools (#13639) 2026-06-10 13:06:20 -04:00
langfuse 🪢 feat: Langfuse Fanout Connection Setting (#14108) 2026-07-29 18:33:10 -04:00
mcp 🧹 fix: Clean Up MCP OAuth State Mappings on Uninstall + Reject Superseded Callbacks (#14549) 2026-07-31 12:11:36 -04:00
memory 🧠 fix: Bound Memory Agent Input (#13606) 2026-06-09 14:38:21 -04:00
middleware 🤏 fix: Filter Admin Config Reads by Section-Scoped Read Capability (#14472) 2026-07-28 07:38:37 -04:00
modelSpecs 🥷 feat: Add showInMenu Option to Model Specs (#14034) 2026-06-30 19:32:59 -04:00
oauth
projects
prompts 🧵 fix: Preserve Fenced Markdown Artifacts (#14121) 2026-07-05 12:04:59 -04:00
rum 📈 fix: Isolate RUM Telemetry Proxy Auth from App Auth (#13765) 2026-06-15 12:49:44 -04:00
shared-links 🔒 fix: Prevent Role Grants on Shared Links (#14137) 2026-07-07 07:04:52 -04:00
skills 🪆 fix: Serve Nested Skill Files Through a Wildcard Route (#14191) 2026-07-09 16:52:11 -04:00
storage 🪣 refactor: Rate-Limit Token Routes and Cap Remote File Downloads (#13978) 2026-06-26 12:19:03 -04:00
stream 🛑 feat: Preemptive Steer - Backend Interrupt & Steer (#14518) 2026-07-30 12:36:13 -04:00
telemetry perf: Reduce Agent Chat Startup Latency (#14423) 2026-07-25 07:58:20 -04:00
tools 🎯 feat: Per-Tool Intent Labels for Model Specs (#14526) 2026-07-30 13:32:04 -04:00
types 🛑 feat: Preemptive Steer - Backend Interrupt & Steer (#14518) 2026-07-30 12:36:13 -04:00
utils 🏷️ feat: Activity Groups With Fast-Model Headers (#14391) 2026-07-29 14:05:47 -04:00
web
index.ts 🔗 fix: Resolve MCP Tool-Key Boundary Against Configured Server Names (#14448) 2026-07-27 14:45:38 -04:00
telemetry.ts