Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform): Fix marketplace leaking secrets #8281

Merged
merged 7 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions autogpt_platform/backend/backend/data/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import uuid
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Literal
from typing import Any, Dict, Literal
aarushik93 marked this conversation as resolved.
Show resolved Hide resolved

import prisma.types
from prisma.models import AgentGraph, AgentGraphExecution, AgentNode, AgentNodeLink
Expand Down Expand Up @@ -330,7 +330,7 @@ def get_input_schema(self) -> list[InputSchemaItem]:
return input_schema

@staticmethod
def from_db(graph: AgentGraph):
def from_db(graph: AgentGraph, hide_secrets: bool = False):
nodes = [
*(graph.AgentNodes or []),
*(
Expand All @@ -341,7 +341,7 @@ def from_db(graph: AgentGraph):
]
return Graph(
**GraphMeta.from_db(graph).model_dump(),
nodes=[Node.from_db(node) for node in nodes],
nodes=[Graph._process_node(node, hide_secrets) for node in nodes],
links=list(
{
Link.from_db(link)
Expand All @@ -355,6 +355,26 @@ def from_db(graph: AgentGraph):
},
)

@staticmethod
def _process_node(node: AgentNode, hide_secrets: bool) -> Node:
node_dict = node.model_dump()
if hide_secrets and "constantInput" in node_dict:
constant_input = json.loads(node_dict["constantInput"])
Graph._hide_secrets_in_input(constant_input)
node_dict["constantInput"] = json.dumps(constant_input)
return Node.from_db(AgentNode(**node_dict))

@staticmethod
def _hide_secrets_in_input(input_data: Dict[str, Any]):
sensitive_keys = ["api_key", "password", "token", "secret"]
aarushik93 marked this conversation as resolved.
Show resolved Hide resolved
for key, value in input_data.items():
if isinstance(value, dict):
Graph._hide_secrets_in_input(value)
elif isinstance(value, str) and any(
sensitive_key in key.lower() for sensitive_key in sensitive_keys
):
input_data[key] = "*******"
aarushik93 marked this conversation as resolved.
Show resolved Hide resolved


AGENT_NODE_INCLUDE: prisma.types.AgentNodeInclude = {
"Input": True,
Expand Down Expand Up @@ -431,6 +451,7 @@ async def get_graph(
version: int | None = None,
template: bool = False,
user_id: str | None = None,
hide_secrets: bool = False,
aarushik93 marked this conversation as resolved.
Show resolved Hide resolved
) -> Graph | None:
"""
Retrieves a graph from the DB.
Expand All @@ -456,7 +477,7 @@ async def get_graph(
include=AGENT_GRAPH_INCLUDE,
order={"version": "desc"},
)
return Graph.from_db(graph) if graph else None
return Graph.from_db(graph, hide_secrets) if graph else None


async def set_graph_active_version(graph_id: str, version: int, user_id: str) -> None:
Expand Down
5 changes: 4 additions & 1 deletion autogpt_platform/backend/backend/server/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,11 @@ async def get_graph(
graph_id: str,
user_id: Annotated[str, Depends(get_user_id)],
version: int | None = None,
hide_secrets: bool = False,
) -> graph_db.Graph:
graph = await graph_db.get_graph(graph_id, version, user_id=user_id)
graph = await graph_db.get_graph(
graph_id, version, user_id=user_id, hide_secrets=hide_secrets
)
if not graph:
raise HTTPException(status_code=404, detail=f"Graph #{graph_id} not found.")
return graph
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const SubmitPage: React.FC = () => {
const fetchAgentGraph = async () => {
if (selectedAgentId) {
const api = new AutoGPTServerAPI();
const graph = await api.getGraph(selectedAgentId);
const graph = await api.getGraph(selectedAgentId, undefined, true);
setSelectedAgentGraph(graph);
setValue("name", graph.name);
setValue("description", graph.description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ export default class BaseAutoGPTServerAPI {
return this._get("/templates");
}

getGraph(id: string, version?: number): Promise<Graph> {
const query = version !== undefined ? `?version=${version}` : "";
getGraph(
id: string,
version?: number,
hide_secrets?: boolean,
): Promise<Graph> {
let query = version !== undefined ? `?version=${version}` : "";
if (hide_secrets !== undefined) {
query += query ? "&" : "?";
query += `hide_secrets=${hide_secrets}`;
}
return this._get(`/graphs/${id}` + query);
aarushik93 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Loading