2024-05-02 08:48:26 +02:00
|
|
|
const rateLimit = require('express-rate-limit');
|
|
|
|
const { ViolationTypes } = require('librechat-data-provider');
|
2025-07-15 16:24:31 -06:00
|
|
|
const { limiterCache } = require('~/cache/cacheFactory');
|
2024-05-02 08:48:26 +02:00
|
|
|
const logViolation = require('~/cache/logViolation');
|
|
|
|
|
|
|
|
const getEnvironmentVariables = () => {
|
|
|
|
const IMPORT_IP_MAX = parseInt(process.env.IMPORT_IP_MAX) || 100;
|
|
|
|
const IMPORT_IP_WINDOW = parseInt(process.env.IMPORT_IP_WINDOW) || 15;
|
|
|
|
const IMPORT_USER_MAX = parseInt(process.env.IMPORT_USER_MAX) || 50;
|
|
|
|
const IMPORT_USER_WINDOW = parseInt(process.env.IMPORT_USER_WINDOW) || 15;
|
⚖️ feat: Add Violation Scores (#8304)
- Introduced new violation scores for TTS, STT, Fork, Import, and File Upload actions in the .env.example file.
- Updated logViolation function to accept a score parameter, allowing for dynamic severity levels based on the action type.
- Modified limiters for Fork, Import, Message, STT, TTS, Tool Call, and File Upload to utilize the new violation scores when logging violations.
2025-07-07 17:08:40 -04:00
|
|
|
const IMPORT_VIOLATION_SCORE = process.env.IMPORT_VIOLATION_SCORE;
|
2024-05-02 08:48:26 +02:00
|
|
|
|
|
|
|
const importIpWindowMs = IMPORT_IP_WINDOW * 60 * 1000;
|
|
|
|
const importIpMax = IMPORT_IP_MAX;
|
|
|
|
const importIpWindowInMinutes = importIpWindowMs / 60000;
|
|
|
|
|
|
|
|
const importUserWindowMs = IMPORT_USER_WINDOW * 60 * 1000;
|
|
|
|
const importUserMax = IMPORT_USER_MAX;
|
|
|
|
const importUserWindowInMinutes = importUserWindowMs / 60000;
|
|
|
|
|
|
|
|
return {
|
|
|
|
importIpWindowMs,
|
|
|
|
importIpMax,
|
|
|
|
importIpWindowInMinutes,
|
|
|
|
importUserWindowMs,
|
|
|
|
importUserMax,
|
|
|
|
importUserWindowInMinutes,
|
⚖️ feat: Add Violation Scores (#8304)
- Introduced new violation scores for TTS, STT, Fork, Import, and File Upload actions in the .env.example file.
- Updated logViolation function to accept a score parameter, allowing for dynamic severity levels based on the action type.
- Modified limiters for Fork, Import, Message, STT, TTS, Tool Call, and File Upload to utilize the new violation scores when logging violations.
2025-07-07 17:08:40 -04:00
|
|
|
importViolationScore: IMPORT_VIOLATION_SCORE,
|
2024-05-02 08:48:26 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const createImportHandler = (ip = true) => {
|
⚖️ feat: Add Violation Scores (#8304)
- Introduced new violation scores for TTS, STT, Fork, Import, and File Upload actions in the .env.example file.
- Updated logViolation function to accept a score parameter, allowing for dynamic severity levels based on the action type.
- Modified limiters for Fork, Import, Message, STT, TTS, Tool Call, and File Upload to utilize the new violation scores when logging violations.
2025-07-07 17:08:40 -04:00
|
|
|
const {
|
|
|
|
importIpMax,
|
|
|
|
importUserMax,
|
|
|
|
importViolationScore,
|
|
|
|
importIpWindowInMinutes,
|
|
|
|
importUserWindowInMinutes,
|
|
|
|
} = getEnvironmentVariables();
|
2024-05-02 08:48:26 +02:00
|
|
|
|
|
|
|
return async (req, res) => {
|
|
|
|
const type = ViolationTypes.FILE_UPLOAD_LIMIT;
|
|
|
|
const errorMessage = {
|
|
|
|
type,
|
|
|
|
max: ip ? importIpMax : importUserMax,
|
|
|
|
limiter: ip ? 'ip' : 'user',
|
|
|
|
windowInMinutes: ip ? importIpWindowInMinutes : importUserWindowInMinutes,
|
|
|
|
};
|
|
|
|
|
⚖️ feat: Add Violation Scores (#8304)
- Introduced new violation scores for TTS, STT, Fork, Import, and File Upload actions in the .env.example file.
- Updated logViolation function to accept a score parameter, allowing for dynamic severity levels based on the action type.
- Modified limiters for Fork, Import, Message, STT, TTS, Tool Call, and File Upload to utilize the new violation scores when logging violations.
2025-07-07 17:08:40 -04:00
|
|
|
await logViolation(req, res, type, errorMessage, importViolationScore);
|
2024-05-02 08:48:26 +02:00
|
|
|
res.status(429).json({ message: 'Too many conversation import requests. Try again later' });
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const createImportLimiters = () => {
|
|
|
|
const { importIpWindowMs, importIpMax, importUserWindowMs, importUserMax } =
|
|
|
|
getEnvironmentVariables();
|
|
|
|
|
2025-03-21 14:14:45 -04:00
|
|
|
const ipLimiterOptions = {
|
2024-05-02 08:48:26 +02:00
|
|
|
windowMs: importIpWindowMs,
|
|
|
|
max: importIpMax,
|
|
|
|
handler: createImportHandler(),
|
2025-07-15 16:24:31 -06:00
|
|
|
store: limiterCache('import_ip_limiter'),
|
2025-03-21 14:14:45 -04:00
|
|
|
};
|
|
|
|
const userLimiterOptions = {
|
2024-05-02 08:48:26 +02:00
|
|
|
windowMs: importUserWindowMs,
|
|
|
|
max: importUserMax,
|
|
|
|
handler: createImportHandler(false),
|
|
|
|
keyGenerator: function (req) {
|
|
|
|
return req.user?.id; // Use the user ID or NULL if not available
|
|
|
|
},
|
2025-07-15 16:24:31 -06:00
|
|
|
store: limiterCache('import_user_limiter'),
|
2025-03-21 14:14:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const importIpLimiter = rateLimit(ipLimiterOptions);
|
|
|
|
const importUserLimiter = rateLimit(userLimiterOptions);
|
2024-05-02 08:48:26 +02:00
|
|
|
return { importIpLimiter, importUserLimiter };
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { createImportLimiters };
|