class Rack::Attack

Constants

PathNormalizer
VERSION

Attributes

blocklisted_response[RW]
notifier[RW]
throttled_response[RW]

Public Class Methods

blacklist(name, &block) click to toggle source
# File lib/rack/attack.rb, line 37
def blacklist(name, &block)
  warn "[DEPRECATION] 'Rack::Attack.blacklist' is deprecated.  Please use 'blocklist' instead."
  blocklist(name, &block)
end
blacklisted?(req) click to toggle source
# File lib/rack/attack.rb, line 82
def blacklisted?(req)
  warn "[DEPRECATION] 'Rack::Attack.blacklisted?' is deprecated.  Please use 'blocklisted?' instead."
  blocklisted?(req)
end
blacklisted_response() click to toggle source
# File lib/rack/attack.rb, line 116
def blacklisted_response
  warn "[DEPRECATION] 'Rack::Attack.blacklisted_response' is deprecated.  Please use 'blocklisted_response' instead."
  self.blocklisted_response
end
blacklisted_response=(res) click to toggle source
# File lib/rack/attack.rb, line 111
def blacklisted_response=(res)
  warn "[DEPRECATION] 'Rack::Attack.blacklisted_response=' is deprecated.  Please use 'blocklisted_response=' instead."
  self.blocklisted_response=(res)
end
blacklists() click to toggle source
# File lib/rack/attack.rb, line 60
def blacklists
  warn "[DEPRECATION] 'Rack::Attack.blacklists' is deprecated.  Please use 'blocklists' instead."
  blocklists
end
blocklist(name, &block) click to toggle source
# File lib/rack/attack.rb, line 33
def blocklist(name, &block)
  self.blocklists[name] = Blocklist.new(name, block)
end
blocklisted?(req) click to toggle source
# File lib/rack/attack.rb, line 76
def blocklisted?(req)
  blocklists.any? do |name, blocklist|
    blocklist[req]
  end
end
blocklists() click to toggle source
# File lib/rack/attack.rb, line 51
def blocklists; @blocklists ||= {}; end
cache() click to toggle source
# File lib/rack/attack.rb, line 103
def cache
  @cache ||= Cache.new
end
clear!() click to toggle source
# File lib/rack/attack.rb, line 107
def clear!
  @safelists, @blocklists, @throttles, @tracks = {}, {}, {}, {}
end
instrument(req) click to toggle source
# File lib/rack/attack.rb, line 99
def instrument(req)
  notifier.instrument('rack.attack', req) if notifier
end
new(app) click to toggle source
# File lib/rack/attack.rb, line 131
def initialize(app)
  @app = app
end
safelist(name, &block) click to toggle source
# File lib/rack/attack.rb, line 24
def safelist(name, &block)
  self.safelists[name] = Safelist.new(name, block)
end
safelisted?(req) click to toggle source
# File lib/rack/attack.rb, line 65
def safelisted?(req)
  safelists.any? do |name, safelist|
    safelist[req]
  end
end
safelists() click to toggle source
# File lib/rack/attack.rb, line 50
def safelists; @safelists ||= {}; end
throttle(name, options, &block) click to toggle source
# File lib/rack/attack.rb, line 42
def throttle(name, options, &block)
  self.throttles[name] = Throttle.new(name, options, block)
end
throttled?(req) click to toggle source
# File lib/rack/attack.rb, line 87
def throttled?(req)
  throttles.any? do |name, throttle|
    throttle[req]
  end
end
throttles() click to toggle source
# File lib/rack/attack.rb, line 52
def throttles;  @throttles  ||= {}; end
track(name, options = {}, &block) click to toggle source
# File lib/rack/attack.rb, line 46
def track(name, options = {}, &block)
  self.tracks[name] = Track.new(name, options, block)
end
tracked?(req) click to toggle source
# File lib/rack/attack.rb, line 93
def tracked?(req)
  tracks.each_value do |tracker|
    tracker[req]
  end
end
tracks() click to toggle source
# File lib/rack/attack.rb, line 53
def tracks;     @tracks     ||= {}; end
whitelist(name, &block) click to toggle source
# File lib/rack/attack.rb, line 28
def whitelist(name, &block)
  warn "[DEPRECATION] 'Rack::Attack.whitelist' is deprecated.  Please use 'safelist' instead."
  safelist(name, &block)
end
whitelisted?(req) click to toggle source
# File lib/rack/attack.rb, line 71
def whitelisted?(req)
  warn "[DEPRECATION] 'Rack::Attack.whitelisted?' is deprecated.  Please use 'safelisted?' instead."
  safelisted?(req)
end
whitelists() click to toggle source
# File lib/rack/attack.rb, line 55
def whitelists
  warn "[DEPRECATION] 'Rack::Attack.whitelists' is deprecated.  Please use 'safelists' instead."
  safelists
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/attack.rb, line 135
def call(env)
  env['PATH_INFO'] = PathNormalizer.normalize_path(env['PATH_INFO'])
  req = Rack::Attack::Request.new(env)

  if safelisted?(req)
    @app.call(env)
  elsif blocklisted?(req)
    self.class.blocklisted_response.call(env)
  elsif throttled?(req)
    self.class.throttled_response.call(env)
  else
    tracked?(req)
    @app.call(env)
  end
end