mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-07-05 07:47:26 +02:00
fix: use req.ip instead, since guests can upload as well
This commit is contained in:
committed by
Andrew Rodrigues
parent
a9978fcfd2
commit
ea22cd302a
@@ -4,7 +4,6 @@ const LRU = require('lru-cache');
|
||||
const meta = require('../meta');
|
||||
const helpers = require('./helpers');
|
||||
const user = require('../user');
|
||||
const controllerHelpers = require('../controllers/helpers');
|
||||
|
||||
const cache = new LRU({
|
||||
maxAge: meta.config.uploadRateLimitThreshold * 1000,
|
||||
@@ -13,20 +12,16 @@ const cache = new LRU({
|
||||
module.exports = function (middleware) {
|
||||
middleware.ratelimitUploads = helpers.try(async (req, res, next) => {
|
||||
const { uid } = req;
|
||||
if (!uid) {
|
||||
return controllerHelpers.notAllowed(req, res);
|
||||
}
|
||||
|
||||
if (!meta.config.uploadRateLimitThreshold || await user.isAdminOrGlobalMod(req.uid)) {
|
||||
if (!meta.config.uploadRateLimitThreshold || uid && await user.isAdminOrGlobalMod(uid)) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const count = (cache.peek(`${uid}:uploaded_file_count`) || 0) + req.files.files.length;
|
||||
const count = (cache.peek(`${req.ip}:uploaded_file_count`) || 0) + req.files.files.length;
|
||||
if (count > meta.config.uploadRateLimitThreshold) {
|
||||
return next(new Error(['[[error:upload-ratelimit-reached]]']));
|
||||
}
|
||||
|
||||
cache.set(`${uid}:uploaded_file_count`, count);
|
||||
cache.set(`${req.ip}:uploaded_file_count`, count);
|
||||
next();
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user