talira

Rack::Attack

Rack middleware for blocking & throttling abusive requests

open sourceMITfree

Rack::Attack is Rack middleware that protects Rails and other Rack apps from abusive clients by deciding, for each request, whether to allow, block or throttle it. You define safelists, blocklists, throttles and tracks in Ruby, matching an IP, a subnet or any property of the request (such as the email on a login form), plus Fail2Ban and Allow2Ban rules that ban an IP after repeated bad requests. Throttle and ban counters live in a cache store, Rails.cache by default, and the README recommends a separate Redis or Memcached database that every process shares. It was introduced on Kickstarter's engineering blog and now lives in the Rack GitHub organization; a Rails app uses it as soon as the gem is installed.

Problems it solves

  • bots are hammering our login page trying thousands of passwords
  • limit how many requests a single IP can make to my Rails API per minute
  • throttle password reset attempts per email address, not just per IP
  • a badly written scraper keeps taking our site down with too many requests
  • block requests from a list of bad IP addresses and subnets in my Ruby web app
  • automatically ban an IP for an hour after it probes for /wp-admin or /etc/passwd
  • send a 429 with a Retry-After header when a client calls us too fast
  • always let our office IPs and health checks through while rate limiting everyone else
  • give API clients that send a key a higher request limit than anonymous traffic
  • log and graph requests from a suspicious user agent without blocking them yet
  • our in-app rate limit doesn't hold across several Puma workers and servers
  • stop credential stuffing at the application level when we don't have a WAF in front
  • clear rate limit state between tests so specs don't throttle each other

Features

  • safelists and blocklists by IP address, subnet or any request property
  • throttles with a limit and period, fixed or computed per request
  • Fail2Ban and Allow2Ban rules that ban an IP after repeated matches
  • tracks that log matching requests without blocking them
  • custom responses for blocked and throttled requests (403 and 429 by default)
  • optional Retry-After header, and RateLimit headers through a custom responder
  • ActiveSupport::Notifications events for logging and metrics
  • any ActiveSupport cache store that implements increment and write
  • enabled switch, reset! and clear_configuration for tests

Limitations

  • Doesn't share throttle counters between processes with an in-memory cache: throttles need an external store such as Redis or Memcached1

Capabilities

Has an APIplan not known