Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Rails 3 firewall gem
Ruby HTML JavaScript
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Firewool is an IP firewall for rails. You set what IPs to block and what IPs to allow. Specifics below.

Why would I need this?

  • A layer 7 firewall is too expensive.

  • Anonymous authentication doesn't equate to all access authorization.

  • Belt and suspenders style double security check.


gem install firewool

  • Tested on rails 3.0.4.

  • Untested on rails 2.x. Probably won't work because no engines.


Add firewool and dependency to Gemfile:

gem 'firewool'
gem 'ipaddress'

Create a configuration file in config/firewool.yml

# config/firewool.yml
# changing any valves requires app server restart (apache/webrick/etc)

  ip_restriction: true

allow: [ ]

  ip_restriction: false

  ip_restriction: true
  allow: [,, ]
  deny:  [ ]

Add these lines to your controller you want to protect:

class DummyController < ApplicationController
  include Firewool
  before_filter :ip_filter

Optionally, you can just filter certain actions like any filter:

before_filter :ip_filter, :only => [:admin, :secret]


Firewool has an implicit deny by default. This means that Firewool does the following evalation:

Deny first
Allow all in allow list
Deny all in deny list

This allows you to have security by default, a whitelist and then exceptions to that whitelist. However, sometimes you want a default allow and only exceptions to that rule. In that case, use an allow with like this:

allow: [ ]
deny:  [ whatever ]

So then firewool will do allow -> deny.

IPs can be spoofed so in the case of strong security, you'll want to use this with one or more factor authentication.

Quick Network Primer

So how do I write the rules when I'm not a network guy? No problem, let's go through some examples.

First, the IP is four numbers separated by periods. Each number is called an ocet. The slash number (like /16 up above) is how many octets match. So to match every usable IP from to, we can just say: (matches 10.0.0.*)      (match)    (match)      (no match)      (no match)

If we just want to match one IP we can use the /32 or just specify the IP by itself.  (matches only     (matches only, same meaning as /32)       (matches 5.*.*.*)      (matches 5.6.*.*)      (matches 5.6.0.*)      (matches 5.6.7.*)

These are the simplest examples of this notation (called CIDR if you want to read more) but it's enough to build a few use cases. Let's say we want to allow anyone from our company network but block anyone coming from Evil Hackers' Inc. Our company's external network is 5.6.7.* (ie: what users see when they go to from inside their network) and let's say that Evil Hackers' proxy is This would be our firewool.yml config:

  ip_restriction: true
  allow: [ ]
  deny:  [ ]

Now we'd want to be careful that 5.6.7.* was really where our users are coming from. If people that we want to keep out are coming from then we'd want to tighten up our rule a little bit and not allow all of the network in. Maybe we research what our IP block really is, or add only the IPs we know about as /32 IPs.

As a special case, means ..., or all IPs.

Pretty up

If 403.html doesn't exist in your public directory, then a blocked user will simply see “Public Access Denied.” which isn't that great. Create a 403.html file in public, you can use this 403.html template as an example.

Thanks to

Bluemonk for his awesome ipaddress gem.

Something went wrong with that request. Please try again.