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

Consider dropping explicit CSRF protection entirely? #877

Closed
simonw opened this issue Jun 30, 2020 · 9 comments
Closed

Consider dropping explicit CSRF protection entirely? #877

simonw opened this issue Jun 30, 2020 · 9 comments

Comments

@simonw
Copy link
Owner

simonw commented Jun 30, 2020

https://scotthelme.co.uk/csrf-is-dead/ from Feb 2017 has background here. The SameSite=lax cookie property effectively eliminates CSRF in modern browsers. https://caniuse.com/#search=SameSite shows 92.13% global support for it.

Datasette already uses SameSite=lax when it sets cookies by default:

def set_cookie(
self,
key,
value="",
max_age=None,
expires=None,
path="/",
domain=None,
secure=False,
httponly=False,
samesite="lax",
):
assert samesite in SAMESITE_VALUES, "samesite should be one of {}".format(
SAMESITE_VALUES
)

A few options then. I could ditch CSRF protection entirely. I could make it optional - turn it off by default, but let users who care about that remaining 7.87% of global users opt back into it.

One catch: login CSRF: I don't see how SameSite=lax protects against that attack.

@simonw
Copy link
Owner Author

simonw commented Jun 30, 2020

https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#login-csrf

Login CSRF can be mitigated by creating pre-sessions (sessions before a user is authenticated) and including tokens in login form.

Sounds like regular CSRF protection to me.

@simonw
Copy link
Owner Author

simonw commented Jun 30, 2020

Relevant: #835

@abdusco
Copy link
Contributor

abdusco commented Jul 1, 2020

Does this mean custom routes get to expose endpoints accepting POST requests? I've tried earlier to add some POST endpoints, but requests were being rejected by Datasette due to CSRF

@simonw
Copy link
Owner Author

simonw commented Jul 1, 2020

Have you tried the method described here? https://datasette.readthedocs.io/en/latest/internals.html#csrf-protection - I'm happy to bulk out that section of the documentation if that doesn't help solve your problem.

I just closed #835 which should make CSRF protection easier to work with - it won't interfere with requests without cookies or requests with Authentication: Bearer token tokens. See also simonw/asgi-csrf#11

You can try out pip install datasette==0.45a5 to get those features. Hopefully releasing a full 0.45 tomorrow.

@abdusco
Copy link
Contributor

abdusco commented Jul 1, 2020

I am calling the API from another origin, so injecting CSRF token into templates wouldn't work.

EDIT:

I'll try the new version, it sounds promising

@abdusco
Copy link
Contributor

abdusco commented Jul 1, 2020

Bearer tokens sound interesting. Where do tokens come from? An auth provider of my choosing? How do they get verified?

@simonw
Copy link
Owner Author

simonw commented Jul 1, 2020

Tokens get verified by plugins. So far there's only one: https://github.com/simonw/datasette-auth-tokens - which has you hard-coding plugins in a configuration file. I have a issue there to add support for database-backed tokens too: simonw/datasette-auth-tokens#1

@simonw
Copy link
Owner Author

simonw commented Jul 1, 2020

The latest release of https://github.com/simonw/datasette-auth-tokens (0.2) now supports SQL configuration of tokens.

@simonw
Copy link
Owner Author

simonw commented Sep 15, 2020

I'm not going to drop CSRF protection - it's still needed for older browsers - but I have relaxed the circumstances under which it is applied. It only applies to requests that include cookies for example, so API clients that don't send cookies don't need to worry about it.

@simonw simonw closed this as completed Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants