gorilla/csrf

[question] General CSRF Protection and Library Questions

donaldthai opened this issue · 5 comments

Hi, I had a few questions regarding the library and general CSRF protection:

  • This library uses the "Double-Submit" method, so that means that the token is only stored in the secure cookie and client side right? What stops an attacker from visiting a site with csrf protection setup to generate that secure cookie and masked token and forging a request for another user?

  • Should the 32 byte auth key be tied to the user session to make it unique just for that particular user? Don't really see a way to do this with this library.

  • Do login forms need CSRF protection? Does it make sense to generate a csrf token for it before login?

Thanks!

This library uses the "Double-Submit" method, so that means that the token is only stored in the secure cookie and client side right? What stops an attacker from visiting a site with csrf protection setup to generate that secure cookie and masked token and forging a request for another user?

The attacker won't have the same cookie value as the user.

Should the 32 byte auth key be tied to the user session to make it unique just for that particular user? Don't really see a way to do this with this library.

It will always be unique - 32 bytes (256 bits) is a significant amount of entropy. An "attacker" could obtain 1 trillion cookies and still be unable to get the same value as another user.

Do login forms need CSRF protection? Does it make sense to generate a csrf token for it before login?

Yes, as a common CSRF attack would be to have a user login with attacker-provided credentials.

This library uses the "Double-Submit" method, so that means that the token is only stored in the secure cookie and client side right? What stops an attacker from visiting a site with csrf protection setup to generate that secure cookie and masked token and forging a request for another user?

The attacker won't have the same cookie value as the user.

I might be missing some key concepts about how "double-submit" works. Regardless if an attacker won't have the same cookie value, can't an attacker just use their cookie token value to forge a request since the server validates the unmasked and the decoded masked value?

The attacker can't write cookies as your domain, unless:

  1. They man-in-the-middle your connection (which is a whole class of problems - you're screwed anyway here)
  2. They can read a user's cookie from malicious JavaScript inserted on your site (i.e. a compromised JS library, or XSS attack that adds JS)

I see! Been working to get this library integrated into an app I'm contributing to. I think I have a better grasp on how it works. Thank you for your informative answer!

stale commented

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.