Skip to content

Health: FastAPI's app-wide @app.middleware("http") login check counts (#86) - #87

Merged
haydenschmitty merged 1 commit into
haydenschmitty:mainfrom
ahmedjanwar:fix/fastapi-http-middleware-guard
Nov 15, 2026
Merged

haydenschmitty merged 1 commit into
haydenschmitty:mainfrom
ahmedjanwar:fix/fastapi-http-middleware-guard

Conversation

@ahmedjanwar

Copy link
Copy Markdown
Contributor

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". pyRoutes recognises Flask's @app.before_request as 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: a return or raise whose statement carries a 401 or 403, such as JSONResponse(..., status_code=401), HTTPException(status_code=403) or status.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 own AuthenticationMiddleware doesn't reject anyone, so that case needs its own care. I'm happy to look at it separately.

How I tested it

  • New tests/fastapi-http-middleware.test.mjs (Python, FastAPI, one repo, routes in a second file through include_router):
    • counted: a cookie check returning a 401 JSONResponse; an allow list followed by a multi-line 401 and a redirect; an API-key check raising HTTPException(403);
    • not counted: a header-only middleware, one that logs 401 responses, and auth_timer, which is named like a guard but only times requests.
  • The two "counted" tests fail without the change. The "not counted" one passes both ways, as a guard against over-matching.
  • Mapped the official full-stack-fastapi-template (FastAPI + React) before and after: same 23 endpoints and identical Health issues. Its add_middleware(CORSMiddleware) correctly still doesn't count.
  • On my own FastAPI project, protected by one HTTP middleware, the false "None of your endpoints check who is calling" is gone.

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

  • No new dependencies
  • Anything the map shows people is in plain language
  • No secret values stored or printed
  • No details of a real private project in code, comments, docs or screenshots
  • npm test passes (205 passed, 1 platform skip)
  • claude plugin validate . passes
  • CHANGELOG.md updated, if people will notice the change

…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.
@haydenschmitty

Copy link
Copy Markdown
Owner

Merged and released in 0.9.0. Thank you!

Update with claude plugin update floorplan@floorplan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Health: FastAPI's app-wide @app.middleware("http") login check isn't recognised

2 participants