Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
async-ratelimiter
Advanced tools
Rate limit made simple, easy, async. Based on ratelimiter.
$ npm install async-ratelimiter --save
A simple middleware implementation for whatever HTTP server:
'use strict'
const RateLimiter = require('async-ratelimiter')
const { getClientIp } = require('request-ip')
const Redis = require('ioredis')
const rateLimiter = new RateLimiter({
db: new Redis()
})
const apiQuota = async (req, res, next) => {
const clientIp = getClientIp(req)
const limit = await rateLimiter.get({ id: req.clientIp })
if (!res.finished && !res.headersSent) {
res.setHeader('X-Rate-Limit-Limit', limit.total)
res.setHeader('X-Rate-Limit-Remaining', Math.max(0, limit.remaining - 1))
res.setHeader('X-Rate-Limit-Reset', limit.reset)
}
return !limit.remaining
? sendFail({
req,
res,
code: HTTPStatus.TOO_MANY_REQUESTS,
message: MESSAGES.RATE_LIMIT_EXCEDEED()
})
: next(req, res)
}
It creates an rate limiter instance.
Required
Type: object
The redis connection instance.
Type: number
Default: 2500
The maximum number of requests within duration
.
Type: number
Default: 3600000
How long keep records of requests in milliseconds.
Type: string
Default: 'limit'
The prefix used for compound the key.
Type: string
The identifier to limit against (typically a user id).
You can pass this value using when you use .get
method as well.
Given an id
, returns a Promise with the status of the limit with the following structure:
total
: max
value.remaining
: number of calls left in current duration
without decreasing current get
.reset
: time since epoch in seconds that the rate limiting period will end (or already ended).Type: string
Default: this.id
The identifier to limit against (typically a user id).
Type: number
Default: this.max
The maximum number of requests within duration
. If provided, it overrides the default max
value. This is useful for custom limits that differ between IDs.
Type: number
Default: this.max
How long keep records of requests in milliseconds. If provided, it overrides the default duration
value.
Type: boolean
Default: true
When set to false
, the remaining number of calls is not decreased.
In some scenarios it might be useful to be able to read the current "remaining" value for a limiter.
const loginHandler = async (req, res, next) => {
const clientIp = getClientIp(req)
const limit = await rateLimiter.get({ id: clientIp, decrease: false })
if (!limit.remaining) return sendError(req, res, 429)
try {
await doLogin(req)
} catch (err) {
if (err) {
await rateLimiter.get({ id: req.clientIp })
return sendError(req, res, 401)
}
}
next(req, res)
}
In this example, new login attempts are rejected when more at least 10 unsuccessful login attempts happened in the last 60 seconds.
async-ratelimiter © microlink.io, released under the MIT License.
Authored and maintained by microlink.io with help from contributors.
microlink.io · GitHub microlink.io · Twitter @microlinkhq
FAQs
Rate limit made simple, easy, async.
The npm package async-ratelimiter receives a total of 21,858 weekly downloads. As such, async-ratelimiter popularity was classified as popular.
We found that async-ratelimiter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.