-
Notifications
You must be signed in to change notification settings - Fork 78
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
feat: soft-launch rate-limiting of API endpoints #389
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eae0664
to
d689f17
Compare
Converting to draft while switching to app-wide limits |
d689f17
to
53fcbee
Compare
liangyuanruo
reviewed
Oct 6, 2020
liangyuanruo
reviewed
Oct 6, 2020
liangyuanruo
approved these changes
Oct 6, 2020
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approved with minor changes requested
fef87bd
to
939ba0d
Compare
Merged
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
We want to limit the rate of requests to public endpoints as a security measure against DDoS attacks. However, we need to ensure that the limits do not affect regular application usage. Hence it is necessary to ensure that the limits do not get exceeded on a regular basis, before actually denying requests which exceed the limit.
After some discussion, we decided to apply the limits to three endpoints:
/auth/sendotp
as well as both submissions endpoints,v2/submissions/email
andv2/submissions/encrypt
.Solution
Use
express-rate-limit
with a custom handler which logs a message if the rate limit is exceeded, then carries on handling the request as normal. Note that the package uses per-IP limits by default.The log message looks like this:
![Screenshot 2020-10-06 at 10 35 24 AM](https://user-images.githubusercontent.com/29480346/95152466-b282da00-07bf-11eb-991a-84bc625fbed5.png)
Determining the limit
It is important to apply a limit which does not affect regular application usage. To determine the limit, I counted the per-minute requests per IP address over the past week to each of the endpoints.
Between both
![Screenshot 2020-10-05 at 5 27 49 PM](https://user-images.githubusercontent.com/29480346/95151562-88c8b380-07bd-11eb-85a5-0b935b9c9aa4.png)
v2/submissions
endpoints, the maximum was 46 requests to/v2/submissions/email
:Multiplying this by 4 to get the maximum seemed reasonable, since programmatic attacks are likely to exceed regular usage numbers by far more. Moreover, the limit is applied per-instance rather than across instances (see next section). Hence I chose a per-minute limit of 200.
As for the
/auth/sendotp
endpoint, the maximum was 4 requests in a minute. To be safe, I set the per-minute maximum at 60.Per-instance vs instance-wide limits
express-rate-limit
uses an in-memory store by default. This means that the rate limits are applied per-instance. Applying them instance-wide would require setting up a store in our database using MongoDB plugin. However, the plugin does not seem to support reusing existing connections, which means we would have to create a new connection to the database. I decided against this to avoid concurrency issues with multiple instances accessing the same documents, so the limits are applied per-instance.Tests
SUBMISSIONS_RATE_LIMIT=200
andSEND_AUTH_OTP_RATE_LIMIT=60
.SUBMISSIONS_RATE_LIMIT=2
andSEND_AUTH_OTP_RATE_LIMIT=2
.SUBMISSIONS_RATE_LIMIT=200
andSEND_AUTH_OTP_RATE_LIMIT=60
.