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

Minimum length validation for the recaptcha token param #462

Merged
merged 10 commits into from
Jan 14, 2025
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ Recaptcha.configure do |config|
config.verify_url = 'https://hcaptcha.com/siteverify'
config.api_server_url = 'https://hcaptcha.com/1/api.js'
config.response_limit = 100000
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would break anyone already using that field, how about adding response_minimum = 100 instead ?

config.response_minimum = 100
end
```

Expand Down
2 changes: 1 addition & 1 deletion lib/recaptcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def self.skip_env?(env)
end

def self.invalid_response?(resp)
resp.empty? || resp.length > configuration.response_limit
resp.empty? || resp.length > configuration.response_limit || resp.length < configuration.response_minimum
end

def self.verify_via_api_call(response, options)
Expand Down
3 changes: 2 additions & 1 deletion lib/recaptcha/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Configuration
}.freeze

attr_accessor :default_env, :skip_verify_env, :proxy, :secret_key, :site_key, :handle_timeouts_gracefully,
:hostname, :enterprise, :enterprise_api_key, :enterprise_project_id, :response_limit
:hostname, :enterprise, :enterprise_api_key, :enterprise_project_id, :response_limit, :response_minimum
attr_writer :api_server_url, :verify_url

def initialize # :nodoc:
Expand All @@ -57,6 +57,7 @@ def initialize # :nodoc:
@api_server_url = nil

@response_limit = 4000
@response_minimum = 100
end

def secret_key!
Expand Down
12 changes: 11 additions & 1 deletion test/verify_enterprise_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def initialize
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
it "does not verify via http call when response length exceeds 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
Expand All @@ -190,6 +190,16 @@ def initialize
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
end

it "does not verify via http call when response length below limit" do
# this returns a 400 or 413 instead of a 200 response with error code
# typical response length is less than 100 characters
str = "a" * 99
@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
12 changes: 11 additions & 1 deletion test/verify_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def initialize
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
it "does not verify via http call when response length exceeds 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
Expand All @@ -209,6 +209,16 @@ def initialize
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
end

it "does not verify via http call when response length below limit" do
# this returns a 400 or 413 instead of a 200 response with error code
# typical response length is less than 100 characters
str = "a" * 99
@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