Sinatraã§CSRFã®ãã§ãã¯
Sinatraã£ã¦ãããRackã®ããã«ã¦ã§ã¢ã§ããã¾ããã
sudo gem install rack_csrf
# app.rb require 'rubygems' require 'sinatra' require 'rack/csrf' get '/' do @msg = 'Hello World' erb :index end post '/' do @msg = 'Hello CSRF' erb :index end configure do set :app_file, __FILE__ use Rack::Session::Cookie, :secret => 'change me' use Rack::Csrf, :raise => true end helpers do def csrf_token Rack::Csrf.csrf_token(env) end def csrf_tag Rack::Csrf.csrf_tag(env) end end # views/index.erb <%= @msg %> <form name="a" method="post" action="/"> <%= csrf_tag %> <input type="text" name="name" /> <input type="submit" value="go" /> </form>
ãã§ãã¯ããããªãæã¯:skipãæå®
use Rack::Csrf, :raise => true, :skip => ['POST:/']