Module: Rack::Attack

Defined in:
lib/rack/attack/cache.rb,
lib/rack/attack/check.rb,
lib/rack/attack/track.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.rb

Defined Under Namespace

Classes: Allow2Ban, Blacklist, Cache, Check, Fail2Ban, StoreProxy, Throttle, Track, Whitelist

Constant Summary collapse

VERSION =
'3.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.blacklisted_responseObject

Returns the value of attribute blacklisted_response.



15
16
17
# File 'lib/rack/attack.rb', line 15

def blacklisted_response
  @blacklisted_response
end

.notifierObject

Returns the value of attribute notifier.



15
16
17
# File 'lib/rack/attack.rb', line 15

def notifier
  @notifier
end

.throttled_responseObject

Returns the value of attribute throttled_response.



15
16
17
# File 'lib/rack/attack.rb', line 15

def throttled_response
  @throttled_response
end

Class Method Details

.blacklist(name, &block) ⇒ Object



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

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

.blacklisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.blacklistsObject



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

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

.cacheObject



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

def cache
  @cache ||= Cache.new
end

.call(env) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rack/attack.rb', line 52

def call(env)
  req = Rack::Request.new(env)

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

.clear!Object



99
100
101
# File 'lib/rack/attack.rb', line 99

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

.instrument(req) ⇒ Object



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

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

.new(app) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rack/attack.rb', line 38

def new(app)
  @app = app

  # Set defaults
  @notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
  @blacklisted_response ||= lambda {|env| [403, {}, ["Forbidden\n"]] }
  @throttled_response   ||= lambda {|env|
    retry_after = env['rack.attack.match_data'][:period] rescue nil
    [429, {'Retry-After' => retry_after.to_s}, ["Retry later\n"]]
  }

  self
end

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



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

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

.throttled?(req) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/rack/attack.rb', line 79

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

.throttlesObject



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

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

.track(name, &block) ⇒ Object



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

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

.tracked?(req) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/rack/attack.rb', line 85

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

.tracksObject



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

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

.whitelist(name, &block) ⇒ Object



17
18
19
# File 'lib/rack/attack.rb', line 17

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

.whitelisted?(req) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.whitelistsObject



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

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