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): resolve marketplace agent access in get_graph_execution endpoint (#11396)
Zamil Majdy02757d68f3c7
diff --git a/autogpt_platform/backend/backend/data/graph.py b/autogpt_platform/backend/backend/data/graph.pyindex 91d3c224a6..53993fd7e1 100644--- a/autogpt_platform/backend/backend/data/graph.py+++ b/autogpt_platform/backend/backend/data/graph.py@@ -18,6 +18,7 @@ from prisma.types import (AgentGraphWhereInput,AgentNodeCreateInput,AgentNodeLinkCreateInput,+ StoreListingVersionWhereInput,)from pydantic import BaseModel, Field, create_modelfrom pydantic.fields import computed_field@@ -884,9 +885,9 @@ async def get_graph_metadata(graph_id: str, version: int | None = None) -> Graphasync def get_graph(graph_id: str,- version: int | None = None,+ version: int | None,+ user_id: str | None,*,- user_id: str | None = None,for_export: bool = False,include_subgraphs: bool = False,skip_access_check: bool = False,@@ -897,25 +898,43 @@ async def get_graph(Returns `None` if the record is not found."""- where_clause: AgentGraphWhereInput = {- "id": graph_id,- }+ graph = None- if version is not None:- where_clause["version"] = version+ # Only search graph directly on owned graph (or access check is skipped)+ if skip_access_check or user_id is not None:+ graph_where_clause: AgentGraphWhereInput = {+ "id": graph_id,+ }+ if version is not None:+ graph_where_clause["version"] = version+ if not skip_access_check and user_id is not None:+ graph_where_clause["userId"] = user_id- graph = await AgentGraph.prisma().find_first(- where=where_clause,- include=AGENT_GRAPH_INCLUDE,- order={"version": "desc"},- )+ graph = await AgentGraph.prisma().find_first(+ where=graph_where_clause,+ include=AGENT_GRAPH_INCLUDE,+ order={"version": "desc"},+ )++ # Use store listed graph to find not owned graphif graph is None:- return None+ store_where_clause: StoreListingVersionWhereInput = {+ "agentGraphId": graph_id,+ "submissionStatus": SubmissionStatus.APPROVED,+ "isDeleted": False,+ }+ if version is not None:+ store_where_clause["agentGraphVersion"] = version- if not skip_access_check and graph.userId != user_id:- # For access, the graph must be owned by the user or listed in the store- if not await is_graph_published_in_marketplace(graph_id, graph.version):- return None+ if store_listing := await StoreListingVersion.prisma().find_first(+ where=store_where_clause,+ order={"agentGraphVersion": "desc"},+ include={"AgentGraph": {"include": AGENT_GRAPH_INCLUDE}},+ ):+ graph = store_listing.AgentGraph++ if graph is None:+ return Noneif include_subgraphs or for_export:sub_graphs = await get_sub_graphs(graph)diff --git a/autogpt_platform/backend/backend/executor/activity_status_generator.py b/autogpt_platform/backend/backend/executor/activity_status_generator.pyindex 76477a027c..b2aa420b66 100644--- a/autogpt_platform/backend/backend/executor/activity_status_generator.py+++ b/autogpt_platform/backend/backend/executor/activity_status_generator.py@@ -136,7 +136,12 @@ async def generate_activity_status_for_execution(# Get graph metadata and full graph structure for name, description, and linksgraph_metadata = await db_client.get_graph_metadata(graph_id, graph_version)- graph = await db_client.get_graph(graph_id, graph_version)+ graph = await db_client.get_graph(+ graph_id=graph_id,+ version=graph_version,+ user_id=user_id,+ skip_access_check=True,+ )graph_name = graph_metadata.name if graph_metadata else f"Graph {graph_id}"graph_description = graph_metadata.description if graph_metadata else ""diff --git a/autogpt_platform/backend/backend/server/external/routes/v1.py b/autogpt_platform/backend/backend/server/external/routes/v1.pyindex db232ab811..d4175b5d9d 100644--- a/autogpt_platform/backend/backend/server/external/routes/v1.py+++ b/autogpt_platform/backend/backend/server/external/routes/v1.py@@ -106,10 +106,6 @@ async def get_graph_execution_results(graph_exec_id: str,api_key: APIKeyInfo = Security(require_permission(APIKeyPermission.READ_GRAPH)),) -> GraphExecutionResult:- graph = await graph_db.get_graph(graph_id, user_id=api_key.user_id)- if not graph:- raise HTTPException(status_code=404, detail=f"Graph #{graph_id} not found.")-graph_exec = await execution_db.get_graph_execution(user_id=api_key.user_id,execution_id=graph_exec_id,@@ -120,6 +116,13 @@ async def get_graph_execution_results(status_code=404, detail=f"Graph execution #{graph_exec_id} not found.")+ if not await graph_db.get_graph(+ graph_id=graph_exec.graph_id,+ version=graph_exec.graph_version,+ user_id=api_key.user_id,+ ):+ raise HTTPException(status_code=404, detail=f"Graph #{graph_id} not found.")+return GraphExecutionResult(execution_id=graph_exec_id,status=graph_exec.status.value,diff --git a/autogpt_platform/backend/backend/server/routers/v1.py b/autogpt_platform/backend/backend/server/routers/v1.pyindex 4bd5619b07..e38dd77ac5 100644--- a/autogpt_platform/backend/backend/server/routers/v1.py+++ b/autogpt_platform/backend/backend/server/routers/v1.py@@ -803,7 +803,9 @@ async def create_new_graph(async def delete_graph(graph_id: str, user_id: Annotated[str, Security(get_user_id)]) -> DeleteGraphResponse:- if active_version := await graph_db.get_graph(graph_id, user_id=user_id):+ if active_version := await graph_db.get_graph(+ graph_id=graph_id, version=None, user_id=user_id+ ):await on_graph_deactivate(active_version, user_id=user_id)return {"version_counts": await graph_db.delete_graph(graph_id, user_id=user_id)}@@ -883,7 +885,11 @@ async def set_graph_active_version(if not new_active_graph:raise HTTPException(404, f"Graph #{graph_id} v{new_active_version} not found")- current_active_graph = await graph_db.get_graph(graph_id, user_id=user_id)+ current_active_graph = await graph_db.get_graph(+ graph_id=graph_id,+ version=None,+ user_id=user_id,+ )# Handle activation of the new graph first to ensure continuityawait on_graph_activate(new_active_graph, user_id=user_id)@@ -1069,22 +1075,25 @@ async def get_graph_execution(graph_exec_id: str,user_id: Annotated[str, Security(get_user_id)],) -> execution_db.GraphExecution | execution_db.GraphExecutionWithNodes:- graph = await graph_db.get_graph(graph_id=graph_id, user_id=user_id)- if not graph:- raise HTTPException(- status_code=HTTP_404_NOT_FOUND, detail=f"Graph #{graph_id} not found"- )-result = await execution_db.get_graph_execution(user_id=user_id,execution_id=graph_exec_id,- include_node_executions=graph.user_id == user_id,+ include_node_executions=True,)if not result or result.graph_id != graph_id:raise HTTPException(status_code=404, detail=f"Graph execution #{graph_exec_id} not found.")+ if not await graph_db.get_graph(+ graph_id=result.graph_id,+ version=result.graph_version,+ user_id=user_id,+ ):+ raise HTTPException(+ status_code=HTTP_404_NOT_FOUND, detail=f"Graph #{graph_id} not found"+ )+# Apply feature flags to filter out disabled featuresresult = await hide_activity_summary_if_disabled(result, user_id)diff --git a/autogpt_platform/backend/backend/server/v2/otto/service.py b/autogpt_platform/backend/backend/server/v2/otto/service.pyindex 8efa4f642f..5f00022ff2 100644--- a/autogpt_platform/backend/backend/server/v2/otto/service.py+++ b/autogpt_platform/backend/backend/server/v2/otto/service.py@@ -27,7 +27,9 @@ class OttoService:return Nonetry:- graph = await graph_db.get_graph(request.graph_id, user_id=user_id)+ graph = await graph_db.get_graph(+ graph_id=request.graph_id, version=None, user_id=user_id+ )if not graph:return Nonediff --git a/autogpt_platform/backend/backend/server/v2/store/db.py b/autogpt_platform/backend/backend/server/v2/store/db.pyindex 054d5a2d1b..f6f0f812fe 100644--- a/autogpt_platform/backend/backend/server/v2/store/db.py+++ b/autogpt_platform/backend/backend/server/v2/store/db.py@@ -1343,6 +1343,7 @@ async def get_agent(store_listing_version_id: str) -> GraphModel:graph = await get_graph(graph_id=store_listing_version.agentGraphId,version=store_listing_version.agentGraphVersion,+ user_id=None,for_export=True,)if not graph:diff --git a/autogpt_platform/backend/backend/server/v2/store/routes.py b/autogpt_platform/backend/backend/server/v2/store/routes.pyindex da53d4265d..5f357b2df0 100644--- a/autogpt_platform/backend/backend/server/v2/store/routes.py+++ b/autogpt_platform/backend/backend/server/v2/store/routes.py@@ -542,7 +542,9 @@ async def generate_image(Returns:JSONResponse: JSON containing the URL of the generated image"""- agent = await backend.data.graph.get_graph(agent_id, user_id=user_id)+ agent = await backend.data.graph.get_graph(+ graph_id=agent_id, version=None, user_id=user_id+ )if not agent:raise fastapi.HTTPException(