-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
The "Missing CSRF token" error #720
Comments
Not found |
Hello @qellyka can you please post what the issue is happening aside here? logs or something |
If you’re encountering a “Missing CSRF token” error on routes protected with the On the client side, remember to retrieve the CSRF token from the cookie and send it in the request header named after @app.post("/login", summary="Authorizes the user", tags=["User"])
def login(creds: LoginUserSchema, response: Response):
if creds.login == "admin" and creds.password == "admin1234":
token = security.create_access_token(uid="3422342")
security.set_access_cookies(response=response, token=token)
return {"access_token": token}
raise HTTPException(status_code=401, detail="Incorrect login or password") With this setup, your POST route is protected by CSRF, and you can send the token in the header on subsequent requests. |
Got it, thanks a lot. |
oops |
I just wanted to clarify, if you add this part of the code, or rather replace it, then everything should work? It's just that if so, then I still get this error, even though the CSRF token has appeared. |
(backend-M2WzjYYo-py3.11) D:\dev\dot-hub.net\backend
uvicorn main:app --reload
INFO: Will watch for changes in these directories: ['D:\\dev\\dot-hub.net\\backend']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [14200] using WatchFiles
INFO: Started server process [10068]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:51124 - "GET / HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:51124 - "GET / HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:51129 - "GET /docs HTTP/1.1" 200 OK
INFO: 127.0.0.1:51129 - "GET /openapi.json HTTP/1.1" 200 OK
INFO: 127.0.0.1:51137 - "POST /login HTTP/1.1" 200 OK
INFO: 127.0.0.1:51158 - "POST /setup_db HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 409, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\applications.py", line 113, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
raise exc
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
await self.app(scope, receive, _send)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
raise exc
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\routing.py", line 715, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\routing.py", line 735, in app
await route.handle(scope, receive, send)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\routing.py", line 288, in handle
await self.app(scope, receive, send)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\routing.py", line 76, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
raise exc
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\starlette\routing.py", line 73, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\fastapi\routing.py", line 291, in app
solved_result = await solve_dependencies(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\fastapi\dependencies\utils.py", line 638, in solve_dependencies
solved = await call(**solved_result.values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\authx\main.py", line 619, in _auth_required
return await self._auth_required(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\authx\main.py", line 351, in _auth_required
request_token = await method(
^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\authx\main.py", line 308, in get_access_token_from_request
return await self._get_token_from_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\authx\main.py", line 281, in _get_token_from_request
return await _get_token_from_request(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\wylan\AppData\Local\pypoetry\Cache\virtualenvs\backend-M2WzjYYo-py3.11\Lib\site-packages\authx\core.py", line 156, in _get_token_from_request
raise MissingTokenError(*(str(err) for err in errors))
authx.exceptions.MissingTokenError: Missing CSRF token console |
can you send the code you use to test the endpoint? |
create database @app.post("/setup_db", summary="Creates a new database", tags=["The database"], dependencies=[Depends(security.access_token_required)])
async def setup_db():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
return {"ok": True} add book @app.post("/books", summary="Adds a new book to the database", tags=["Books"], dependencies=[Depends(security.access_token_required)])
async def add_book(data: BookPostSchema, session: SessionDep):
new_book = BookModel(
title = data.title,
author = data.author,
)
session.add(new_book)
await session.commit() |
I meant the code you used to check if a request to a specific url works. I wanted to make sure that when you send a request you send the csrf token in the request header |
I use the documentation built into FastApi, which is available at 127.0.0.1:8000/docs |
You must specify the csrf token in request header taken from the cookie. |
Then why does everything work for him if he uses Swagger UI like me? |
in its protected route the GET method is used.
As I already said by default csrf protection works for methods defined in |
Okay, got it, thanks. |
I did the authorization according to the guide using the JWT token in cookies, everything works well until I check if the user has rights, here is my code:
Please help, I don't know how to solve this problem.
The text was updated successfully, but these errors were encountered: