rack /rack-attack

Rack middleware for blocking & throttling

★ 5,767⑂ 348👁 155⑃ 369● 291▤ 25
RubyMITCreated 2012-07-29
下载:v6.8.0v6.8.0 ↓6.8.02025-10-14
+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 value

Top issues

Discussions

Latest release

v6.8.0 v6.8.0 2025-10-14
What'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

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON