fix(middleware): apply_cors/apply_csrf compose instead of clobbering the middleware stack - #231
Merged
Merged
Conversation
…the middleware stack set_middleware() replaces the entire single-slot request-middleware list, but apply_cors() and apply_csrf() both called it internally. Calling add_middleware(auth) followed by apply_cors(...) silently deleted the auth middleware; calling apply_csrf(...) followed by apply_cors(...) silently deleted the CSRF guard. No error, no warning — a composed security stack just lost a layer. - Add App.add_middleware(handler, phase="request"|"response"|"both"), wiring the previously-unused native add_middleware pymethod. - apply_cors() and apply_csrf() now use add_middleware() instead of set_middleware(), so they compose with anything already registered (including with each other) instead of replacing it. - set_middleware() now warns when it would clobber existing middleware registered via add_middleware, for callers who still use the single-slot API directly. Closes #209
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.
Problem
App.set_middleware()replaces the entire single-slot request-middleware list. Bothapply_cors()andapply_csrf()called it internally:app.add_middleware(auth)followed byapply_cors(app, cfg)silently deletedauth.apply_csrf(app, cfg)followed byapply_cors(app, cfg)silently deleted the CSRF guard.No error, no warning — a composed security stack just lost a layer.
Fix
App.add_middleware(handler, phase="request"|"response"|"both")on the PythonAppclass — the nativeadd_middlewarepymethod existed but had no Python-level entry point.apply_cors()andapply_csrf()now calladd_middleware()instead ofset_middleware(), so they compose with anything already registered (including with each other) instead of replacing it.set_middleware()now emits awarnings.warnwhen it would clobber existingadd_middleware-registered middleware, for callers who still use the single-slot API directly.Testing
New tests in
tests/test_middleware.py:test_add_middleware_then_apply_cors_does_not_clobber_authtest_apply_csrf_then_apply_cors_compose_instead_of_clobberingtest_set_middleware_warns_when_clobbering_add_middlewareUpdated
tests/test_csrf_units.py's app stub (apply_csrfnow callsadd_middleware, notset_middleware).ruff check/ruff format --check: clean.Closes #209