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/throttle.rb,
lib/rack/attack/allow2ban.rb,
lib/rack/attack/blacklist.rb,
lib/rack/attack/whitelist.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, Blacklist, Cache, Check, Fail2Ban, Request, Throttle, Track, Whitelist

Constant Summary collapse

VERSION =
'4.4.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.



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

def initialize(app)
  @app = app
end

Class Attribute Details

.blacklisted_responseObject

Returns the value of attribute blacklisted_response.



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

def blacklisted_response
  @blacklisted_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



28
29
30
# File 'lib/rack/attack.rb', line 28

def blacklist(name, &block)
  self.blacklists[name] = Blacklist.new(name, block)
end

.blacklisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/rack/attack.rb', line 51

def blacklisted?(req)
  blacklists.any? do |name, blacklist|
    blacklist[req]
  end
end

.blacklistsObject



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

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

.cacheObject



73
74
75
# File 'lib/rack/attack.rb', line 73

def cache
  @cache ||= Cache.new
end

.clear!Object



77
78
79
# File 'lib/rack/attack.rb', line 77

def clear!
  @whitelists, @blacklists, @throttles, @tracks = {}, {}, {}, {}
end

.instrument(req) ⇒ Object



69
70
71
# File 'lib/rack/attack.rb', line 69

def instrument(req)
  notifier.instrument('rack.attack', req) if notifier
end

.throttle(name, options, &block) ⇒ Object



32
33
34
# File 'lib/rack/attack.rb', line 32

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

.throttled?(req) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/rack/attack.rb', line 57

def throttled?(req)
  throttles.any? do |name, throttle|
    throttle[req]
  end
end

.throttlesObject



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

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

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



36
37
38
# File 'lib/rack/attack.rb', line 36

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

.tracked?(req) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/rack/attack.rb', line 63

def tracked?(req)
  tracks.each_value do |tracker|
    tracker[req]
  end
end

.tracksObject



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

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

.whitelist(name, &block) ⇒ Object



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

def whitelist(name, &block)
  self.whitelists[name] = Whitelist.new(name, block)
end

.whitelisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/rack/attack.rb', line 45

def whitelisted?(req)
  whitelists.any? do |name, whitelist|
    whitelist[req]
  end
end

.whitelistsObject



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

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

Instance Method Details

#call(env) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rack/attack.rb', line 95

def call(env)
  env['PATH_INFO'] = PathNormalizer.normalize_path(env['PATH_INFO'])
  req = Rack::Attack::Request.new(env)

  if whitelisted?(req)
    @app.call(env)
  elsif blacklisted?(req)
    self.class.blacklisted_response.call(env)
  elsif throttled?(req)
    self.class.throttled_response.call(env)
  else
    tracked?(req)
    @app.call(env)
  end
end