Class: Rack::Attack::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/attack/configuration.rb

Constant Summary collapse

DEFAULT_BLOCKLISTED_RESPONDER =
lambda { |_req| [403, { 'content-type' => 'text/plain' }, ["Forbidden\n"]] }
DEFAULT_THROTTLED_RESPONDER =
lambda do |req|
  if Rack::Attack.configuration.throttled_response_retry_after_header
    match_data = req.env['rack.attack.match_data']
    now = match_data[:epoch_time]
    retry_after = match_data[:period] - (now % match_data[:period])

    [429, { 'content-type' => 'text/plain', 'retry-after' => retry_after.to_s }, ["Retry later\n"]]
  else
    [429, { 'content-type' => 'text/plain' }, ["Retry later\n"]]
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



39
40
41
# File 'lib/rack/attack/configuration.rb', line 39

def initialize
  set_defaults
end

Instance Attribute Details

#anonymous_blocklistsObject (readonly)

Returns the value of attribute anonymous_blocklists.



22
23
24
# File 'lib/rack/attack/configuration.rb', line 22

def anonymous_blocklists
  @anonymous_blocklists
end

#anonymous_safelistsObject (readonly)

Returns the value of attribute anonymous_safelists.



22
23
24
# File 'lib/rack/attack/configuration.rb', line 22

def anonymous_safelists
  @anonymous_safelists
end

#blocklisted_responderObject

Returns the value of attribute blocklisted_responder.



23
24
25
# File 'lib/rack/attack/configuration.rb', line 23

def blocklisted_responder
  @blocklisted_responder
end

#blocklisted_responseObject

Keeping these for backwards compatibility



25
26
27
# File 'lib/rack/attack/configuration.rb', line 25

def blocklisted_response
  @blocklisted_response
end

#blocklistsObject (readonly)

Returns the value of attribute blocklists.



22
23
24
# File 'lib/rack/attack/configuration.rb', line 22

def blocklists
  @blocklists
end

#safelistsObject (readonly)

Returns the value of attribute safelists.



22
23
24
# File 'lib/rack/attack/configuration.rb', line 22

def safelists
  @safelists
end

#throttled_responderObject

Returns the value of attribute throttled_responder.



23
24
25
# File 'lib/rack/attack/configuration.rb', line 23

def throttled_responder
  @throttled_responder
end

#throttled_responseObject

Keeping these for backwards compatibility



25
26
27
# File 'lib/rack/attack/configuration.rb', line 25

def throttled_response
  @throttled_response
end

#throttled_response_retry_after_headerObject

Returns the value of attribute throttled_response_retry_after_header.



23
24
25
# File 'lib/rack/attack/configuration.rb', line 23

def throttled_response_retry_after_header
  @throttled_response_retry_after_header
end

#throttlesObject (readonly)

Returns the value of attribute throttles.



22
23
24
# File 'lib/rack/attack/configuration.rb', line 22

def throttles
  @throttles
end

Instance Method Details

#blocklist(name = nil, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



63
64
65
# 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) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# 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

#clear_configurationObject



101
102
103
# File 'lib/rack/attack/configuration.rb', line 101

def clear_configuration
  set_defaults
end

#safelist(name = nil, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



67
68
69
# 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) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
# 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) ⇒ Object



71
72
73
# File 'lib/rack/attack/configuration.rb', line 71

def throttle(name, options, &block)
  @throttles[name] = Throttle.new(name, options, &block)
end

#throttled?(request) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/rack/attack/configuration.rb', line 89

def throttled?(request)
  @throttles.any? do |_name, throttle|
    throttle.matched_by?(request)
  end
end

#track(name, options = {}, &block) ⇒ Object



75
76
77
# File 'lib/rack/attack/configuration.rb', line 75

def track(name, options = {}, &block)
  @tracks[name] = Track.new(name, options, &block)
end

#tracked?(request) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/rack/attack/configuration.rb', line 95

def tracked?(request)
  @tracks.each_value do |track|
    track.matched_by?(request)
  end
end