More safelist/blocklist refactoring

- Add Rack::Attack namespace to deprecation warning.
- Add deprecated Rack::Attack.blacklisted_response attr methods.
This commit is contained in:
Aaron Suggs 2016-07-04 21:11:35 -04:00
parent 4f462c33dc
commit f5f08d56e5
3 changed files with 45 additions and 14 deletions

View file

@ -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

View file

@ -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

View file

@ -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