Merge pull request #54 from stevehodgkiss/dont_override_new_and_return_a_class

Rack::Attack.new should return an instance of a class, not the Rack::Attack constant
This commit is contained in:
Aaron Suggs 2014-04-03 22:37:05 -04:00
commit ea09a49538
11 changed files with 46 additions and 40 deletions

View file

@ -1,5 +1,7 @@
require 'rack'
module Rack::Attack
require 'forwardable'
class Rack::Attack
autoload :Cache, 'rack/attack/cache'
autoload :Check, 'rack/attack/check'
autoload :Throttle, 'rack/attack/throttle'
@ -35,35 +37,6 @@ module Rack::Attack
def throttles; @throttles ||= {}; end
def tracks; @tracks ||= {}; end
def new(app)
@app = app
# Set defaults
@notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
@blacklisted_response ||= lambda {|env| [403, {}, ["Forbidden\n"]] }
@throttled_response ||= lambda {|env|
retry_after = env['rack.attack.match_data'][:period] rescue nil
[429, {'Retry-After' => retry_after.to_s}, ["Retry later\n"]]
}
self
end
def call(env)
req = Rack::Request.new(env)
if whitelisted?(req)
@app.call(env)
elsif blacklisted?(req)
blacklisted_response[env]
elsif throttled?(req)
throttled_response[env]
else
tracked?(req)
@app.call(env)
end
end
def whitelisted?(req)
whitelists.any? do |name, whitelist|
whitelist[req]
@ -101,4 +74,37 @@ module Rack::Attack
end
end
# Set defaults
@notifier = ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
@blacklisted_response = lambda {|env| [403, {}, ["Forbidden\n"]] }
@throttled_response = lambda {|env|
retry_after = env['rack.attack.match_data'][:period] rescue nil
[429, {'Retry-After' => retry_after.to_s}, ["Retry later\n"]]
}
def initialize(app)
@app = app
end
def call(env)
req = Rack::Request.new(env)
if whitelisted?(req)
@app.call(env)
elsif blacklisted?(req)
self.class.blacklisted_response[env]
elsif throttled?(req)
self.class.throttled_response[env]
else
tracked?(req)
@app.call(env)
end
end
extend Forwardable
def_delegators self, :whitelisted?,
:blacklisted?,
:throttled?,
:tracked?
end

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
class Allow2Ban < Fail2Ban
class << self
protected

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
class Blacklist < Check
def initialize(name, block)
super

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
class Cache
attr_accessor :prefix

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
class Check
attr_reader :name, :block, :type
def initialize(name, block)

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
class Fail2Ban
class << self
def filter(discriminator, options)

View file

@ -1,7 +1,7 @@
require 'delegate'
module Rack
module Attack
class Attack
class StoreProxy
def self.build(store)
# RedisStore#increment needs different behavior, so detect that

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
class Throttle
MANDATORY_OPTIONS = [:limit, :period]
attr_reader :name, :limit, :period, :block

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
class Track < Check
def initialize(name, block)
super

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
VERSION = '3.0.0'
end
end

View file

@ -1,5 +1,5 @@
module Rack
module Attack
class Attack
class Whitelist < Check
def initialize(name, block)
super