Repository navigation
🔑 fix: Stringify Ban Cache Keys When Redis Is Off - #16039
Merged
danny-avila merged 3 commits intoSep 19, 2026
Merged
danny-avila merged 3 commits into
danny-avila merged 3 commits into
Conversation
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]>
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
checkBanwas passing a Mongo ObjectId into Keyv when Redis is off. keyv 5.6.0 (v0.8.7 shipped 5.5.2) callskey.startsWithwhile namespacing a key, so the lookup threwkey.startsWith is not a function. On the OAuth callbackreq.useris a lean document with an ObjectId_idand noid, andcheckBanruns before the session cookies are set, so every social login failed with a 500 wheneverBAN_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
_idfallback taken whenreq.useris a lean document.req.user.idis already a string on every path that sets it (the JWT strategies, Mongoose'sidvirtual, the 2FA limiter's temp-token id), and the email lookup already callstoString(), sogetBanCacheKeyis unchanged.logViolationbuilt its keys the same way (rawreq.user.id ?? req.user._idwhen Redis is off) and passes that id tobanViolation, which writes the ban record. With the same change, the violation count, the general log and the ban record all use the string key thatcheckBanreads:Change Type
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: drivescheckBanandlogViolationthrough real namespaced Keyv instances (the Mongo-backed ban store onmongodb-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 reportedkey.startsWith is not a function.7 suites, 70 tests passed.
Checklist