mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
Merge pull request #183 from ktheory/v5.0-prep
More safelist/blocklist refactoring
This commit is contained in:
commit
f9a67202e4
9 changed files with 58 additions and 67 deletions
|
|
@ -6,11 +6,8 @@ rvm:
|
|||
- jruby-19mode
|
||||
|
||||
gemfile:
|
||||
- gemfiles/activesupport3.2.gemfile
|
||||
- gemfiles/activesupport4.0.gemfile
|
||||
- gemfiles/activesupport4.1.gemfile
|
||||
- gemfiles/activesupport4.2.gemfile
|
||||
- gemfiles/dalli1.1.gemfile
|
||||
- gemfiles/dalli2.gemfile
|
||||
|
||||
services:
|
||||
|
|
|
|||
|
|
@ -2,6 +2,15 @@
|
|||
|
||||
## master (unreleased)
|
||||
|
||||
## v5.0.0 (beta)
|
||||
|
||||
- Deprecate `whitelist`/`blacklist` in favor of `safelist`/`blocklist`. (#181,
|
||||
thanks @renee-travisci).
|
||||
|
||||
To upgrade and fix deprecations, find and replace instances of `whitelist` and
|
||||
`blacklist` with `safelist` and `blocklist`. If you reference `rack.attack.match_type`,
|
||||
note that it will have values like `:safelist`/`:blocklist`.
|
||||
|
||||
## v4.4.1 17 Feb 2016
|
||||
|
||||
- Fix a bug affecting apps using Redis::Store and ActiveSupport that could generate an error
|
||||
|
|
|
|||
10
README.md
10
README.md
|
|
@ -97,7 +97,7 @@ Define safelists, blocklists, throttles, and tracks as blocks that return truthy
|
|||
these go in an initializer in `config/initializers/`.
|
||||
A [Rack::Request](http://www.rubydoc.info/gems/rack/Rack/Request) object is passed to the block (named 'req' in the examples).
|
||||
|
||||
### safelists
|
||||
### Safelists
|
||||
|
||||
```ruby
|
||||
# Always allow requests from localhost
|
||||
|
|
@ -108,7 +108,7 @@ Rack::Attack.safelist('allow from localhost') do |req|
|
|||
end
|
||||
```
|
||||
|
||||
### blocklists
|
||||
### Blocklists
|
||||
|
||||
```ruby
|
||||
# Block requests from 1.2.3.4
|
||||
|
|
@ -138,11 +138,11 @@ Rack::Attack.blocklist('fail2ban pentesters') do |req|
|
|||
# so the request is blocked
|
||||
Rack::Attack::Fail2Ban.filter("pentesters-#{req.ip}", :maxretry => 3, :findtime => 10.minutes, :bantime => 5.minutes) do
|
||||
# The count for the IP is incremented if the return value is truthy
|
||||
CGI.unescape(req.query_string) =~ %r{/etc/passwd} ||
|
||||
CGI.unescape(req.query_string) =~ %r{/etc/passwd} ||
|
||||
req.path.include?('/etc/passwd') ||
|
||||
req.path.include?('wp-admin') ||
|
||||
req.path.include?('wp-admin') ||
|
||||
req.path.include?('wp-login')
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
```
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
# This file was generated by Appraisal
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
gem "activesupport", "~> 3.2.0"
|
||||
gem "actionpack", "~> 3.2.0"
|
||||
gem "listen", "<= 3.0.6", platforms: [:ruby_20, :ruby_21, :jruby]
|
||||
|
||||
group :development do
|
||||
gem "pry"
|
||||
gem "guard"
|
||||
gem "guard-minitest"
|
||||
end
|
||||
|
||||
gemspec :path => "../"
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
# This file was generated by Appraisal
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
gem "activesupport", "~> 4.0.0"
|
||||
gem "actionpack", "~> 4.0.0"
|
||||
gem "listen", "<= 3.0.6", platforms: [:ruby_20, :ruby_21, :jruby]
|
||||
|
||||
group :development do
|
||||
gem "pry"
|
||||
gem "guard"
|
||||
gem "guard-minitest"
|
||||
end
|
||||
|
||||
gemspec :path => "../"
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
# This file was generated by Appraisal
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
gem "dalli", "1.1.5"
|
||||
gem "rack", "<= 1.4.7", platforms: [:ruby_20, :ruby_21, :jruby]
|
||||
gem "activesupport", "<= 3.2.22.2", platforms: [:ruby_20, :ruby_21, :jruby]
|
||||
gem "listen", "<= 3.0.6", platforms: [:ruby_20, :ruby_21, :jruby]
|
||||
|
||||
group :development do
|
||||
gem "pry"
|
||||
gem "guard"
|
||||
gem "guard-minitest"
|
||||
end
|
||||
|
||||
gemspec :path => "../"
|
||||
|
|
@ -24,9 +24,9 @@ class Rack::Attack
|
|||
def safelist(name, &block)
|
||||
self.safelists[name] = Safelist.new(name, block)
|
||||
end
|
||||
|
||||
|
||||
def whitelist(name, &block)
|
||||
warn "[DEPRECATION] 'whitelist' is deprecated. Please use 'safelist' instead."
|
||||
warn "[DEPRECATION] 'Rack::Attack.whitelist' is deprecated. Please use 'safelist' instead."
|
||||
safelist(name, &block)
|
||||
end
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ class Rack::Attack
|
|||
end
|
||||
|
||||
def blacklist(name, &block)
|
||||
warn "[DEPRECATION] 'blacklist' is deprecated. Please use 'blocklist' instead."
|
||||
warn "[DEPRECATION] 'Rack::Attack.blacklist' is deprecated. Please use 'blocklist' instead."
|
||||
blocklist(name, &block)
|
||||
end
|
||||
|
||||
|
|
@ -53,12 +53,12 @@ class Rack::Attack
|
|||
def tracks; @tracks ||= {}; end
|
||||
|
||||
def whitelists
|
||||
warn "[DEPRECATION] 'whitelists' is deprecated. Please use 'safelists' instead."
|
||||
warn "[DEPRECATION] 'Rack::Attack.whitelists' is deprecated. Please use 'safelists' instead."
|
||||
safelists
|
||||
end
|
||||
|
||||
def blacklists
|
||||
warn "[DEPRECATION] 'blacklists' is deprecated. Please use 'blocklists' instead."
|
||||
warn "[DEPRECATION] 'Rack::Attack.blacklists' is deprecated. Please use 'blocklists' instead."
|
||||
blocklists
|
||||
end
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ class Rack::Attack
|
|||
end
|
||||
|
||||
def whitelisted?
|
||||
warn "[DEPRECATION] 'whitelisted?' is deprecated. Please use 'safelisted?' instead."
|
||||
warn "[DEPRECATION] 'Rack::Attack.whitelisted?' is deprecated. Please use 'safelisted?' instead."
|
||||
safelisted?
|
||||
end
|
||||
|
||||
|
|
@ -80,7 +80,7 @@ class Rack::Attack
|
|||
end
|
||||
|
||||
def blacklisted?
|
||||
warn "[DEPRECATION] 'blacklisted?' is deprecated. Please use 'blocklisted?' instead."
|
||||
warn "[DEPRECATION] 'Rack::Attack.blacklisted?' is deprecated. Please use 'blocklisted?' instead."
|
||||
blocklisted?
|
||||
end
|
||||
|
||||
|
|
@ -108,6 +108,16 @@ class Rack::Attack
|
|||
@safelists, @blocklists, @throttles, @tracks = {}, {}, {}, {}
|
||||
end
|
||||
|
||||
def blacklisted_response=(res)
|
||||
warn "[DEPRECATION] 'Rack::Attack.blacklisted_response=' is deprecated. Please use 'blocklisted_response=' instead."
|
||||
self.blocklisted_response=(res)
|
||||
end
|
||||
|
||||
def blacklisted_response
|
||||
warn "[DEPRECATION] 'Rack::Attack.blacklisted_response' is deprecated. Please use 'blocklisted_response' instead."
|
||||
self.blocklisted_response
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Set defaults
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
module Rack
|
||||
class Attack
|
||||
VERSION = '4.4.1'
|
||||
VERSION = '5.0.0.beta1'
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -23,12 +23,12 @@ describe 'Rack::Attack' do
|
|||
it('has a blocklist') {
|
||||
Rack::Attack.blocklists.key?("ip #{@bad_ip}").must_equal true
|
||||
}
|
||||
|
||||
|
||||
it('has a blacklist with a deprication warning') {
|
||||
stdout, stderror = capture_io do
|
||||
_, stderror = capture_io do
|
||||
Rack::Attack.blacklists.key?("ip #{@bad_ip}").must_equal true
|
||||
end
|
||||
assert_match "[DEPRECATION] 'blacklists' is deprecated. Please use 'blocklists' instead.", stderror
|
||||
assert_match "[DEPRECATION] 'Rack::Attack.blacklists' is deprecated. Please use 'blocklists' instead.", stderror
|
||||
}
|
||||
|
||||
describe "a bad request" do
|
||||
|
|
@ -55,10 +55,10 @@ describe 'Rack::Attack' do
|
|||
it('has a safelist'){ Rack::Attack.safelists.key?("good ua") }
|
||||
|
||||
it('has a whitelist with a deprication warning') {
|
||||
stdout, stderror = capture_io do
|
||||
_, stderror = capture_io do
|
||||
Rack::Attack.whitelists.key?("good ua")
|
||||
end
|
||||
assert_match "[DEPRECATION] 'whitelists' is deprecated. Please use 'safelists' instead.", stderror
|
||||
assert_match "[DEPRECATION] 'Rack::Attack.whitelists' is deprecated. Please use 'safelists' instead.", stderror
|
||||
}
|
||||
|
||||
describe "with a request match both safelist & blocklist" do
|
||||
|
|
@ -73,6 +73,27 @@ describe 'Rack::Attack' do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#blocklisted_response' do
|
||||
it 'should exist' do
|
||||
Rack::Attack.blocklisted_response.must_respond_to :call
|
||||
end
|
||||
|
||||
it 'should give a deprication warning for blacklisted_response' do
|
||||
_, stderror = capture_io do
|
||||
Rack::Attack.blacklisted_response
|
||||
end
|
||||
assert_match "[DEPRECATION] 'Rack::Attack.blacklisted_response' is deprecated. Please use 'blocklisted_response' instead.", stderror
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe '#throttled_response' do
|
||||
it 'should exist' do
|
||||
Rack::Attack.throttled_response.must_respond_to :call
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue