AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
test(backend): credits authz invariants see the whole app and assert instead of exempt (#14632)
Reinier van der Leer1317899ab3b4
diff --git a/autogpt_platform/backend/backend/api/features/v1_credits_authz_test.py b/autogpt_platform/backend/backend/api/features/v1_credits_authz_test.pyindex 0c1b35aa3e..1ac43f521b 100644--- a/autogpt_platform/backend/backend/api/features/v1_credits_authz_test.py+++ b/autogpt_platform/backend/backend/api/features/v1_credits_authz_test.py@@ -5,20 +5,26 @@ their credit model through the request's org context, so for a real (pooled)org they read/mutate the shared ``OrgBalance``. They must therefore requireorg-level ``MANAGE_BILLING`` (owner or billing_manager) — a plain org membermust be rejected with 403. Personal-org owners always carry ``is_org_owner``,-so the gate is a no-op for them. The ``/credits`` routes that serve the-caller's own data rather than the org's are exempt, and say why in-``UNGATED_CREDITS_ROUTES``.+so the gate is a no-op for them.The gate is applied as an independent per-route dependency (there is noshared router-level enforcement), so every gated route is asserted here:dropping the dependency from any single route must fail this suite. Routecoverage is not left to the hand-maintained ``GATED_ROUTES`` list either —-two introspection tests walk the mounted app, so both a *newly added* ungated-``/credits`` route and a route reaching ``get_credit_model`` under any other-prefix fail.+every test in this module drives the real mounted application, so a+``/api/credits`` route served by any module is in view, and two introspection+tests walk the routing table.++Every ungated route is asserted by the property that makes it safe rather+than listed by name (SECRT-2650): a name in an exemption list survives the+removal of the thing that justified it."""+import ast+import importlib.utilimport inspect+import sys+import textwrapfrom collections.abc import Callablefrom typing import Anyfrom unittest.mock import AsyncMock, Mock@@ -35,19 +41,14 @@ from autogpt_libs.auth.permissions import OrgActionfrom fastapi.dependencies.models import Dependantfrom fastapi.routing import APIRoute-from backend.api.rest_api import app as real_app+from backend.api.rest_api import app+from backend.data.credit import UserCreditBase, get_credit_modelfrom backend.data.model import AutoTopUpConfig, TransactionHistoryfrom backend.data.org_credit import OrgCreditModel-from .billing.credits import routes as credits_routes-from .billing.subscriptions import routes as subscriptions_routes--app = fastapi.FastAPI()-# Every /credits route this matrix exercises now lives in one of these two-# modules; the durable fix is to give the request-level tests the real app as-# the two introspection tests already use.-app.include_router(credits_routes.router)-app.include_router(subscriptions_routes.router)+# The real application, not a locally-mounted subset: a private app carries only+# the routers someone remembered to include, so a /credits route that moves to+# another module leaves the suite passing over nothing.client = fastapi.testclient.TestClient(app)ORG_ID = "test-org"@@ -114,73 +115,57 @@ GATED_ROUTES: list[GatedRoute] = [GatedRoute(name="get_user_credits",method="GET",- path="/credits",+ path="/api/credits",check_ok=lambda r: r.json() == {"credits": 1000},),GatedRoute(name="request_top_up",method="POST",- path="/credits",+ path="/api/credits",body={"credit_amount": 500},check_ok=lambda r: r.json()["checkout_url"].startswith("https://"),),GatedRoute(name="refund_top_up",method="POST",- path="/credits/test-transaction-key/refund",+ path="/api/credits/test-transaction-key/refund",body={"reason": "duplicate charge"},check_ok=lambda r: r.json() == 500,),GatedRoute(name="fulfill_checkout",method="PATCH",- path="/credits",+ path="/api/credits",check_ok=lambda r: r.content == b"",),GatedRoute(name="configure_user_auto_top_up",method="POST",- path="/credits/auto-top-up",+ path="/api/credits/auto-top-up",body={"amount": 500, "threshold": 100},check_ok=lambda r: r.json() == "Auto top-up settings updated",),GatedRoute(name="get_credit_history",method="GET",- path="/credits/transactions",+ path="/api/credits/transactions",check_ok=lambda r: r.json()["transactions"] == [],),GatedRoute(name="get_refund_requests",method="GET",- path="/credits/refunds",+ path="/api/credits/refunds",check_ok=lambda r: r.json() == [],),GatedRoute(name="list_invoices",method="GET",- path="/credits/invoices",+ path="/api/credits/invoices",check_ok=lambda r: r.json() == [],),]-# ``/credits*`` routes that are deliberately NOT behind MANAGE_BILLING, keyed-# by endpoint function name with the reason they are exempt. Adding an entry-# here is an explicit product decision, not a way to silence the introspection-# test below.-UNGATED_CREDITS_ROUTES: dict[str, str] = {- "get_subscription_status": "subscriptions are user-level, not org-pooled",- "update_subscription_tier": "subscriptions are user-level, not org-pooled",- "stripe_webhook": "unauthenticated by design; verified by Stripe signature",- "get_user_auto_top_up": "reads the caller's own User.top_up_config, not org data",- "manage_payment_method": (- "opens the caller's own Stripe portal; OrgCreditModel does not override "- "create_billing_portal_session"- ),-}--class CreditStubs(pydantic.BaseModel):"""Patched data-layer entry points reachable from the gated routes."""@@ -194,9 +179,13 @@ class CreditStubs(pydantic.BaseModel):@pytest.fixture(autouse=True)def _auth(mock_jwt_user):+ # Restored, not cleared: this is the real app, and the session-scoped+ # SpinTestServer fixture keeps its own get_user_id override on it.+ previous = dict(app.dependency_overrides)app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]yieldapp.dependency_overrides.clear()+ app.dependency_overrides.update(previous)@pytest.fixture@@ -281,8 +270,8 @@ def test_credits_route_requires_manage_billing(@pytest.mark.parametrize("path, expected_json",[- ("/credits/auto-top-up", {"amount": 500, "threshold": 100}),- ("/credits/manage", {"url": "https://billing.example.com/portal"}),+ ("/api/credits/auto-top-up", {"amount": 500, "threshold": 100}),+ ("/api/credits/manage", {"url": "https://billing.example.com/portal"}),],ids=["get_user_auto_top_up", "manage_payment_method"],)@@ -346,11 +335,11 @@ def _enforced_org_actions(dependant: Dependant) -> set[OrgAction]:return enforced-# The eight /api/credits* routes served by modules other than the credits-# router. T250.2's audit (SECRT-2650) found none of them needs MANAGE_BILLING,-# so they are asserted by WHAT makes them safe rather than listed by name: a-# name survives the removal of the thing that justified it, which is the latent-# defect in UNGATED_CREDITS_ROUTES above.+# The ``/api/credits*`` routes deliberately NOT behind MANAGE_BILLING. T250.2's+# audit (SECRT-2650) found that none of them needs it, so each is asserted by+# WHAT makes it safe: a name in an exemption list survives the removal of the+# thing that justified it, and eight of these sat outside this suite's view from+# the day it shipped, because it mounted one router rather than the app.ADMIN_CREDITS_ROUTES = {"add_user_credits","admin_get_all_user_history",@@ -363,6 +352,18 @@ TRIAL_CREDITS_ROUTES = {"get_trial_status","start_trial_checkout",}+# Routes that never resolve the org-pooled credit model at all. The reason is+# prose; the exemption is checked by the transitive walk at the bottom of this+# module.+POOLED_MODEL_UNREACHABLE_ROUTES: dict[str, str] = {+ "get_subscription_status": "subscriptions are user-level, not org-pooled",+ "update_subscription_tier": "subscriptions are user-level, not org-pooled",+ "get_user_auto_top_up": "reads the caller's own User.top_up_config, not org data",+ "stripe_webhook": (+ "unauthenticated by design, verified by Stripe signature; it fulfils "+ "against UserCredit() directly and resolves no org"+ ),+}def _dependency_names(dependant: Dependant) -> set[str]:@@ -377,33 +378,23 @@ def _dependency_names(dependant: Dependant) -> set[str]:def test_every_credits_route_is_gated_or_explicitly_exempt():- """Introspect the mounted app so a *new* ungated /credits route fails.+ """Introspect the routing table so a *new* ungated /credits route fails.``GATED_ROUTES`` above is hand-maintained, so on its own it can only prove- that the routes someone remembered to list are gated. This walks the routing- table instead: every ``/credits*`` route must either carry the MANAGE_BILLING- dependency or be an explicit, documented exemption.-- It walks ``real_app``, not a locally-mounted router: a local mount only sees- the routers this file happens to mount, so a ``/credits`` route served by any- other one would pass vacuously. (#14475's docstring asked for a- ``PENDING_AUDIT`` set of names; T250.2's audit then found none of the eight- needs a gate, so they are asserted by what makes them safe instead — see- ADMIN_CREDITS_ROUTES and TRIAL_CREDITS_ROUTES above, SECRT-2650.)-- A ninth unexpected ``/api/credits*`` route still fails this, which is the- property the widening exists to restore.+ that the routes someone remembered to list are gated. This walks every+ ``/api/credits*`` route the real application serves — whichever module+ serves it — and makes each ungated one prove its exemption."""gated: set[str] = set()- ungated: set[str] = set()- for route in real_app.routes:+ unexplained: set[str] = set()+ for route in app.routes:if not isinstance(route, APIRoute) or not route.path.startswith("/api/credits"):continueif OrgAction.MANAGE_BILLING in _enforced_org_actions(route.dependant):gated.add(route.name)elif route.name in ADMIN_CREDITS_ROUTES:# Safe because an admin JWT role claim gates it, which no org role- # reaches — assert that, not the name.+ # reaches — not even the org owner. Assert that, not the name.assert "requires_admin_user" in _dependency_names(route.dependant), (f"{route.name} is treated as admin-gated but no longer resolves ""requires_admin_user; it now needs MANAGE_BILLING or its own reason"@@ -415,15 +406,32 @@ def test_every_credits_route_is_gated_or_explicitly_exempt():f"{route.name} now resolves an org context; it can reach pooled ""credit and needs gating or its own documented reason")+ elif route.name in POOLED_MODEL_UNREACHABLE_ROUTES:+ assert not _reaches_credit_model(route.endpoint), (+ f"{route.name} now reaches get_credit_model, so it can resolve a "+ "pooled org balance; gate it or re-argue the exemption"+ )+ elif route.name == "manage_payment_method":+ # Safe because OrgCreditModel inherits create_billing_portal_session+ # rather than overriding it, so the route mints a portal for the+ # caller's own Stripe customer even under a pooled org.+ assert (+ OrgCreditModel.create_billing_portal_session+ is UserCreditBase.create_billing_portal_session+ ), (+ "OrgCreditModel now overrides create_billing_portal_session, so "+ "manage_payment_method can act on the org's billing account and "+ "needs MANAGE_BILLING"+ )else:- ungated.add(route.name)+ unexplained.add(route.name)assert gated, "no /api/credits routes found — did the router or prefix change?"- assert ungated == set(UNGATED_CREDITS_ROUTES), (- "A /credits route is not behind MANAGE_BILLING. Gate it with "- "`ctx: BillingManagerContext`, or — if it is genuinely not org-pooled "- "— add it to UNGATED_CREDITS_ROUTES with the reason. Unexpected: "- f"{sorted(ungated - set(UNGATED_CREDITS_ROUTES))}"+ assert not unexplained, (+ "A /api/credits route is neither behind MANAGE_BILLING nor covered by an "+ "asserted exemption. Gate it with `ctx: BillingManagerContext`, or — if "+ "it genuinely serves no org-pooled data — add it to the set above whose "+ f"assertion says why it is safe. Unexpected: {sorted(unexplained)}")assert gated == {route.name for route in GATED_ROUTES}, ("GATED_ROUTES is out of sync with the routes actually carrying the "@@ -440,9 +448,27 @@ ORG_BALANCE_UNGATED: dict[str, str] = {"the executor spends the pooled balance; gating it would stop plain ""members running agents, and it reveals no balance (402 only)"),- "manage_payment_method": UNGATED_CREDITS_ROUTES["manage_payment_method"],+ "manage_payment_method": (+ "opens the caller's own Stripe portal; OrgCreditModel does not override "+ "create_billing_portal_session"+ ),+ "get_home_dashboard": (+ "SECRT-2648 withholds /home's pooled balance from a member without "+ "MANAGE_BILLING, but it gates the read inside the service, where "+ "_get_credits calls check_org_permission, and this walk reads the "+ "route's DEPENDENCY tree — so the route still resolves the credit model "+ "with no route-level gate and this entry stays; removing it turns this "+ "test red"+ ),}+# How deep the walk below follows calls. ``/home`` reaches the credit model+# three hops down (get_home_dashboard -> build_home_dashboard ->+# _load_home_source_data -> _get_credits), so 4 leaves one hop of headroom;+# raising it to 6 adds no further routes.+_CREDIT_MODEL_MAX_HOPS = 4+_WALKED_MODULES = ("backend.api.", "backend.data.")+def test_every_org_balance_reader_is_gated_or_explicitly_exempt():"""Catch org-pooled balance access outside the ``/credits`` prefix.@@ -450,12 +476,20 @@ def test_every_org_balance_reader_is_gated_or_explicitly_exempt():The path-prefix test above cannot see ``execute_graph``, which already readsthe pooled balance from ``/graphs/{id}/execute``, nor a future org-billingroute under another prefix.++ Coverage is every route reaching ``get_credit_model`` within+ ``_CREDIT_MODEL_MAX_HOPS`` calls through functions of+ ``backend.api``/``backend.data``, named either at module level or by an+ import inside the calling function — not every caller in the app: a longer+ chain, a call through an instance attribute, or a dynamically resolved name+ stays out of view. The one-hop source match this replaced could not see+ ``/home`` at all, which is SECRT-2648."""ungated = {route.name- for route in real_app.routes+ for route in app.routesif isinstance(route, APIRoute)- and "get_credit_model" in inspect.getsource(route.endpoint)+ and _reaches_credit_model(route.endpoint)and OrgAction.MANAGE_BILLING not in _enforced_org_actions(route.dependant)}@@ -467,6 +501,159 @@ def test_every_org_balance_reader_is_gated_or_explicitly_exempt():)+def _defers_its_credit_import():+ """Stand-in for a route that imports the credit model inside its body."""+ from backend.data.credit import get_credit_model as _model_getter++ return _model_getter+++def _defers_an_unrelated_import():+ """The same shape, importing something that never reaches the org pool."""+ from backend.data.model import AutoTopUpConfig++ return AutoTopUpConfig+++ "func, expected",+ [(_defers_its_credit_import, True), (_defers_an_unrelated_import, False)],+ ids=["deferred credit import", "deferred unrelated import"],+)+def test_walk_resolves_function_local_imports(func: Callable, expected: bool):+ """A deferred import must not hide the credit model from the walk.++ ``backend/api`` and ``backend/data`` carry 116 function-local ``import+ backend…`` statements, so a route that defers its credit import is an+ ordinary shape here, not a contrivance — and a name bound that way is+ absent from ``__globals__``.+ """+ assert _reaches_credit_model(func) is expected+++def test_unloaded_watched_import_fails_closed():+ """A watched module the walk cannot follow must break it, not shrink it."""+ with pytest.raises(AssertionError, match="backend.data.not_a_real_module"):+ _import_module("backend.data.not_a_real_module", "backend.api.features")+++def _reaches_credit_model(endpoint: Callable) -> bool:+ """Whether ``endpoint`` reaches ``get_credit_model`` within the hop budget.++ Breadth-first, so every function is explored at its shortest distance from+ the endpoint and the budget means what it says. Names are resolved through+ each function's own module globals and compared by identity, so an aliased+ import is followed while a look-alike like ``get_user_credit_model`` — which+ returns the user's own wallet, never the org's — is not a match.+ """+ frontier, seen = [endpoint], {id(endpoint)}+ for _ in range(_CREDIT_MODEL_MAX_HOPS + 1):+ next_frontier: list[Callable] = []+ for func in frontier:+ for obj in _globals_referenced_by(func):+ if obj is get_credit_model:+ return True+ if (+ callable(obj)+ and (getattr(obj, "__module__", "") or "").startswith(+ _WALKED_MODULES+ )+ and id(obj) not in seen+ ):+ seen.add(id(obj))+ next_frontier.append(obj)+ frontier = next_frontier+ return False+++def _globals_referenced_by(func: Callable) -> list[Any]:+ """Objects named in ``func``'s source, resolved to objects.++ Deferred imports are common here — 116 function-local ``import backend…``+ statements under ``backend/api`` and ``backend/data`` — and a name bound by+ one is absent from ``__globals__``, so the import's own bindings are+ resolved alongside them. Missing them would make a route that defers its+ credit import invisible to an invariant whose whole job is to see it.+ """+ try:+ tree = ast.parse(textwrap.dedent(inspect.getsource(func)))+ except (OSError, TypeError, SyntaxError):+ return []+ names = {**getattr(func, "__globals__", {}), **_local_import_bindings(tree, func)}+ referenced: list[Any] = []+ for node in ast.walk(tree):+ if isinstance(node, ast.Name) and node.id in names:+ referenced.append(names[node.id])+ elif isinstance(node, ast.Attribute):+ obj = _resolve_dotted(node, names)+ if obj is not None:+ referenced.append(obj)+ return referenced+++def _local_import_bindings(tree: ast.AST, func: Callable) -> dict[str, Any]:+ """Names bound by ``import``/``from … import`` statements inside ``func``."""+ package = getattr(func, "__module__", "").rsplit(".", 1)[0]+ bindings: dict[str, Any] = {}+ for node in ast.walk(tree):+ if isinstance(node, ast.Import):+ for alias in node.names:+ # `import a.b.c` binds `a`, and _resolve_dotted walks the rest;+ # `import a.b.c as x` binds the leaf module to `x`.+ name = alias.asname or alias.name.split(".")[0]+ module = _import_module(alias.name if alias.asname else name, package)+ if module is not None:+ bindings[name] = module+ elif isinstance(node, ast.ImportFrom):+ module = _import_module("." * node.level + (node.module or ""), package)+ if module is None:+ continue+ for alias in node.names:+ target = getattr(module, alias.name, None)+ if target is not None:+ bindings[alias.asname or alias.name] = target+ return bindings+++def _import_module(name: str, package: str) -> Any:+ """Resolve an import target to an already-loaded module.++ Nothing is imported here, because importing would run a deferred import's+ side effects inside a test. A watched module that is not loaded is a hole in+ the walk rather than a route that reaches nothing, so it raises instead of+ resolving to ``None``: under-reporting is the one answer this invariant must+ never give quietly.+ """+ try:+ absolute = importlib.util.resolve_name(name, package)+ except (ImportError, ValueError):+ return None+ module = sys.modules.get(absolute)+ assert module is not None or not absolute.startswith(_WALKED_MODULES), (+ f"{absolute} is imported inside a walked function but is not loaded, so "+ "the credit-model walk cannot follow it and would under-report. Import "+ "it in this suite, or teach the walk to read its source."+ )+ return module+++def _resolve_dotted(node: ast.Attribute, module_globals: dict) -> Any:+ """Resolve a dotted reference such as ``credit.get_credit_model``."""+ attrs: list[str] = []+ value: ast.expr = node+ while isinstance(value, ast.Attribute):+ attrs.append(value.attr)+ value = value.value+ if not isinstance(value, ast.Name) or value.id not in module_globals:+ return None+ obj = module_globals[value.id]+ for attr in reversed(attrs):+ obj = getattr(obj, attr, None)+ if obj is None:+ return None+ return obj++def _org_member(*, owner: bool = False, admin: bool = False, billing: bool = False) -> Mock:@@ -503,7 +690,7 @@ def test_personal_org_owner_passes_real_context_resolution(mock_prisma.orgmember.find_first = AsyncMock(return_value=Mock(orgId="personal-1"))mock_prisma.orgmember.find_unique = AsyncMock(return_value=_org_member(owner=True))- resp = client.get("/credits")+ resp = client.get("/api/credits")assert resp.status_code == 200, resp.textassert resp.json() == {"credits": 1000}@@ -519,7 +706,7 @@ def test_plain_member_of_shared_org_rejected_real_context_resolution("""The same real resolution path rejects a plain member of a pooled org."""mock_prisma.orgmember.find_unique = AsyncMock(return_value=_org_member())- resp = client.get("/credits", headers={"X-Org-Id": "shared-org"})+ resp = client.get("/api/credits", headers={"X-Org-Id": "shared-org"})assert resp.status_code == 403, resp.textassert resp.json()["detail"] == "Missing org permission: MANAGE_BILLING"