express-rate-limit 5.5.0 → 6.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md DELETED
@@ -1,349 +0,0 @@
1
- # Express Rate Limit
2
-
3
- [![Node.js CI](https://github.com/nfriedly/express-rate-limit/workflows/Node.js%20CI/badge.svg)](https://github.com/nfriedly/express-rate-limit/actions)
4
- [![NPM version](https://img.shields.io/npm/v/express-rate-limit.svg)](https://npmjs.org/package/express-rate-limit "View this project on NPM")
5
- [![npm downloads](https://img.shields.io/npm/dm/express-rate-limit)](https://www.npmjs.com/package/express-rate-limit)
6
-
7
-
8
- Basic rate-limiting middleware for Express. Use to limit repeated requests to public APIs and/or endpoints such as password reset.
9
-
10
- Plays nice with [express-slow-down](https://www.npmjs.com/package/express-slow-down).
11
-
12
- Note: this module does not share state with other processes/servers by default. It also buckets all requests to an internal clock rather than starting a new timer for each end-user. It's fine for abuse-prevention but might not produce the desired effect when attempting to strictly enforce API rate-limits or similar. If you need a more robust solution, I recommend using an external store:
13
-
14
- ### Stores
15
-
16
- - Memory Store _(default, built-in)_ - stores hits in-memory in the Node.js process. Does not share state with other servers or processes, and does not start a separate timer for each end user.
17
- - [Redis Store](https://npmjs.com/package/rate-limit-redis)
18
- - [Memcached Store](https://npmjs.org/package/rate-limit-memcached)
19
- - [Mongo Store](https://www.npmjs.com/package/rate-limit-mongo)
20
-
21
- ### Alternate Rate-limiters
22
-
23
- This module was designed to only handle the basics and didn't even support external stores initially. These other options all are excellent pieces of software and may be more appropriate for some situations:
24
-
25
- - [rate-limiter-flexible](https://www.npmjs.com/package/rate-limiter-flexible)
26
- - [express-brute](https://www.npmjs.com/package/express-brute)
27
- - [rate-limiter](https://www.npmjs.com/package/express-limiter)
28
-
29
- ## Install
30
-
31
- ```sh
32
- $ npm install --save express-rate-limit
33
- ```
34
-
35
- ## Usage
36
-
37
- For an API-only server where the rate-limiter should be applied to all requests:
38
-
39
- ```js
40
- const rateLimit = require("express-rate-limit");
41
-
42
- // Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
43
- // see https://expressjs.com/en/guide/behind-proxies.html
44
- // app.set('trust proxy', 1);
45
-
46
- const limiter = rateLimit({
47
- windowMs: 15 * 60 * 1000, // 15 minutes
48
- max: 100 // limit each IP to 100 requests per windowMs
49
- });
50
-
51
- // apply to all requests
52
- app.use(limiter);
53
- ```
54
-
55
- For a "regular" web server (e.g. anything that uses `express.static()`), where the rate-limiter should only apply to certain requests:
56
-
57
- ```js
58
- const rateLimit = require("express-rate-limit");
59
-
60
- // Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
61
- // see https://expressjs.com/en/guide/behind-proxies.html
62
- // app.set('trust proxy', 1);
63
-
64
- const apiLimiter = rateLimit({
65
- windowMs: 15 * 60 * 1000, // 15 minutes
66
- max: 100
67
- });
68
-
69
- // only apply to requests that begin with /api/
70
- app.use("/api/", apiLimiter);
71
- ```
72
-
73
- Create multiple instances to apply different rules to different routes:
74
-
75
- ```js
76
- const rateLimit = require("express-rate-limit");
77
-
78
- // Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
79
- // see https://expressjs.com/en/guide/behind-proxies.html
80
- // app.set('trust proxy', 1);
81
-
82
- const apiLimiter = rateLimit({
83
- windowMs: 15 * 60 * 1000, // 15 minutes
84
- max: 100
85
- });
86
- app.use("/api/", apiLimiter);
87
-
88
- const createAccountLimiter = rateLimit({
89
- windowMs: 60 * 60 * 1000, // 1 hour window
90
- max: 5, // start blocking after 5 requests
91
- message:
92
- "Too many accounts created from this IP, please try again after an hour"
93
- });
94
- app.post("/create-account", createAccountLimiter, function(req, res) {
95
- //...
96
- });
97
- ```
98
-
99
- **Note:** most stores will require additional configuration, such as custom prefixes, when using multiple instances. The default built-in memory store is an exception to this rule.
100
-
101
- ## Request API
102
-
103
- A `req.rateLimit` property is added to all requests with the `limit`, `current`, and `remaining` number of requests and, if the store provides it, a `resetTime` Date object. These may be used in your application code to take additional actions or inform the user of their status.
104
-
105
- The property name can be configured with the configuration option `requestPropertyName`
106
-
107
- ## Configuration options
108
-
109
- ### max
110
-
111
- Max number of connections during `windowMs` milliseconds before sending a 429 response.
112
-
113
- May be a number, or a function that returns a number or a promise. If `max` is a function, it will be called with `req` and `res` params.
114
-
115
- Defaults to `5`. Set to `0` to disable.
116
-
117
- Example of using a function:
118
-
119
- ```js
120
- const rateLimit = require("express-rate-limit");
121
-
122
- function isPremium(req) {
123
- //...
124
- }
125
-
126
- const limiter = rateLimit({
127
- windowMs: 15 * 60 * 1000, // 15 minutes
128
-
129
- // max could also be an async function or return a promise
130
- max: function(req, res) {
131
- if (isPremium(req)) {
132
- return 10;
133
- }
134
- return 5;
135
- }
136
- });
137
-
138
- // apply to all requests
139
- app.use(limiter);
140
- ```
141
-
142
- ### windowMs
143
-
144
- Timeframe for which requests are checked/remembered. Also used in the Retry-After header when the limit is reached.
145
-
146
- Note: with non-default stores, you may need to configure this value twice, once here and once on the store. In some cases the units also differ (e.g. seconds vs miliseconds)
147
-
148
- Defaults to `60000` (1 minute).
149
-
150
- ### message
151
-
152
- Error message sent to user when `max` is exceeded.
153
-
154
- May be a String, JSON object, or any other value that Express's [res.send](https://expressjs.com/en/4x/api.html#res.send) supports.
155
-
156
- Defaults to `'Too many requests, please try again later.'`
157
-
158
- ### statusCode
159
-
160
- HTTP status code returned when `max` is exceeded.
161
-
162
- Defaults to `429`.
163
-
164
- ### headers
165
-
166
- Enable headers for request limit (`X-RateLimit-Limit`) and current usage (`X-RateLimit-Remaining`) on all responses and time to wait before retrying (`Retry-After`) when `max` is exceeded.
167
-
168
- Defaults to `true`. Behavior may change in the next major release.
169
-
170
- ### draft_polli_ratelimit_headers
171
-
172
- Enable headers conforming to the [ratelimit standardization proposal](https://tools.ietf.org/id/draft-polli-ratelimit-headers-01.html): `RateLimit-Limit`, `RateLimit-Remaining`, and, if the store supports it, `RateLimit-Reset`. May be used in conjunction with, or instead of the `headers` option.
173
-
174
- Defaults to `false`. Behavior and name will likely change in future releases.
175
-
176
- ### keyGenerator
177
-
178
- Function used to generate keys.
179
-
180
- Defaults to req.ip, similar to this:
181
-
182
- ```js
183
- function (req /*, res*/) {
184
- return req.ip;
185
- }
186
- ```
187
-
188
- ### handler
189
-
190
- The function to handle requests once the max limit is exceeded. It receives the request and the response objects. The "next" param is available if you need to pass to the next middleware. Finally, the options param has all of the options that originally passed in when creating the current limiter and the default values for other options.
191
-
192
- The`req.rateLimit` object has `limit`, `current`, and `remaining` number of requests and, if the store provides it, a `resetTime` Date object.
193
-
194
- Defaults to:
195
-
196
- ```js
197
- function (req, res, next, options) {
198
- res.status(options.statusCode).send(options.message);
199
- }
200
- ```
201
-
202
- ### onLimitReached
203
-
204
- Function that is called the first time a user hits the rate limit within a given window.
205
-
206
- The`req.rateLimit` object has `limit`, `current`, and `remaining` number of requests and, if the store provides it, a `resetTime` Date object.
207
-
208
- Default is an empty function:
209
-
210
- ```js
211
- function (req, res, options) {
212
- /* empty */
213
- }
214
- ```
215
-
216
- ### requestWasSuccessful
217
-
218
- Function that is called when `skipFailedRequests` and/or `skipSuccessfulRequests` are set to `true`.
219
- May be overridden if, for example, a service sends out a 200 status code on errors.
220
-
221
- Defaults to
222
-
223
- ```js
224
- function (req, res) {
225
- return res.statusCode < 400;
226
- }
227
- ```
228
-
229
- ### skipFailedRequests
230
-
231
- When set to `true`, failed requests won't be counted. Request considered failed when:
232
-
233
- - response status >= 400
234
- - requests that were cancelled before last chunk of data was sent (response `close` event triggered)
235
- - response `error` event was triggrered by response
236
-
237
- (Technically they are counted and then un-counted, so a large number of slow requests all at once could still trigger a rate-limit. This may be fixed in a future release.)
238
-
239
- Defaults to `false`.
240
-
241
- ### skipSuccessfulRequests
242
-
243
- When set to `true` successful requests (response status < 400) won't be counted.
244
- (Technically they are counted and then un-counted, so a large number of slow requests all at once could still trigger a rate-limit. This may be fixed in a future release.)
245
-
246
- Defaults to `false`.
247
-
248
- ### skip
249
-
250
- Function used to skip (whitelist) requests. Returning `true`, or a promise that resolves with `true`, from the function will skip limiting for that request.
251
-
252
- Defaults to always `false` (count all requests):
253
-
254
- ```js
255
- function (/*req, res*/) {
256
- return false;
257
- }
258
- ```
259
-
260
- ### requestPropertyName
261
- Parameter to add to `req`-Object.
262
-
263
- Defaults to `rateLimit`.
264
-
265
- ### store
266
-
267
- The storage to use when persisting rate limit attempts.
268
-
269
- By default, the [MemoryStore](lib/memory-store.js) is used.
270
-
271
- Available data stores are:
272
-
273
- - MemoryStore: _(default)_ Simple in-memory option. Does not share state when app has multiple processes or servers.
274
- - [rate-limit-redis](https://npmjs.com/package/rate-limit-redis): A [Redis](http://redis.io/)-backed store, more suitable for large or demanding deployments.
275
- - [rate-limit-memcached](https://npmjs.org/package/rate-limit-memcached): A [Memcached](https://memcached.org/)-backed store.
276
- - [rate-limit-mongo](https://www.npmjs.com/package/rate-limit-mongo): A [MongoDB](https://www.mongodb.com/)-backed store.
277
-
278
- You may also create your own store. It must implement the following in order to function:
279
-
280
- ```js
281
- function SomeStore() {
282
- /**
283
- * Increments the value in the underlying store for the given key.
284
- * @method function
285
- * @param {string} key - The key to use as the unique identifier passed
286
- * down from RateLimit.
287
- * @param {Function} cb - The callback issued when the underlying
288
- * store is finished.
289
- *
290
- * The callback should be called with three values:
291
- * - error (usually null)
292
- * - hitCount for this IP
293
- * - resetTime - JS Date object (optional, but necessary for X-RateLimit-Reset header)
294
- */
295
- this.incr = function(key, cb) {
296
- // increment storage
297
- cb(null, hits, resetTime);
298
- };
299
-
300
- /**
301
- * Decrements the value in the underlying store for the given key. Used only when skipFailedRequests is true
302
- * @method function
303
- * @param {string} key - The key to use as the unique identifier passed
304
- * down from RateLimit.
305
- */
306
- this.decrement = function(key) {
307
- // decrement storage
308
- };
309
-
310
- /**
311
- * Resets a value with the given key.
312
- * @method function
313
- * @param {[type]} key - The key to reset
314
- */
315
- this.resetKey = function(key) {
316
- // remove key from storage or reset it to 0
317
- };
318
- }
319
- ```
320
-
321
- ## Instance API
322
-
323
- ### instance.resetKey(key)
324
-
325
- Resets the rate limiting for a given key. (Allow users to complete a captcha or whatever to reset their rate limit, then call this method.)
326
-
327
- ## Summary of breaking changes:
328
-
329
- ### v5 changes
330
-
331
- - Removed index.d.ts. (See [#138](https://github.com/nfriedly/express-rate-limit/issues/138))
332
-
333
- ### v4 Changes
334
-
335
- - Express Rate Limit no longer modifies the passed-in options object, it instead makes a clone of it.
336
-
337
- ### v3 Changes
338
-
339
- - Removed `delayAfter` and `delayMs` options; they were moved to a new module: [express-slow-down](https://npmjs.org/package/express-slow-down).
340
- - Simplified the default `handler` function so that it no longer changes the response format. Now uses [res.send](https://expressjs.com/en/4x/api.html#res.send).
341
- - `onLimitReached` now only triggers once for a given ip and window. only `handle` is called for every blocked request.
342
-
343
- ### v2 Changes
344
-
345
- v2 uses a less precise but less resource intensive method of tracking hits from a given IP. v2 also adds the `limiter.resetKey()` API and removes the `global: true` option.
346
-
347
- ## License
348
-
349
- MIT © [Nathan Friedly](http://nfriedly.com/)
@@ -1,190 +0,0 @@
1
- "use strict";
2
- const MemoryStore = require("./memory-store");
3
-
4
- function RateLimit(options) {
5
- options = Object.assign(
6
- {
7
- windowMs: 60 * 1000, // milliseconds - how long to keep records of requests in memory
8
- max: 5, // max number of recent connections during `window` milliseconds before sending a 429 response
9
- message: "Too many requests, please try again later.",
10
- statusCode: 429, // 429 status = Too Many Requests (RFC 6585)
11
- headers: true, //Send custom rate limit header with limit and remaining
12
- draft_polli_ratelimit_headers: false, //Support for the new RateLimit standardization headers
13
- // ability to manually decide if request was successful. Used when `skipSuccessfulRequests` and/or `skipFailedRequests` are set to `true`
14
- requestWasSuccessful: function (req, res) {
15
- return res.statusCode < 400;
16
- },
17
- skipFailedRequests: false, // Do not count failed requests
18
- skipSuccessfulRequests: false, // Do not count successful requests
19
- // allows to create custom keys (by default user IP is used)
20
- keyGenerator: function (req /*, res*/) {
21
- if (!req.ip) {
22
- throw new Error(
23
- "express-rate-limit: req.ip is undefined - are you sure you're using express?"
24
- );
25
- }
26
- return req.ip;
27
- },
28
- skip: function (/*req, res*/) {
29
- return false;
30
- },
31
- handler: function (req, res /*, next, optionsUsed*/) {
32
- res.status(options.statusCode).send(options.message);
33
- },
34
- onLimitReached: function (/*req, res, optionsUsed*/) {},
35
- requestPropertyName: "rateLimit", // Parameter name appended to req object
36
- },
37
- options
38
- );
39
-
40
- // store to use for persisting rate limit data
41
- options.store = options.store || new MemoryStore(options.windowMs);
42
-
43
- // ensure that the store has the incr method
44
- if (
45
- typeof options.store.incr !== "function" ||
46
- typeof options.store.resetKey !== "function" ||
47
- (options.skipFailedRequests &&
48
- typeof options.store.decrement !== "function")
49
- ) {
50
- throw new Error("The store is not valid.");
51
- }
52
-
53
- ["global", "delayMs", "delayAfter"].forEach((key) => {
54
- // note: this doesn't trigger if delayMs or delayAfter are set to 0, because that essentially disables them
55
- if (options[key]) {
56
- throw new Error(
57
- `The ${key} option was removed from express-rate-limit v3.`
58
- );
59
- }
60
- });
61
-
62
- function rateLimit(req, res, next) {
63
- Promise.resolve(options.skip(req, res))
64
- .then((skip) => {
65
- if (skip) {
66
- return next();
67
- }
68
-
69
- const key = options.keyGenerator(req, res);
70
-
71
- options.store.incr(key, function (err, current, resetTime) {
72
- if (err) {
73
- return next(err);
74
- }
75
-
76
- const maxResult =
77
- typeof options.max === "function"
78
- ? options.max(req, res)
79
- : options.max;
80
-
81
- Promise.resolve(maxResult)
82
- .then((max) => {
83
- req[options.requestPropertyName] = {
84
- limit: max,
85
- current: current,
86
- remaining: Math.max(max - current, 0),
87
- resetTime: resetTime,
88
- };
89
-
90
- if (options.headers && !res.headersSent) {
91
- res.setHeader("X-RateLimit-Limit", max);
92
- res.setHeader(
93
- "X-RateLimit-Remaining",
94
- req[options.requestPropertyName].remaining
95
- );
96
- if (resetTime instanceof Date) {
97
- // if we have a resetTime, also provide the current date to help avoid issues with incorrect clocks
98
- res.setHeader("Date", new Date().toUTCString());
99
- res.setHeader(
100
- "X-RateLimit-Reset",
101
- Math.ceil(resetTime.getTime() / 1000)
102
- );
103
- }
104
- }
105
- if (options.draft_polli_ratelimit_headers && !res.headersSent) {
106
- res.setHeader("RateLimit-Limit", max);
107
- res.setHeader(
108
- "RateLimit-Remaining",
109
- req[options.requestPropertyName].remaining
110
- );
111
- if (resetTime) {
112
- const deltaSeconds = Math.ceil(
113
- (resetTime.getTime() - Date.now()) / 1000
114
- );
115
- res.setHeader("RateLimit-Reset", Math.max(0, deltaSeconds));
116
- }
117
- }
118
-
119
- if (
120
- options.skipFailedRequests ||
121
- options.skipSuccessfulRequests
122
- ) {
123
- let decremented = false;
124
- const decrementKey = () => {
125
- if (!decremented) {
126
- options.store.decrement(key);
127
- decremented = true;
128
- }
129
- };
130
-
131
- if (options.skipFailedRequests) {
132
- res.on("finish", function () {
133
- if (!options.requestWasSuccessful(req, res)) {
134
- decrementKey();
135
- }
136
- });
137
-
138
- res.on("close", () => {
139
- if (!res.finished) {
140
- decrementKey();
141
- }
142
- });
143
-
144
- res.on("error", () => decrementKey());
145
- }
146
-
147
- if (options.skipSuccessfulRequests) {
148
- res.on("finish", function () {
149
- if (options.requestWasSuccessful(req, res)) {
150
- options.store.decrement(key);
151
- }
152
- });
153
- }
154
- }
155
-
156
- if (max && current === max + 1) {
157
- options.onLimitReached(req, res, options);
158
- }
159
-
160
- if (max && current > max) {
161
- if (options.headers && !res.headersSent) {
162
- res.setHeader(
163
- "Retry-After",
164
- Math.ceil(options.windowMs / 1000)
165
- );
166
- }
167
- return options.handler(req, res, next, options);
168
- }
169
-
170
- next();
171
-
172
- return null;
173
- })
174
- .catch(next);
175
- });
176
-
177
- return null;
178
- })
179
- .catch(next);
180
- }
181
-
182
- rateLimit.resetKey = options.store.resetKey.bind(options.store);
183
-
184
- // Backward compatibility function
185
- rateLimit.resetIp = rateLimit.resetKey;
186
-
187
- return rateLimit;
188
- }
189
-
190
- module.exports = RateLimit;
@@ -1,47 +0,0 @@
1
- "use strict";
2
-
3
- function calculateNextResetTime(windowMs) {
4
- const d = new Date();
5
- d.setMilliseconds(d.getMilliseconds() + windowMs);
6
- return d;
7
- }
8
-
9
- function MemoryStore(windowMs) {
10
- let hits = {};
11
- let resetTime = calculateNextResetTime(windowMs);
12
-
13
- this.incr = function (key, cb) {
14
- if (hits[key]) {
15
- hits[key]++;
16
- } else {
17
- hits[key] = 1;
18
- }
19
-
20
- cb(null, hits[key], resetTime);
21
- };
22
-
23
- this.decrement = function (key) {
24
- if (hits[key]) {
25
- hits[key]--;
26
- }
27
- };
28
-
29
- // export an API to allow hits all IPs to be reset
30
- this.resetAll = function () {
31
- hits = {};
32
- resetTime = calculateNextResetTime(windowMs);
33
- };
34
-
35
- // export an API to allow hits from one IP to be reset
36
- this.resetKey = function (key) {
37
- delete hits[key];
38
- };
39
-
40
- // simply reset ALL hits every windowMs
41
- const interval = setInterval(this.resetAll, windowMs);
42
- if (interval.unref) {
43
- interval.unref();
44
- }
45
- }
46
-
47
- module.exports = MemoryStore;