Repository navigation
Health: FastAPI checks added where a router is included - #15
Merged
haydenschmitty merged 1 commit intoOct 30, 2026
Merged
haydenschmitty merged 1 commit into
haydenschmitty merged 1 commit into
Conversation
A FastAPI app often adds its sign-in check once, where each router is
included:
app.include_router(admin.router, dependencies=[Depends(require_admin)])
The Python reader kept only the include's prefix, so every endpoint of
such a router was reported as open to anyone. It now reads the include's
dependencies into the mount's middleware, which Health already honours
for Express (app.use('/admin', requireStaff, adminRouter)). The mount
check now uses isGuardDep, so a database session added there
(Depends(get_db_session)) still isn't a sign-in check.
Finding the router by the name main.py binds:
- `from app.routes import campaigns as campaign_routes` binds
campaign_routes, which matched no import, so the router lost its
prefix and its checks;
- `from app.platform import pricing` matched a `platform` router first,
because its module ends in the same name.
The import that binds the mounted name in the file now wins, before the
old fallbacks.
A sample app under tests/fixtures/fastapi-include-router covers all
three: guarded includes (one over several lines beside a database
session), a renamed import, a package sharing a router's name, and an
include whose only dependency is a database session, which stays open.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
|
Thanks @stevensiht, nicely done, and the test with a database session alone is exactly the right negative case. Merged and released in 0.3.1. One edge case for later: a list nested inside |
haydenschmitty
added a commit
that referenced
this pull request
Oct 30, 2026
The temp folder is /var/… but Node runs serve.mjs from /private/var/…. Found by @stevensiht in #15.
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 was wrong
Many FastAPI apps add their sign-in check once, where each router is included, rather than on every endpoint:
Health didn't see this, so every endpoint of such a router was reported as "Anyone could change data through …". On a real FastAPI + React app (one repo, ~170 endpoints, every router guarded this way and held by a route-guard test) the map showed 3 high, 5 medium and 5 low false alarms. After this change it shows none, and its real "very large files" item stays.
The fix
extract.mjs: Pythoninclude_router(...)now readsdependencies=[Depends(x), Security(y)]into the mount'smw. The arguments are read up to the call's own closing bracket, becauseDepends(...)has brackets of its own, and an include can span several lines.resolveRoutesalready carries mount middleware down to each route (mountMw), and Health already honours it for Express'sapp.use('/admin', requireStaff, router).health.mjs: the mount check usesisGuardDepinstead ofisGuardName, so a database session added at the include (Depends(get_db_session)) still isn't a sign-in check. For Express middleware names this changes nothing in practice.routes.mjs: two cases where an include pointed at the wrong file, both found on the same app:from app.routes import campaigns as campaign_routestheninclude_router(campaign_routes.router)matched no import, so the router lost its prefix as well as its checks;from app.platform import pricingmatched aplatformrouter first, because the module ends in the same name.mountTargetnow looks first for the import that binds the mounted name in the file, then falls back to the old matching.Tests
A new sample app,
tests/fixtures/fastapi-include-router, and a test intests/health.test.mjscover:Without the fix, the test fails (
DELETE /items/{item_id}andGET /staff/reportreported open, and the renamed router's prefix missing). With it, the test passes.npm test: 69 of 70 pass. The one failure,ask.test.mjs"with only Codex installed…", fails the same way onmainon macOS: the temp folder comes back as/private/var/…against/var/…. It's unrelated to this change.claude plugin validate .passes. A line is added under "Unreleased" inCHANGELOG.md.🤖 Generated with Claude Code