The handler is one expression
LibreChat is a self-hostable ChatGPT-style UI. Users register accounts on whatever instance they signed up for, then enter their own API keys for upstream providers: OpenAI, Anthropic, Google, others. The keys are stored encrypted, scoped per-user, and read back when the user makes a chat request. The endpoint that writes a user's stored key is PUT /api/keys. The CVE record for the bug in that handler is CVE-2026-31942. Pre-patch, the entire handler is four lines.
router.put('/', requireJwtAuth, async (req, res) => {
await updateUserKey({ userId: req.user.id, ...req.body });
res.status(201).send();
});
requireJwtAuth is the authentication middleware. It validates the bearer token and sets req.user. By the time the handler body runs, req.user.id is the MongoDB ObjectId of the caller, signed and verified.
The author wrote userId: req.user.id first. Read in source order, the intent is unambiguous: the userId we are about to store under is the authenticated caller. Then the author wrote ...req.body. Read in source order, the intent is to merge in whatever name, value, and expiresAt the caller provided.
Object spread in a literal does not honor the author's intent. It honors the runtime semantics of the operator. Later keys overwrite earlier keys.
const merged = { userId: 'server-pinned', ...{ userId: 'attacker-controlled', name: 'openAI' } };
// merged.userId === 'attacker-controlled'
That is the bug. There is no race. There is no timing window. There is no chain. There is one expression, evaluated left-to-right, and the right side overrides the left.
The exploit primitive is one request
The attacker registers an account on the LibreChat instance. They authenticate normally. They obtain a valid JWT, signed by the instance for their own user.
They then send:
PUT /api/keys HTTP/1.1
Host: librechat.example.com
Authorization: Bearer <attacker's valid JWT>
Content-Type: application/json
{
"userId": "<victim's MongoDB ObjectId>",
"name": "openAI",
"value": "sk-attacker-controlled-key",
"expiresAt": "2099-12-31"
}
requireJwtAuth validates the attacker's token. req.user.id is the attacker's own ObjectId. The handler runs. The object literal evaluates to:
{
userId: '<victim>', // the body's userId wins
name: 'openAI',
value: 'sk-attacker-controlled-key',
expiresAt: '2099-12-31'
}
updateUserKey destructures the four expected fields from its argument. It encrypts the value with the server's static AES key. It upserts on { userId, name }, replacing the victim's stored OpenAI key with the attacker's.
The next time the victim opens LibreChat and sends a message to OpenAI, the LibreChat server fetches the stored key for (victim, openAI), decrypts it with CREDS_KEY, and uses it to authenticate to OpenAI. The decrypted key is the attacker's. The victim's prompt and the model's completion are now traffic on the attacker's OpenAI account.
The victim sees a working chat. The attacker sees a dashboard.
The CVSS scored the wrong asset
The CVE record assigns CVE-2026-31942 the vector CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:L. No confidentiality impact, high integrity impact, low availability impact. The reasoning is that the stored key value is encrypted at rest with CREDS_KEY, a server-wide environment secret, and the attacker cannot read the existing ciphertext.
The reasoning is correct about the stored credential. It is not the credential that gets compromised. The asset that gets compromised is every prompt the victim sends after the overwrite, because the attacker's upstream provider account now sits in the middle of the victim's chats. C:N is the score for "we cannot read the victim's prior key." It is not the score for "we cannot read the victim's traffic after the overwrite."
The integrity reading the score does name is the rest of the menu. The attacker can write garbage as the value, breaking the victim's chat entirely until the victim notices and re-enters their key. The attacker can backdate expiresAt, silently expiring the victim's working key. The attacker can iterate through every provider name the victim might have keys for and do this to all of them at once.
MongoDB ObjectIds are enumerable. The first four bytes are a Unix timestamp; the next five are per-process randomness that stays stable across a deployment; the last three are an incrementing counter. An attacker on a registration-open instance can register their own account, read the issued ObjectId from their own session, and grind from there. There is no entropy gate on userId. The route hands the merge whatever userId the body says, and the body is in the attacker's hand.
The fix is to stop spreading
The patch (commit b8c31e73, merged 2026-02-12, shipped in v0.8.3-rc1) is six lines.
router.put('/', requireJwtAuth, async (req, res) => {
- await updateUserKey({ userId: req.user.id, ...req.body });
+ if (req.body == null || typeof req.body !== 'object') {
+ return res.status(400).send({ error: 'Invalid request body.' });
+ }
+ const { name, value, expiresAt } = req.body;
+ await updateUserKey({ userId: req.user.id, name, value, expiresAt });
res.status(201).send();
});
The fix does not add a security check. The fix removes the spread.
The destructure enumerates the three fields the handler accepts from the body: name, value, expiresAt. Everything else in req.body is dropped on the floor of the function. userId is no longer reachable from the body because the handler no longer reads it from the body. The pin holds because there is nothing left to overwrite it.
The same commit added a test file. One of its tests is named, verbatim:
it('should not allow userId override via request body (IDOR prevention)', async () => {
updateUserKey.mockResolvedValue({});
const response = await request(app).put('/api/keys').send({
userId: 'attacker-injected-id',
name: 'openAI',
value: 'sk-attacker-key',
});
expect(response.status).toBe(201);
expect(updateUserKey).toHaveBeenCalledWith({
userId: 'test-user-123',
name: 'openAI',
value: 'sk-attacker-key',
expiresAt: undefined,
});
});
The test exists because the maintainer understood what the bug was. The maintainer's commit title also understood: 🔱 chore: Harden API Routes Against IDOR and DoS Attacks. The CVE record for CVE-2026-31942 says IDOR. The CWE assigned is 639, Authorization Bypass Through User-Controlled Key.
None of those labels describe what actually happened in this code. The user-controlled key was not in the URL. It was in the request body. The route's own code put it there.
Pin then spread
Mass assignment is a thirty-year-old bug class. Rails introduced attr_protected in 2005 and strong_params in 2013 because every framework that lets attacker-controlled JSON merge directly into a server model lets the attacker write any attribute they can name. Express, which LibreChat uses, has no equivalent gate. The default Express request body is a plain object, and the default JavaScript way to combine it with another object is the spread operator.
The bug recurs because the spread operator reads as assignment. { userId: req.user.id, ...req.body } reads, to a hurried reviewer, as "set userId from auth, then add whatever else the body has." It evaluates to "left-to-right, last key wins, and req.body.userId is a key like any other." There is no warning. The TypeScript signature on updateUserKey says userId: string. Both the pin and the override satisfy string. The type system is silent.
The same codebase contains the correct ordering in api/server/routes/messages.js. The handler that saves a chat message writes:
const message = { ...req.body, conversationId: req.params.conversationId };
// ...
const savedMessage = await db.saveMessage(
reqCtx,
{ ...message, user: req.user.id },
{ context: 'POST /api/messages/:conversationId' },
);
Spread first, pin last. conversationId overrides anything in the body called conversationId. user overrides anything in message called user. The pin survives because the pin runs last. The author of these lines understood the rule.
The author of keys.js understood it too, or thought they did, and put the pin where it reads first instead of where it runs last. Three keystrokes of source-order separate one route from the other. The maintainer's patch fixed the route the CVE named. The pattern survives wherever the next handler builds an updateX payload by spreading req.body after a pin instead of before.
The pattern this CVE belongs to is pin-then-spread: a handler pins a server-derived authoritative field at the head of an object literal and immediately spreads attacker-controlled input over the same object. The pin reads to code review as enforcement. The runtime applies it as a default.
PoC: @offseq on infosec.exchange
The pin came first. The spread came last. Last wins.