Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created rate limit middleware #113

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

JackVarney
Copy link
Contributor

GitHub issue

WIP...

Wanted to get feedback on a couple of issues

  • Would this be better implemented else where (e.g. whoever hosts this might have their own rate limiting capabilities)?
  • I've arbitrarily set it to default to 100 requests every 15 minutes and for auth 10 every 30 minutes. What actually would be the ideal config here?
  • Finally, would this be better set as an environment variable or in some config somewhere?

@JackVarney JackVarney self-assigned this Apr 26, 2020
Copy link
Member

@js-kyle js-kyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WIP is looking great

Would this be better implemented else where (e.g. whoever hosts this might have their own rate limiting capabilities)?

I agree that it would be good to have some infrastructure handle this rather than a middleware, but I'm not yet sure about the setup of that

I think the last two questions would be better answered by someone who knows expected volumes of the app

@@ -42,6 +42,7 @@
"bcrypt": "^4.0.1",
"class-transformer": "^0.2.3",
"class-validator": "^0.11.1",
"express-rate-limit": "^5.1.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module, by default uses an in-memory store for tracking the rate limits - this means that if the environment running the application has multiple processes running, the limits can be bypassed to a certain extent. I'm not yet familiar enough to comment whether or not that would be a large issue for the project, my guess is that it wouldn't be a big deal. Alternatively it could be passed an external postgres store to mitigate that, as the state would be shared across processes/instances

@JackVarney JackVarney requested a review from f10l April 26, 2020 21:54
Copy link
Member

@f10l f10l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR!

  • Would this be better implemented else where (e.g. whoever hosts this might have their own rate limiting capabilities)?

Probably yes! But I do not think it harms for the moment to have the possibility on each instance directly.
We could default "off" the rate limiting by env variables.

  • I've arbitrarily set it to default to 100 requests every 15 minutes and for auth 10 every 30 minutes. What actually would be the ideal config here?

I would put it a bit higher, such as 50 for the auth and 1000 requests every 15 minutes for the rest.
Also here, maybe changeable per environment variables?

  • Finally, would this be better set as an environment variable or in some config somewhere?

Would make sense to me, this is something we might want to adjust. Could you check if there is a nice way to integrate it with the existing configuration module?

@JackVarney JackVarney force-pushed the feature/rate-limiting branch from c5712c6 to 65f54ae Compare April 27, 2020 22:32
@JackVarney
Copy link
Contributor Author

@fanick-ber

Is this more inline with what you was thinking?

@f10l f10l self-requested a review April 28, 2020 21:26
Copy link
Member

@f10l f10l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly.

I think the configuration has be to ConfigurationService from the ConfigurationModule

I added the middleware to the app module in this way:

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(DefaultRateLimitMiddleware).forRoutes('*');
  }
}

It gets called on every call, but I do not get an error when going above the limits. Any idea what I am missing?

@f10l f10l linked an issue Apr 29, 2020 that may be closed by this pull request
@JackVarney JackVarney force-pushed the feature/rate-limiting branch from 65f54ae to fe5db65 Compare May 4, 2020 17:48
@JackVarney
Copy link
Contributor Author

I think it was because I was creating a new instance of rate limit each time lol.

Its using the same version everytime now if you'd like to give it another go.

@JackVarney
Copy link
Contributor Author

@fanick-ber

@f10l
Copy link
Member

f10l commented May 4, 2020

Thanks @JackVarney , I will test it tomorrow! :-)

Copy link
Member

@f10l f10l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worked nice! :-) Thanks!

I had to change the ConfigurationService, see comments.

export class DefaultRateLimitMiddleware implements NestMiddleware {
private configuredMiddleware: RequestHandler;

constructor(private configService: ConfigService) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was failed for me, if I did not use the custom ConfigurationService instead of the nestjs configservice.

export class AuthRateLimitMiddleware implements NestMiddleware {
private configuredMiddleware: RequestHandler;

constructor(private configService: ConfigService) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dito

private configuredMiddleware: RequestHandler;

constructor(private configService: ConfigService) {
const defaultWindowMs = this.configService.get(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable is named a bit weird, Min instead of Ms would be correct. (Same for the Auth limiter below)

@f10l f10l self-requested a review May 5, 2020 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implement rate limiting
3 participants