Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CRSF token with SameSite=strict #11188

Open
silverwind opened this issue Apr 23, 2020 · 20 comments
Open

Replace CRSF token with SameSite=strict #11188

silverwind opened this issue Apr 23, 2020 · 20 comments
Labels
type/proposal The new feature has not been accepted yet but needs to be discussed first.

Comments

@silverwind
Copy link
Member

silverwind commented Apr 23, 2020

SameSite=strict effectively prevents Cookie-based CRSF attacks and it also brings the benefit of simplifying our code. From Wikipedia:

An additional "SameSite" attribute can be included when the server sets a cookie, instructing the browser on whether to attach the cookie to cross-site requests. If this attribute is set to "strict", then the cookie will only be sent on same-origin requests, making CSRF ineffective.

Browser support is pretty good on it. It also means cookies will never be send to other domains like when STATIC_URL_PREFIX is set differently, but as far as I'm aware, cookies are not needed for static assets.

Related: #5583

@silverwind silverwind changed the title Remove CRSF cookie Remove CRSF cookie and JS variable Apr 23, 2020
@silverwind silverwind changed the title Remove CRSF cookie and JS variable Replace CRSF token with SameSite=strict Apr 23, 2020
@silverwind
Copy link
Member Author

silverwind commented Apr 23, 2020

There is one concerning bit in the RFC draft which distinguishes a "registrable domain":

A request is "same-site" if its target's URI's origin's registrable domain is an exact match for the request's initiator's "site for cookies", and "cross-site" otherwise.

Unless I'm missing something, CRSF from sub1.example.com to sub2.example.com would still possible with that, thought I guess the risk is much lower in such a scenario.

@lafriks
Copy link
Member

lafriks commented Apr 23, 2020

There is still problem if Gitea is hosted in subdir with other apps

@silverwind
Copy link
Member Author

silverwind commented Apr 23, 2020

@lafriks indeed, but I'd say that's a rare scenario and most of today's web's security relies on origins so they would be vulnerable to other attack vectors as well. Maybe we should just add a note that running on subdirectory is dangerous and should be avoided.

@guillep2k guillep2k added the type/proposal The new feature has not been accepted yet but needs to be discussed first. label Apr 24, 2020
@guillep2k
Copy link
Member

I'd rather not rely on draft recommendations for security enforcement. Especially since older browsers will just not implement it.

@silverwind
Copy link
Member Author

silverwind commented Apr 24, 2020

I don't know why it's still draft but it's implemented since 4 years so it should be rather stable. Browsers are in process of enabling the lax setting for all sites (Chrome Status) which further reinforces the stability of that feature.

@CirnoT
Copy link
Contributor

CirnoT commented Apr 24, 2020

There is a very limiting factor to Strict cookies - if a link to Gitea issue or PR is provided on external site, clicking it will be seen as cross-site request and user will be logged out upon following it.

Using Lax will ensure they are sent on cross-site request, but only for protocols deemed safe, so usually GET only (note that this does include forms if their methods is GET)

The subdomains are also an issue, imagine a situation where smaller company sets up web email and Gitea on two subdomains and user receives phishing email.

@mohe2015
Copy link
Contributor

According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie subdomains don't get the cookies if no domain is set.

I think explicitly setting SameSite=Lax should be enough. According to https://web.dev/samesite-cookies-explained/: "The default behaviour applied by Chrome is slightly more permissive than an explicit SameSite=Lax as it will allow certain cookies to be sent on top-level POST requests"

@CirnoT
Copy link
Contributor

CirnoT commented Apr 24, 2020

Lax is certainly a good idea to implement alone, but not as direct replacement for proper CSRF tokens as it still allows for misuse of GET forms via cross-site requests and depends on user-agent support.

@mohe2015
Copy link
Contributor

According to https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html: "Do not use GET requests for state changing operations."

So I think these occurrences should be changed then.

Non state-changing should be fine AFAIK.

@silverwind
Copy link
Member Author

silverwind commented Aug 1, 2020

Pretty sure we have some non-semantic GETs which may block us to some degree here. One such example is #12403.

@DuckDuckWhale
Copy link
Contributor

#12403 and its original issue #10725 seems to be fixed. Any updates?

@silverwind
Copy link
Member Author

Some work had been done to get SameSite support into macaron but I'm not sure where it stands currently as there's also the migration to chi ongoing.

@lunny
Copy link
Member

lunny commented Feb 18, 2021

https://gitea.com/macaron/csrf/commit/9889eb38af52ef888ac7c44ebf739b728647ca75 hasn't been ported to Gitea based chi.

@zeripath
Copy link
Contributor

zeripath commented Mar 6, 2021

#14900 will make all our cookies SameSite lax by default

@zeripath
Copy link
Contributor

zeripath commented Mar 6, 2021

I guess the next step would be then to add configuration to drop the csrf but leave it on unless a server admin wants it off.

@CirnoT
Copy link
Contributor

CirnoT commented Mar 7, 2021

CSRF tokens should still be observed along with cookie configuration. Removal (or even adding option for it) of it would be a very premature action considering browsers are still not yet fully enforcing new cookie configuration in various edge scenarios as to not break the hell out of everything and we have to deal with multiple LTS releases such as Firefox ESR being used.

ref. https://medium.com/@renwa/bypass-samesite-cookies-default-to-lax-and-get-csrf-343ba09b9f2b

@mohe2015
Copy link
Contributor

mohe2015 commented Mar 7, 2021

What about SameSite strict or a combination (lax for GET and both for POST as far as I can remember)?

@silverwind
Copy link
Member Author

silverwind commented Mar 9, 2021

CSRF removal would be nice to have to eliminate a few bugs we have around it. I'm not sure how those bugs exactly but there are ways CRSF tokens will be rejected by legimitate clients. I guess one way to reproduce may be to open form, restart server, send form. I guess it only is feasible once we enforce strict mode.

@jub0bs
Copy link

jub0bs commented Jul 9, 2021

I'm not part of the Gitea community, but I urge you not to rely solely on SameSite to the detriment of anti-CSRF tokens. The SameSite cookie attribute is powerless against cross-origin, same-site attacks, and many orgs do not scrutinize the security level of their subdomains.

@silverwind That section from Wikipedia that you quoted was incorrect and has since been corrected. It now reads:

An additional "SameSite" attribute can be included when the server sets a cookie, instructing the browser on whether to attach the cookie to cross-site requests. If this attribute is set to "strict", then the cookie will only be sent on same-site requests, making CSRF ineffective.

(my emphasis)

@mohe2015 Whether the Domain attribute was set or not in the Set-Cookie header is irrelevant. Requests issued from same-site origins to the target origin will carry all relevant cookies associated with that origin domain.

@mohe2015
Copy link
Contributor

mohe2015 commented Jul 9, 2021

I'm not part of the Gitea community, but I urge you not to rely solely on SameSite to the detriment of anti-CSRF tokens. The SameSite cookie attribute is powerless against cross-origin, same-site attacks, and many orgs do not scrutinize the security level of their subdomains.

Great so now we have an RFC with a security feature that is not a security feature. Why is there no SameOrigin cookie-option...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/proposal The new feature has not been accepted yet but needs to be discussed first.
Projects
None yet
Development

No branches or pull requests

9 participants