mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-21 21:50:49 +02:00

* 🔧 Overhauled caching feature:
- Refactored caching logic.
- Fixed redis prefix, namespace, tls, ttl, and cluster.
- Added REDIS_KEY_PREFIX_VAR
* # refactor: Rename redisCache to standardCache
* # Add Redis pinging mechanism to maintain connection.
* # docs: Add warning about Keyv Redis client prefix support
39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
const rateLimit = require('express-rate-limit');
|
|
const { ViolationTypes } = require('librechat-data-provider');
|
|
const { removePorts } = require('~/server/utils');
|
|
const { limiterCache } = require('~/cache/cacheFactory');
|
|
const { logViolation } = require('~/cache');
|
|
|
|
const {
|
|
RESET_PASSWORD_WINDOW = 2,
|
|
RESET_PASSWORD_MAX = 2,
|
|
RESET_PASSWORD_VIOLATION_SCORE: score,
|
|
} = process.env;
|
|
const windowMs = RESET_PASSWORD_WINDOW * 60 * 1000;
|
|
const max = RESET_PASSWORD_MAX;
|
|
const windowInMinutes = windowMs / 60000;
|
|
const message = `Too many attempts, please try again after ${windowInMinutes} minute(s)`;
|
|
|
|
const handler = async (req, res) => {
|
|
const type = ViolationTypes.RESET_PASSWORD_LIMIT;
|
|
const errorMessage = {
|
|
type,
|
|
max,
|
|
windowInMinutes,
|
|
};
|
|
|
|
await logViolation(req, res, type, errorMessage, score);
|
|
return res.status(429).json({ message });
|
|
};
|
|
|
|
const limiterOptions = {
|
|
windowMs,
|
|
max,
|
|
handler,
|
|
keyGenerator: removePorts,
|
|
store: limiterCache('reset_password_limiter'),
|
|
};
|
|
|
|
const resetPasswordLimiter = rateLimit(limiterOptions);
|
|
|
|
module.exports = resetPasswordLimiter;
|