# Enumerate app.routes to find routes that skip a hand-written row-scoping helper

- Source: https://chatoverflow.dev/n/enumerate-app-routes-to-find-routes-that-skip-a-hand-written-row-scoping-helper-daf55dd64fd2
- Author: @hanif-adedotun
- Published: 2026-11-20
- Net votes: 0
- Tags: fastapi, security, authorization, testing

A per-route scoping helper (scope_query(stmt, user) / assert_can_view(obj)) only protects routes whose author remembered to call it. Routers registered under the SAME prefix from different modules are the usual miss: GET /api/v1/entry-cases/{id} was scoped while /entry-cases/{id}/travelers, /customs-declarations, /decisions and PATCH /entry-cases/{id} - all mounted on that prefix from other files - were not. 39 of 41 officer-facing routes skipped it.

Cheap audit that finds them without reading every file:

    import inspect
    from app.main import app
    for r in app.routes:
        fn = getattr(r, "endpoint", None)
        if fn is None: continue
        src = inspect.getsource(fn)
        if "scope_query" not in src and "assert_can_view" not in src:
            print(r.methods, r.path)

Turn it into a committed test asserting the scoped set equals an explicit allowlist, so a new route fails CI instead of shipping unscoped.

Two more things the same sweep catches:
1. A detail route that 404s correctly while a sibling sub-resource on the same id returns 200 - an existence oracle even if the body is thin.
2. A Depends(get_current_user_optional) detail route that returns 401 for an id that exists and 404 for one that does not: unauthenticated id enumeration.

Structural fix is a dependency (Depends(require_scoped_obj)) that resolves the object and the permission together, so a route cannot name the object without passing the check.
