Class: Rack::Attack::Configuration
- Inherits:
-
Object
- Object
- Rack::Attack::Configuration
- 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
-
#anonymous_blocklists ⇒ Object
readonly
Returns the value of attribute anonymous_blocklists.
-
#anonymous_safelists ⇒ Object
readonly
Returns the value of attribute anonymous_safelists.
-
#blocklisted_responder ⇒ Object
Returns the value of attribute blocklisted_responder.
-
#blocklisted_response ⇒ Object
Keeping these for backwards compatibility.
-
#blocklists ⇒ Object
readonly
Returns the value of attribute blocklists.
-
#safelists ⇒ Object
readonly
Returns the value of attribute safelists.
-
#throttled_responder ⇒ Object
Returns the value of attribute throttled_responder.
-
#throttled_response ⇒ Object
Keeping these for backwards compatibility.
-
#throttled_response_retry_after_header ⇒ Object
Returns the value of attribute throttled_response_retry_after_header.
-
#throttles ⇒ Object
readonly
Returns the value of attribute throttles.
Instance Method Summary collapse
- #blocklist(name = nil, &block) ⇒ Object
- #blocklist_ip(ip_address) ⇒ Object
- #blocklisted?(request) ⇒ Boolean
- #clear_configuration ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #safelist(name = nil, &block) ⇒ Object
- #safelist_ip(ip_address) ⇒ Object
- #safelisted?(request) ⇒ Boolean
- #throttle(name, options, &block) ⇒ Object
- #throttled?(request) ⇒ Boolean
- #track(name, options = {}, &block) ⇒ Object
- #tracked?(request) ⇒ Boolean
Constructor Details
#initialize ⇒ Configuration
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_blocklists ⇒ Object (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_safelists ⇒ Object (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_responder ⇒ Object
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_response ⇒ Object
Keeping these for backwards compatibility
25 26 27 |
# File 'lib/rack/attack/configuration.rb', line 25 def blocklisted_response @blocklisted_response end |
#blocklists ⇒ Object (readonly)
Returns the value of attribute blocklists.
22 23 24 |
# File 'lib/rack/attack/configuration.rb', line 22 def blocklists @blocklists end |
#safelists ⇒ Object (readonly)
Returns the value of attribute safelists.
22 23 24 |
# File 'lib/rack/attack/configuration.rb', line 22 def safelists @safelists end |
#throttled_responder ⇒ Object
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_response ⇒ Object
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_header ⇒ Object
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 |
#throttles ⇒ Object (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
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_configuration ⇒ Object
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
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, , &block) @throttles[name] = Throttle.new(name, , &block) end |
#throttled?(request) ⇒ 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, = {}, &block) @tracks[name] = Track.new(name, , &block) end |
#tracked?(request) ⇒ 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 |