class Rack::Attack::Configuration

Constants

DEFAULT_BLOCKLISTED_RESPONDER
DEFAULT_THROTTLED_RESPONDER

Attributes

anonymous_blocklists[R]
anonymous_safelists[R]
blocklisted_responder[RW]
blocklisted_response[R]
blocklists[R]
safelists[R]
throttled_responder[RW]
throttled_response[R]
throttled_response_retry_after_header[RW]
throttles[R]

Public Class Methods

new() click to toggle source
# File lib/rack/attack/configuration.rb, line 39
def initialize
  set_defaults
end

Public Instance Methods

blocklist(name = nil, &block) click to toggle source
# File lib/rack/attack/configuration.rb, line 53
def blocklist(name = nil, &block)
  blocklist = Blocklist.new(name, &block)

  if name
    @blocklists[name] = blocklist
  else
    @anonymous_blocklists << blocklist
  end
end
blocklist_ip(ip_address) click to toggle source
# File lib/rack/attack/configuration.rb, line 63
def blocklist_ip(ip_address)
  @anonymous_blocklists << Blocklist.new { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
end
blocklisted?(request) click to toggle source
# File lib/rack/attack/configuration.rb, line 84
def blocklisted?(request)
  @anonymous_blocklists.any? { |blocklist| blocklist.matched_by?(request) } ||
    @blocklists.any? { |_name, blocklist| blocklist.matched_by?(request) }
end
blocklisted_response=(responder) click to toggle source
# File lib/rack/attack/configuration.rb, line 27
def blocklisted_response=(responder)
  warn "[DEPRECATION] Rack::Attack.blocklisted_response is deprecated. "\
    "Please use Rack::Attack.blocklisted_responder instead."
  @blocklisted_response = responder
end
clear_configuration() click to toggle source
# File lib/rack/attack/configuration.rb, line 101
def clear_configuration
  set_defaults
end
safelist(name = nil, &block) click to toggle source
# File lib/rack/attack/configuration.rb, line 43
def safelist(name = nil, &block)
  safelist = Safelist.new(name, &block)

  if name
    @safelists[name] = safelist
  else
    @anonymous_safelists << safelist
  end
end
safelist_ip(ip_address) click to toggle source
# File lib/rack/attack/configuration.rb, line 67
def safelist_ip(ip_address)
  @anonymous_safelists << Safelist.new { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
end
safelisted?(request) click to toggle source
# File lib/rack/attack/configuration.rb, line 79
def safelisted?(request)
  @anonymous_safelists.any? { |safelist| safelist.matched_by?(request) } ||
    @safelists.any? { |_name, safelist| safelist.matched_by?(request) }
end
throttle(name, options, &block) click to toggle source
# File lib/rack/attack/configuration.rb, line 71
def throttle(name, options, &block)
  @throttles[name] = Throttle.new(name, options, &block)
end
throttled?(request) click to toggle source
# File lib/rack/attack/configuration.rb, line 89
def throttled?(request)
  @throttles.any? do |_name, throttle|
    throttle.matched_by?(request)
  end
end
throttled_response=(responder) click to toggle source
# File lib/rack/attack/configuration.rb, line 33
def throttled_response=(responder)
  warn "[DEPRECATION] Rack::Attack.throttled_response is deprecated. "\
    "Please use Rack::Attack.throttled_responder instead"
  @throttled_response = responder
end
track(name, options = {}, &block) click to toggle source
# File lib/rack/attack/configuration.rb, line 75
def track(name, options = {}, &block)
  @tracks[name] = Track.new(name, options, &block)
end
tracked?(request) click to toggle source
# File lib/rack/attack/configuration.rb, line 95
def tracked?(request)
  @tracks.each_value do |track|
    track.matched_by?(request)
  end
end

Private Instance Methods

set_defaults() click to toggle source
# File lib/rack/attack/configuration.rb, line 107
def set_defaults
  @safelists = {}
  @blocklists = {}
  @throttles = {}
  @tracks = {}
  @anonymous_blocklists = []
  @anonymous_safelists = []
  @throttled_response_retry_after_header = false

  @blocklisted_responder = DEFAULT_BLOCKLISTED_RESPONDER
  @throttled_responder = DEFAULT_THROTTLED_RESPONDER

  # Deprecated: Keeping these for backwards compatibility
  @blocklisted_response = nil
  @throttled_response = nil
end