Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
adonis-request-throttler
Advanced tools
Request limiter for Adonis JS 5
Install Adonis redis client, if you want to use redis as storage for request info.
npm i --save adonis-request-throttler
Install provider:
node ace configure adonis-request-throttler
config/request-throttler.ts
.After adding request throttler to your app, you must register the middleware.
// start/kernel.ts
Server.middleware.registerNamed({
throttle: 'Adonis/Addons/RequestThrottler/Middleware'
})
And then you can add middleware to your routes:
Route
.get('subscribers', 'SubscriberController.index')
.middleware('throttle')
This middleware will limit user requests to endpoint. Configure default count and timeout in throttle config. For custom configuration you can add values as middleware params:
Route
.get('subscribers', 'SubscriberController.index')
.middleware('throttle:10,20')
First param is responsible for max attempt count, second params means time limit after exceeding the quota.
You can also use throttler in your services by client identifier:
import RequestThrottler from '@ioc:Adonis/Addons/RequestThrottler'
RequestThrottler.verifyClient(userId, 10, 15)
Or you can verify request in your controller:
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
import RequestThrottler from '@ioc:Adonis/Addons/RequestThrottler'
export default class ControllerExample {
public async index({ request }: HttpContextContract) {
await RequestThrottler.verifyRequest(request)
return // endpoint data
}
}
For configuring request throttler use request-throttler.ts file in config dir.
import { ThrottleConfig } from '@ioc:Adonis/Addons/RequestThrottler'
export default {
maxAttempts: 10,
maxAttemptPeriod: 600000,
ttlUnits: 'ms',
cacheStorage: 'redis',
useOwnCache: true,
limitExceptionParams: {
code: 'E_LIMIT_EXCEPTION',
message: 'Maximum number of login attempts exceeded. Please try again later.',
status: 429,
},
requestKeysForRecognizing: ['method', 'hostname', 'url', 'ip'],
} as ThrottleConfig
You can configure such options:
maxAttempts - permitted request count for user for permitted request count
maxAttemptPeriod - specify ttl for record, which store info about last user request
ttlUnits - time units for maxAttemptPeriod property
cacheStorage - specify storage for requests information
useOwnCache - specify is request throttler uses own cache provider or takes already instantiated cache provider from Adonis IoC container
limitExceptionParams - specify params for limit exception, you can change http status or add localization for message
requestKeysForRecognizing - specify request keys for recognizing the client and the route
By default for request verifying throttler takes info about method, hostname, url, ip of request. If you need to specify request recognizing you should implement ClientRecognizerContract interface in your custom recognizer.
import { RequestContract } from '@ioc:Adonis/Core/Request'
import { ClientRecognizerContract } from '@ioc:Adonis/Addons/RequestThrottler'
export default class CustomClientRecognizer implements ClientRecognizerContract {
public identifyClient(request: RequestContract): Promise<string> | string {
return // client-identifier
}
}
And then you should register your recognizer. For example you can do it in this way:
import RequestThrottler from '@ioc:Adonis/Addons/RequestThrottler'
RequestThrottler.useClientRecognizer(new CustomClientRecognizer())
Then your requests will recognize using your custom recognizer.
This packages based on adonis cache package. Throttler can work in two modes. By default throttler creates own cache client and uses it for storing info about requests. If you use Adonis cache you can use already instantiated provider for throttler. For using adonis cache set false to useOwnCache parameter in your throttler config.
You need the same ttlUnits
in your cache
and request-throttler
config.
You should register cache provider before request-throttler:
{
"providers": [
"./providers/AppProvider",
"@adonisjs/core",
"@adonisjs/lucid",
"@adonisjs/redis",
"adonis5-cache",
"adonis-request-throttler"
]
}
When you use adonis5-cache as storage, you can add custom storages for storing request data. Read more about this in adonis5-cache docs.
FAQs
Request throttler provider for AdonisJS 5
We found that adonis-request-throttler demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.