Skip to content

Commit de031f3

Browse files
authored
Merge pull request #4620 from alanorth/upgrade-express-rate-limit
2 parents 5f027e1 + cd978ea commit de031f3

File tree

8 files changed

+44
-19
lines changed

8 files changed

+44
-19
lines changed

config/config.example.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ ui:
1010
port: 4000
1111
# NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript
1212
nameSpace: /
13-
# The rateLimiter settings limit each IP to a 'max' of 500 requests per 'windowMs' (1 minute).
13+
# The rateLimiter settings limit each IP to a 'limit' of 500 requests per 'windowMs' (1 minute).
1414
rateLimiter:
1515
windowMs: 60000 # 1 minute
16-
max: 500 # limit each IP to 500 requests per windowMs
16+
limit: 500 # limit each IP to 500 requests per windowMs
17+
# IPv6 subnet mask applied to IPv6 addresses.
18+
# See: https://express-rate-limit.mintlify.app/reference/configuration#ipv6subnet
19+
ipv6Subnet: 56
1720
# Trust X-FORWARDED-* headers from proxies (default = true)
1821
useProxies: true
1922

package-lock.json

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"deepmerge": "^4.3.1",
115115
"ejs": "^3.1.10",
116116
"express": "^4.22.1",
117-
"express-rate-limit": "^5.1.3",
117+
"express-rate-limit": "^8.2.1",
118118
"fast-json-patch": "^3.1.1",
119119
"filesize": "^10.1.6",
120120
"http-proxy-middleware": "^2.0.9",

server.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,14 @@ export function app() {
176176
* When it is present, the rateLimiter will be enabled. When it is undefined, the rateLimiter will be disabled.
177177
*/
178178
if (hasValue((environment.ui as UIServerConfig).rateLimiter)) {
179-
const RateLimit = require('express-rate-limit');
180-
const limiter = new RateLimit({
179+
const { rateLimit } = require('express-rate-limit')
180+
const limiter = rateLimit({
181181
windowMs: (environment.ui as UIServerConfig).rateLimiter.windowMs,
182-
max: (environment.ui as UIServerConfig).rateLimiter.max,
182+
limit: (environment.ui as UIServerConfig).rateLimiter.limit,
183+
standardHeaders: true,
184+
legacyHeaders: false,
185+
// don't log ERR_ERL_PERMISSIVE_TRUST_PROXY if we are trusting proxies
186+
validate: {trustProxy: !environment.ui.useProxies},
183187
});
184188
server.use(limiter);
185189
}

src/config/config.util.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ describe('Config Util', () => {
4444
const appConfig = new DefaultAppConfig();
4545
expect(appConfig.cache.msToLive.default).toEqual(15 * 60 * 1000); // 15 minute
4646
expect(appConfig.ui.rateLimiter.windowMs).toEqual(1 * 60 * 1000); // 1 minute
47-
expect(appConfig.ui.rateLimiter.max).toEqual(500);
47+
expect(appConfig.ui.rateLimiter.limit).toEqual(500);
48+
expect(appConfig.ui.rateLimiter.ipv6Subnet).toEqual(56);
4849
expect(appConfig.ui.useProxies).toEqual(true);
4950

5051
expect(appConfig.submission.autosave.metadata).toEqual([]);
@@ -58,7 +59,8 @@ describe('Config Util', () => {
5859

5960
const rateLimiter = {
6061
windowMs: 5 * 50 * 1000, // 5 minutes
61-
max: 1000,
62+
limit: 1000,
63+
ipv6Subnet: 56,
6264
};
6365
appConfig.ui.rateLimiter = rateLimiter;
6466

@@ -82,7 +84,8 @@ describe('Config Util', () => {
8284

8385
expect(mockProductionEnvironment.cache.msToLive.default).toEqual(msToLive);
8486
expect(mockProductionEnvironment.ui.rateLimiter.windowMs).toEqual(rateLimiter.windowMs);
85-
expect(mockProductionEnvironment.ui.rateLimiter.max).toEqual(rateLimiter.max);
87+
expect(mockProductionEnvironment.ui.rateLimiter.limit).toEqual(rateLimiter.limit);
88+
expect(mockProductionEnvironment.ui.rateLimiter.ipv6Subnet).toEqual(rateLimiter.ipv6Subnet);
8689
expect(mockProductionEnvironment.ui.useProxies).toEqual(false);
8790
expect(mockProductionEnvironment.submission.autosave.metadata[0]).toEqual(autoSaveMetadata[0]);
8891
expect(mockProductionEnvironment.submission.autosave.metadata[1]).toEqual(autoSaveMetadata[1]);

src/config/default-app-config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ export class DefaultAppConfig implements AppConfig {
4949
// NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript
5050
nameSpace: '/',
5151

52-
// The rateLimiter settings limit each IP to a 'max' of 500 requests per 'windowMs' (1 minute).
52+
// The rateLimiter settings limit each IP to a 'limit' of 500 requests per 'windowMs' (1 minute).
5353
rateLimiter: {
5454
windowMs: 1 * 60 * 1000, // 1 minute
55-
max: 500, // limit each IP to 500 requests per windowMs
55+
limit: 500, // limit each IP to 500 requests per windowMs
56+
ipv6Subnet: 56, // IPv6 subnet mask applied to IPv6 addresses
5657
},
5758

5859
// Trust X-FORWARDED-* headers from proxies

src/config/ui-server-config.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export class UIServerConfig extends ServerConfig {
88
// rateLimiter is used to limit the amount of requests a user is allowed make in an amount of time, in order to prevent overloading the server
99
rateLimiter?: {
1010
windowMs: number;
11-
max: number;
11+
limit: number;
12+
ipv6Subnet: number;
1213
};
1314

1415
// Trust X-FORWARDED-* headers from proxies

src/environments/environment.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ export const environment: BuildConfig = {
4444
// NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript
4545
nameSpace: '/angular-dspace',
4646
baseUrl: 'http://dspace.com/angular-dspace',
47-
// The rateLimiter settings limit each IP to a 'max' of 500 requests per 'windowMs' (1 minute).
47+
// The rateLimiter settings limit each IP to a 'limit' of 500 requests per 'windowMs' (1 minute).
4848
rateLimiter: {
4949
windowMs: 1 * 60 * 1000, // 1 minute
50-
max: 500, // limit each IP to 500 requests per windowMs
50+
limit: 500, // limit each IP to 500 requests per windowMs
51+
ipv6Subnet: 56,
5152
},
5253
useProxies: true,
5354
},

0 commit comments

Comments
 (0)