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.
fix(backend): allow admins to download submitted agents pending review (#12535)
Nicholas Tindleb80e5ea98796
diff --git a/autogpt_platform/backend/backend/api/features/admin/store_admin_routes_test.py b/autogpt_platform/backend/backend/api/features/admin/store_admin_routes_test.pynew file mode 100644index 0000000000..d52a03fa20--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/admin/store_admin_routes_test.py@@ -0,0 +1,93 @@+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.data.graph import get_graph_as_admin++# Shared constants+ADMIN_USER_ID = "admin-user-id"+CREATOR_USER_ID = "other-creator-id"+GRAPH_ID = "test-graph-id"+GRAPH_VERSION = 3+++def _make_mock_graph(user_id: str = CREATOR_USER_ID) -> MagicMock:+ graph = MagicMock()+ graph.userId = user_id+ graph.id = GRAPH_ID+ graph.version = GRAPH_VERSION+ graph.Nodes = []+ return graph+++async def test_admin_can_access_pending_agent_not_owned() -> None:+ """Admin must be able to access a graph they don't own even if it's not+ APPROVED in the marketplace. This is the core use case: reviewing a+ submitted-but-pending agent from the admin dashboard."""+ mock_graph = _make_mock_graph()+ mock_graph_model = MagicMock(name="GraphModel")++ with (+ patch(+ "backend.data.graph.AgentGraph.prisma",+ ) as mock_prisma,+ patch(+ "backend.data.graph.GraphModel.from_db",+ return_value=mock_graph_model,+ ),+ ):+ mock_prisma.return_value.find_first = AsyncMock(return_value=mock_graph)++ result = await get_graph_as_admin(+ graph_id=GRAPH_ID,+ version=GRAPH_VERSION,+ user_id=ADMIN_USER_ID,+ for_export=False,+ )++ assert (+ result is not None+ ), "Admin should be able to access a pending agent they don't own"+ assert result is mock_graph_model+++async def test_admin_download_pending_agent_with_subagents() -> None:+ """Admin export (for_export=True) of a pending agent must include+ sub-graphs. This exercises the full export code path that the Download+ button uses."""+ mock_graph = _make_mock_graph()+ mock_sub_graph = MagicMock(name="SubGraph")+ mock_graph_model = MagicMock(name="GraphModel")++ with (+ patch(+ "backend.data.graph.AgentGraph.prisma",+ ) as mock_prisma,+ patch(+ "backend.data.graph.get_sub_graphs",+ new_callable=AsyncMock,+ return_value=[mock_sub_graph],+ ) as mock_get_sub,+ patch(+ "backend.data.graph.GraphModel.from_db",+ return_value=mock_graph_model,+ ) as mock_from_db,+ ):+ mock_prisma.return_value.find_first = AsyncMock(return_value=mock_graph)++ result = await get_graph_as_admin(+ graph_id=GRAPH_ID,+ version=GRAPH_VERSION,+ user_id=ADMIN_USER_ID,+ for_export=True,+ )++ assert result is not None, "Admin export of pending agent must succeed"+ mock_get_sub.assert_awaited_once_with(mock_graph)+ mock_from_db.assert_called_once_with(+ graph=mock_graph,+ sub_graphs=[mock_sub_graph],+ for_export=True,+ )diff --git a/autogpt_platform/backend/backend/data/graph.py b/autogpt_platform/backend/backend/data/graph.pyindex a9a92dcdd5..4d85ba69b3 100644--- a/autogpt_platform/backend/backend/data/graph.py+++ b/autogpt_platform/backend/backend/data/graph.py@@ -1207,13 +1207,9 @@ async def get_graph_as_admin(order={"version": "desc"},)- # For access, the graph must be owned by the user or listed in the store- if graph is None or (- graph.userId != user_id- and not await is_graph_published_in_marketplace(- graph_id, version or graph.version- )- ):+ # Admin access bypasses ownership and marketplace checks — route-level+ # auth already ensures only admins can call this function.+ if graph is None:return Noneif for_export:diff --git a/autogpt_platform/backend/backend/data/graph_test.py b/autogpt_platform/backend/backend/data/graph_test.pyindex f07317b879..0034f9bdf8 100644--- a/autogpt_platform/backend/backend/data/graph_test.py+++ b/autogpt_platform/backend/backend/data/graph_test.py@@ -1,6 +1,6 @@import jsonfrom typing import Any-from unittest.mock import AsyncMock, patch+from unittest.mock import AsyncMock, MagicMock, patchfrom uuid import UUIDimport fastapi.exceptions@@ -13,7 +13,7 @@ from backend.api.model import CreateGraphfrom backend.blocks._base import BlockSchema, BlockSchemaInputfrom backend.blocks.basic import StoreValueBlockfrom backend.blocks.io import AgentInputBlock, AgentOutputBlock-from backend.data.graph import Graph, Link, Node+from backend.data.graph import Graph, Link, Node, get_graphfrom backend.data.model import SchemaFieldfrom backend.data.user import DEFAULT_USER_IDfrom backend.usecases.sample import create_test_user@@ -595,3 +595,82 @@ def test_mcp_credential_combine_no_discriminator_values():f"Expected 1 credential entry for MCP blocks without discriminator_values, "f"got {len(combined)}: {list(combined.keys())}")+++# --------------- get_graph access-control regression tests --------------- #+# These protect the behavior introduced in PR #11323 (Reinier, 2025-11-05):+# non-owners can access APPROVED marketplace agents but NOT pending ones.+++def _make_mock_db_graph(user_id: str = "owner-user-id") -> MagicMock:+ graph = MagicMock()+ graph.userId = user_id+ graph.id = "graph-id"+ graph.version = 1+ graph.Nodes = []+ return graph+++async def test_get_graph_non_owner_approved_marketplace_agent() -> None:+ """A non-owner should be able to access a graph that has an APPROVED+ marketplace listing. This is the normal marketplace download flow."""+ owner_id = "owner-user-id"+ requester_id = "different-user-id"+ graph_id = "graph-id"+ mock_graph = _make_mock_db_graph(owner_id)+ mock_graph_model = MagicMock(name="GraphModel")++ mock_listing = MagicMock()+ mock_listing.AgentGraph = mock_graph++ with (+ patch("backend.data.graph.AgentGraph.prisma") as mock_ag_prisma,+ patch(+ "backend.data.graph.StoreListingVersion.prisma",+ ) as mock_slv_prisma,+ patch(+ "backend.data.graph.GraphModel.from_db",+ return_value=mock_graph_model,+ ),+ ):+ # First lookup (owned graph) returns None — requester != owner+ mock_ag_prisma.return_value.find_first = AsyncMock(return_value=None)+ # Marketplace fallback finds an APPROVED listing+ mock_slv_prisma.return_value.find_first = AsyncMock(return_value=mock_listing)++ result = await get_graph(+ graph_id=graph_id,+ version=1,+ user_id=requester_id,+ )++ assert result is not None, "Non-owner should access APPROVED marketplace agent"+++async def test_get_graph_non_owner_pending_marketplace_agent_denied() -> None:+ """A non-owner must NOT be able to access a graph that only has a PENDING+ (not APPROVED) marketplace listing. The marketplace fallback filters on+ submissionStatus=APPROVED, so pending agents should be invisible."""+ requester_id = "different-user-id"+ graph_id = "graph-id"++ with (+ patch("backend.data.graph.AgentGraph.prisma") as mock_ag_prisma,+ patch(+ "backend.data.graph.StoreListingVersion.prisma",+ ) as mock_slv_prisma,+ ):+ # First lookup (owned graph) returns None+ mock_ag_prisma.return_value.find_first = AsyncMock(return_value=None)+ # Marketplace fallback finds nothing (not APPROVED)+ mock_slv_prisma.return_value.find_first = AsyncMock(return_value=None)++ result = await get_graph(+ graph_id=graph_id,+ version=1,+ user_id=requester_id,+ )++ assert result is None, "Non-owner must not access a pending marketplace agent"