2022-10-05 08:21:34 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const utils = require('@strapi/utils');
|
2022-11-02 18:49:05 +01:00
|
|
|
const { toLower } = require('lodash/fp');
|
2022-10-05 08:21:34 -07:00
|
|
|
|
|
|
|
const { RateLimitError } = utils.errors;
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
(config, { strapi }) =>
|
|
|
|
async (ctx, next) => {
|
2022-12-16 10:42:28 -07:00
|
|
|
let ratelimitConfig = strapi.config.get('admin.ratelimit');
|
|
|
|
|
2022-12-19 11:40:37 -07:00
|
|
|
if (!ratelimitConfig || !has('enabled', ratelimitConfig)) {
|
2022-12-16 10:42:28 -07:00
|
|
|
ratelimitConfig = {
|
|
|
|
enabled: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ratelimitConfig.enabled === true) {
|
|
|
|
const ratelimit = require('koa2-ratelimit').RateLimit;
|
|
|
|
|
|
|
|
const userEmail = toLower(ctx.request.body.email) || 'unknownEmail';
|
|
|
|
|
|
|
|
return ratelimit.middleware({
|
|
|
|
interval: { min: 5 },
|
|
|
|
max: 5,
|
|
|
|
prefixKey: `${userEmail}:${ctx.request.path}:${ctx.request.ip}`,
|
|
|
|
handler() {
|
|
|
|
throw new RateLimitError();
|
|
|
|
},
|
|
|
|
...ratelimitConfig,
|
|
|
|
...config,
|
|
|
|
})(ctx, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
2022-10-05 08:21:34 -07:00
|
|
|
};
|