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

Fail fast for long g-responses #309

Merged
merged 1 commit into from
Apr 1, 2019
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
3 changes: 2 additions & 1 deletion lib/recaptcha/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module Recaptcha
module Verify
G_RESPONSE_LIMIT = 4000
# Your private API can be specified in the +options+ hash or preferably
# using the Configuration.
def verify_recaptcha(options = {})
Expand All @@ -15,7 +16,7 @@ def verify_recaptcha(options = {})
recaptcha_response = options[:response] || params['g-recaptcha-response'].to_s

begin
verified = if recaptcha_response.empty?
verified = if recaptcha_response.empty? || recaptcha_response.length > G_RESPONSE_LIMIT
false
else
recaptcha_verify_via_api_call(request, recaptcha_response, options)
Expand Down
10 changes: 10 additions & 0 deletions test/verify_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
end

it "does not verify via http call when response length exceeds G_RESPONSE_LIMIT" do
# this returns a 400 or 413 instead of a 200 response with error code
# typical response length is less than 400 characters
str = "a" * 4001
@controller.params = { 'g-recaptcha-response' => "#{str}"}
assert_not_requested :get, %r{\.google\.com}
assert_equal false, @controller.verify_recaptcha
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
end

describe ':hostname' do
let(:hostname) { 'fake.hostname.com' }

Expand Down