e115934061
Publish `@librechat/data-schemas` to NPM / pack (push) Failing after 8s
Publish `@librechat/client` to NPM / pack (push) Failing after 0s
GitNexus Index / index (push) Failing after 1s
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Has been skipped
Sync Helm Chart Tags / Sync chart tags (push) Failing after 2s
Publish `librechat-data-provider` to NPM / pack (push) Failing after 1s
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Failing after 1s
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Failing after 0s
Sync Helm Chart Tags / Ignore non-main push (push) Has been skipped
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Failing after 1s
Publish `@librechat/client` to NPM / publish-npm (push) Has been cancelled
Publish `librechat-data-provider` to NPM / publish-npm (push) Has been cancelled
GitNexus Index / post-index (push) Has been cancelled
Publish `@librechat/data-schemas` to NPM / publish-npm (push) Has been cancelled
69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
const { handleError } = require('@librechat/api');
|
|
const { ViolationTypes } = require('librechat-data-provider');
|
|
const { getModelsConfig } = require('~/server/controllers/ModelController');
|
|
const { getEndpointsConfig } = require('~/server/services/Config');
|
|
const { logViolation } = require('~/cache');
|
|
|
|
const MAX_MODEL_STRING_LENGTH = 256;
|
|
const MODEL_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9_.:/@+-]*$/;
|
|
|
|
/**
|
|
* Validates the model of the request.
|
|
*
|
|
* @async
|
|
* @param {ServerRequest} 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 { endpoint } = req.body;
|
|
const rawModel = req.body.model;
|
|
|
|
if (!rawModel || typeof rawModel !== 'string') {
|
|
return handleError(res, { text: 'Model not provided' });
|
|
}
|
|
|
|
const model = rawModel.trim();
|
|
if (!model || model.length > MAX_MODEL_STRING_LENGTH || !MODEL_PATTERN.test(model)) {
|
|
return handleError(res, { text: 'Invalid model identifier' });
|
|
}
|
|
|
|
req.body.model = model;
|
|
|
|
const endpointsConfig = await getEndpointsConfig(req);
|
|
const endpointConfig = endpointsConfig?.[endpoint];
|
|
|
|
if (endpointConfig?.userProvide) {
|
|
return next();
|
|
}
|
|
|
|
const modelsConfig = await getModelsConfig(req);
|
|
|
|
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 (validModel) {
|
|
return next();
|
|
}
|
|
|
|
const { ILLEGAL_MODEL_REQ_SCORE: score = 1 } = 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;
|