class Rack::Attack::Configuration

Constants

DEFAULT_BLOCKLISTED_RESPONSE
DEFAULT_THROTTLED_RESPONSE

Attributes

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

Public Class Methods

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

Public Instance Methods

blocklist(name = nil, &block) click to toggle source
# File lib/rack/attack/configuration.rb, line 39
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 49
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 70
def blocklisted?(request)
  @anonymous_blocklists.any? { |blocklist| blocklist.matched_by?(request) } ||
    @blocklists.any? { |_name, blocklist| blocklist.matched_by?(request) }
end
clear_configuration() click to toggle source
# File lib/rack/attack/configuration.rb, line 87
def clear_configuration
  set_defaults
end
safelist(name = nil, &block) click to toggle source
# File lib/rack/attack/configuration.rb, line 29
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 53
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 65
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 57
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 75
def throttled?(request)
  @throttles.any? do |_name, throttle|
    throttle.matched_by?(request)
  end
end
track(name, options = {}, &block) click to toggle source
# File lib/rack/attack/configuration.rb, line 61
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 81
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 93
def set_defaults
  @safelists = {}
  @blocklists = {}
  @throttles = {}
  @tracks = {}
  @anonymous_blocklists = []
  @anonymous_safelists = []
  @throttled_response_retry_after_header = false

  @blocklisted_response = DEFAULT_BLOCKLISTED_RESPONSE
  @throttled_response = DEFAULT_THROTTLED_RESPONSE
end