Class: Rack::Attack
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/rack/attack/cache.rb,
lib/rack/attack/check.rb,
lib/rack/attack/track.rb,
lib/rack/attack/request.rb,
lib/rack/attack/version.rb,
lib/rack/attack/fail2ban.rb,
lib/rack/attack/safelist.rb,
lib/rack/attack/throttle.rb,
lib/rack/attack/allow2ban.rb,
lib/rack/attack/blocklist.rb,
lib/rack/attack/store_proxy.rb,
lib/rack/attack/path_normalizer.rb,
lib/rack/attack/store_proxy/dalli_proxy.rb,
lib/rack/attack/store_proxy/mem_cache_proxy.rb,
lib/rack/attack/store_proxy/redis_store_proxy.rb,
lib/rack/attack.rb
Defined Under Namespace
Modules: FallbackPathNormalizer, StoreProxy
Classes: Allow2Ban, Blocklist, Cache, Check, Fail2Ban, Request, Safelist, Throttle, Track
Constant Summary
collapse
- VERSION =
'5.0.1'
- PathNormalizer =
if defined?(::ActionDispatch::Journey::Router::Utils)
::ActionDispatch::Journey::Router::Utils
elsif defined?(::Journey::Router::Utils)
::Journey::Router::Utils
else
FallbackPathNormalizer
end
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(app) ⇒ Attack
Returns a new instance of Attack.
131
132
133
|
# File 'lib/rack/attack.rb', line 131
def initialize(app)
@app = app
end
|
Class Attribute Details
.blocklisted_response ⇒ Object
Returns the value of attribute blocklisted_response.
22
23
24
|
# File 'lib/rack/attack.rb', line 22
def blocklisted_response
@blocklisted_response
end
|
.notifier ⇒ Object
Returns the value of attribute notifier.
22
23
24
|
# File 'lib/rack/attack.rb', line 22
def notifier
@notifier
end
|
.throttled_response ⇒ Object
Returns the value of attribute throttled_response.
22
23
24
|
# File 'lib/rack/attack.rb', line 22
def throttled_response
@throttled_response
end
|
Class Method Details
.blacklist(name, &block) ⇒ Object
37
38
39
40
|
# 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) ⇒ Boolean
82
83
84
85
|
# 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 ⇒ Object
116
117
118
119
|
# 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) ⇒ Object
111
112
113
114
|
# 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 ⇒ Object
60
61
62
63
|
# 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) ⇒ Object
33
34
35
|
# File 'lib/rack/attack.rb', line 33
def blocklist(name, &block)
self.blocklists[name] = Blocklist.new(name, block)
end
|
.blocklisted?(req) ⇒ Boolean
76
77
78
79
80
|
# File 'lib/rack/attack.rb', line 76
def blocklisted?(req)
blocklists.any? do |name, blocklist|
blocklist[req]
end
end
|
.blocklists ⇒ Object
51
|
# File 'lib/rack/attack.rb', line 51
def blocklists; @blocklists ||= {}; end
|
.cache ⇒ Object
103
104
105
|
# File 'lib/rack/attack.rb', line 103
def cache
@cache ||= Cache.new
end
|
.clear! ⇒ Object
107
108
109
|
# File 'lib/rack/attack.rb', line 107
def clear!
@safelists, @blocklists, @throttles, @tracks = {}, {}, {}, {}
end
|
.instrument(req) ⇒ Object
99
100
101
|
# File 'lib/rack/attack.rb', line 99
def instrument(req)
notifier.instrument('rack.attack', req) if notifier
end
|
.safelist(name, &block) ⇒ Object
24
25
26
|
# File 'lib/rack/attack.rb', line 24
def safelist(name, &block)
self.safelists[name] = Safelist.new(name, block)
end
|
.safelisted?(req) ⇒ Boolean
65
66
67
68
69
|
# File 'lib/rack/attack.rb', line 65
def safelisted?(req)
safelists.any? do |name, safelist|
safelist[req]
end
end
|
.safelists ⇒ Object
50
|
# File 'lib/rack/attack.rb', line 50
def safelists; @safelists ||= {}; end
|
.throttle(name, options, &block) ⇒ Object
42
43
44
|
# File 'lib/rack/attack.rb', line 42
def throttle(name, options, &block)
self.throttles[name] = Throttle.new(name, options, block)
end
|
.throttled?(req) ⇒ Boolean
87
88
89
90
91
|
# File 'lib/rack/attack.rb', line 87
def throttled?(req)
throttles.any? do |name, throttle|
throttle[req]
end
end
|
.throttles ⇒ Object
52
|
# File 'lib/rack/attack.rb', line 52
def throttles; @throttles ||= {}; end
|
.track(name, options = {}, &block) ⇒ Object
46
47
48
|
# File 'lib/rack/attack.rb', line 46
def track(name, options = {}, &block)
self.tracks[name] = Track.new(name, options, block)
end
|
.tracked?(req) ⇒ Boolean
93
94
95
96
97
|
# File 'lib/rack/attack.rb', line 93
def tracked?(req)
tracks.each_value do |tracker|
tracker[req]
end
end
|
.tracks ⇒ Object
53
|
# File 'lib/rack/attack.rb', line 53
def tracks; @tracks ||= {}; end
|
.whitelist(name, &block) ⇒ Object
28
29
30
31
|
# 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) ⇒ Boolean
71
72
73
74
|
# 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 ⇒ Object
55
56
57
58
|
# File 'lib/rack/attack.rb', line 55
def whitelists
warn "[DEPRECATION] 'Rack::Attack.whitelists' is deprecated. Please use 'safelists' instead."
safelists
end
|
Instance Method Details
#call(env) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# 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
|