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

Add CSRF recipe into the documentation #633

Open
frankie567 opened this issue May 13, 2021 · 4 comments
Open

Add CSRF recipe into the documentation #633

frankie567 opened this issue May 13, 2021 · 4 comments
Labels
documentation Improvements or additions to documentation polar Issues that can be backed through Polar

Comments

@frankie567
Copy link
Member

frankie567 commented May 13, 2021

It's possible to have it thanks to asgi-csrf. A detailed explanation and example in the doc would be nice.

Add an alert in the Cookie authentication backend to invite the user to check it out.

Fund with Polar
@frankie567 frankie567 added the documentation Improvements or additions to documentation label May 13, 2021
@PunishedSnakePr
Copy link

PunishedSnakePr commented May 10, 2022

Hi. Any news on this? Regarding CSRF protection or whatever comes from the client, is there a way to hook into the fastapi-users default routes to add custom behavior / protection /verification?

@frankie567
Copy link
Member Author

Not yet, sorry. Regarding CSRF, a common way is to handle it is through a middleware, so it should be quite transparent for FastAPI Users.

Another nice option is also router-level or app-level dependencies:

@PunishedSnakePr
Copy link

Thanks for your super fast response! :)

@frankie567 frankie567 added polar Issues that can be backed through Polar and removed polar Issues that can be backed through Polar labels May 31, 2023
@rbracco
Copy link

rbracco commented Oct 14, 2023

Adding onto this I think there might be a way to avoid CSRF entirely, and certain fastapi csrf extensions like this one are recommending it.

The solution is to send two cookies instead of one, one using samesite=lax and one using samesite=strict. This can be solved by implementing a DualCookieTransport that is similar to CookieTransport but just sets two cookies (using the same token), one lax and one strict. Then for routes that can do anything important, like mutate data, you can check for the presence of a strict token. This can be implemented as a dependency, but it also might be possible by extending the fastapi_users.current_user() pattern to have a strict attribute, in addition to superuser/active/verified...etc, so that then the route could just require a strict_user if necessary. Since CSRF is only a risk with cookie transport though, this might be a bad idea since it would be coupling fastapi_users.current_user to CookieTransport.

This stuff is frankly above my level but I thought I'd share it here in case anybody wanted to take it and run with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation polar Issues that can be backed through Polar
Projects
None yet
Development

No branches or pull requests

3 participants