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/keysendpoint - 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
userIdfield 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
userIdoverrides 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.

