3 ms·
I don't LOVE the default implementation in Rails 3.2.19 or Rails 4. It seems like they're trying to be overly clever (clearing authentication info but letting t
by Someone1234 12y ago
I don't LOVE the default implementation in Rails 3.2.19 or Rails 4. It seems like they're trying to be overly clever (clearing authentication info but letting the request go through) when, as shown here, that opens up potential security lapses while not really offering anything.
In Microsoft's MVC they simply just throw an exception when a CSRF token mismatch occurs. That to me is rational. It stops the request dead in its tracks, there's no gotchas and no potential exploits in the future (that might be application specific).
I guess it seems like Rail's approach is depending on a blacklist (i.e. "here are things a CSRF failed request cannot contain, everything else is just fine") whereas Microsoft's approach is just to assume the whole request is bad, then let the application author manually write in overrides if they know better (or to just pick up the exception and handle that).
Frankly the author's solution is what Rails should be doing:
def handle_unverified_request
raise(ActionController::InvalidAuthenticityToken)
end
- gingerlime 12y agoI didn't think I'd be upvoting something that compares Microsoft to Rails, but in this case I totally agree. I wasn't aware that all this cleverness is taking place instead of raising an error. Does this also mean that any user who is attacked with CSRF gets logged out of the website?
- Someone1234 12y ago> Does this also mean that any user who is attacked with CSRF gets logged out of the website? Nope. It only strips it from the specific request that fails the CSRF validator. A client shouldn't notice.
- yourmysin 12y agoExactly. The session information is stripped from the request, but the session is not really "destroyed." Rails leverages client side sessions, which are signed to prevent tampering. Because of this nature, however, sessions can be constantly replayed.
- gingerlime 12y agoBut most apps move away from client-side sessions and use some kind of a session store (in memory, redis, memcache typically). In this case, the session.destroy should really destroy the session, meaning the user will be logged out (that's as far as I can tell without actually trying it)
- neerajdotname2 12y agoIn Rails4 you can do protect_from_forgery with: :exception This is also the default value if you generate a brand new rails application.
- yourmysin 12y agoYou're correct. Thankfully Rails4 provides the option of doing this by default, but in my opinion, it should still be default behavior, and should not require an option to the protect_from_forgery method. Unfortunately, the Rails team have stated that it was introduced as an option, rather than default behavior for backwards compatibility reasons. It leaves me to wonder how many applications would actually have an issue if the default behavior was to generate an exception.