~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/lib/action_controller/request_forgery_protection.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module ActionController #:nodoc:
2
 
  class InvalidAuthenticityToken < ActionControllerError #:nodoc:
3
 
  end
4
 
 
5
 
  module RequestForgeryProtection
6
 
    def self.included(base)
7
 
      base.class_eval do
8
 
        helper_method :form_authenticity_token
9
 
        helper_method :protect_against_forgery?
10
 
      end
11
 
      base.extend(ClassMethods)
12
 
    end
13
 
    
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.
19
 
    #
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
23
 
    # applications.
24
 
    #
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:
29
 
    #
30
 
    #   <%= javascript_tag "window._token = '#{form_authenticity_token}'" %>
31
 
    #
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:
34
 
    #
35
 
    #   # Disable request forgery protection in test environment
36
 
    #   config.action_controller.allow_forgery_protection = false
37
 
    # 
38
 
    # == Learn more about CSRF (Cross-Site Request Forgery) attacks
39
 
    #
40
 
    # Here are some resources:
41
 
    # * http://isc.sans.org/diary.html?storyid=1750
42
 
    # * http://en.wikipedia.org/wiki/Cross-site_request_forgery
43
 
    #
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:
46
 
    # 
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"
51
 
    #
52
 
    module ClassMethods
53
 
      # Turn on request forgery protection. Bear in mind that only non-GET, HTML/JavaScript requests are checked.
54
 
      #
55
 
      # Example:
56
 
      #
57
 
      #   class FooController < ApplicationController
58
 
      #     protect_from_forgery :except => :index
59
 
      #
60
 
      #     # you can disable csrf protection on controller-by-controller basis:
61
 
      #     skip_before_filter :verify_authenticity_token
62
 
      #   end
63
 
      #
64
 
      # Valid Options:
65
 
      #
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)
72
 
        end
73
 
      end
74
 
    end
75
 
 
76
 
    protected
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)
80
 
      end
81
 
      
82
 
      # Returns true or false if a request is verified.  Checks:
83
 
      #
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?
87
 
      def verified_request?
88
 
        !protect_against_forgery?     ||
89
 
          request.method == :get      ||
90
 
          request.xhr?                ||
91
 
          !verifiable_request_format? ||
92
 
          form_authenticity_token == form_authenticity_param
93
 
      end
94
 
 
95
 
      def form_authenticity_param
96
 
        params[request_forgery_protection_token]
97
 
      end
98
 
 
99
 
      def verifiable_request_format?
100
 
        !request.content_type.nil? && request.content_type.verify_request?
101
 
      end
102
 
    
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)
107
 
      end
108
 
 
109
 
      def protect_against_forgery?
110
 
        allow_forgery_protection && request_forgery_protection_token
111
 
      end
112
 
  end
113
 
end