/

CVE-2026-31942

Severity:

HIGH RISK

(7.1)

Broken Access Control via IDOR (CWE-862) in LibreChat API Key Management

CVE-2026-31942

Analyzed

Vulnerability type

Category:

Broken Access Control / IDOR

CWE:

CWE-862

Impact:

Cross-account data and configuration tampering

Severity:

HIGH RISK

(7.1)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

LibreChat versions up to and including 0.7.6 contain an Insecure Direct Object Reference (IDOR) in the API key management endpoint that allows any authenticated user to modify another user's API key configuration.

By injecting a userId field into the PUT /api/keys request body, an attacker can overwrite victims' AI provider keys, potentially redirecting their conversations through attacker-controlled keys or causing denial of service with invalid credentials.

Affected context

This vulnerability arises in LibreChat deployments that:

  • Expose the API key management functionality via the PUT /api/keys endpoint
  • Associate API key records with a userId field
  • Trust client-supplied userId values in the request body when updating stored keys
  • Rely on application-layer logic (rather than server-side ownership checks) to bind changes to the authenticated user

It becomes relevant wherever multiple users share a LibreChat instance and can authenticate to manage their own provider configurations.

Exploit preconditions

  • Attack surface: HTTP API endpoint PUT /api/keys
  • Required access: Any authenticated LibreChat account (no special roles or admin rights necessary)
  • Trigger point: Sending a crafted JSON body that includes a userId field targeting another user's identifier
  • Why exploitation is possible: The server constructs the update payload by first setting the authenticated user's ID, then spreading the client-provided object over it. Because the spread operator is applied after setting userId, a malicious client-supplied userId overrides the server-assigned value, bypassing ownership checks and enabling modification of arbitrary users' API key records.

Why this happens

The core defect is a broken access control pattern where authorization decisions rely on client-controlled data rather than a server-enforced ownership check.

In the handler for PUT /api/keys, the code builds an update object that includes userId from the authenticated session, but then uses the JavaScript object spread operator ({ userId: authUserId, ...req.body }). This ordering allows any userId property present in the request body to overwrite the server-derived userId, effectively decoupling the operation from the caller's identity.

This maps to CWE-862: Missing Authorization and the more specific IDOR pattern: object identifiers (userId) are taken from the client without a robust check that the authenticated user is permitted to operate on the referenced resource. The invisible nature of this bug—caused by object spread semantics—makes it easy to introduce when composing request payloads without explicit whitelisting or post-merge validation.

Potential impact

  • Attacker may overwrite another user's API keys for providers such as OpenAI, Anthropic, Azure, etc.
  • Attacker could reroute a victim's conversations through attacker-controlled provider keys, potentially exposing prompts and responses via the attacker's billing account or logging systems.
  • Attacker can cause denial of service for the victim by replacing valid API keys with invalid or expired credentials.
  • Attacker might induce unexpected billing behavior by forcing use of high-cost models or providers under an attacker-selected configuration.
  • In multi-tenant or shared environments, widespread key tampering could degrade trust in the platform and complicate incident response.

General mitigation guidance

Effective mitigation focuses on tightening server-side authorization and eliminating reliance on client-controlled identifiers when applying security-sensitive updates.

Recommended defensive patterns include:

  • Server-enforced ownership: Derive the acting user's identity exclusively from the authenticated session or token, and apply it after processing any client input. Avoid accepting userId (or similar ownership fields) from the client for operations bound to the caller's account.
  • Whitelisting allowed fields: Explicitly construct update objects from a vetted set of fields, excluding identifiers and ownership metadata from client influence.
  • Post-merge validation: If object spreading or generic merging is used, perform a final integrity check ensuring that fields such as userId, accountId, or tenantId match the authenticated context and cannot be overridden.
  • Layered access control checks: Implement central authorization checks that verify the caller's right to modify the targeted resource (e.g., "does this user own these API keys?") before committing changes.
  • Principle of least privilege: Ensure that ordinary users can only modify resources in their own scope and restrict administrative capabilities to dedicated flows with stronger controls and auditing.

How teams detect issues like this

Identifying and monitoring for this class of vulnerability generally involves a mix of code analysis, testing, and runtime observation:

  • Static analysis and code scanning:
  • Flag patterns where ownership or identity fields (userId, accountId, tenantId) are taken from req.body, query parameters, or URL segments without a corresponding server-side authorization check.
  • Detect use of generic object merging or spread operations in request handlers that combine server-derived and client-supplied objects, particularly where sensitive fields may be overridden.
  • Security-focused code review:
  • Review all endpoints that modify user-specific resources (API keys, settings, profiles) to ensure they derive the target user from the authenticated context, not from client input.
  • Check that resource access decisions are centralized and not left to implicit assumptions based on incoming data.
  • Dynamic testing and fuzzing:
  • During QA or security testing, attempt to modify objects belonging to other users by tampering with IDs or ownership fields in API requests.
  • Use automated test suites that verify "same user" invariants for modify and delete operations across key endpoints.
  • Runtime monitoring and logging:
  • Log changes to security- and billing-sensitive resources, including which authenticated user initiated the change and which userId the change affected.
  • Set up anomaly detection or alerting when a single user repeatedly attempts to modify resources associated with many different user IDs.

Sources

SourceLink
github.comgithub.com/danny-avila/LibreChat/security/advisories/GHSA-5jcj-rh68-cgj7

Frequently asked

questions

It is remotely exploitable over HTTP(S) as long as the attacker can authenticate to the LibreChat instance. No special network position is required beyond normal client access to the service.

No. Any authenticated user can exploit this vulnerability. The flaw lies in how the endpoint trusts the userId in the request body, not in role-based privileges.

Ensure that all sensitive operations derive the target user or resource ownership from the authenticated session or token, never from client input. Avoid allowing userId or similar identifiers to be influenced by the request body, and add explicit authorization checks that confirm the caller is allowed to modify the targeted resource.

Adopt a consistent access control model where identifiers are server-controlled, centralize authorization logic, and enforce secure coding guidelines that forbid trusting client-supplied ownership fields. Complement this with automated tests that attempt cross-user access, static analysis checks for IDOR patterns, and mandatory security review for endpoints that handle user-specific configurations or secrets.

Find out what's already
exploitable in your codebase.