built-in CSRF protect
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.post("/")
def post_root():
with open("some_data", 'w') as f:
f.write("superimportant_data")
Description
Now we have plugin fastapi_csrf_protect, but it looks like strange. Just see code here:
https://www.stackhawk.com/blog/csrf-protection-in-fastapi/
Wanted Solution
Feature request for built-in csrf settings, which will looks like cors - as middleware.
Just something like https://github.com/frankie567/starlette-csrf, but built-in fastapi
Wanted Code
from fastapi import FastAPI
from fastapi.middlewares import CSRFMiddleware
from app.core import app_config
app = FastAPI()
app.add_middleware(CSRFMiddleware, secret="__CHANGE_ME__")
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.post("/", csrf_token=True) # by default
def post_root():
with open("secure_file", 'w') as f:
f.write("superimportant_data")
@app.post("/access_post_without_csrf/", csrf_token=False)
with open("unsecure_file", 'w') as f:
f.write("some_data")
Alternatives
https://github.com/frankie567/starlette-csrf
Operating System
Linux, Windows, macOS
Operating System Details
No response
FastAPI Version
latest
Python Version
3.10.1
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:5
Top Results From Across the Web
Cross-Site Request Forgery Prevention Cheat Sheet
Check if your framework has built-in CSRF protection and use it · Remember that any Cross-Site Scripting (XSS) can be used to defeat...
Read more >A Guide to CSRF Protection in Spring Security - Baeldung
Learn how CSRF attacks work on a practical Spring application, and then how to enable protection against these kinds of attacks with Spring ......
Read more >React CSRF Protection Guide: Examples and How to Enable It
CSRF stands for cross-site request forgery. Let's break down that term. Cross-Site Request. The "cross-site request" part simply means a request ...
Read more >Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ...
When using Windows Authentication, application endpoints must be protected against CSRF attacks in the same way as done for cookies. The browser ...
Read more >CSRF Protection in Flask - TestDriven.io
CSRF, which stands for Cross-Site Request Forgery, is an attack against a web application in which the attacker attempts to trick an ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Update- why you don’t need a middleware for CSRF in 2022: https://github.com/gnat/csrf-starlette-fastapi#why-you-may-not-need-a-csrf-middleware-in-2022
tl;dr: Browsers have improved enough that you can use a dual-cookie system: one lax, one strict.
@gnat it’s okay for newer browsers. What about older?
Also, not protected with subdomain attack