+0
日增长
5,767
stars
348
forks
155
watchers
369
prs
291
issues
25
releases
README
:warning: You are viewing the development's branch version of README which might contain documentation for unreleased features.
For the README consistent with the latest released version see https://github.com/rack/rack-attack/blob/6-stable/README.md.
Rack::Attack
Rack middleware for blocking & throttling abusive requests
Protect your Rails and Rack apps from bad clients. Rack::Attack lets you easily decide when to allow, block and throttle based on properties of the request.
See the Backing & Hacking blog post introducing Rack::Attack.
Table of contents
- Getting started
- Installing
- Plugging into the application
- Usage
- Safelisting
- safelist_ip(ip_address_string)
- safelist_ip(ip_subnet_string)
- safelist(name, &block)
- Blocking
- blocklist_ip(ip_address_string)
- blocklist_ip(ip_subnet_string)
- blocklist(name, &block)
- Fail2Ban
- Allow2Ban
- Throttling
- throttle(name, options, &block)
- Tracks
- Cache store configuration
- Customizing responses
- RateLimit headers for well-behaved clients
- Logging & Instrumentation
- Testing
- How it works
- About Tracks
- Performance
- Motivation
- Contributing
- Code of Conduct
- Development setup
- License
Getting started
Installing
Add this line to your application's Gemfile:
ruby
In your Gemfile
gem "rack-attack", "~ 6.8"
And then execute:
$ bundle
Or install it yourself as:
$ gem install rack-attack
Plugging into the application
Then tell your ruby web application to use rack-attack as a middleware.
a) For __rails__ applications it is used by default.
You can disable it permanently (like for specific environment) or temporarily (can be useful for specific test cases) by writing:
ruby
Rack::Attack.enabled = false
b) For __rack__ applications:
ruby
In config.ru
require "rack/attack"
use Rack::Attack
__IMPORTANT__: By default, rack-attack won't perform any blocking or throttling, until you specifically tell it what to protect against by configuring some rules.
Usage
Tip: If you just want to get going asap, then you can take our example configuration
and tailor it to your needs, or check out the advanced configuration examples.
Define rules by calling Rack::Attack public methods, in any file that runs when your application is being initialized. For rails applications this means creating a new file named config/initializers/rack_attack.rb and writing your rules there.
Safelisting
Safelists have the most precedence, so any request matching a safelist would be allowed despite matching any number of blocklists or throttles.
safelist_ip(ip_address_string)
E.g.
ruby
config/initializers/rack_attack.rb (for rails app)
Rack::Attack.safelist_ip("5.6.7.8")
safelist_ip(ip_subnet_string)
E.g.
ruby
config/initializers/rack_attack.rb (for rails app)
Rack::Attack.safelist_ip("5.6.7.0/24")
safelist(name, &block)
Name your custom safelist and make your ruby-block argument return a truthy valueTop issues
#511Don't fail when Redis is misbehaving, e.g. OOM command not allowed when used memory > 'maxmemory'17 comments · 2020-12-20#459v6.2 auto-load prevents specific middleware placement16 comments · 2019-11-08#651designing and testing throttles8 comments · 2024-01-23#645Feature request: testing helpers2 comments · 2023-12-30#722Is this normal behavior?0 comments · 2026-10-05#718Broken link0 comments · 2026-07-27#687feature: refactor Rack::Attack#call to more easily patch in tracing0 comments · 2025-05-16
Discussions
#712Occasional IPAddr::InvalidAddressError0 comments · 2026-03-14#711Add the ability to define rack-attack rules via a JSON file0 comments · 2026-03-04#703recommended redis maxmemory_policy?2 comments · 2025-10-30#676Services that will verify throttling levels?1 comments · 2024-12-02#674Using SET in Redis for fail2ban visibility and fine control0 comments · 2024-11-13#663Is it possible to only have the middleware enabled for specific endpoints / controllers1 comments · 2024-06-02#665Resetting a throttle rule for a specific client1 comments · 2024-05-28#659Is Rack Attack useful against DDOS attacks?1 comments · 2024-05-14
Latest release
v6.8.0 v6.8.0 2025-10-14What's Changed Changed - Avoid RedisCacheStoreincrement on Rails 6+ (which might improve performance) by @jdelStrother (597) Fixed - Fix 588 don't fail if request.ip is missing, by @santib (630) - Fix reset! when using namespaced cache store by @santib (673) Deprecated - Soft deprecate integration with rack 1.x, may or may not work with it. - Soft deprecate integration with rails 5.2 or less, may or may not work with it. Removed - Remove support for unmaintained ActiveSupport::Cache::RedisStore, by @mitchellhenke (625) New Contributors @floehopper made the