forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛡️ feat: Model Validation Middleware (danny-avila#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
- Loading branch information
1 parent
3e7aa3e
commit fa26186
Showing
19 changed files
with
534 additions
and
372 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.