Skip to content

Commit eff11da

Browse files
authored
Merge pull request #462 from Akashkarmakar787/master
Minimum length validation for the recaptcha token param
2 parents 6e289d9 + 98ffb25 commit eff11da

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ Recaptcha.configure do |config|
589589
config.verify_url = 'https://hcaptcha.com/siteverify'
590590
config.api_server_url = 'https://hcaptcha.com/1/api.js'
591591
config.response_limit = 100000
592+
config.response_minimum = 100
592593
end
593594
```
594595

lib/recaptcha.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def self.skip_env?(env)
5555
end
5656

5757
def self.invalid_response?(resp)
58-
resp.empty? || resp.length > configuration.response_limit
58+
resp.empty? || resp.length > configuration.response_limit || resp.length < configuration.response_minimum
5959
end
6060

6161
def self.verify_via_api_call(response, options)

lib/recaptcha/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Configuration
3838
}.freeze
3939

4040
attr_accessor :default_env, :skip_verify_env, :proxy, :secret_key, :site_key, :handle_timeouts_gracefully,
41-
:hostname, :enterprise, :enterprise_api_key, :enterprise_project_id, :response_limit
41+
:hostname, :enterprise, :enterprise_api_key, :enterprise_project_id, :response_limit, :response_minimum
4242
attr_writer :api_server_url, :verify_url
4343

4444
def initialize # :nodoc:
@@ -57,6 +57,7 @@ def initialize # :nodoc:
5757
@api_server_url = nil
5858

5959
@response_limit = 4000
60+
@response_minimum = 100
6061
end
6162

6263
def secret_key!

test/verify_enterprise_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def initialize
180180
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
181181
end
182182

183-
it "does not verify via http call when response length exceeds G_RESPONSE_LIMIT" do
183+
it "does not verify via http call when response length exceeds limit" do
184184
# this returns a 400 or 413 instead of a 200 response with error code
185185
# typical response length is less than 400 characters
186186
str = "a" * 4001
@@ -190,6 +190,16 @@ def initialize
190190
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
191191
end
192192

193+
it "does not verify via http call when response length below limit" do
194+
# this returns a 400 or 413 instead of a 200 response with error code
195+
# typical response length is less than 100 characters
196+
str = "a" * 99
197+
@controller.params = { 'g-recaptcha-response' => "#{str}"}
198+
assert_not_requested :get, %r{\.google\.com}
199+
assert_equal false, @controller.verify_recaptcha
200+
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
201+
end
202+
193203
describe ':hostname' do
194204
let(:hostname) { 'fake.hostname.com' }
195205

test/verify_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def initialize
199199
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
200200
end
201201

202-
it "does not verify via http call when response length exceeds G_RESPONSE_LIMIT" do
202+
it "does not verify via http call when response length exceeds limit" do
203203
# this returns a 400 or 413 instead of a 200 response with error code
204204
# typical response length is less than 400 characters
205205
str = "a" * 4001
@@ -209,6 +209,16 @@ def initialize
209209
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
210210
end
211211

212+
it "does not verify via http call when response length below limit" do
213+
# this returns a 400 or 413 instead of a 200 response with error code
214+
# typical response length is less than 100 characters
215+
str = "a" * 99
216+
@controller.params = { 'g-recaptcha-response' => "#{str}"}
217+
assert_not_requested :get, %r{\.google\.com}
218+
assert_equal false, @controller.verify_recaptcha
219+
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
220+
end
221+
212222
describe ':hostname' do
213223
let(:hostname) { 'fake.hostname.com' }
214224

0 commit comments

Comments
 (0)