2024-05-30 18:39:21 -04:00
|
|
|
const rateLimit = require('express-rate-limit');
|
2025-10-02 07:33:58 -06:00
|
|
|
const { limiterCache } = require('@librechat/api');
|
2024-05-30 18:39:21 -04:00
|
|
|
const { ViolationTypes } = require('librechat-data-provider');
|
|
|
|
|
const logViolation = require('~/cache/logViolation');
|
|
|
|
|
|
|
|
|
|
const getEnvironmentVariables = () => {
|
|
|
|
|
const STT_IP_MAX = parseInt(process.env.STT_IP_MAX) || 100;
|
|
|
|
|
const STT_IP_WINDOW = parseInt(process.env.STT_IP_WINDOW) || 1;
|
|
|
|
|
const STT_USER_MAX = parseInt(process.env.STT_USER_MAX) || 50;
|
|
|
|
|
const STT_USER_WINDOW = parseInt(process.env.STT_USER_WINDOW) || 1;
|
⚖️ 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 STT_VIOLATION_SCORE = process.env.STT_VIOLATION_SCORE;
|
2024-05-30 18:39:21 -04:00
|
|
|
|
|
|
|
|
const sttIpWindowMs = STT_IP_WINDOW * 60 * 1000;
|
|
|
|
|
const sttIpMax = STT_IP_MAX;
|
|
|
|
|
const sttIpWindowInMinutes = sttIpWindowMs / 60000;
|
|
|
|
|
|
|
|
|
|
const sttUserWindowMs = STT_USER_WINDOW * 60 * 1000;
|
|
|
|
|
const sttUserMax = STT_USER_MAX;
|
|
|
|
|
const sttUserWindowInMinutes = sttUserWindowMs / 60000;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
sttIpWindowMs,
|
|
|
|
|
sttIpMax,
|
|
|
|
|
sttIpWindowInMinutes,
|
|
|
|
|
sttUserWindowMs,
|
|
|
|
|
sttUserMax,
|
|
|
|
|
sttUserWindowInMinutes,
|
⚖️ 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
|
|
|
sttViolationScore: STT_VIOLATION_SCORE,
|
2024-05-30 18:39:21 -04:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createSTTHandler = (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 { sttIpMax, sttIpWindowInMinutes, sttUserMax, sttUserWindowInMinutes, sttViolationScore } =
|
2024-05-30 18:39:21 -04:00
|
|
|
getEnvironmentVariables();
|
|
|
|
|
|
|
|
|
|
return async (req, res) => {
|
|
|
|
|
const type = ViolationTypes.STT_LIMIT;
|
|
|
|
|
const errorMessage = {
|
|
|
|
|
type,
|
|
|
|
|
max: ip ? sttIpMax : sttUserMax,
|
|
|
|
|
limiter: ip ? 'ip' : 'user',
|
|
|
|
|
windowInMinutes: ip ? sttIpWindowInMinutes : sttUserWindowInMinutes,
|
|
|
|
|
};
|
|
|
|
|
|
⚖️ 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, sttViolationScore);
|
2024-05-30 18:39:21 -04:00
|
|
|
res.status(429).json({ message: 'Too many STT requests. Try again later' });
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createSTTLimiters = () => {
|
|
|
|
|
const { sttIpWindowMs, sttIpMax, sttUserWindowMs, sttUserMax } = getEnvironmentVariables();
|
|
|
|
|
|
2025-03-21 14:14:45 -04:00
|
|
|
const ipLimiterOptions = {
|
2024-05-30 18:39:21 -04:00
|
|
|
windowMs: sttIpWindowMs,
|
|
|
|
|
max: sttIpMax,
|
|
|
|
|
handler: createSTTHandler(),
|
2025-07-15 16:24:31 -06:00
|
|
|
store: limiterCache('stt_ip_limiter'),
|
2025-03-21 14:14:45 -04:00
|
|
|
};
|
2024-05-30 18:39:21 -04:00
|
|
|
|
2025-03-21 14:14:45 -04:00
|
|
|
const userLimiterOptions = {
|
2024-05-30 18:39:21 -04:00
|
|
|
windowMs: sttUserWindowMs,
|
|
|
|
|
max: sttUserMax,
|
|
|
|
|
handler: createSTTHandler(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('stt_user_limiter'),
|
2025-03-21 14:14:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sttIpLimiter = rateLimit(ipLimiterOptions);
|
|
|
|
|
const sttUserLimiter = rateLimit(userLimiterOptions);
|
2024-05-30 18:39:21 -04:00
|
|
|
|
|
|
|
|
return { sttIpLimiter, sttUserLimiter };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = createSTTLimiters;
|