Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Rack middleware for blocking & throttling

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time


A DSL for blocking & thottling abusive clients

Rack::Attack is a rack middleware to protect your web app from bad clients. It allows whitelisting, blacklisting, and thottling based on arbitrary properties of the request.

Thottle state is stored in a configurable cache (e.g. Rails.cache), presumably backed by memcached.


Install the rack-attack gem; or add it to you Gemfile with bundler:

# In your Gemfile
gem 'rack-attack'

Tell your app to use the Rack::Attack middleware. For Rails 3 apps:

# In config/application.rb
config.middleware.use Rack::Attack

Or for Rackup files:

# In
use Rack::Attack

Optionally configure the cache store for throttling: = # defaults to Rails.cache

Note that Rack::Attack.cache is only used for throttling; not blacklisting & whitelisting. Your cache store must implement increment and write like ActiveSupport::Cache::Store.

How it works

The Rack::Attack middleware compares each request against whitelists, blacklists, and throttles that you define. There are none by default.

  • If the request matches any whitelist, it is allowed. Blacklists and throttles are not checked.
  • If the request matches any blacklist, it is blocked. Throttles are not checked.
  • If the request matches any throttle, a counter is incremented in the Rack::Attack.cache. If the throttle limit is exceeded, the request is blocked and further throttles are not checked.


Define blacklists, throttles, and whitelists as blocks that return truthy of falsy values. A Rack::Request object is passed to the block (named 'req' in the examples).


# Block requests from
Rack::Attack.blacklist('block') do |req|
  # Request are blocked if the return value is truthy
  '' == req.ip

# Block logins from a bad user agent
Rack::Attack.blacklist('block bad UA logins') do |req|
  req.path == '/login' && && req.user_agent == 'BadUA'


# Throttle requests to 5 requests per second per ip
Rack::Attack.throttle('req/ip', :limit => 5, :period => 1.second) do |req|
  # If the return value is truthy, the cache key for the return value
  # is incremented and compared with the limit. In this case:
  #   "rack::attack:#{}:req/ip:#{req.ip}"
  # If falsy, the cache key is neither incremented nor checked.


# Throttle login attempts for a given email parameter to 6 reqs/minute
Rack::Attack.throttle('logins/email', :limit => 6, :period => 60.seconds) do |req|
  request.path == '/login' && && req.params['email']


# Always allow requests from localhost
# (blacklist & throttles are skipped)
Rack::Attack.whitelist('allow from localhost') do |req|
  # Requests are allowed if the return value is truthy
  '' == req.ip


Customize the response of blacklisted and throttled requests using an object that adheres to the Rack app interface.

Rack:Attack.blacklisted_response = lambda do |env|
  [ 503, {}, ['Blocked']]

Rack:Attack.throttled_response = lambda do |env|
  # name and other data about the matched throttle
  body = [

  [ 503, {}, [body]]

For responses that did not exceed a throttle limit, Rack::Attack annotates the env with match data:

request.env['rack.attack.throttle_data'][name] # => { :count => n, :period => p, :limit => l }

Logging & Instrumentation

Rack::Attack uses the ActiveSupport::Notifications API if available.

You can subscribe to 'rack.attack' events and log it, graph it, etc:

ActiveSupport::Notifications.subscribe('rack.attack') do |name, start, finish, request_id, req|
  puts req.inspect


Abusive clients range from malicious login crackers to naively-written scrapers. They hinder the security, performance, & availability of web applications.

It is impractical if not impossible to block abusive clients completely.

Rack::Attack aims to let developers quickly mitigate abusive requests and rely less on short-term, one-off hacks to block a particular attack.

Rack::Attack complements tools like iptables and nginx's limit_zone module.

Travis CI


Copyright (c) 2012 Kickstarter, Inc

Released under an MIT License

Something went wrong with that request. Please try again.