mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
More safelist/blocklist refactoring
- Add Rack::Attack namespace to deprecation warning. - Add deprecated Rack::Attack.blacklisted_response attr methods.
This commit is contained in:
parent
4f462c33dc
commit
f5f08d56e5
3 changed files with 45 additions and 14 deletions
|
|
@ -97,7 +97,7 @@ Define safelists, blocklists, throttles, and tracks as blocks that return truthy
|
||||||
these go in an initializer in `config/initializers/`.
|
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).
|
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
|
```ruby
|
||||||
# Always allow requests from localhost
|
# Always allow requests from localhost
|
||||||
|
|
@ -108,7 +108,7 @@ Rack::Attack.safelist('allow from localhost') do |req|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
### blocklists
|
### Blocklists
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
# Block requests from 1.2.3.4
|
# Block requests from 1.2.3.4
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,9 @@ class Rack::Attack
|
||||||
def safelist(name, &block)
|
def safelist(name, &block)
|
||||||
self.safelists[name] = Safelist.new(name, block)
|
self.safelists[name] = Safelist.new(name, block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def whitelist(name, &block)
|
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)
|
safelist(name, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -35,7 +35,7 @@ class Rack::Attack
|
||||||
end
|
end
|
||||||
|
|
||||||
def blacklist(name, &block)
|
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)
|
blocklist(name, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -53,12 +53,12 @@ class Rack::Attack
|
||||||
def tracks; @tracks ||= {}; end
|
def tracks; @tracks ||= {}; end
|
||||||
|
|
||||||
def whitelists
|
def whitelists
|
||||||
warn "[DEPRECATION] 'whitelists' is deprecated. Please use 'safelists' instead."
|
warn "[DEPRECATION] 'Rack::Attack.whitelists' is deprecated. Please use 'safelists' instead."
|
||||||
safelists
|
safelists
|
||||||
end
|
end
|
||||||
|
|
||||||
def blacklists
|
def blacklists
|
||||||
warn "[DEPRECATION] 'blacklists' is deprecated. Please use 'blocklists' instead."
|
warn "[DEPRECATION] 'Rack::Attack.blacklists' is deprecated. Please use 'blocklists' instead."
|
||||||
blocklists
|
blocklists
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -69,7 +69,7 @@ class Rack::Attack
|
||||||
end
|
end
|
||||||
|
|
||||||
def whitelisted?
|
def whitelisted?
|
||||||
warn "[DEPRECATION] 'whitelisted?' is deprecated. Please use 'safelisted?' instead."
|
warn "[DEPRECATION] 'Rack::Attack.whitelisted?' is deprecated. Please use 'safelisted?' instead."
|
||||||
safelisted?
|
safelisted?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -80,7 +80,7 @@ class Rack::Attack
|
||||||
end
|
end
|
||||||
|
|
||||||
def blacklisted?
|
def blacklisted?
|
||||||
warn "[DEPRECATION] 'blacklisted?' is deprecated. Please use 'blocklisted?' instead."
|
warn "[DEPRECATION] 'Rack::Attack.blacklisted?' is deprecated. Please use 'blocklisted?' instead."
|
||||||
blocklisted?
|
blocklisted?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -108,6 +108,16 @@ class Rack::Attack
|
||||||
@safelists, @blocklists, @throttles, @tracks = {}, {}, {}, {}
|
@safelists, @blocklists, @throttles, @tracks = {}, {}, {}, {}
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
# Set defaults
|
# Set defaults
|
||||||
|
|
|
||||||
|
|
@ -23,12 +23,12 @@ describe 'Rack::Attack' do
|
||||||
it('has a blocklist') {
|
it('has a blocklist') {
|
||||||
Rack::Attack.blocklists.key?("ip #{@bad_ip}").must_equal true
|
Rack::Attack.blocklists.key?("ip #{@bad_ip}").must_equal true
|
||||||
}
|
}
|
||||||
|
|
||||||
it('has a blacklist with a deprication warning') {
|
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
|
Rack::Attack.blacklists.key?("ip #{@bad_ip}").must_equal true
|
||||||
end
|
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
|
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 safelist'){ Rack::Attack.safelists.key?("good ua") }
|
||||||
|
|
||||||
it('has a whitelist with a deprication warning') {
|
it('has a whitelist with a deprication warning') {
|
||||||
stdout, stderror = capture_io do
|
_, stderror = capture_io do
|
||||||
Rack::Attack.whitelists.key?("good ua")
|
Rack::Attack.whitelists.key?("good ua")
|
||||||
end
|
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
|
describe "with a request match both safelist & blocklist" do
|
||||||
|
|
@ -73,6 +73,27 @@ describe 'Rack::Attack' do
|
||||||
end
|
end
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue