Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/forms/recaptcha_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class RecaptchaForm

VERIFICATION_ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify'
RESULT_ERRORS = ['missing-input-secret', 'invalid-input-secret'].freeze
EXEMPT_RESULT_REASONS = ['LOW_CONFIDENCE_SCORE'].freeze

attr_reader :recaptcha_action,
:recaptcha_token,
Expand Down Expand Up @@ -95,6 +96,7 @@ def faraday

def recaptcha_result_valid?(result)
return true if result.blank?
return true if result_reason_exempt?(result)

if result.success?
result.score >= score_threshold
Expand All @@ -107,6 +109,10 @@ def is_result_error?(error_code)
RESULT_ERRORS.include?(error_code)
end

def result_reason_exempt?(result)
(EXEMPT_RESULT_REASONS & result.reasons).any?
end

def log_analytics(result: nil, error: nil)
analytics&.recaptcha_verify_result_received(
recaptcha_result: result.to_h.presence,
Expand Down
40 changes: 40 additions & 0 deletions spec/forms/recaptcha_enterprise_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,46 @@
form_class: 'RecaptchaEnterpriseForm',
)
end

context 'with low confidence score as one of the reasons for failure' do
before do
stub_recaptcha_response(
body: {
tokenProperties: { valid: true, action: },
riskAnalysis: { score:, reasons: ['LOW_CONFIDENCE_SCORE'] },
event: {},
name:,
},
action:,
token:,
)
end

it 'is successful with assessment id' do
response, assessment_id = result

expect(response.to_h).to eq(success: true)
expect(assessment_id).to eq(name)
end

it 'logs analytics of the body' do
result

expect(analytics).to have_logged_event(
'reCAPTCHA verify result received',
recaptcha_result: {
success: true,
score:,
reasons: ['LOW_CONFIDENCE_SCORE'],
errors: [],
assessment_id: name,
},
evaluated_as_valid: true,
score_threshold: score_threshold,
form_class: 'RecaptchaEnterpriseForm',
)
end
end
end

context 'with successful score from validation service' do
Expand Down