mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00
🛡️ fix: Enhance File Upload Security & Error Handling (#4705)
* fix: sanitize filename in multer storage callback * fix: ensure temporary image upload file is deleted after processing * fix: prevent cleanup flag from being set to false before actually deleted * refactor: user avatar, typing, use 'file' for formData instead of 'input', add disk storage, use localization * fix: update Avatar component to include image dimensions in formData and refactor editor reference type * fix: refactor avatar upload handling to use fs for file reading and enhance file validation * fix: ensure temporary image upload file is deleted after processing * fix: refactor avatar upload routes and handlers for agents and assistants, improve file handling and validation * fix: improve audio file validation and cleanup * fix: add filename sanitization utility and integrate it into multer storage configuration * fix: update group project ID check for null and refactor delete prompt group response type * fix: invalid access control for deleting prompt groups * fix: add error handling and logging to checkBan middleware * fix: catch conversation parsing errors * chore: revert unnecessary height and width parameters from avatar upload * chore: update librechat-data-provider version to 0.7.55 * style: ensure KaTeX can spread across visible space
This commit is contained in:
parent
3c94ff2c04
commit
d012da0065
33 changed files with 373 additions and 186 deletions
|
@ -27,7 +27,12 @@ const buildFunction = {
|
|||
|
||||
async function buildEndpointOption(req, res, next) {
|
||||
const { endpoint, endpointType } = req.body;
|
||||
let parsedBody = parseCompactConvo({ endpoint, endpointType, conversation: req.body });
|
||||
let parsedBody;
|
||||
try {
|
||||
parsedBody = parseCompactConvo({ endpoint, endpointType, conversation: req.body });
|
||||
} catch (error) {
|
||||
return handleError(res, { text: 'Error parsing conversation' });
|
||||
}
|
||||
|
||||
if (req.app.locals.modelSpecs?.list && req.app.locals.modelSpecs?.enforce) {
|
||||
/** @type {{ list: TModelSpec[] }}*/
|
||||
|
@ -56,11 +61,15 @@ async function buildEndpointOption(req, res, next) {
|
|||
});
|
||||
}
|
||||
|
||||
parsedBody = parseCompactConvo({
|
||||
endpoint,
|
||||
endpointType,
|
||||
conversation: currentModelSpec.preset,
|
||||
});
|
||||
try {
|
||||
parsedBody = parseCompactConvo({
|
||||
endpoint,
|
||||
endpointType,
|
||||
conversation: currentModelSpec.preset,
|
||||
});
|
||||
} catch (error) {
|
||||
return handleError(res, { text: 'Error parsing model spec' });
|
||||
}
|
||||
}
|
||||
|
||||
const endpointFn = buildFunction[endpointType ?? endpoint];
|
||||
|
|
|
@ -6,6 +6,7 @@ const keyvMongo = require('~/cache/keyvMongo');
|
|||
const denyRequest = require('./denyRequest');
|
||||
const { getLogStores } = require('~/cache');
|
||||
const { findUser } = require('~/models');
|
||||
const { logger } = require('~/config');
|
||||
|
||||
const banCache = new Keyv({ store: keyvMongo, namespace: ViolationTypes.BAN, ttl: 0 });
|
||||
const message = 'Your account has been temporarily banned due to violations of our service.';
|
||||
|
@ -45,92 +46,96 @@ const banResponse = async (req, res) => {
|
|||
* @returns {Promise<function|Object>} - Returns a Promise which when resolved calls next middleware if user or source IP is not banned. Otherwise calls `banResponse()` and sets ban details in `banCache`.
|
||||
*/
|
||||
const checkBan = async (req, res, next = () => {}) => {
|
||||
const { BAN_VIOLATIONS } = process.env ?? {};
|
||||
try {
|
||||
const { BAN_VIOLATIONS } = process.env ?? {};
|
||||
|
||||
if (!isEnabled(BAN_VIOLATIONS)) {
|
||||
return next();
|
||||
}
|
||||
if (!isEnabled(BAN_VIOLATIONS)) {
|
||||
return next();
|
||||
}
|
||||
|
||||
req.ip = removePorts(req);
|
||||
let userId = req.user?.id ?? req.user?._id ?? null;
|
||||
req.ip = removePorts(req);
|
||||
let userId = req.user?.id ?? req.user?._id ?? null;
|
||||
|
||||
if (!userId && req?.body?.email) {
|
||||
const user = await findUser({ email: req.body.email }, '_id');
|
||||
userId = user?._id ? user._id.toString() : userId;
|
||||
}
|
||||
if (!userId && req?.body?.email) {
|
||||
const user = await findUser({ email: req.body.email }, '_id');
|
||||
userId = user?._id ? user._id.toString() : userId;
|
||||
}
|
||||
|
||||
if (!userId && !req.ip) {
|
||||
return next();
|
||||
}
|
||||
if (!userId && !req.ip) {
|
||||
return next();
|
||||
}
|
||||
|
||||
let cachedIPBan;
|
||||
let cachedUserBan;
|
||||
let cachedIPBan;
|
||||
let cachedUserBan;
|
||||
|
||||
let ipKey = '';
|
||||
let userKey = '';
|
||||
let ipKey = '';
|
||||
let userKey = '';
|
||||
|
||||
if (req.ip) {
|
||||
ipKey = isEnabled(process.env.USE_REDIS) ? `ban_cache:ip:${req.ip}` : req.ip;
|
||||
cachedIPBan = await banCache.get(ipKey);
|
||||
}
|
||||
if (req.ip) {
|
||||
ipKey = isEnabled(process.env.USE_REDIS) ? `ban_cache:ip:${req.ip}` : req.ip;
|
||||
cachedIPBan = await banCache.get(ipKey);
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
userKey = isEnabled(process.env.USE_REDIS) ? `ban_cache:user:${userId}` : userId;
|
||||
cachedUserBan = await banCache.get(userKey);
|
||||
}
|
||||
if (userId) {
|
||||
userKey = isEnabled(process.env.USE_REDIS) ? `ban_cache:user:${userId}` : userId;
|
||||
cachedUserBan = await banCache.get(userKey);
|
||||
}
|
||||
|
||||
const cachedBan = cachedIPBan || cachedUserBan;
|
||||
const cachedBan = cachedIPBan || cachedUserBan;
|
||||
|
||||
if (cachedBan) {
|
||||
req.banned = true;
|
||||
return await banResponse(req, res);
|
||||
}
|
||||
|
||||
const banLogs = getLogStores(ViolationTypes.BAN);
|
||||
const duration = banLogs.opts.ttl;
|
||||
|
||||
if (duration <= 0) {
|
||||
return next();
|
||||
}
|
||||
|
||||
let ipBan;
|
||||
let userBan;
|
||||
|
||||
if (req.ip) {
|
||||
ipBan = await banLogs.get(req.ip);
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
userBan = await banLogs.get(userId);
|
||||
}
|
||||
|
||||
const isBanned = !!(ipBan || userBan);
|
||||
|
||||
if (!isBanned) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const timeLeft = Number(isBanned.expiresAt) - Date.now();
|
||||
|
||||
if (timeLeft <= 0 && ipKey) {
|
||||
await banLogs.delete(ipKey);
|
||||
}
|
||||
|
||||
if (timeLeft <= 0 && userKey) {
|
||||
await banLogs.delete(userKey);
|
||||
return next();
|
||||
}
|
||||
|
||||
if (ipKey) {
|
||||
banCache.set(ipKey, isBanned, timeLeft);
|
||||
}
|
||||
|
||||
if (userKey) {
|
||||
banCache.set(userKey, isBanned, timeLeft);
|
||||
}
|
||||
|
||||
if (cachedBan) {
|
||||
req.banned = true;
|
||||
return await banResponse(req, res);
|
||||
} catch (error) {
|
||||
logger.error('Error in checkBan middleware:', error);
|
||||
}
|
||||
|
||||
const banLogs = getLogStores(ViolationTypes.BAN);
|
||||
const duration = banLogs.opts.ttl;
|
||||
|
||||
if (duration <= 0) {
|
||||
return next();
|
||||
}
|
||||
|
||||
let ipBan;
|
||||
let userBan;
|
||||
|
||||
if (req.ip) {
|
||||
ipBan = await banLogs.get(req.ip);
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
userBan = await banLogs.get(userId);
|
||||
}
|
||||
|
||||
const isBanned = !!(ipBan || userBan);
|
||||
|
||||
if (!isBanned) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const timeLeft = Number(isBanned.expiresAt) - Date.now();
|
||||
|
||||
if (timeLeft <= 0 && ipKey) {
|
||||
await banLogs.delete(ipKey);
|
||||
}
|
||||
|
||||
if (timeLeft <= 0 && userKey) {
|
||||
await banLogs.delete(userKey);
|
||||
return next();
|
||||
}
|
||||
|
||||
if (ipKey) {
|
||||
banCache.set(ipKey, isBanned, timeLeft);
|
||||
}
|
||||
|
||||
if (userKey) {
|
||||
banCache.set(userKey, isBanned, timeLeft);
|
||||
}
|
||||
|
||||
req.banned = true;
|
||||
return await banResponse(req, res);
|
||||
};
|
||||
|
||||
module.exports = checkBan;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue