123456789_123456789_123456789_123456789_123456789_

Module: ActionController::RequestForgeryProtection

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Base, ::ActionView::TestCase::TestController, Rails::ApplicationController, Rails::InfoController, Rails::MailersController, Rails::WelcomeController
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Defined in: actionpack/lib/action_controller/metal/request_forgery_protection.rb

Overview

Controller actions are protected from Cross-Site Request Forgery (CSRF) attacks by including a token in the rendered HTML for your application. This token is stored as a random string in the session, to which an attacker does not have access. When a request reaches your application, Rails verifies the received token with the token in the session. Only HTML and JavaScript requests are checked, so this will not protect your XML API (presumably you'll have a different authentication scheme there anyway).

GET requests are not protected since they don't have side effects like writing to the database and don't leak sensitive information. JavaScript requests are an exception: a third-party site can use a <script> tag to reference a JavaScript URL on your site. When your JavaScript response loads on their site, it executes. With carefully crafted JavaScript on their end, sensitive data in your JavaScript response may be extracted. To prevent this, only XmlHttpRequest (known as XHR or Ajax) requests are allowed to make GET requests for JavaScript responses.

It's important to remember that XML or JSON requests are also affected and if you're building an API you'll need something like:

class ApplicationController < ActionController::Base
  protect_from_forgery
  skip_before_action :verify_authenticity_token, if: :json_request?

  protected

  def json_request?
    request.format.json?
  end
end

CSRF protection is turned on with the protect_from_forgery method, which checks the token and resets the session if it doesn't match what was expected. A call to this method is generated for new Rails applications by default.

The token parameter is named authenticity_token by default. The name and value of this token must be added to every layout that renders forms by including csrf_meta_tags in the HTML head.

Learn more about CSRF attacks and securing your application in the Ruby on {Rails Security Guide}.

Constant Summary

::ActiveSupport::Callbacks - Included

CALLBACK_FILTER_TYPES

::AbstractController::Helpers - Attributes & Methods

Class Method Summary

::ActiveSupport::DescendantsTracker - self

clear, descendants, direct_descendants,
store_inherited

This is the only method that is not thread safe, but is only ever called during the eager loading phase.

::ActiveSupport::Concern - Extended

Instance Method Summary

::AbstractController::Callbacks - Included

#process_action

Override AbstractController::Base's process_action to run the process_action callbacks around the normal behavior.

::ActiveSupport::Callbacks - Included

#run_callbacks

Runs the callbacks for the given event.

DSL Calls

included

[ GitHub ]


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'actionpack/lib/action_controller/metal/request_forgery_protection.rb', line 58

included do
  # Sets the token parameter name for RequestForgery. Calling protect_from_forgery
  # sets it to <tt>:authenticity_token</tt> by default.
  config_accessor :request_forgery_protection_token
  self.request_forgery_protection_token ||= :authenticity_token

  # Holds the class which implements the request forgery protection.
  config_accessor :forgery_protection_strategy
  self.forgery_protection_strategy = nil

  # Controls whether request forgery protection is turned on or not. Turned off by default only in test mode.
  config_accessor :allow_forgery_protection
  self.allow_forgery_protection = true if allow_forgery_protection.nil?

  # Controls whether a CSRF failure logs a warning. On by default.
  config_accessor :log_warning_on_csrf_failure
  self.log_warning_on_csrf_failure = true

  helper_method :form_authenticity_token
  helper_method :protect_against_forgery?
end

Class Attribute Details

._helper_methods (rw)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/helpers.rb', line 11

class_attribute :_helper_methods

._helper_methods?Boolean (rw)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/helpers.rb', line 11

class_attribute :_helper_methods

._helpers (rw)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/helpers.rb', line 8

class_attribute :_helpers

._helpers?Boolean (rw)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/helpers.rb', line 8

class_attribute :_helpers

Instance Attribute Details

#_helper_methods (rw)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/helpers.rb', line 11

class_attribute :_helper_methods

#_helper_methods?Boolean (rw)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/helpers.rb', line 11

class_attribute :_helper_methods

#_helpers (rw)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/helpers.rb', line 8

class_attribute :_helpers

#_helpers?Boolean (rw)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/helpers.rb', line 8

class_attribute :_helpers