Skip to content

Commit

Permalink
Adds rate limits to assistant route
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve-Mcl committed Jul 16, 2024
1 parent 10b7e1f commit bbbebe0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions forge/routes/api/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ module.exports = async function (app) {
* use an alternative means of accessing it.
*/
app.post('/:method', {
config: {
rateLimit: app.config.rate_limits
? {
hook: 'preHandler', // apply the rate as a preHandler so that session is available
max: 5, // max requests per window
timeWindow: 30000, // 30 seconds
keyGenerator: (request) => {
return request.session?.ownerId || request.ip
}
}
: false
},
schema: {
hide: true, // dont show in swagger
body: {
Expand Down
13 changes: 11 additions & 2 deletions test/unit/forge/routes/api/rateLimits/rateLimits_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ describe('Endpoint Rate Limiting', () => {
{ url: '/account/register', method: 'POST', shouldLimit: true },
{ url: '/account/forgot_password', method: 'POST', shouldLimit: true },
{ url: '/account/reset_password/:token', method: 'POST', shouldLimit: true },
{ url: '/api/v1/assistant/:method', method: 'POST', shouldLimit: true, customLimits: true },
// routes that are never rate limited
{ url: '/api/comms/auth/client', method: 'POST', shouldLimit: false },
{ url: '/api/comms/auth/acl', method: 'POST', shouldLimit: false },
Expand Down Expand Up @@ -281,7 +282,11 @@ describe('Endpoint Rate Limiting', () => {
it(`Route ${route.method} ${route.url} should be rate limited`, async function () {
const routeConfig = route.fastifyRoute.config
routeConfig.should.have.property('rateLimit').and.be.an.Object()
if (routeConfig.rateLimit.hard) {
if (route.customLimits) {
// should have one of the following properties: max, timeWindow, keyGenerator
const hasKeys = Object.keys(routeConfig.rateLimit).some((key) => ['max', 'timeWindow', 'keyGenerator'].includes(key))
should(hasKeys).be.true()
} else if (routeConfig.rateLimit.hard) {
routeConfig.rateLimit.should.have.property('max')
routeConfig.rateLimit.max.should.be.equalOneOf(5, 2)
routeConfig.rateLimit.should.have.property('timeWindow')
Expand Down Expand Up @@ -395,7 +400,11 @@ describe('Endpoint Rate Limiting', () => {
it(`Route ${route.method} ${route.url} should be rate limited`, async function () {
const routeConfig = route.fastifyRoute.config
routeConfig.should.have.property('rateLimit').and.be.an.Object()
if (routeConfig.rateLimit.hard) {
if (route.customLimits) {
// should have one of the following properties: max, timeWindow, keyGenerator
const hasKeys = Object.keys(routeConfig.rateLimit).some((key) => ['max', 'timeWindow', 'keyGenerator'].includes(key))
should(hasKeys).be.true()
} else if (routeConfig.rateLimit.hard) {
routeConfig.rateLimit.should.have.property('max')
routeConfig.rateLimit.max.should.be.equalOneOf(5, 2)
routeConfig.rateLimit.should.have.property('timeWindow')
Expand Down

0 comments on commit bbbebe0

Please sign in to comment.