Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

CSRF not working Rails 3.0.7

#6743

Example project: https://github.com/mrbrdo/csrf-test (its just rails new csrf_test, rails g scaffold post title:string)
CSRF is enabled in application controller.

Run (while rails server is running): curl -d "post[title]=hacked" http://localhost:3000/posts

It works (post is created), even though there is no CSRF token sent. Maybe I did something wrong here, but if not, this is a very serious bug.

Reported by mrbrdo · April 25th, 2011 @ 03:16 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Matthew Daubert
    Matthew Daubert

    The request_forgery_protection tests pass for me for both 3.0.7 and master branches but I also see the same behavior with your test app. I wonder if the change below could have something to do with it? This was where the InvalidAuthenticityToken exception used to be thrown. I thought that Metal threw this exception and then Dispatch rescued it as an :unprocessable_entity. Looks like this changed at some point to now call reset_session (if the application doesn't overload the handle_unverified_request method).

    I'm not sure what's going on but if it's a real issue then there is also a problem with false positives in the test suite.

    https://github.com/rails/rails/commit/66ce3843d32e9f2ac3b1da20067af...

    April 25th, 2011 @ 04:39 PM

  2. Matthew Daubert
    Matthew Daubert

    Looks like this is the expected behavior as of Rails 3.0.4:
    http://jasoncodes.com/posts/rails-csrf-vulnerability

    For the old functionality you can add this to your ApplicationController:

    def handle_unverified_request
      raise(ActionController::InvalidAuthenticityToken)
    end
    

    April 25th, 2011 @ 04:49 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to invalid
    • Importance changed from to Low

    Please read http://weblog.rubyonrails.org/2011/2/8/csrf-protection-bypass-in-ru...
    Basically you won't get an exception anymore, a session reset will happen now.

    April 25th, 2011 @ 05:00 PM

  4. mrbrdo
    mrbrdo

    I see. Can you explain why the exception is not raised anymore? Is there a security issue if you raise an exception as suggested by Matthew Daubert?

    Thank you.

    April 25th, 2011 @ 05:38 PM

  5. Santiago Pastorino
  6. Matthew Daubert
    Matthew Daubert

    mrbrdo - keep in mind that CSRF protection is meant to prevent session hijacking and has nothing to do with authorizing actions within your application. The Security Guide is a great resource but a bit out of date on this topic (I noticed that docrails/master was updated to reflect the change in 3.0.4 so I imagine the guides site will reflect this soon enough).

    April 25th, 2011 @ 06:15 PM