Lead Maintainer: DomenicoPizzata
hapi-multi-rate-limit is a plugin for hapi that enables rate limiting.
It relies on cache
being defined in the server.
It allows different options for user-path configuration in terms of time (seconds, minutes, hours, days).
const Hapi = require('hapi');
const server = Hapi.server({
cache: { engine: require('catbox-memory'), name: 'memory' }
});
server.register({
plugin: require('hapi-multi-rate-limit'),
options: {}
});
Defaults are given here
enabled
:true
whether or not rate limiting is enabled at all. Set this tofalse
in a route's config to bypass all rate limiting for that routeuserLimit
:300
number of total requests a user can make per period. Set tofalse
to disable limiting requests per user.userCache
: Object with the following properties:segment
:hapi-multi-rate-limit-user
Name of the cache segment to use for storing user rate limit infoexpiresIn
:600000
Time (in milliseconds) of period foruserLimit
userAttribute
:id
credentials attribute to use when determining distinct authenticated usersuserWhitelist
:[]
array of users (as defined byuserAttribute
for whom to bypass rate limiting. This is only applied to authenticated users, for ip whitelisting useipWhitelist
.addressOnly
:false
if true, only consider user address when determining distinct authenticated userspathLimit
:50
number of total requests that can be made on a given path per period. Set tofalse
to disable limiting requests per path.pathCache
: Object with the following properties:segment
:hapi-multi-rate-limit-path
Name of the cache segment to use for storing path rate limit infoexpiresIn
:60000
Time (in milliseconds) of period forpathLimit
userPathLimitSeconds
:false
number of total requests that can be made on a given path per user per period (seconds). Set tofalse
to disable limiting requests per path per user.userPathLimitMinutes
:false
number of total requests that can be made on a given path per user per period (minutes). Set tofalse
to disable limiting requests per path per user.userPathLimitHours
:false
number of total requests that can be made on a given path per user per period (hours). Set tofalse
to disable limiting requests per path per user.userPathLimitDays
:false
number of total requests that can be made on a given path per user per period (days). Set tofalse
to disable limiting requests per path per user.userPathCacheSeconds
: Object with the following properties:segment
:hapi-multi-rate-limit-userPathSeconds
Name of the cache segment to use for storing userPath rate limit infoexpiresIn
:1000
Time (in milliseconds) of period foruserPathLimitSeconds
userPathCacheMinutes
: Object with the following properties:segment
:hapi-multi-rate-limit-userPathMinutes
Name of the cache segment to use for storing userPath rate limit infoexpiresIn
:60000
Time (in milliseconds) of period foruserPathLimitMinutes
userPathCacheHours
: Object with the following properties:segment
:hapi-multi-rate-limit-userPathHours
Name of the cache segment to use for storing userPath rate limit infoexpiresIn
:3600000
Time (in milliseconds) of period foruserPathLimitHours
userPathCacheDays
: Object with the following properties:segment
:hapi-multi-rate-limit-userPathDays
Name of the cache segment to use for storing userPath rate limit infoexpiresIn
:86400000
Time (in milliseconds) of period foruserPathLimitDays
headers
:true
Whether or not to include headers in responsesipWhitelist
:[]
array of IPs for whom to bypass rate limiting. Note that a whitelisted IP would also bypass restrictions an authenticated user would otherwise have.trustProxy
:false
If true, honor theX-Forwarded-For
header. See note below.getIpFromProxyHeader
:undefined
a function which will extract the remote address from theX-Forwarded-For
header. The default implementation takes the first entry.
A user is considered a single remoteAddress
for routes that are unauthenticated. On authenticated routes it is the userAtribute
(default id
) of the authenticated user.
If trustProxy
is true, the address from the X-Forwarded-For
header will be use instead of remoteAddress
, if present.
If trustProxy
is true and getIpFromProxyHeader
is not defined, the address will be determined using the first entry in the X-Forwarded-For
header.
If you set trustProxy
to true, make sure that your proxy server is the only thing that can access the server, and be sure to configure your proxy to strip all incoming X-Forwarded-For
headers.
For example if you were using haproxy you would add reqidel ^X-Forwarded-For
to your config.
Failure to do this would allow anyone to spoof that header to bypass your rate limiting.
The following headers will be included in server responses if their respective limits are enabled
x-ratelimit-pathlimit
: Will equalpathLimit
x-ratelimit-pathremaining
: Remaining number of requests path has this - periodx-ratelimit-pathreset
: Time (in milliseconds) until reset ofpathLimit
periodx-ratelimit-userlimit
: Will equaluserLimit
x-ratelimit-userremaining
: Remaining number of requests user has this periodx-ratelimit-userreset
: Time (in milliseconds) until reset ofuserLimit
periodx-ratelimit-userpathlimit-seconds
: Will equaluserPathLimitSeconds
x-ratelimit-userpathremaining-seconds
: Remaining number of requests user has this period for this pathx-ratelimit-userpathreset-seconds
: Time (in milliseconds) until reset ofuserPathLimitSeconds
periodx-ratelimit-userpathlimit-minutes
: Will equaluserPathLimitMinutes
x-ratelimit-userpathremaining-minutes
: Remaining number of requests user has this period for this pathx-ratelimit-userpathreset-minutes
: Time (in milliseconds) until reset ofuserPathLimitMinutes
periodx-ratelimit-userpathlimit-hours
: Will equaluserPathLimitHours
x-ratelimit-userpathremaining-hours
: Remaining number of requests user has this period for this pathx-ratelimit-userpathreset-hours
: Time (in milliseconds) until reset ofuserPathLimitHours
periodx-ratelimit-userpathlimit-days
: Will equaluserPathLimitDays
x-ratelimit-userpathremaining-days
: Remaining number of requests user has this period for this pathx-ratelimit-userpathreset-days
: Time (in milliseconds) until reset ofuserPathLimitDays
period
All of the settings (except for userLimit
and userCache
) can be overridden in your route's config.
For instance, to disable pathLimit
for a route you would add this to its config
attribute
plugins: {
'hapi-multi-rate-limit': {
pathLimit: false
}
}
To disable all rate limiting for a route you woul add this to its config
attribute
plugins: {
'hapi-multi-rate-limit': {
enabled: false
}
}
License: MIT