mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

* fix: ReDoS in ChatGPT Import * ci: should correctly process citations from real ChatGPT data * ci: Add ReDoS vulnerability test for processAssistantMessage * refactor: Update thread management and citation handling * refactor(validateImageRequest): robust validation * refactor(Prompt.js): update name search regex to escape special characters * refactor(Preset): exclude user from preset update to prevent mass assignment * refactor(files.js): Improve file deletion process * ci: updated validateImageRequest.spec.js * a11y: plugin pagination * refactor(CreatePromptForm.tsx): Improve input field styling * chore(Prompts): typing and accessibility * fix: prompt creation access role check * chore: remove duplicate jsdocs
69 lines
2 KiB
JavaScript
69 lines
2 KiB
JavaScript
const cookies = require('cookie');
|
|
const jwt = require('jsonwebtoken');
|
|
const { logger } = require('~/config');
|
|
|
|
const OBJECT_ID_LENGTH = 24;
|
|
const OBJECT_ID_PATTERN = /^[0-9a-f]{24}$/i;
|
|
|
|
/**
|
|
* Validates if a string is a valid MongoDB ObjectId
|
|
* @param {string} id - String to validate
|
|
* @returns {boolean} - Whether string is a valid ObjectId format
|
|
*/
|
|
function isValidObjectId(id) {
|
|
if (typeof id !== 'string') {
|
|
return false;
|
|
}
|
|
if (id.length !== OBJECT_ID_LENGTH) {
|
|
return false;
|
|
}
|
|
return OBJECT_ID_PATTERN.test(id);
|
|
}
|
|
|
|
/**
|
|
* Middleware to validate image request.
|
|
* Must be set by `secureImageLinks` via custom config file.
|
|
*/
|
|
function validateImageRequest(req, res, next) {
|
|
if (!req.app.locals.secureImageLinks) {
|
|
return next();
|
|
}
|
|
|
|
const refreshToken = req.headers.cookie ? cookies.parse(req.headers.cookie).refreshToken : null;
|
|
if (!refreshToken) {
|
|
logger.warn('[validateImageRequest] Refresh token not provided');
|
|
return res.status(401).send('Unauthorized');
|
|
}
|
|
|
|
let payload;
|
|
try {
|
|
payload = jwt.verify(refreshToken, process.env.JWT_REFRESH_SECRET);
|
|
} catch (err) {
|
|
logger.warn('[validateImageRequest]', err);
|
|
return res.status(403).send('Access Denied');
|
|
}
|
|
|
|
if (!isValidObjectId(payload.id)) {
|
|
logger.warn('[validateImageRequest] Invalid User ID');
|
|
return res.status(403).send('Access Denied');
|
|
}
|
|
|
|
const currentTimeInSeconds = Math.floor(Date.now() / 1000);
|
|
if (payload.exp < currentTimeInSeconds) {
|
|
logger.warn('[validateImageRequest] Refresh token expired');
|
|
return res.status(403).send('Access Denied');
|
|
}
|
|
|
|
const fullPath = decodeURIComponent(req.originalUrl);
|
|
const pathPattern = new RegExp(`^/images/${payload.id}/[^/]+$`);
|
|
|
|
if (pathPattern.test(fullPath)) {
|
|
logger.debug('[validateImageRequest] Image request validated');
|
|
next();
|
|
} else {
|
|
logger.warn('[validateImageRequest] Invalid image path');
|
|
res.status(403).send('Access Denied');
|
|
}
|
|
}
|
|
|
|
module.exports = validateImageRequest;
|