CaptchaMiddleware.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const { fetchJson } = require('@overleaf/fetch-utils')
  2. const logger = require('@overleaf/logger')
  3. const Settings = require('@overleaf/settings')
  4. const Metrics = require('@overleaf/metrics')
  5. const OError = require('@overleaf/o-error')
  6. const DeviceHistory = require('./DeviceHistory')
  7. const AuthenticationController = require('../Authentication/AuthenticationController')
  8. const { expressify } = require('../../util/promises')
  9. function respondInvalidCaptcha(req, res) {
  10. res.status(400).json({
  11. errorReason: 'cannot_verify_user_not_robot',
  12. message: {
  13. text: req.i18n.translate('cannot_verify_user_not_robot'),
  14. },
  15. })
  16. }
  17. async function initializeDeviceHistory(req) {
  18. req.deviceHistory = new DeviceHistory()
  19. try {
  20. await req.deviceHistory.parse(req)
  21. } catch (err) {
  22. logger.err({ err }, 'cannot parse deviceHistory')
  23. }
  24. }
  25. async function canSkipCaptcha(req, res) {
  26. await initializeDeviceHistory(req)
  27. const canSkip = req.deviceHistory.has(req.body?.email)
  28. Metrics.inc('captcha_pre_flight', 1, {
  29. status: canSkip ? 'skipped' : 'missing',
  30. })
  31. res.json(canSkip)
  32. }
  33. function validateCaptcha(action) {
  34. return expressify(async function (req, res, next) {
  35. if (!Settings.recaptcha?.siteKey || Settings.recaptcha.disabled[action]) {
  36. if (action === 'login') {
  37. AuthenticationController.setAuditInfo(req, { captcha: 'disabled' })
  38. }
  39. Metrics.inc('captcha', 1, { path: action, status: 'disabled' })
  40. return next()
  41. }
  42. const reCaptchaResponse = req.body['g-recaptcha-response']
  43. if (action === 'login') {
  44. await initializeDeviceHistory(req)
  45. if (!reCaptchaResponse && req.deviceHistory.has(req.body?.email)) {
  46. // The user has previously logged in from this device, which required
  47. // solving a captcha or keeping the device history alive.
  48. // We can skip checking the (missing) captcha response.
  49. AuthenticationController.setAuditInfo(req, { captcha: 'skipped' })
  50. Metrics.inc('captcha', 1, { path: action, status: 'skipped' })
  51. return next()
  52. }
  53. }
  54. if (!reCaptchaResponse) {
  55. Metrics.inc('captcha', 1, { path: action, status: 'missing' })
  56. return respondInvalidCaptcha(req, res)
  57. }
  58. let body
  59. try {
  60. body = await fetchJson(Settings.recaptcha.endpoint, {
  61. method: 'POST',
  62. body: new URLSearchParams([
  63. ['secret', Settings.recaptcha.secretKey],
  64. ['response', reCaptchaResponse],
  65. ]),
  66. })
  67. } catch (err) {
  68. Metrics.inc('captcha', 1, { path: action, status: 'error' })
  69. throw OError.tag(err, 'failed recaptcha siteverify request', {
  70. body: err.body,
  71. })
  72. }
  73. if (!body.success) {
  74. logger.warn(
  75. { statusCode: 200, body },
  76. 'failed recaptcha siteverify request'
  77. )
  78. Metrics.inc('captcha', 1, { path: action, status: 'failed' })
  79. return respondInvalidCaptcha(req, res)
  80. }
  81. Metrics.inc('captcha', 1, { path: action, status: 'solved' })
  82. if (action === 'login') {
  83. AuthenticationController.setAuditInfo(req, { captcha: 'solved' })
  84. }
  85. next()
  86. })
  87. }
  88. module.exports = {
  89. respondInvalidCaptcha,
  90. validateCaptcha,
  91. canSkipCaptcha: expressify(canSkipCaptcha),
  92. }