Understanding Rails' protect_from_forgery
blog.nvisium.com
blog.nvisium.com
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)
endI 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?
Nope. It only strips it from the specific request that fails the CSRF validator. A client shouldn't notice.
Rails leverages client side sessions, which are signed to prevent tampering. Because of this nature, however, sessions can be constantly replayed.
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)
protect_from_forgery with: :exception
This is also the default value if you generate a brand new rails application.
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.
https://auth0.com/blog/2014/01/07/angularjs-authentication-w...
That's how I've always implemented them (not using Rails). Basically a separate token which must be presented with cookies to perform certain actions. If the cookie identifies a valid session but the token does not pass verification you assume it's a forged request.
If a user would ever open a second tab for your site then that entire model breaks down (as they would have to re-authenticate each and every time). Ditto every time a user leaves and returns.
For example, if you ran a shopping site this kind of token-based tracking would work fine for the checkout wizard-workflow, but not really for the actual store (e.g. recommendations, shopping basket, dynamic shipping calculation based on location, etc).
It definitely has its uses. But those uses are still highly limited.
Token auth is vulnerable to XSS attacks, but not CSRF. So additional precautions can be added.
But I do tend to agree with Egor Homakov where XSS is a serious risk to use auth token stored via httpOnly cookie: https://twitter.com/homakov/status/298744658706714624