No commit activity in last 3 years
No release in over 3 years
A simpler CSRF middleware for Rack.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

envygeeks-coveralls
~> 1.0
~> 3.3

Runtime

~> 1.5
 Project Readme

Rack::Csrf

Build Status Coverage Status Code Climate Dependency Status

Rack::SimpleCsrf is my personal version of CSRF for Rack. It implements only a skip list where everything else must be run through the validator. It does not allow you to be explicit in what you validate, only explicit in what you do not validate. The goal is to increase security and make you think about what you are doing before you decide to do it.

Usage

Rack::SimpleCsrf has a default output of "Denied", the example belows shows you passing your own caller for us.

require "sinatra/base"
require "rack/simple_csrf"
require "logger"

class MyApp < Sinatra::Base
  set(:logger, Logger.new($stdout))

  CSRF_SKIP_LIST = [
    "/my-path",
    "POST:/my-other-path",
    "/regexp-path/.*"
  ]

  class << self
    def denied!(exception)
      MyApp.logger.error { exception }
      [403, {}, ["Nice try asshole!"]]
    end
  end

  post "/" do
    puts "Hello World"
  end

  helpers Rack::SimpleCsrf::Helpers
  use Rack::SimpleCsrf, {
    :skip => CSRF_SKIP_LIST,
    :render_with => proc { |*a|
      denied!(*a)
    }
  }
end

Options

:header - HTTP_X_CSRF_TOKEN The header key
:key - csrf -- The cookie key
:field - auth -- The auth_field token (meta and form)
:raise - false -- Raise Rack::SimpleCsrf::CSRFFailedToValidateError

Skip supports an array with values as "METHOD:/url" or "/url".

If you chose not to raise you can optionally set :render_with with a callback. The callback will always recieve the env for you to call Rack::Lint or Sinatra::Request yourself. It is done this way so that people who wish to log can log since I don't accept a logger directly, you might also want to do other shit that I don't care about, so rather than giving a shit I might as well just accept a callback and let you do whatever the hell you want.

Helpers

csrf_meta_tag(:field => "auth")
csrf_form_tag(:tag => "div", :field => "auth")