Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Anti-CSRF Rack middleware

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.rdoc

Rack::Csrf

This is just a small Rack middleware whose only goal is to lessen the hazards posed by CSRF attacks by trying to ensure that all requests of particular types come from the right client, not from a mischievous impersonator.

Rack::Csrf is not tailored to any particular web framework, so it can be used with your preferred Rack-based framework.

Usage

First of all, beyond Rack itself, there is only one prerequisite: you must set up your rack with a session middleware, inserted anywhere before Rack::Csrf.

Every POST, PUT and DELETE request will be searched for the anti-forging token, randomly generated by Rack::Csrf and stored inside the session. If there's a token and it matches with the stored one, then the request is handed over to the next rack component; if not, Rack::Csrf immediately replies with an empty response.

I have not tested Rack::Csrf with Rack 0.4.0 or earlier versions, but it could possibly work.

Options

The following options allow you to tweak Rack::Csrf.

:raise

Set it to true to change the handling of bad requests: instead of producing an empty response, Rack::Csrf will raise an exception of class Rack::Csrf::InvalidCsrfToken.

use Rack::Csrf, :raise => true

Default value: false.

:skip

By default, Rack::Csrf checks every POST, PUT and DELETE request; passing an array of HTTP method/URL (regular expressions allowed) to this option you can choose what to let pass unchecked:

use Rack::Csrf, :skip => ['POST:/not_checking', 'PUT:/me_too',
  'DELETE:/cars/.*\.xml']

Please, note that the regular expressions are not escaped and it is your duty to write them correctly.

Default value: empty.

:field

Default field name (see below) is _csrf; you can adapt it to specific needs.

use Rack::Csrf, :field => '_my_own_csrf_field'

Default value: _csrf

:browser_only

Set it to true to inspect only requests with Content-Type typically produced only by web browsers. This means that curl, Active Resource, etc. can send any request without worring about the token.

use Rack::Csrf, :browser_only => true

Default value: false.

Helpers

The following class methods try to ease the insertion of the anti-forging token.

Rack::Csrf.csrf_field

Returns the name of the field that must be present in the request.

Rack::Csrf.csrf_token(env)

Given the request's environment, it generates a random token, stuffs it in the session and returns it to the caller or simply retrieves the already stored one.

Rack::Csrf.csrf_tag(env)

Given the request's environment, it generates a small HTML fragment to insert the token in a standard form like an hidden input field with the right value already entered for you.

Working examples

In the examples directory there are some small, working web applications written with different Rack-based frameworks. They are named after the used framework; see the various README files for other details.

Contributing

If you want to help:

  • fork the project on GitHub;

  • work in a topic branch;

  • add features/specs for your additions or bug fixes;

  • write your additions/bug fixes;

  • commit;

  • send me a pull request for the topic branch.

If you have any issue, please post them on the project’s issue list on GitHub.

Warning! Warning! Warning!

I cannot stress enough that this middleware is not a bulletproof vest or a panacea for the CSRF plague; it is just an aid and by using it you cannot forgo responsibilities for keeping your application as safe as possible.

Something went wrong with that request. Please try again.