Class: Rack::Attack

Inherits:
Object
  • 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)
  # For Rails 4+ apps
  ::ActionDispatch::Journey::Router::Utils
elsif defined?(::Journey::Router::Utils)
  # for Rails 3.2
  ::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_responseObject

Returns the value of attribute blocklisted_response.



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

def blocklisted_response
  @blocklisted_response
end

.notifierObject

Returns the value of attribute notifier.



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

def notifier
  @notifier
end

.throttled_responseObject

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

Returns:

  • (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_responseObject



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

.blacklistsObject



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

Returns:

  • (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

.blocklistsObject



51
# File 'lib/rack/attack.rb', line 51

def blocklists; @blocklists ||= {}; end

.cacheObject



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

Returns:

  • (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

.safelistsObject



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

Returns:

  • (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

.throttlesObject



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

Returns:

  • (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

.tracksObject



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

Returns:

  • (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

.whitelistsObject



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