-
-
Notifications
You must be signed in to change notification settings - Fork 347
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
Incorrect middleware call order #104
Comments
Middleware are always called, also in Starlette. Why shouldn't they run for an As for the order of middlewares, this is the pertinent code: def build_middleware_stack(
self,
user_middleware: List[Union[Middleware, Type[BaseHTTPMiddleware], Type[MiddlewareProtocol]]],
allowed_hosts: Optional[List[str]],
cors_config: Optional[CORSConfig],
) -> ASGIApp:
"""
Builds the middleware stack by passing middlewares in a specific order
"""
current_app: ASGIApp = self.asgi_router
if allowed_hosts:
current_app = TrustedHostMiddleware(app=current_app, allowed_hosts=allowed_hosts)
if cors_config:
current_app = CORSMiddleware(app=current_app, **cors_config.dict())
for middleware in user_middleware:
if isinstance(middleware, Middleware):
current_app = middleware.cls(app=current_app, **middleware.options)
else:
current_app = middleware(app=current_app)
return current_app |
See my log, for |
Look how Starlette calls middlewares
and Starlette app
Also Starlette provide |
Lets keep this thread ordered.
|
Despite the fact that in Starlie |
Added PR #105 |
Thanks for your contribution! v1.3.0 has been released with your fixes. |
Great! Thanks. |
So I have an app with configured trusted host and CORS and some middlewares
There are two problems here:
SomeMiddleware2
called beforeSomeMiddleware
. Normally we expect that middlewares will be called in order they were passed.cors_config
andallowed_hosts
settings can't be used with other middlewares, as the other middlewares will be called before host check which in most cases does not make sense. See log below, my middlewares called even for preflight OPTIONS request.The text was updated successfully, but these errors were encountered: