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

Rate limit endpoints that send emails using express middleware #4

Open
omarchehab98 opened this issue Mar 16, 2018 · 1 comment · May be fixed by #8
Open

Rate limit endpoints that send emails using express middleware #4

omarchehab98 opened this issue Mar 16, 2018 · 1 comment · May be fixed by #8

Comments

@omarchehab98
Copy link
Collaborator

Using the package express-limiter we should make it so that an email can only request a password reset or resending verification up to 3 emails per 15 minutes for example.

Take a look at the snippet below.

    limiter({
      path: '/forgotPassword',
      method: 'post',
      lookup: rateLimitEmailLookup,
      total: 3,
      expire: 15 * 60 * 1000,
      onRateLimited
    })

    limiter({
      path: '/requestActivateEmail',
      method: 'post',
      lookup: rateLimitEmailLookup,
      total: 3,
      expire: 15 * 60 * 1000,
      onRateLimited
    })

function onRateLimited (req, res, next) {
  res.status(429)
  res.json({
    message: 'Too many requests. Please try again later.'
  })
}

function rateLimitEmailLookup (req, res, opts, next) {
  if (req.body && req.body.email) {
    opts.lookup = 'body.email'
  }
  return next()
}

function rateLimitSessionOrIpLookup (req, res, opts, next) {
  const isProxied = req.connection.remoteAddress === '::1' ||
    req.connection.remoteAddress === '::ffff:127.0.0.1'
  if (isProxied) {
    opts.lookup = 'headers.x-forwarded-for'
    opts.total = 120
  } else {
    opts.lookup = 'connection.remoteAddress'
    opts.total = 120
  }
  const user = req.session && req.session.user
  if (user) {
    opts.lookup = 'session.user._id'
    opts.total = 240
  }
  return next()
}
@vinmaster
Copy link

Hi, I would like to tackle this issue

@vinmaster vinmaster linked a pull request Oct 2, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants