Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <blockquote> <p>EDIT >> I posted this answer in a blog post as well: <a href="http://zadasnotes.blogspot.com/2010/11/rails-3-forgery-csrf-protection-for.html" rel="noreferrer">http://zadasnotes.blogspot.com/2010/11/rails-3-forgery-csrf-protection-for.html</a></p> <p>EDIT 2 >> This was changed in Rails 3.0.4. See follow up post here: <a href="http://zadasnotes.blogspot.com/2011/02/rails-forgery-csrf-protection-for-ajax.html" rel="noreferrer">http://zadasnotes.blogspot.com/2011/02/rails-forgery-csrf-protection-for-ajax.html</a></p> </blockquote> </blockquote> <p>After researching it for a while, I decided to dig a bit into the rails code documentation to find out. </p> <p>Starting here: <a href="http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html#method-i-form_authenticity_token" rel="noreferrer">http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html#method-i-form_authenticity_token</a></p> <p><strong>protect_from_forgery</strong> adds a <strong>before_filter</strong> on <strong>verify_authenticity_token</strong> which is shown below: </p> <pre><code># File actionpack/lib/action_controller/metal/request_forgery_protection.rb, line 95 95: def verify_authenticity_token 96: verified_request? || raise(ActionController::InvalidAuthenticityToken) 97: end </code></pre> <p>And the <strong>verified_request?</strong> is shown here:</p> <pre><code># File actionpack/lib/action_controller/metal/request_forgery_protection.rb, line 104: def verified_request? 105: !protect_against_forgery? || request.forgery_whitelisted? || 106: form_authenticity_token == params[request_forgery_protection_token] 107: end </code></pre> <p>Finally <strong>request.forgery_whitelisted?</strong>:</p> <pre><code> # File actionpack/lib/action_dispatch/http/request.rb, line 126 126: def forgery_whitelisted? 127: get? || xhr? || content_mime_type.nil? || !content_mime_type.verify_request? 128: end </code></pre> <p>Notice <strong>xhr?</strong>. xmlHttpRequest is whitelisted and is not on the protect_from_forgery list. So it appears that this is by design. </p> <p>After researching further on xmlHttpRequests it appears that there are restrictions on running them across domains, which makes it unnecessary to apply the csrf check on xhr. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload