Skip to content

🔑 fix: Stringify Ban Cache Keys When Redis Is Off - #16039

Merged
danny-avila merged 3 commits into
LibreChat-AI:devfrom
gokay-ai:cursor/fix-ban-cache-string-keys-dcca
Sep 19, 2026
Merged

danny-avila merged 3 commits into
LibreChat-AI:devfrom
gokay-ai:cursor/fix-ban-cache-string-keys-dcca

Conversation

@gokay-ai

@gokay-ai gokay-ai commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

checkBan was passing a Mongo ObjectId into Keyv when Redis is off. keyv 5.6.0 (v0.8.7 shipped 5.5.2) calls key.startsWith while namespacing a key, so the lookup threw key.startsWith is not a function. On the OAuth callback req.user is a lean document with an ObjectId _id and no id, and checkBan runs before the session cookies are set, so every social login failed with a 500 whenever BAN_VIOLATIONS=true (the default). Ban and violation keys are now always strings.

Fixes #16025

Thanks for the report.

How it works

The id is converted once, where a non-string can enter: the _id fallback taken when req.user is a lean document. req.user.id is already a string on every path that sets it (the JWT strategies, Mongoose's id virtual, the 2FA limiter's temp-token id), and the email lookup already calls toString(), so getBanCacheKey is unchanged.

 // api/server/middleware/checkBan.js
-    let userId = req.user?.id ?? req.user?._id ?? null;
+    let userId = req.user?.id ?? req.user?._id?.toString() ?? null;

logViolation built its keys the same way (raw req.user.id ?? req.user._id when Redis is off) and passes that id to banViolation, which writes the ban record. With the same change, the violation count, the general log and the ban record all use the string key that checkBan reads:

 // api/cache/logViolation.js
-  const userId = req.user?.id ?? req.user?._id;
+  const userId = req.user?.id ?? req.user?._id?.toString();

Change Type

  • Bug fix (non-breaking change which fixes an issue)

Testing

  • api/test/server/middleware/checkBan.test.js: ObjectId and numeric user keys on both Redis and non-Redis paths, including the email-lookup case (mocked Keyv).
  • api/server/middleware/checkBan.spec.js: drives checkBan and logViolation through real namespaced Keyv instances (the Mongo-backed ban store on mongodb-memory-server, in-memory stores for the file-backed violation logs) with an ObjectId-only OAuth user. An unbanned user passes, and a ban logged from one address is enforced for the same user from another. With either fix removed, the spec fails with the reported key.startsWith is not a function.
cd api && npx jest server/middleware/checkBan.spec.js test/server/middleware/checkBan.test.js cache/banViolation.spec.js server/middleware/limiters

7 suites, 70 tests passed.

Checklist

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • My changes do not introduce new warnings
  • I have written tests demonstrating that my changes are effective or that my feature works
  • Local unit tests pass with my changes

Keyv/Mongo throws when checkBan passes a raw ObjectId as the cache key
with Redis disabled, which 500s every social OAuth login.

Fixes LibreChat-AI#16025

Signed-off-by: GokayAI <[email protected]>
Copilot AI lite review requested due to automatic review settings September 17, 2026 06:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

logViolation keyed its violation and ban logs by the raw
`req.user.id ?? req.user._id`, the same shape checkBan used, so a lean
user document would hit the same keyv 5.6 `key.startsWith` TypeError on
the namespaced violation stores and record its ban under an ObjectId.

The new spec drives checkBan and logViolation through real namespaced
Keyv instances (Mongo-backed ban store, in-memory violation logs) with
an ObjectId-only OAuth user: an unbanned user passes, and a ban logged
from one address is enforced for the same user from another.
The id is converted once, where a non-string can enter: the lean
document's `_id` fallback. `req.user.id` is already a string on every
path that sets it (the JWT strategies, Mongoose's `id` virtual, the 2FA
limiter's temp-token id), and the email lookup already stringifies, so
`getBanCacheKey` and that line go back to their dev form.
@danny-avila
danny-avila merged commit f80f0be into LibreChat-AI:dev Sep 19, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: checkBan throws "key.startsWith is not a function" and answers 500 on /oauth when USE_REDIS is off

3 participants