RateLimiterMiddleware.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const RateLimiter = require('../../infrastructure/RateLimiter')
  2. const logger = require('@overleaf/logger')
  3. const SessionManager = require('../Authentication/SessionManager')
  4. const LoginRateLimiter = require('./LoginRateLimiter')
  5. const settings = require('@overleaf/settings')
  6. /*
  7. Do not allow more than opts.maxRequests from a single client in
  8. opts.timeInterval. Pass an array of opts.params to segment this based on
  9. parameters in the request URL, e.g.:
  10. app.get "/project/:project_id", RateLimiterMiddleware.rateLimit(endpointName: "open-editor", params: ["project_id"])
  11. will rate limit each project_id separately.
  12. Unique clients are identified by user_id if logged in, and IP address if not.
  13. */
  14. function rateLimit(opts) {
  15. const getUserId =
  16. opts.getUserId || (req => SessionManager.getLoggedInUserId(req.session))
  17. return function (req, res, next) {
  18. const userId = getUserId(req) || req.ip
  19. if (
  20. settings.smokeTest &&
  21. settings.smokeTest.userId &&
  22. settings.smokeTest.userId.toString() === userId.toString()
  23. ) {
  24. // ignore smoke test user
  25. return next()
  26. }
  27. const params = (opts.params || []).map(p => req.params[p])
  28. params.push(userId)
  29. let subjectName = params.join(':')
  30. if (opts.ipOnly) {
  31. subjectName = req.ip
  32. }
  33. if (opts.endpointName == null) {
  34. throw new Error('no endpointName provided')
  35. }
  36. const options = {
  37. endpointName: opts.endpointName,
  38. timeInterval: opts.timeInterval || 60,
  39. subjectName,
  40. throttle: opts.maxRequests || 6,
  41. }
  42. return RateLimiter.addCount(options, function (error, canContinue) {
  43. if (error != null) {
  44. return next(error)
  45. }
  46. if (canContinue) {
  47. return next()
  48. } else {
  49. logger.warn(options, 'rate limit exceeded')
  50. res.status(429) // Too many requests
  51. res.write('Rate limit reached, please try again later')
  52. return res.end()
  53. }
  54. })
  55. }
  56. }
  57. function loginRateLimit(req, res, next) {
  58. const { email } = req.body
  59. if (!email) {
  60. return next()
  61. }
  62. LoginRateLimiter.processLoginRequest(email, function (err, isAllowed) {
  63. if (err) {
  64. return next(err)
  65. }
  66. if (isAllowed) {
  67. return next()
  68. } else {
  69. logger.warn({ email }, 'rate limit exceeded')
  70. res.status(429) // Too many requests
  71. res.write('Rate limit reached, please try again later')
  72. return res.end()
  73. }
  74. })
  75. }
  76. const RateLimiterMiddleware = {
  77. rateLimit,
  78. loginRateLimit,
  79. }
  80. module.exports = RateLimiterMiddleware