Просмотр исходного кода

Merge pull request #1713 from sharelatex/ns-captchas-two

turn off captchas for project invite

GitOrigin-RevId: 3e044ba8c6bb2ffb77ea4486f7b51a587133839c
nate stemen 7 лет назад
Родитель
Сommit
bec56ccafa

+ 22 - 17
services/web/app/coffee/Features/Captcha/CaptchaMiddleware.coffee

@@ -3,21 +3,26 @@ logger = require 'logger-sharelatex'
 Settings = require 'settings-sharelatex'
 Settings = require 'settings-sharelatex'
 
 
 module.exports = CaptchaMiddleware =
 module.exports = CaptchaMiddleware =
-	validateCaptcha: (req, res, next) ->
-		if !Settings.recaptcha?
-			return next()
-		response = req.body['g-recaptcha-response']
-		options =
-			form:
-				secret: Settings.recaptcha.secretKey
-				response: response
-			json: true
-		request.post "https://www.google.com/recaptcha/api/siteverify", options, (error, response, body) ->
-			return next(error) if error?
-			if !body?.success
-				logger.warn {statusCode: response.statusCode, body: body}, 'failed recaptcha siteverify request'
-				return res.status(400).send({errorReason:"cannot_verify_user_not_robot", message:
-					{text:"Sorry, we could not verify that you are not a robot. Please check that Google reCAPTCHA is not being blocked by an ad blocker or firewall."}
-				})
-			else
+	validateCaptcha: (action) ->
+		return (req, res, next) ->
+			if !Settings.recaptcha?.siteKey?
 				return next()
 				return next()
+			inviteAndCaptchaDisabled = action == 'invite' and Settings.recaptcha.disabled.invite
+			registerAndCaptchaDisabled = action == 'register' and Settings.recaptcha.disabled.register
+			if inviteAndCaptchaDisabled or registerAndCaptchaDisabled
+				return next()
+			response = req.body['g-recaptcha-response']
+			options =
+				form:
+					secret: Settings.recaptcha.secretKey
+					response: response
+				json: true
+			request.post "https://www.google.com/recaptcha/api/siteverify", options, (error, response, body) ->
+				return next(error) if error?
+				if !body?.success
+					logger.warn {statusCode: response.statusCode, body: body}, 'failed recaptcha siteverify request'
+					return res.status(400).send({errorReason:"cannot_verify_user_not_robot", message:
+						{text:"Sorry, we could not verify that you are not a robot. Please check that Google reCAPTCHA is not being blocked by an ad blocker or firewall."}
+					})
+				else
+					return next()

+ 1 - 1
services/web/app/coffee/Features/Collaborators/CollaboratorsRouter.coffee

@@ -33,7 +33,7 @@ module.exports =
 				maxRequests: 100
 				maxRequests: 100
 				timeInterval: 60 * 10
 				timeInterval: 60 * 10
 			}),
 			}),
-			CaptchaMiddleware.validateCaptcha,
+			CaptchaMiddleware.validateCaptcha('invite'),
 			AuthenticationController.requireLogin(),
 			AuthenticationController.requireLogin(),
 			AuthorizationMiddleware.ensureUserCanAdminProject,
 			AuthorizationMiddleware.ensureUserCanAdminProject,
 			CollaboratorsInviteController.inviteToProject
 			CollaboratorsInviteController.inviteToProject

+ 1 - 0
services/web/app/coffee/infrastructure/ExpressLocals.coffee

@@ -374,4 +374,5 @@ module.exports = (app, webRouter, privateApiRouter, publicApiRouter)->
 			appName: Settings.appName
 			appName: Settings.appName
 			siteUrl: Settings.siteUrl
 			siteUrl: Settings.siteUrl
 			recaptchaSiteKeyV3: Settings.recaptcha?.siteKeyV3
 			recaptchaSiteKeyV3: Settings.recaptcha?.siteKeyV3
+			recaptchaDisabled: Settings.recaptcha?.disabled
 		next()
 		next()

+ 5 - 0
services/web/config/settings.defaults.coffee

@@ -422,6 +422,11 @@ module.exports = settings =
 		# Example:
 		# Example:
 		#   header_extras: [{text: "Some Page", url: "http://example.com/some/page", class: "subdued"}]
 		#   header_extras: [{text: "Some Page", url: "http://example.com/some/page", class: "subdued"}]
 
 
+	recaptcha:
+		disabled:
+			invite: true
+			register: false
+
 	customisation: {}
 	customisation: {}
 
 
 #	templates: [{
 #	templates: [{

+ 2 - 1
services/web/public/src/ide/share/controllers/ShareProjectModalController.js

@@ -164,6 +164,7 @@ define(['base'], App =>
           // do v3 captcha to collect data only
           // do v3 captcha to collect data only
           validateCaptchaV3('invite')
           validateCaptchaV3('invite')
           // do v2 captcha
           // do v2 captcha
+          const ExposedSettings = window.ExposedSettings
           return validateCaptcha(function(response) {
           return validateCaptcha(function(response) {
             let inviteId, request
             let inviteId, request
             $scope.grecaptchaResponse = response
             $scope.grecaptchaResponse = response
@@ -231,7 +232,7 @@ define(['base'], App =>
                   return ($scope.state.errorReason = null)
                   return ($scope.state.errorReason = null)
                 }
                 }
               })
               })
-          })
+          }, ExposedSettings.recaptchaDisabled.invite)
         })()
         })()
       }
       }
 
 

+ 8 - 8
services/web/public/src/services/validateCaptcha.js

@@ -1,5 +1,4 @@
 /* eslint-disable
 /* eslint-disable
-    max-len,
     no-return-assign,
     no-return-assign,
     no-undef,
     no-undef,
 */
 */
@@ -7,27 +6,28 @@
 // Fix any style issues and re-enable lint.
 // Fix any style issues and re-enable lint.
 /*
 /*
  * decaffeinate suggestions:
  * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
  * DS102: Remove unnecessary code created because of implicit returns
  * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  */
  */
 define(['base'], App =>
 define(['base'], App =>
   App.factory('validateCaptcha', function() {
   App.factory('validateCaptcha', function() {
     let _recaptchaCallbacks = []
     let _recaptchaCallbacks = []
     const onRecaptchaSubmit = function(token) {
     const onRecaptchaSubmit = function(token) {
-      for (let cb of Array.from(_recaptchaCallbacks)) {
+      for (let cb of _recaptchaCallbacks) {
         cb(token)
         cb(token)
       }
       }
-      return (_recaptchaCallbacks = [])
+      _recaptchaCallbacks = []
     }
     }
 
 
     let recaptchaId = null
     let recaptchaId = null
-    const validateCaptcha = callback => {
+    const validateCaptcha = (callback, captchaDisabled) => {
       if (callback == null) {
       if (callback == null) {
         callback = function(response) {}
         callback = function(response) {}
       }
       }
-      if (typeof grecaptcha === 'undefined' || grecaptcha === null) {
+      if (
+        typeof grecaptcha === 'undefined' ||
+        grecaptcha === null ||
+        captchaDisabled
+      ) {
         return callback()
         return callback()
       }
       }
       const reset = () => grecaptcha.reset()
       const reset = () => grecaptcha.reset()