ERROR:uvicorn.error:Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\<path-to-virtual-env>\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 394, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\allseas_api\api.py", line 63, in __call__
await self._fast_api.__call__(scope, receive, send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\fastapi\applications.py", line 179, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc from None
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\middleware\gzip.py", line 18, in __call__
await responder(scope, receive, send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\middleware\gzip.py", line 35, in __call__
await self.app(scope, receive, self.send_with_gzip)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\middleware\authentication.py", line 48, in __call__
await self.app(scope, receive, send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc from None
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\routing.py", line 227, in handle
await self.app(scope, receive, send)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\routing.py", line 41, in app
response = await func(request)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\fastapi\routing.py", line 182, in app
raw_response = await run_endpoint_function(
File "C:\Users\<path-to-virtual-env>\lib\site-packages\fastapi\routing.py", line 133, in run_endpoint_function
return await dependant.call(**values)
File "C:\Users\<path-to-virtual-env>\lib\site-packages\starlette\authentication.py", line 60, in async_wrapper
request = kwargs.get("request", args[idx] if args else None)
IndexError: tuple index out of range
Checklist
master.Describe the bug
We are using a wrapper around FastAPI which makes it possible to create routes on classes with endpoints on the methods. This makes it easier to manage dependency injection among others.
When we add authentication middleware to our application, the authenticated endpoints fail due to
IndexError: tuple index out of range.This error occurs at
authentication.py:60We use the following definitions:
We have created the class decorator for endpoint ourselves.
I looked in the debugger and both
argsandkwargscontain values.argscontainsselffor the instance method andkwargscontainsrequest. This causes theif argscheck to pass, which meansargs[idx]will cause an IndexError. This can be fixed by adding an additional check:To reproduce
It is not easily possible to create a reproduction example due to our custom wrapper around FastAPI. Implementing this fix does fix the problem for us though.
To reproduce:
Details
Expected behavior
When a request is made to an authentication endpoint the request either returns "forbidden" when the authentication is not valid or returns the required response.
Actual behavior
The authentication wrapper
requiresfails when the request is made. This happens due to anIndexErrorwhen idx >= len(args).Debugging material
Traceback:
Details
Environment
Additional context
We need to add bearer token authentication to our endpoints. Our endpoints are methods on class instances.