magda-io / magda

A federated, open-source data catalog for all your big data and small data
https://magda.io
Apache License 2.0
513 stars 93 forks source link

Rate Limiting By IP #1283

Open AlexGilleran opened 6 years ago

AlexGilleran commented 6 years ago

Description

Currently it's possible for anyone to hit any API as fast as they want. This has been mostly fine so far because the public APIs we expose aren't really that expensive (they just retrieve info which can be cached anyway), but it'd be wise to build this in, especially as we start exposing other, more expensive operations via API keys.

Acceptance Criteria

Technical Notes

sajidanower23 commented 4 years ago

Adding my note on rate limiting in #2883

Rate Limiting

Rate limiting is important to prevent the abuse of our API, since API keys are being provided for free.

Express has a library express-rate-limit. that can be used to rate-limit endpoints as express middleware.

Code for that would look like this:

import rateLimit from 'express-rate-limit';

export const rateLimiter = rateLimit({
  windowMs: 60 * 1000, // 1 minute, in milliseconds
  max: 1000,
  message: 'You have exceeded the API rate limit',
  headers: true,
});

router.get('/endpoint', rateLimiter, function(req, res) {
    return "Woah";
})

This will result in denial of requests if the limit is exceeded.

To go into more depth, check out these articles:

https://nordicapis.com/everything-you-need-to-know-about-api-rate-limiting

https://blog.logrocket.com/rate-limiting-node-js