fix(litestar): register plugin middleware at the outer boundary - #731
Closed
CyberneticX-Tech wants to merge 1 commit into
Closed
fix(litestar): register plugin middleware at the outer boundary#731CyberneticX-Tech wants to merge 1 commit into
CyberneticX-Tech wants to merge 1 commit into
Conversation
The plugin appended its middleware to app_config.middleware, which makes them the innermost entries in the ASGI pipeline. A request rejected by user middleware that runs earlier (auth, CORS, rate limiting) never reached the correlation middleware, so its access log and error hooks had no correlation ID. Prepend instead so the correlation context is established before user middleware runs. Closes litestar-org#729
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
SQLSpecPlugin.on_app_initappended its middleware toapp_config.middleware:Litestar runs
middlewareoutermost first, so appending makes the plugin's entries the innermost in the pipeline. Any user middleware that rejects a request early (auth, CORS, rate limiting) short-circuits before the correlation middleware ever runs, and the access log and error hooks for exactly the requests you most want to trace have no correlation ID.Fix
Prepend instead, so the correlation context is established at the outer boundary:
Verification
Reproduced with a small app: a user middleware that returns 401 before the handler, with
X-Request-ID: abc-123on the request.Before:
After:
Tests
Added
test_correlation_middleware_runs_before_user_middlewareto the existing correlation middleware tests. It installs a middleware that rejects with 401, then asserts the correlation ID was populated at that point.Confirmed it is a real regression test. With the fix reverted:
and with the fix restored all 7 pass.
ruff checkandruff format --checkpass on both touched files.Closes #729