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(frontend/builder): validate key and handle against node schemas in handleApplyAction
Zamil Majdya4282d927a8f
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.tsindex 594d2c886a..58a521d64a 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts@@ -127,7 +127,7 @@ describe("useBuilderChatPanel – handleApplyAction", () => {});it("connect_nodes: calls addEdge when both nodes exist", () => {- mockNodes.push({ id: "src" }, { id: "tgt" });+ mockNodes.push({ id: "src", data: {} }, { id: "tgt", data: {} });const { result } = renderHook(() => useBuilderChatPanel());act(() => {@@ -151,7 +151,7 @@ describe("useBuilderChatPanel – handleApplyAction", () => {});it("connect_nodes: does NOT call addEdge when source node is missing", () => {- mockNodes.push({ id: "tgt" });+ mockNodes.push({ id: "tgt", data: {} });const { result } = renderHook(() => useBuilderChatPanel());act(() => {@@ -168,7 +168,7 @@ describe("useBuilderChatPanel – handleApplyAction", () => {});it("connect_nodes: does NOT call addEdge when target node is missing", () => {- mockNodes.push({ id: "src" });+ mockNodes.push({ id: "src", data: {} });const { result } = renderHook(() => useBuilderChatPanel());act(() => {@@ -183,6 +183,137 @@ describe("useBuilderChatPanel – handleApplyAction", () => {expect(mockAddEdge).not.toHaveBeenCalled();});++ it("update_node_input: rejects key not present in inputSchema when schema is defined", () => {+ mockNodes.push({+ id: "node-1",+ data: {+ hardcodedValues: {},+ inputSchema: { properties: { allowed_key: {} } },+ },+ });+ const { result } = renderHook(() => useBuilderChatPanel());++ act(() => {+ result.current.handleApplyAction({+ type: "update_node_input",+ nodeId: "node-1",+ key: "forbidden_key",+ value: "test",+ });+ });++ expect(mockUpdateNodeData).not.toHaveBeenCalled();+ });++ it("update_node_input: allows key present in inputSchema", () => {+ mockNodes.push({+ id: "node-1",+ data: {+ hardcodedValues: {},+ inputSchema: { properties: { query: {} } },+ },+ });+ const { result } = renderHook(() => useBuilderChatPanel());++ act(() => {+ result.current.handleApplyAction({+ type: "update_node_input",+ nodeId: "node-1",+ key: "query",+ value: "AI news",+ });+ });++ expect(mockUpdateNodeData).toHaveBeenCalledWith("node-1", {+ hardcodedValues: { query: "AI news" },+ });+ });++ it("connect_nodes: rejects sourceHandle not in outputSchema when schema is defined", () => {+ mockNodes.push(+ {+ id: "src",+ data: { outputSchema: { properties: { result: {} } } },+ },+ {+ id: "tgt",+ data: { inputSchema: { properties: { input: {} } } },+ },+ );+ const { result } = renderHook(() => useBuilderChatPanel());++ act(() => {+ result.current.handleApplyAction({+ type: "connect_nodes",+ source: "src",+ target: "tgt",+ sourceHandle: "nonexistent_output",+ targetHandle: "input",+ });+ });++ expect(mockAddEdge).not.toHaveBeenCalled();+ });++ it("connect_nodes: rejects targetHandle not in inputSchema when schema is defined", () => {+ mockNodes.push(+ {+ id: "src",+ data: { outputSchema: { properties: { result: {} } } },+ },+ {+ id: "tgt",+ data: { inputSchema: { properties: { input: {} } } },+ },+ );+ const { result } = renderHook(() => useBuilderChatPanel());++ act(() => {+ result.current.handleApplyAction({+ type: "connect_nodes",+ source: "src",+ target: "tgt",+ sourceHandle: "result",+ targetHandle: "nonexistent_input",+ });+ });++ expect(mockAddEdge).not.toHaveBeenCalled();+ });++ it("connect_nodes: calls addEdge when both handles are valid according to schemas", () => {+ mockNodes.push(+ {+ id: "src",+ data: { outputSchema: { properties: { result: {} } } },+ },+ {+ id: "tgt",+ data: { inputSchema: { properties: { input: {} } } },+ },+ );+ const { result } = renderHook(() => useBuilderChatPanel());++ act(() => {+ result.current.handleApplyAction({+ type: "connect_nodes",+ source: "src",+ target: "tgt",+ sourceHandle: "result",+ targetHandle: "input",+ });+ });++ expect(mockAddEdge).toHaveBeenCalledWith({+ id: "src:result->tgt:input",+ source: "src",+ target: "tgt",+ sourceHandle: "result",+ targetHandle: "input",+ type: "custom",+ });+ });});describe("useBuilderChatPanel – initial state", () => {diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.tsindex 73d25e258e..1b16662d3a 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts@@ -188,6 +188,10 @@ export function useBuilderChatPanel({if (action.type === "update_node_input") {const node = nodes.find((n) => n.id === action.nodeId);if (!node) return;+ // Reject keys not present in the node's input schema to prevent writing+ // arbitrary fields that the block does not support.+ const schemaProps = node.data.inputSchema?.properties;+ if (schemaProps && !(action.key in schemaProps)) return;updateNodeData(action.nodeId, {hardcodedValues: {...node.data.hardcodedValues,@@ -195,10 +199,14 @@ export function useBuilderChatPanel({},});} else if (action.type === "connect_nodes") {- // Validate both nodes exist before adding the edge to prevent dangling edges- const sourceExists = nodes.some((n) => n.id === action.source);- const targetExists = nodes.some((n) => n.id === action.target);- if (!sourceExists || !targetExists) return;+ const sourceNode = nodes.find((n) => n.id === action.source);+ const targetNode = nodes.find((n) => n.id === action.target);+ if (!sourceNode || !targetNode) return;+ // Validate that the referenced handles exist on the respective nodes.+ const srcProps = sourceNode.data.outputSchema?.properties;+ const tgtProps = targetNode.data.inputSchema?.properties;+ if (srcProps && !(action.sourceHandle in srcProps)) return;+ if (tgtProps && !(action.targetHandle in tgtProps)) return;addEdge({id: `${action.source}:${action.sourceHandle}->${action.target}:${action.targetHandle}`,source: action.source,