Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3344880

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:07:14+00:00 2026-05-18T01:07:14+00:00

Rails seems to be ignoring authenticity tokens for AJAX requests. For instance, I purposely

  • 0

Rails seems to be ignoring authenticity tokens for AJAX requests. For instance, I purposely changed my AJAX call to test this with an invalid token and requests seem to go through normally.

The application has the default configuration to use session cookie store and has the protect_from_forgery call in the ApplicationController.

Any ideas what else I could be missing?

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:07:15+00:00Added an answer on May 18, 2026 at 1:07 am

    EDIT >> I posted this answer in a blog post as well: http://zadasnotes.blogspot.com/2010/11/rails-3-forgery-csrf-protection-for.html
    [archive.org]

    EDIT 2 >> This was changed in Rails 3.0.4. See follow up post here: http://zadasnotes.blogspot.com/2011/02/rails-forgery-csrf-protection-for-ajax.html
    [archive.org]

    After researching it for a while, I decided to dig a bit into the rails code documentation to find out.

    Starting here: http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html#method-i-form_authenticity_token

    protect_from_forgery adds a before_filter on verify_authenticity_token which is shown below:

    # 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
    

    And the verified_request? is shown here:

    # 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
    

    Finally request.forgery_whitelisted?:

       # 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
    

    Notice xhr?. xmlHttpRequest is whitelisted and is not on the protect_from_forgery list. So it appears that this is by design.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following this tutorial (seems good) for Rails. After I run ruby script/generate scaffold
I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is
As popular as Ruby and Rails are, it seems like this problem would already
Since Rails is not multithreaded (yet), it seems like a threaded web framework would
I've got a really simple rails question here but I can't seem to find
The question says it all. I can't seem to find any recent rails tutorials
Rails uses the concept of migrations to deal with model changes using the ActiveRecord
Rails comes with a handy session hash into which we can cram stuff to
Rails comes with RESTful resources out of the box, but do you use those
Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.