Repository navigation
Health: FastAPI's app-wide @app.middleware("http") login check counts (#86) - #87
Merged
haydenschmitty merged 1 commit intoNov 15, 2026
Conversation
…haydenschmitty#86) A FastAPI app whose routes are all protected by one HTTP middleware was told "None of your endpoints check who is calling": pyRoutes knew Flask's @app.before_request but had no equivalent for FastAPI and Starlette. An @app.middleware("http") function is now a global guard when it turns callers away itself: a return or raise whose statement carries a 401 or 403 (JSONResponse(..., status_code=401), HTTPException(403), status.HTTP_401_UNAUTHORIZED). A middleware that only adds headers, measures time or reads a 401 from the response doesn't count, so it can't hide open endpoints.
Owner
|
Merged and released in 0.9.0. Thank you! Update with |
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.
What this changes
Fixes #86. A FastAPI app whose routes are all protected by one
@app.middleware("http")function was told "None of your endpoints check who is calling".pyRoutesrecognises Flask's@app.before_requestas an app-wide guard, but had no equivalent for FastAPI/Starlette.An
@app.middleware("http")function now counts as a global guard when it turns callers away itself: areturnorraisewhose statement carries a 401 or 403, such asJSONResponse(..., status_code=401),HTTPException(status_code=403)orstatus.HTTP_401_UNAUTHORIZED, including statements spread over several lines. That's the same idea as the existing Flask check, only stricter. The 401/403 has to be in what the middleware returns or raises, so one that only adds headers, measures time or reads a 401 from the response (if response.status_code == 401: log(...)) protects nothing, whatever its name. The aim is not to create the kind of false "all clear" #72 is about.Not included: class-based middleware added with
app.add_middleware(...). Starlette's ownAuthenticationMiddlewaredoesn't reject anyone, so that case needs its own care. I'm happy to look at it separately.How I tested it
tests/fastapi-http-middleware.test.mjs(Python, FastAPI, one repo, routes in a second file throughinclude_router):JSONResponse; an allow list followed by a multi-line 401 and a redirect; an API-key check raisingHTTPException(403);auth_timer, which is named like a guard but only times requests.add_middleware(CORSMiddleware)correctly still doesn't count.This branch and #85 each add an "Unreleased" section to
CHANGELOG.md. Whichever merges second will need that one small merge fixed, and I'm happy to rebase.Checklist
npm testpasses (205 passed, 1 platform skip)claude plugin validate .passesCHANGELOG.mdupdated, if people will notice the change