Bouncer

A simple blacklist based access filtering middleware for Rack.

It allows for easy filtering of IP addresses and User Agent strings. This filter is primarily aimed at quickly blocking abusive users or bots.

It is suitable for websites with moderate traffic suffering from a handful of abusive clients and will come handy for anyone who doesn’t want to fiddle with obscure web server configuration directives.

Usage

For instance, to add this middleware to your Rails app, in environment.rb:

# Require the gem, use rake gems:install to install it
config.gem 'xavier-bouncer', :lib => 'bouncer', :source => 'http://gems.github.com'

# Add the middleware to the stack and configure it
config.middleware.add 'Bouncer', :deny_ip_address => ["1.2.3.4", /^10\.0\./], :deny_user_agent => /msnbot/

The black list can be either a single or an array with a combination of strings, regular expressions and Proc objects. If any of the given conditions are met, the client will be greeted with an HTTP 403 response (access denied).

Similar Middleware

If you are after more advanced features, you may want to have a look at:

If you are seriously under attack, you may want to have a look at ModSecurity.

MIT Licence

Copyright © 2009 Xavier Defrang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.