fix: resolve route handler for lazily included routers (FastAPI >= 0.137)#285
Open
diogocacarneiro-lang wants to merge 1 commit into
Open
Conversation
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.
Fixes the lookup gap reported in #281: with FastAPI >= 0.137, routes added through
include_router()are never rate limited by either middleware, and the requests are silently treated as exempt.Cause
app.routesno longer holds the included routes themselves, only a wrapper. The wrapper has noendpoint, so_find_route_handlerreturnsNonefor those paths, and_should_exempt(limiter, None)isTrue. The result fails open and without a log line: the limiter looks installed and configured, but never rejects anything on those routes.Routes declared directly on the app still limit correctly, which is why this is easy to miss.
Change
_find_route_handlerfollows the indirection: if a route exposeseffective_route_contexts(), its contexts are resolved with the same lookup (each context has both.matches()and.endpoint).getattr(..., None)makes it a no-op on plain Starlette apps and on FastAPI versions that include routers eagerly, so no version guard is needed.Both
SlowAPIMiddlewareandSlowAPIASGIMiddlewareshare this function, so both are fixed by the one change.Tests
Two tests, in
tests/test_fastapi_extension.py:test_default_limits_apply_to_routes_from_include_routeruses the existingbuild_fastapi_appfixture, so it runs against both public middlewares (3 parametrisations) and asserts[200, 200, 200, 429, 429]on a3/minutedefault limit.test_find_route_handler_resolves_lazily_included_routesis a unit test against a stub that exposeseffective_route_contexts(), so it documents the contract on any FastAPI version.Verified red-before / green-after on
fastapi 0.140.0,starlette 1.3.1: with the change reverted, all 4 fail; with it, all 4 pass.Full suite on the same environment: 12 failed, 91 passed before this branch and 12 failed, 95 passed after, i.e. the same failure set plus the 4 new tests. Those 12 pre-existing failures are unrelated to this change (they come from running against a much newer Starlette than the one pinned in
pyproject.toml).Deliberately not included
handler is Nonemeaning "exempt" is what made this failure silent, and it will make the next lookup gap silent too. A warning when a route matched but the handler could not be resolved would turn that into a noisy failure, but it is a behaviour change and belongs in its own PR rather than being smuggled into a fix.#230 reports
_find_route_handlerfailing for generated routes, which looks like the same lookup being too narrow; this PR does not attempt to cover that case.