rack-attack-recaptcha 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d867b620635d9c9982cb3de3d29ea2ccfa31978e
4
+ data.tar.gz: 5ae0d7485b457f2f3371b2ad1a56dbfdee35d2f7
5
+ SHA512:
6
+ metadata.gz: c589069fdc7844543f70b124f8a06c0d30e65d30a5a82366b6b73191d01f8024ac6a16a558d55ade91ff79e4bf24b0e72753fe59589d1cf702c309c875068451
7
+ data.tar.gz: 70ee51e0dc169413fac883117a1b9930bebc882ab94fc6b41f12b4206b3186ad263514efeec032c0c27bc12b63a8d0fbe4ced74bfb8ae35a5b07eff0785742d1
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-attack-recaptcha.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Omer Rauchwerger
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Rack::Attack::Recaptcha
2
+
3
+ rack-attack-recaptcha allows you to easily respond to requests that get
4
+ throttled by [rack-attack](https://github.com/kickstarter/rack-attack) by showing a captcha instead of the "Retry
5
+ later" message you get with [rack-attack](https://github.com/kickstarter/rack-attack). This will allow you to keep
6
+ your app functional even in the event of an attack from a public
7
+ IP, for example, since legit users who share that IP will still be able
8
+ to access the app by entering a captcha.
9
+
10
+ rack-attack-recaptcha works similarly to [recaptch](http://github.com/ambethia/recaptcha). It gives you 2 helper methods
11
+ (`recaptcha_tags_if_under_attack` for views and
12
+ `verify_recaptcha_if_under_attack` for controllers) which will simply
13
+ call `recaptcha_tags` or `verify_recaptcha` (provided by [recaptcha](http://github.com/ambethia/recaptcha)) accordingly when the current
14
+ request should be throttled. If the request is not part of
15
+ an attack,
16
+ `verify_tags_if_under_attack` will display nothing and
17
+ `verify_recaptcha_if_under_attack` will simply return `true`.
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ gem 'rack-attack-recaptcha'
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Tell your app to use the Rack::Attack middleware. For Rails 3+ apps:
30
+
31
+ # In config/application.rb
32
+ config.middleware.use Rack::Attack::Recaptcha
33
+
34
+ To setup throttles, check out rack-attack's wiki.
35
+ To setup Recaptcha credentials, check out [recaptcha](http://github.com/ambethia/recaptcha)'s wiki.
36
+
37
+ ## Usage
38
+
39
+ After you've setup your Recaptcha credentials and defined some throttles, add `recaptcha_tags_if_under_attack` to each form you want to protect. Place it where
40
+ you want the Recaptcha widget to appear.
41
+
42
+ Example:
43
+
44
+ # app/views/foos/foo.html.erb
45
+
46
+ <%= form_for @foo do |f| %>
47
+ # ... additional lines truncated for brevity ...
48
+ <%= recaptcha_tags_if_under_attack %>
49
+ # ... additional lines truncated for brevity ...
50
+ <% end %>
51
+
52
+ _(If the request is legit, `recaptcha_tags_if_under_attack` will render nothing.)_
53
+
54
+ Now add `verify_recaptcha_if_under_attack` logic to each form action that you've
55
+ protected:
56
+
57
+ # app/controllers/foos_controller.rb
58
+
59
+ def create
60
+ if verify_recaptcha_if_under_attack && @foo.save
61
+ # ...
62
+ else
63
+ # ...
64
+ end
65
+ end
66
+
67
+ _(If the request is legit,
68
+ `verify_recaptcha_if_under_attack` won't actually check the captcha and
69
+ will simply return `true`)_
70
+
71
+ Note that `recaptcha_tags_if_under_attack` and `verify_recaptcha_if_under_attack`
72
+ pass all options to `recaptcha_tags` and `verify_recaptcha` so you can
73
+ use all the configuration values that are provided by [recaptcha](http://github.com/ambethia/recaptcha). (See [recaptcha](http://github.com/ambethia/recaptcha)'s documentation for a list of all the configuration options)
74
+
75
+ ## Contributing
76
+
77
+ 1. Fork it
78
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
79
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
80
+ 4. Push to the branch (`git push origin my-new-feature`)
81
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task :test do
5
+ $LOAD_PATH.unshift('lib', 'spec')
6
+ Dir.glob('./spec/**/*_spec.rb') { |f| require f }
7
+ end
@@ -0,0 +1,11 @@
1
+ module Rack
2
+ module Attack
3
+ module Recaptcha
4
+ module ClientHelper
5
+ def recaptcha_tags_if_under_attack(options={})
6
+ recaptcha_tags(options) if request.env["rack.attack.use_recaptcha"]
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ require "recaptcha/rails"
2
+
3
+ ActionView::Base.send(:include, Rack::Attack::Recaptcha::ClientHelper)
4
+ ActionController::Base.send(:include, Rack::Attack::Recaptcha::VerificationHelper)
@@ -0,0 +1,15 @@
1
+ module Rack
2
+ module Attack
3
+ module Recaptcha
4
+ module VerificationHelper
5
+ def verify_recaptcha_if_under_attack(options={})
6
+ if request.env["rack.attack.use_recaptcha"]
7
+ verify_recaptcha(options)
8
+ else
9
+ true
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ module Attack
3
+ module Recaptcha
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ require "rack/attack"
2
+ require "rack/attack/recaptcha/version"
3
+ require "rack/attack/recaptcha/client_helper"
4
+ require "rack/attack/recaptcha/verification_helper"
5
+ require "rack/attack/recaptcha/rails"
6
+
7
+ module Rack
8
+ module Attack
9
+ module Recaptcha
10
+ class << self
11
+ def new(app)
12
+ @rack_attack = Rack::Attack.new(app).tap { |attack|
13
+ attack.throttled_response = lambda { |env|
14
+ env["rack.attack.use_recaptcha"] = true
15
+ app.call(env)
16
+ }
17
+ }
18
+
19
+ self
20
+ end
21
+
22
+ def call(env)
23
+ @rack_attack.call(env)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/attack/recaptcha/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-attack-recaptcha"
8
+ spec.version = Rack::Attack::Recaptcha::VERSION
9
+ spec.authors = ["Omer Lachish-Rauchwerger"]
10
+ spec.email = ["omer@rauchy.net"]
11
+ spec.description = %q{An extension for Rack::Attack that supports responding to throttled requests with Recaptcha tags}
12
+ spec.summary = %q{Block & throttle abusive requests with Recaptcha}
13
+ spec.homepage = "http://github.com/rauchy/rack-attack-recaptcha"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rack"
24
+ spec.add_development_dependency "rack-test"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency 'activesupport', '>= 3.0.0'
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "rspec-mocks", "~> 3.0.2"
29
+
30
+ spec.add_dependency "rack-attack"
31
+ spec.add_dependency "recaptcha"
32
+ end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+ require "ostruct"
3
+
4
+ class DummyView
5
+ include Rack::Attack::Recaptcha::ClientHelper
6
+
7
+ def recaptcha_tags(options)
8
+ "yay recaptcha! #{options[:foo]}!"
9
+ end
10
+
11
+ def request
12
+ @request ||= OpenStruct.new(env: {})
13
+ end
14
+ end
15
+
16
+ module Rack
17
+ module Attack
18
+ module Recaptcha
19
+ describe ClientHelper do
20
+ describe ".recaptcha_tags_if_under_attack" do
21
+ it "delegates to Recaptcha if under attack" do
22
+ dummy_view = DummyView.new
23
+ dummy_view.request.env["rack.attack.use_recaptcha"] = true
24
+
25
+ tags = dummy_view.recaptcha_tags_if_under_attack(foo: "yay")
26
+
27
+ tags.must_equal("yay recaptcha! yay!")
28
+ end
29
+
30
+ it "does nothing when not under attack" do
31
+ dummy_view = DummyView.new
32
+
33
+ tags = dummy_view.recaptcha_tags_if_under_attack(foo: "yay")
34
+
35
+ tags.must_be_nil
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe "Rack::Attack::Recaptcha" do
4
+ include Rack::Test::Methods
5
+
6
+ def set_last_env(env)
7
+ @last_env = env
8
+ end
9
+
10
+ def app
11
+ that = self
12
+ Rack::Builder.new {
13
+ use Rack::Attack::Recaptcha
14
+ run lambda {|env| that.set_last_env(env) ; [200, {}, ["Hello World"]]}
15
+ }.to_app
16
+ end
17
+
18
+ before do
19
+ @bad_ip = "1.2.3.4"
20
+ Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
21
+ Rack::Attack.throttle("req/ip", :limit => 1, :period => 1) { |req| req.ip }
22
+ end
23
+
24
+ it "always delegates request to the underlying app" do
25
+ get "/", {}, "REMOTE_ADDR" => @bad_ip
26
+
27
+ last_response.body.must_equal "Hello World"
28
+ end
29
+
30
+ it "doesn't add a recaptcha flag to the environment when throttle is below the limit" do
31
+ get "/", {}, "REMOTE_ADDR" => @bad_ip
32
+
33
+ @last_env.keys.wont_include "rack.attack.use_recaptcha"
34
+ end
35
+
36
+ it "adds a recaptcha flag to the environment when hitting the throttle limit" do
37
+ 10.times { get "/", {}, "REMOTE_ADDR" => @bad_ip }
38
+
39
+ @last_env["rack.attack.use_recaptcha"].must_equal true
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ require "minitest/autorun"
2
+ require "minitest/pride"
3
+ require "rack/test"
4
+ require "rack/attack/recaptcha"
5
+ require "active_support"
6
+
7
+ require 'rspec/mocks'
8
+
9
+ module MinitestRSpecMocksIntegration
10
+ include ::RSpec::Mocks::ExampleMethods
11
+
12
+ def before_setup
13
+ ::RSpec::Mocks.setup
14
+ super
15
+ end
16
+
17
+ def after_teardown
18
+ super
19
+ ::RSpec::Mocks.verify
20
+ ensure
21
+ ::RSpec::Mocks.teardown
22
+ end
23
+ end
24
+
25
+ Minitest::Spec.send(:include, MinitestRSpecMocksIntegration)
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+ require "ostruct"
3
+
4
+ class DummyController
5
+ include Rack::Attack::Recaptcha::VerificationHelper
6
+
7
+ def verify_recaptcha(options)
8
+ "yay recaptcha! #{options[:foo]}!"
9
+ end
10
+
11
+ def request
12
+ @request ||= OpenStruct.new(env: {})
13
+ end
14
+ end
15
+
16
+ module Rack
17
+ module Attack
18
+ module Recaptcha
19
+ describe ClientHelper do
20
+ describe ".verify_recaptcha_if_under_attack" do
21
+ it "delegates to Recaptcha if under attack" do
22
+ dummy_controller = DummyController.new
23
+ dummy_controller.request.env["rack.attack.use_recaptcha"] = true
24
+
25
+ verification = dummy_controller.verify_recaptcha_if_under_attack(foo: "yay")
26
+
27
+ verification.must_equal("yay recaptcha! yay!")
28
+ end
29
+
30
+ it "returns true by default when not under attack" do
31
+ dummy_controller = DummyController.new
32
+
33
+ verification = dummy_controller.verify_recaptcha_if_under_attack(foo: "yay")
34
+
35
+ verification.must_equal true
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-attack-recaptcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Omer Lachish-Rauchwerger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-mocks
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: rack-attack
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: recaptcha
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: An extension for Rack::Attack that supports responding to throttled requests
154
+ with Recaptcha tags
155
+ email:
156
+ - omer@rauchy.net
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - Gemfile
163
+ - LICENSE.txt
164
+ - README.md
165
+ - Rakefile
166
+ - lib/rack/attack/recaptcha.rb
167
+ - lib/rack/attack/recaptcha/client_helper.rb
168
+ - lib/rack/attack/recaptcha/rails.rb
169
+ - lib/rack/attack/recaptcha/verification_helper.rb
170
+ - lib/rack/attack/recaptcha/version.rb
171
+ - rack-attack-recaptcha.gemspec
172
+ - spec/client_helper_spec.rb
173
+ - spec/rack_attack_recaptcha_spec.rb
174
+ - spec/spec_helper.rb
175
+ - spec/verification_helper_spec.rb
176
+ homepage: http://github.com/rauchy/rack-attack-recaptcha
177
+ licenses:
178
+ - MIT
179
+ metadata: {}
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubyforge_project:
196
+ rubygems_version: 2.2.2
197
+ signing_key:
198
+ specification_version: 4
199
+ summary: Block & throttle abusive requests with Recaptcha
200
+ test_files:
201
+ - spec/client_helper_spec.rb
202
+ - spec/rack_attack_recaptcha_spec.rb
203
+ - spec/spec_helper.rb
204
+ - spec/verification_helper_spec.rb