mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 21:08:17 +00:00
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
* 🔑 fix: Stringify Ban Cache Keys When Redis Is Off Keyv/Mongo throws when checkBan passes a raw ObjectId as the cache key with Redis disabled, which 500s every social OAuth login. Fixes #16025 Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> * 🔑 fix: Stringify Violation Log Keys and Cover Bans With Real Keyv 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. * 🎯 refactor: Stringify Only the ObjectId Fallback for Ban Keys 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. --------- Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> Co-authored-by: Danny Avila <danny@librechat.ai>
40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
const { isEnabled } = require('@librechat/api');
|
|
const { ViolationTypes } = require('librechat-data-provider');
|
|
const getLogStores = require('./getLogStores');
|
|
const banViolation = require('./banViolation');
|
|
|
|
/**
|
|
* Logs the violation.
|
|
*
|
|
* @param {Object} req - Express request object containing user information.
|
|
* @param {Object} res - Express response object.
|
|
* @param {string} type - The type of violation.
|
|
* @param {Object} errorMessage - The error message to log.
|
|
* @param {number | string} [score=1] - The severity of the violation. Defaults to 1
|
|
*/
|
|
const logViolation = async (req, res, type, errorMessage, score = 1) => {
|
|
const userId = req.user?.id ?? req.user?._id?.toString();
|
|
if (!userId) {
|
|
return;
|
|
}
|
|
const logs = getLogStores(ViolationTypes.GENERAL);
|
|
const violationLogs = getLogStores(type);
|
|
const key = isEnabled(process.env.USE_REDIS) ? `${type}:${userId}` : userId;
|
|
|
|
const userViolations = (await violationLogs.get(key)) ?? 0;
|
|
const violationCount = +userViolations + +score;
|
|
await violationLogs.set(key, violationCount);
|
|
|
|
errorMessage.user_id = userId;
|
|
errorMessage.prev_count = userViolations;
|
|
errorMessage.violation_count = violationCount;
|
|
errorMessage.date = new Date().toISOString();
|
|
|
|
await banViolation(req, res, errorMessage);
|
|
const userLogs = (await logs.get(key)) ?? [];
|
|
userLogs.push(errorMessage);
|
|
delete errorMessage.user_id;
|
|
await logs.set(key, userLogs);
|
|
};
|
|
|
|
module.exports = logViolation;
|