feat: Add zones for rate limiting by ip, user, session, global (#8508)
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
SchemaOptions,
|
||||
SecurityOptions,
|
||||
} from './Options/Definitions';
|
||||
import ParseServer from './cloud-code/Parse.Server';
|
||||
|
||||
function removeTrailingSlash(str) {
|
||||
if (!str) {
|
||||
@@ -609,6 +610,11 @@ export class Config {
|
||||
if (option.errorResponseMessage && typeof option.errorResponseMessage !== 'string') {
|
||||
throw `rateLimit.errorResponseMessage must be a string`;
|
||||
}
|
||||
const options = Object.keys(ParseServer.RateLimitZone);
|
||||
if (option.zone && !options.includes(option.zone)) {
|
||||
const formatter = new Intl.ListFormat('en', { style: 'short', type: 'disjunction' });
|
||||
throw `rateLimit.zone must be one of ${formatter.format(options)}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -601,6 +601,11 @@ module.exports.RateLimitOptions = {
|
||||
'The window of time in milliseconds within which the number of requests set in `requestCount` can be made before the rate limit is applied.',
|
||||
action: parsers.numberParser('requestTimeWindow'),
|
||||
},
|
||||
zone: {
|
||||
env: 'PARSE_SERVER_RATE_LIMIT_ZONE',
|
||||
help:
|
||||
"The type of rate limit to apply. The following types are supported:<br><br>- `global`: rate limit based on the number of requests made by all users <br>- `ip`: rate limit based on the IP address of the request <br>- `user`: rate limit based on the user ID of the request <br>- `session`: rate limit based on the session token of the request <br><br><br>:default: 'ip'",
|
||||
},
|
||||
};
|
||||
module.exports.SecurityOptions = {
|
||||
checkGroups: {
|
||||
|
||||
@@ -111,6 +111,7 @@
|
||||
* @property {String[]} requestMethods Optional, the HTTP request methods to which the rate limit should be applied, default is all methods.
|
||||
* @property {String} requestPath The path of the API route to be rate limited. Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings, string patterns, or regular expression. See: https://expressjs.com/en/guide/routing.html
|
||||
* @property {Number} requestTimeWindow The window of time in milliseconds within which the number of requests set in `requestCount` can be made before the rate limit is applied.
|
||||
* @property {String} zone The type of rate limit to apply. The following types are supported:<br><br>- `global`: rate limit based on the number of requests made by all users <br>- `ip`: rate limit based on the IP address of the request <br>- `user`: rate limit based on the user ID of the request <br>- `session`: rate limit based on the session token of the request <br><br><br>:default: 'ip'
|
||||
*/
|
||||
|
||||
/**
|
||||
|
||||
@@ -334,6 +334,17 @@ export interface RateLimitOptions {
|
||||
/* Optional, the URL of the Redis server to store rate limit data. This allows to rate limit requests for multiple servers by calculating the sum of all requests across all servers. This is useful if multiple servers are processing requests behind a load balancer. For example, the limit of 10 requests is reached if each of 2 servers processed 5 requests.
|
||||
*/
|
||||
redisUrl: ?string;
|
||||
/*
|
||||
The type of rate limit to apply. The following types are supported:
|
||||
<br><br>
|
||||
- `global`: rate limit based on the number of requests made by all users <br>
|
||||
- `ip`: rate limit based on the IP address of the request <br>
|
||||
- `user`: rate limit based on the user ID of the request <br>
|
||||
- `session`: rate limit based on the session token of the request <br>
|
||||
<br><br>
|
||||
:default: 'ip'
|
||||
*/
|
||||
zone: ?string;
|
||||
}
|
||||
|
||||
export interface SecurityOptions {
|
||||
|
||||
@@ -444,9 +444,11 @@ class ParseServer {
|
||||
|
||||
function addParseCloud() {
|
||||
const ParseCloud = require('./cloud-code/Parse.Cloud');
|
||||
const ParseServer = require('./cloud-code/Parse.Server');
|
||||
Object.defineProperty(Parse, 'Server', {
|
||||
get() {
|
||||
return Config.get(Parse.applicationId);
|
||||
const conf = Config.get(Parse.applicationId);
|
||||
return { ...conf, ...ParseServer };
|
||||
},
|
||||
set(newVal) {
|
||||
newVal.appId = Parse.applicationId;
|
||||
|
||||
19
src/cloud-code/Parse.Server.js
Normal file
19
src/cloud-code/Parse.Server.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const ParseServer = {};
|
||||
/**
|
||||
* ...
|
||||
*
|
||||
* @memberof Parse.Server
|
||||
* @property {String} global Rate limit based on the number of requests made by all users.
|
||||
* @property {String} session Rate limit based on the sessionToken.
|
||||
* @property {String} user Rate limit based on the user ID.
|
||||
* @property {String} ip Rate limit based on the request ip.
|
||||
* ...
|
||||
*/
|
||||
ParseServer.RateLimitZone = Object.freeze({
|
||||
global: 'global',
|
||||
session: 'session',
|
||||
user: 'user',
|
||||
ip: 'ip',
|
||||
});
|
||||
|
||||
module.exports = ParseServer;
|
||||
@@ -549,7 +549,22 @@ export const addRateLimit = (route, config, cloud) => {
|
||||
}
|
||||
return request.auth?.isMaster;
|
||||
},
|
||||
keyGenerator: request => {
|
||||
keyGenerator: async request => {
|
||||
if (route.zone === Parse.Server.RateLimitZone.global) {
|
||||
return request.config.appId;
|
||||
}
|
||||
const token = request.info.sessionToken;
|
||||
if (route.zone === Parse.Server.RateLimitZone.session && token) {
|
||||
return token;
|
||||
}
|
||||
if (route.zone === Parse.Server.RateLimitZone.user && token) {
|
||||
if (!request.auth) {
|
||||
await new Promise(resolve => handleParseSession(request, null, resolve));
|
||||
}
|
||||
if (request.auth?.user?.id && request.zone === 'user') {
|
||||
return request.auth.user.id;
|
||||
}
|
||||
}
|
||||
return request.config.ip;
|
||||
},
|
||||
store: redisStore.store,
|
||||
|
||||
Reference in New Issue
Block a user