1
module ActionController #:nodoc:
2
class InvalidAuthenticityToken < ActionControllerError #:nodoc:
5
module RequestForgeryProtection
6
def self.included(base)
8
helper_method :form_authenticity_token
9
helper_method :protect_against_forgery?
11
base.extend(ClassMethods)
14
# Protecting controller actions from CSRF attacks by ensuring that all forms are coming from the current web application, not a
15
# forged link from another site, is done by embedding a token based on a random string stored in the session (which an attacker wouldn't know) in all
16
# forms and Ajax requests generated by Rails and then verifying the authenticity of that token in the controller. Only
17
# HTML/JavaScript requests are checked, so this will not protect your XML API (presumably you'll have a different authentication
18
# scheme there anyway). Also, GET requests are not protected as these should be idempotent anyway.
20
# This is turned on with the <tt>protect_from_forgery</tt> method, which will check the token and raise an
21
# ActionController::InvalidAuthenticityToken if it doesn't match what was expected. You can customize the error message in
22
# production by editing public/422.html. A call to this method in ApplicationController is generated by default in post-Rails 2.0
25
# The token parameter is named <tt>authenticity_token</tt> by default. If you are generating an HTML form manually (without the
26
# use of Rails' <tt>form_for</tt>, <tt>form_tag</tt> or other helpers), you have to include a hidden field named like that and
27
# set its value to what is returned by <tt>form_authenticity_token</tt>. Same applies to manually constructed Ajax requests. To
28
# make the token available through a global variable to scripts on a certain page, you could add something like this to a view:
30
# <%= javascript_tag "window._token = '#{form_authenticity_token}'" %>
32
# Request forgery protection is disabled by default in test environment. If you are upgrading from Rails 1.x, add this to
33
# config/environments/test.rb:
35
# # Disable request forgery protection in test environment
36
# config.action_controller.allow_forgery_protection = false
38
# == Learn more about CSRF (Cross-Site Request Forgery) attacks
40
# Here are some resources:
41
# * http://isc.sans.org/diary.html?storyid=1750
42
# * http://en.wikipedia.org/wiki/Cross-site_request_forgery
44
# Keep in mind, this is NOT a silver-bullet, plug 'n' play, warm security blanket for your rails application.
45
# There are a few guidelines you should follow:
47
# * Keep your GET requests safe and idempotent. More reading material:
48
# * http://www.xml.com/pub/a/2002/04/24/deviant.html
49
# * http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1
50
# * Make sure the session cookies that Rails creates are non-persistent. Check in Firefox and look for "Expires: at end of session"
53
# Turn on request forgery protection. Bear in mind that only non-GET, HTML/JavaScript requests are checked.
57
# class FooController < ApplicationController
58
# protect_from_forgery :except => :index
60
# # you can disable csrf protection on controller-by-controller basis:
61
# skip_before_filter :verify_authenticity_token
66
# * <tt>:only/:except</tt> - Passed to the <tt>before_filter</tt> call. Set which actions are verified.
67
def protect_from_forgery(options = {})
68
self.request_forgery_protection_token ||= :authenticity_token
69
before_filter :verify_authenticity_token, :only => options.delete(:only), :except => options.delete(:except)
70
if options[:secret] || options[:digest]
71
ActiveSupport::Deprecation.warn("protect_from_forgery only takes :only and :except options now. :digest and :secret have no effect", caller)
77
# The actual before_filter that is used. Modify this to change how you handle unverified requests.
78
def verify_authenticity_token
79
verified_request? || raise(ActionController::InvalidAuthenticityToken)
82
# Returns true or false if a request is verified. Checks:
84
# * is the format restricted? By default, only HTML requests are checked.
85
# * is it a GET request? Gets should be safe and idempotent
86
# * Does the form_authenticity_token match the given token value from the params?
88
!protect_against_forgery? ||
89
request.method == :get ||
91
!verifiable_request_format? ||
92
form_authenticity_token == form_authenticity_param
95
def form_authenticity_param
96
params[request_forgery_protection_token]
99
def verifiable_request_format?
100
!request.content_type.nil? && request.content_type.verify_request?
103
# Sets the token value for the current session. Pass a <tt>:secret</tt> option
104
# in +protect_from_forgery+ to add a custom salt to the hash.
105
def form_authenticity_token
106
session[:_csrf_token] ||= ActiveSupport::SecureRandom.base64(32)
109
def protect_against_forgery?
110
allow_forgery_protection && request_forgery_protection_token