mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
🛡️ feat: Model Validation Middleware (#1841)
* refactor: add ViolationTypes enum and add new violation for illegal model requests * feat: validateModel middleware to protect the backend against illicit requests for unlisted models
This commit is contained in:
parent
d8038e3b19
commit
a8a19c6caa
19 changed files with 539 additions and 377 deletions
50
api/server/middleware/validateModel.js
Normal file
50
api/server/middleware/validateModel.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
const { EModelEndpoint, CacheKeys, ViolationTypes } = require('librechat-data-provider');
|
||||
const { logViolation, getLogStores } = require('~/cache');
|
||||
const { handleError } = require('~/server/utils');
|
||||
|
||||
/**
|
||||
* Validates the model of the request.
|
||||
*
|
||||
* @async
|
||||
* @param {Express.Request} req - The Express request object.
|
||||
* @param {Express.Response} res - The Express response object.
|
||||
* @param {Function} next - The Express next function.
|
||||
*/
|
||||
const validateModel = async (req, res, next) => {
|
||||
const { model, endpoint } = req.body;
|
||||
if (!model) {
|
||||
return handleError(res, { text: 'Model not provided' });
|
||||
}
|
||||
|
||||
const cache = getLogStores(CacheKeys.CONFIG_STORE);
|
||||
const modelsConfig = await cache.get(CacheKeys.MODELS_CONFIG);
|
||||
if (!modelsConfig) {
|
||||
return handleError(res, { text: 'Models not loaded' });
|
||||
}
|
||||
|
||||
const availableModels = modelsConfig[endpoint];
|
||||
if (!availableModels) {
|
||||
return handleError(res, { text: 'Endpoint models not loaded' });
|
||||
}
|
||||
|
||||
let validModel = !!availableModels.find((availableModel) => availableModel === model);
|
||||
if (endpoint === EModelEndpoint.gptPlugins) {
|
||||
validModel = validModel && availableModels.includes(req.body.agentOptions?.model);
|
||||
}
|
||||
|
||||
if (validModel) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const { ILLEGAL_MODEL_REQ_SCORE: score = 5 } = process.env ?? {};
|
||||
|
||||
const type = ViolationTypes.ILLEGAL_MODEL_REQUEST;
|
||||
const errorMessage = {
|
||||
type,
|
||||
};
|
||||
|
||||
await logViolation(req, res, type, errorMessage, score);
|
||||
return handleError(res, { text: 'Illegal model request' });
|
||||
};
|
||||
|
||||
module.exports = validateModel;
|
||||
Loading…
Add table
Add a link
Reference in a new issue