From 5f8142d4391377c4592fd7626a16f6f83d47bef4 Mon Sep 17 00:00:00 2001 From: NA-Wen Date: Fri, 16 Oct 2026 14:24:56 +0800 Subject: [PATCH] fix: confine workflow writes and gate unsafe code execution --- .env.example | 7 +- docs/user_guide/en/nodes/python.md | 10 + docs/user_guide/zh/nodes/python.md | 6 + functions/edge_processor/transformers.py | 84 ++-- functions/function_calling/code_executor.py | 4 + functions/function_calling/uv_related.py | 3 + functions/function_calling/video.py | 2 + run.py | 4 +- runtime/edge/conditions/base.py | 3 + runtime/edge/processors/function_processor.py | 3 + runtime/node/agent/tool/tool_manager.py | 2 + runtime/node/executor/python_executor.py | 2 + runtime/sdk.py | 7 +- server/routes/execute_sync.py | 19 +- server/routes/uploads.py | 6 +- server/services/attachment_service.py | 7 +- server/services/websocket_manager.py | 9 +- tests/test_code_execution_security.py | 440 ++++++++++++++++++ tests/test_session_workspace_security.py | 271 +++++++++++ tests/test_websocket_send_message_sync.py | 5 +- utils/code_execution.py | 20 + utils/workspace_paths.py | 34 ++ workflow/graph.py | 4 +- workflow/graph_context.py | 7 +- workflow/runtime/runtime_builder.py | 5 +- 25 files changed, 912 insertions(+), 52 deletions(-) create mode 100644 tests/test_code_execution_security.py create mode 100644 tests/test_session_workspace_security.py create mode 100644 utils/code_execution.py create mode 100644 utils/workspace_paths.py diff --git a/.env.example b/.env.example index fb5dc7da..623f25ed 100644 --- a/.env.example +++ b/.env.example @@ -7,6 +7,11 @@ BASE_URL=https://api.openai.com/v1 API_KEY=sk-your-openai-api-key-here +# Disabled by default. Enable only for trusted workflows; this is not a sandbox. +# Covers Python nodes, code_save_and_run, execute_code, uv tools, Manim, and local MCP. +# Set in the server environment; workflow vars and tool env overrides cannot enable it. +MAC_ALLOW_UNSAFE_CODE_EXECUTION=0 + # Example BASE_URL values: # - OpenAI: https://api.openai.com/v1 # - Gemini: https://generativelanguage.googleapis.com @@ -21,4 +26,4 @@ API_KEY=sk-your-openai-api-key-here # Get from: https://serper.dev # JINA_API_KEY=your-jina-api-key-here -# Get from: https://jina.ai \ No newline at end of file +# Get from: https://jina.ai diff --git a/docs/user_guide/en/nodes/python.md b/docs/user_guide/en/nodes/python.md index 85aa1115..22e486d9 100755 --- a/docs/user_guide/en/nodes/python.md +++ b/docs/user_guide/en/nodes/python.md @@ -2,6 +2,16 @@ The Python node is used to execute Python scripts or inline code within workflows, implementing custom data processing, API calls, file operations, and other logic. Scripts are executed in the shared `code_workspace/` directory and can access workflow context data. +Code execution is disabled by default. For trusted workflows, set +`MAC_ALLOW_UNSAFE_CODE_EXECUTION=1` in the server process environment (or the +deployment `.env` file) and restart the service. Workflow `vars`, node `env`, and +tool arguments cannot enable this permission. The same setting controls +`code_save_and_run`, `execute_code`, uv tools (including package installation), +Manim scripts, and local MCP server processes; remote MCP is unaffected. +Enabling execution grants the backend user's permissions and is not a sandbox. +The path restrictions on `code_save_and_run` remain active when execution is enabled: +filenames must stay within the workspace and must not modify `attachments/`. + ## Configuration | Field | Type | Required | Default | Description | diff --git a/docs/user_guide/zh/nodes/python.md b/docs/user_guide/zh/nodes/python.md index e01b34a4..200ea287 100755 --- a/docs/user_guide/zh/nodes/python.md +++ b/docs/user_guide/zh/nodes/python.md @@ -2,6 +2,12 @@ Python 节点用于在工作流中执行 Python 脚本或内联代码,实现自定义数据处理、API 调用、文件操作等逻辑。脚本在共享的 `code_workspace/` 目录中执行,可访问工作流上下文数据。 +代码执行默认关闭。仅在运行可信工作流时,在服务端进程环境(或部署的 `.env` 文件)中设置 +`MAC_ALLOW_UNSAFE_CODE_EXECUTION=1` 并重启服务。工作流 `vars`、节点 `env` 和工具参数无法开启此权限。 +该开关同时控制 `code_save_and_run`、`execute_code`、uv 工具(包括安装依赖)、Manim 脚本和本地 MCP +服务进程;远程 MCP 不受影响。开启后,代码以后端用户的权限运行,不具备沙箱隔离。 +即使开启执行权限,`code_save_and_run` 的路径限制仍然生效:文件必须位于工作区内,且不得修改 `attachments/`。 + ## 配置项 | 字段 | 类型 | 必填 | 默认值 | 说明 | diff --git a/functions/edge_processor/transformers.py b/functions/edge_processor/transformers.py index fcbd29ef..e34f7a21 100755 --- a/functions/edge_processor/transformers.py +++ b/functions/edge_processor/transformers.py @@ -5,8 +5,11 @@ import shutil import signal import subprocess import time -from pathlib import Path +from pathlib import Path, PureWindowsPath from functions.function_calling.file import FileToolContext +from utils.code_execution import require_unsafe_code_execution +from utils.exceptions import SecurityError, ValidationError +from utils.workspace_paths import validate_path_component def uppercase_payload(data: str, _context: Dict[str, Any]) -> str: """Return an uppercase copy of the payload text.""" @@ -23,6 +26,7 @@ def code_save_and_run(data: str, _context: Dict[str, Any]) -> str: ``` Repeated for multiple files. """ + require_unsafe_code_execution("code_save_and_run") # Parse matches first pattern = re.compile(r"(?P[^\n]+)\n```(?:python)?\n(?P.*?)\n```", re.DOTALL) matches = list(pattern.finditer(data)) @@ -32,6 +36,37 @@ def code_save_and_run(data: str, _context: Dict[str, Any]) -> str: ctx = FileToolContext(_context) workspace_root = ctx.workspace_root + + # Validate every target before deleting any existing workspace files. + files_to_save = [] + attachments_root = workspace_root / "attachments" + if attachments_root.is_symlink(): + raise SecurityError("The attachments directory must not be a symbolic link") + attachments_root = attachments_root.resolve() + for match in matches: + filename = match.group("filename").strip() + relative_path = Path(filename.replace("\\", "/")) + if not filename or relative_path.is_absolute() or PureWindowsPath(filename).drive: + raise SecurityError("Code filenames must be relative paths inside the workspace") + try: + for part in relative_path.parts: + validate_path_component(part, field="filename") + file_path = ctx.resolve_under_workspace(relative_path) + # resolve() preserves case aliases on some filesystems; compare directory identities too. + is_attachment_alias = attachments_root.exists() and any( + ancestor.exists() and ancestor.samefile(attachments_root) + for ancestor in (file_path, *file_path.parents) + ) + except (ValueError, OSError, RuntimeError, ValidationError) as exc: + raise SecurityError(f"Invalid code filename: {filename}") from exc + if ( + file_path == workspace_root + or relative_path.parts[0] == "attachments" + or file_path.is_relative_to(attachments_root) + or is_attachment_alias + ): + raise SecurityError("Code files must not replace the workspace or modify attachments") + files_to_save.append((filename, file_path, match.group("code"))) # 1. Clear workspace if workspace_root.exists(): @@ -39,18 +74,18 @@ def code_save_and_run(data: str, _context: Dict[str, Any]) -> str: if item.name == "attachments": continue - if item.is_dir(): + if item.is_symlink(): + item.unlink() + elif attachments_root.exists() and item.samefile(attachments_root): + continue + elif item.is_dir(): shutil.rmtree(item) else: item.unlink() saved_code_blocks = [] - for match in matches: - filename = match.group("filename").strip() - code = match.group("code") - + for filename, file_path, code in files_to_save: # Save to file - file_path = workspace_root / filename # Ensure parent dirs exist if filename contains path separators file_path.parent.mkdir(parents=True, exist_ok=True) file_path.write_text(code, encoding="utf-8") @@ -67,28 +102,19 @@ def code_save_and_run(data: str, _context: Dict[str, Any]) -> str: env = os.environ.copy() env["PYTHONUNBUFFERED"] = "1" - if os.name == 'nt': - command = f"cd {workspace_root} && dir && uv run main.py" - process = subprocess.Popen( - command, - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - creationflags=subprocess.CREATE_NEW_PROCESS_GROUP, - env=env - ) - else: - - command = "uv run main.py" - process = subprocess.Popen( - command, - shell=True, - cwd=str(workspace_root), - preexec_fn=os.setsid, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - env=env - ) + process_options = ( + {"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP} + if os.name == "nt" + else {"start_new_session": True} + ) + process = subprocess.Popen( + ["uv", "run", "main.py"], + cwd=str(workspace_root), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env=env, + **process_options, + ) try: stdout, stderr = process.communicate(timeout=3) diff --git a/functions/function_calling/code_executor.py b/functions/function_calling/code_executor.py index ac668f3d..385cbf05 100755 --- a/functions/function_calling/code_executor.py +++ b/functions/function_calling/code_executor.py @@ -1,3 +1,6 @@ +from utils.code_execution import require_unsafe_code_execution + + def execute_code(code: str, time_out: int = 60) -> str: """ Execute code and return std outputs and std error. @@ -9,6 +12,7 @@ def execute_code(code: str, time_out: int = 60) -> str: Returns: str: std output and std error """ + require_unsafe_code_execution("execute_code") import os import sys import subprocess diff --git a/functions/function_calling/uv_related.py b/functions/function_calling/uv_related.py index cd8138fb..962e26b5 100755 --- a/functions/function_calling/uv_related.py +++ b/functions/function_calling/uv_related.py @@ -6,6 +6,8 @@ import subprocess from pathlib import Path from typing import Any, Dict, List, Mapping, Sequence +from utils.code_execution import require_unsafe_code_execution + _SAFE_PACKAGE_RE = re.compile(r"^[A-Za-z0-9_.\-+=<>!\[\],@:/]+$") _DEFAULT_TIMEOUT = float(os.getenv("LIB_INSTALL_TIMEOUT", "120")) _OUTPUT_SNIPPET_LIMIT = 240 @@ -156,6 +158,7 @@ def _run_uv_command( env: Dict[str, str] | None = None, timeout: float | None = None, ) -> Dict[str, Any]: + require_unsafe_code_execution("uv command") timeout_value = _DEFAULT_TIMEOUT if timeout is None else timeout env_vars = None if env is None else {**os.environ, **env} try: diff --git a/functions/function_calling/video.py b/functions/function_calling/video.py index 5a43d681..62ff4fa8 100755 --- a/functions/function_calling/video.py +++ b/functions/function_calling/video.py @@ -4,6 +4,7 @@ from pathlib import Path import ast import subprocess import tempfile +from utils.code_execution import require_unsafe_code_execution def _get_class_names(py_file: str) -> list[str]: file_path = Path(py_file) @@ -16,6 +17,7 @@ def render_manim( quality: str = "h", preview: bool = True, ) -> Path: + require_unsafe_code_execution("render_manim") output_dir = Path.cwd() / "media" print("Clearing media folder:", output_dir) shutil.rmtree(output_dir, ignore_errors=True) diff --git a/run.py b/run.py index 09e18edc..1a1a9992 100755 --- a/run.py +++ b/run.py @@ -11,6 +11,7 @@ from entity.messages import Message from utils.attachments import AttachmentStore from utils.schema_exporter import build_schema_response, SchemaResolutionError from utils.task_input import TaskInputBuilder +from utils.workspace_paths import resolve_workspace_path from workflow.graph_context import GraphContext from workflow.graph import GraphExecutor @@ -28,8 +29,7 @@ def build_task_input_payload( if not attachment_paths: return prompt - code_workspace = graph_context.directory / "code_workspace" - attachments_dir = code_workspace / "attachments" + attachments_dir = resolve_workspace_path(graph_context.directory, "code_workspace", "attachments") attachments_dir.mkdir(parents=True, exist_ok=True) store = AttachmentStore(attachments_dir) builder = TaskInputBuilder(store) diff --git a/runtime/edge/conditions/base.py b/runtime/edge/conditions/base.py index 3e7bba82..04beef5b 100755 --- a/runtime/edge/conditions/base.py +++ b/runtime/edge/conditions/base.py @@ -8,6 +8,7 @@ from entity.messages import Message, MessageRole from runtime.node.executor import ExecutionContext from utils.log_manager import LogManager from utils.structured_logger import get_server_logger +from utils.exceptions import SecurityError from entity.configs.node.node import EdgeLink, Node from entity.configs.edge.edge_condition import EdgeConditionTypeConfig from utils.function_manager import FunctionManager @@ -64,6 +65,8 @@ class EdgeConditionManager(Generic[TConfig], ABC): log_manager=log_manager, context=self.execution_context, ) + except SecurityError: + raise except Exception as exc: # pragma: no cover error_msg = ( f"Edge payload processor failed for {from_node.id}->{edge_link.target.id}: {exc}" diff --git a/runtime/edge/processors/function_processor.py b/runtime/edge/processors/function_processor.py index 2469efff..5b98125c 100755 --- a/runtime/edge/processors/function_processor.py +++ b/runtime/edge/processors/function_processor.py @@ -5,6 +5,7 @@ from typing import Any, Callable, Mapping from entity.messages import Message from utils.log_manager import LogManager from utils.structured_logger import get_server_logger +from utils.exceptions import SecurityError from .base import EdgePayloadProcessor, ProcessorFactoryContext from entity.configs.edge.edge_processor import FunctionEdgeProcessorConfig from runtime.node.executor import ExecutionContext @@ -35,6 +36,8 @@ class FunctionEdgePayloadProcessor(EdgePayloadProcessor[FunctionEdgeProcessorCon return payload try: result = self._callable(payload.text_content(), context.global_state) + except SecurityError: + raise except Exception as exc: # pragma: no cover - defensive logging error_msg = f"Processor function '{self._name}' failed: {exc}" log_manager.error(error_msg) diff --git a/runtime/node/agent/tool/tool_manager.py b/runtime/node/agent/tool/tool_manager.py index bf7276a8..411b4d09 100755 --- a/runtime/node/agent/tool/tool_manager.py +++ b/runtime/node/agent/tool/tool_manager.py @@ -22,6 +22,7 @@ from entity.configs.node.tooling import FunctionToolConfig, McpLocalConfig, McpR from entity.messages import MessageBlock, MessageBlockType from entity.tool_spec import ToolSpec from utils.attachments import AttachmentStore +from utils.code_execution import require_unsafe_code_execution from utils.function_manager import FUNCTION_CALLING_DIR, FunctionManager logger = logging.getLogger(__name__) @@ -497,6 +498,7 @@ class ToolManager: return MessageBlockType.FILE def _get_stdio_client(self, config: McpLocalConfig, launch_key: str) -> "_StdioClientWrapper": + require_unsafe_code_execution("Local MCP server") client = self._mcp_stdio_clients.get(launch_key) if client is None: client = _StdioClientWrapper(config) diff --git a/runtime/node/executor/python_executor.py b/runtime/node/executor/python_executor.py index d59a5641..af6a9839 100755 --- a/runtime/node/executor/python_executor.py +++ b/runtime/node/executor/python_executor.py @@ -12,6 +12,7 @@ from entity.configs import Node from entity.configs.node.python_runner import PythonRunnerConfig from entity.messages import Message, MessageRole from runtime.node.executor.base import NodeExecutor +from utils.code_execution import require_unsafe_code_execution _CODE_BLOCK_RE = re.compile(r"```(?P[a-zA-Z0-9_+-]*)?\s*\n(?P.*?)```", re.DOTALL) @@ -36,6 +37,7 @@ class PythonNodeExecutor(NodeExecutor): if node.node_type != "python": raise ValueError(f"Node {node.id} is not a python node") + require_unsafe_code_execution("Python node") workspace = self._ensure_workspace_root() last_message = inputs[-1] if inputs else None code_payload = self._extract_code(last_message) diff --git a/runtime/sdk.py b/runtime/sdk.py index face19cf..9c1ed485 100644 --- a/runtime/sdk.py +++ b/runtime/sdk.py @@ -14,6 +14,7 @@ from utils.attachments import AttachmentStore from utils.exceptions import ValidationError from server.settings import YAML_DIR from utils.task_input import TaskInputBuilder +from utils.workspace_paths import resolve_workspace_path, validate_path_component from workflow.graph import GraphExecutor from workflow.graph_context import GraphContext @@ -39,7 +40,7 @@ class WorkflowRunResult: def _normalize_session_name(yaml_path: Path, session_name: Optional[str]) -> str: if session_name and session_name.strip(): - return session_name.strip() + return validate_path_component(session_name.strip(), field="session_name") timestamp = datetime.now().strftime("%Y%m%d%H%M%S") return f"sdk_{yaml_path.stem}_{timestamp}" @@ -52,7 +53,7 @@ def _build_task_input( if not attachments: return prompt - attachments_dir = graph_context.directory / "code_workspace" / "attachments" + attachments_dir = resolve_workspace_path(graph_context.directory, "code_workspace", "attachments") attachments_dir.mkdir(parents=True, exist_ok=True) store = AttachmentStore(attachments_dir) builder = TaskInputBuilder(store) @@ -95,8 +96,8 @@ def run_workflow( details={"task_prompt_provided": bool(task_prompt)}, ) - design = load_config(yaml_path, fn_module=fn_module, vars_override=variables) normalized_session = _normalize_session_name(yaml_path, session_name) + design = load_config(yaml_path, fn_module=fn_module, vars_override=variables) graph_config = GraphConfig.from_definition( design.graph, diff --git a/server/routes/execute_sync.py b/server/routes/execute_sync.py index 8426a1a8..04da636f 100644 --- a/server/routes/execute_sync.py +++ b/server/routes/execute_sync.py @@ -20,10 +20,11 @@ from runtime.sdk import OUTPUT_ROOT, run_workflow from server.models import WorkflowRunRequest from server.settings import YAML_DIR from utils.attachments import AttachmentStore -from utils.exceptions import ValidationError, WorkflowExecutionError +from utils.exceptions import ValidationError, WorkflowExecutionError, SecurityError from utils.logger import WorkflowLogger from utils.structured_logger import get_server_logger, LogType from utils.task_input import TaskInputBuilder +from utils.workspace_paths import resolve_workspace_path, validate_path_component from workflow.graph import GraphExecutor from workflow.graph_context import GraphContext @@ -34,7 +35,7 @@ _SSE_CONTENT_TYPE = "text/event-stream" def _normalize_session_name(yaml_path: Path, session_name: Optional[str]) -> str: if session_name and session_name.strip(): - return session_name.strip() + return validate_path_component(session_name.strip(), field="session_name") timestamp = datetime.now().strftime("%Y%m%d%H%M%S") return f"sdk_{yaml_path.stem}_{timestamp}" @@ -58,7 +59,7 @@ def _build_task_input( if not attachments: return prompt - attachments_dir = graph_context.directory / "code_workspace" / "attachments" + attachments_dir = resolve_workspace_path(graph_context.directory, "code_workspace", "attachments") attachments_dir.mkdir(parents=True, exist_ok=True) store = AttachmentStore(attachments_dir) builder = TaskInputBuilder(store) @@ -89,8 +90,8 @@ def _run_workflow_with_logger( details={"task_prompt_provided": bool(task_prompt)}, ) - design = load_config(yaml_path, vars_override=variables) normalized_session = _normalize_session_name(yaml_path, session_name) + design = load_config(yaml_path, vars_override=variables) graph_config = GraphConfig.from_definition( design.graph, @@ -152,9 +153,13 @@ def _sse_event(event_type: str, data: Any) -> str: @router.post("/api/workflow/run") async def run_workflow_sync(request: WorkflowRunRequest, http_request: Request): try: + if request.session_name and request.session_name.strip(): + validate_path_component(request.session_name.strip(), field="session_name") resolved_log_level: Optional[LogLevel] = None if request.log_level: resolved_log_level = LogLevel(request.log_level) + except ValidationError as exc: + raise HTTPException(status_code=400, detail=str(exc)) except ValueError: raise HTTPException( status_code=400, @@ -177,6 +182,8 @@ async def run_workflow_sync(request: WorkflowRunRequest, http_request: Request): raise HTTPException(status_code=404, detail=str(exc)) except ValidationError as exc: raise HTTPException(status_code=400, detail=str(exc)) + except SecurityError as exc: + raise HTTPException(status_code=403, detail=str(exc)) except Exception as exc: logger = get_server_logger() logger.log_exception(exc, "Failed to run workflow via sync API") @@ -230,7 +237,7 @@ async def run_workflow_sync(request: WorkflowRunRequest, http_request: Request): "output_dir": str(meta["output_dir"].resolve()), }, ) - except (FileNotFoundError, ValidationError) as exc: + except (FileNotFoundError, ValidationError, SecurityError) as exc: enqueue("error", {"message": str(exc)}) except Exception as exc: logger = get_server_logger() @@ -250,4 +257,4 @@ async def run_workflow_sync(request: WorkflowRunRequest, http_request: Request): if done_event.is_set(): break - return StreamingResponse(stream(), media_type=_SSE_CONTENT_TYPE) \ No newline at end of file + return StreamingResponse(stream(), media_type=_SSE_CONTENT_TYPE) diff --git a/server/routes/uploads.py b/server/routes/uploads.py index 18e63f16..4544251e 100755 --- a/server/routes/uploads.py +++ b/server/routes/uploads.py @@ -14,8 +14,8 @@ async def upload_attachment(session_id: str, file: UploadFile = File(...)): raise HTTPException(status_code=400, detail=str(exc)) try: record = await manager.attachment_service.save_upload_file(session_id, file) - except ValidationError: - raise HTTPException(status_code=400, detail="Session not connected") + except ValidationError as exc: + raise HTTPException(status_code=400, detail=str(exc)) except Exception as exc: logger = get_server_logger() logger.error( @@ -39,7 +39,7 @@ async def upload_attachment(session_id: str, file: UploadFile = File(...)): async def list_attachments(session_id: str): try: manager = ensure_known_session(session_id, require_connection=False) + manifest = manager.attachment_service.list_attachment_manifests(session_id) except ValidationError as exc: raise HTTPException(status_code=400, detail=str(exc)) - manifest = manager.attachment_service.list_attachment_manifests(session_id) return {"attachments": manifest} diff --git a/server/services/attachment_service.py b/server/services/attachment_service.py index 0d8d3ef1..30b3d3a9 100755 --- a/server/services/attachment_service.py +++ b/server/services/attachment_service.py @@ -12,6 +12,7 @@ from fastapi import UploadFile from entity.messages import MessageBlock, MessageBlockType from utils.attachments import AttachmentStore, AttachmentRecord +from utils.workspace_paths import resolve_workspace_path, validate_path_component class AttachmentService: @@ -60,6 +61,7 @@ class AttachmentService: return candidate async def save_upload_file(self, session_id: str, upload: UploadFile) -> AttachmentRecord: + self._session_attachments_path(session_id, create=False) filename = self._safe_upload_filename(upload.filename) temp_dir = Path(tempfile.mkdtemp(prefix="mac_upload_")) temp_path = temp_dir / filename @@ -123,8 +125,11 @@ class AttachmentService: return store.export_manifest() def _session_attachments_path(self, session_id: str, *, create: bool = True) -> Optional[Path]: + validate_path_component(session_id, field="session_id") session_dir_name = session_id if session_id.startswith("session_") else f"session_{session_id}" - path = self.attachments_root / session_dir_name / "code_workspace" / "attachments" + path = resolve_workspace_path( + self.attachments_root, session_dir_name, "code_workspace", "attachments" + ) if create: path.mkdir(parents=True, exist_ok=True) return path diff --git a/server/services/websocket_manager.py b/server/services/websocket_manager.py index f41ab475..aadf0770 100755 --- a/server/services/websocket_manager.py +++ b/server/services/websocket_manager.py @@ -8,13 +8,15 @@ import traceback import uuid from typing import Any, Dict, Optional -from fastapi import WebSocket +from fastapi import WebSocket, WebSocketException from server.services.message_handler import MessageHandler from server.services.attachment_service import AttachmentService from server.services.session_execution import SessionExecutionController from server.services.session_store import WorkflowSessionStore, SessionStatus from server.services.workflow_run_service import WorkflowRunService +from utils.exceptions import ValidationError +from utils.workspace_paths import validate_path_component def _json_default(value): @@ -68,6 +70,11 @@ class WebSocketManager: ) async def connect(self, websocket: WebSocket, session_id: Optional[str] = None) -> str: + if session_id: + try: + validate_path_component(session_id, field="session_id") + except ValidationError as exc: + raise WebSocketException(code=1008, reason=str(exc)) from exc await websocket.accept() # Capture the event loop that owns the WebSocket connections so that # worker threads can safely schedule sends via run_coroutine_threadsafe. diff --git a/tests/test_code_execution_security.py b/tests/test_code_execution_security.py new file mode 100644 index 00000000..feec809b --- /dev/null +++ b/tests/test_code_execution_security.py @@ -0,0 +1,440 @@ +"""Guard untrusted code entry points and processor file writes.""" + +import subprocess +import sys +from pathlib import Path +from unittest.mock import Mock + +import pytest +import yaml +from fastapi import FastAPI +from fastapi.testclient import TestClient + +from runtime import sdk +from entity.configs import Node +from entity.configs.node.tooling import McpLocalConfig +from entity.messages import Message, MessageRole +from functions.edge_processor import transformers +from functions.function_calling.code_executor import execute_code +from functions.function_calling import uv_related, video +from runtime.node.agent.tool import tool_manager +from runtime.node.executor.base import ExecutionContext +from runtime.node.executor.python_executor import PythonNodeExecutor +from server.routes import execute_sync, workflows +from utils.attachments import AttachmentStore +from utils.code_execution import UNSAFE_CODE_EXECUTION_ENV, require_unsafe_code_execution +from utils.error_handler import add_exception_handlers +from utils.exceptions import SecurityError + + +@pytest.fixture(autouse=True) +def disable_execution(monkeypatch): + monkeypatch.delenv(UNSAFE_CODE_EXECUTION_ENV, raising=False) + + +@pytest.fixture +def workspace_context(tmp_path): + workspace = tmp_path / "session" / "code_workspace" + store = AttachmentStore(workspace / "attachments") + (workspace / "keep.txt").write_text("original") + (store.root / "input.txt").write_text("attachment") + return { + "python_workspace_root": workspace, + "graph_directory": workspace.parent, + "attachment_store": store, + } + + +@pytest.fixture +def fake_launch(monkeypatch): + process = Mock(returncode=0) + process.communicate.return_value = (b"executed\n", b"") + launch = Mock(return_value=process) + monkeypatch.setattr(transformers.subprocess, "Popen", launch) + return launch + + +def _code_block(filename, code="print('ok')"): + return f"{filename}\n```python\n{code}\n```" + + +@pytest.mark.parametrize("value", [None, "", "0", "false", "no", "off", "typo"]) +def test_execution_requires_explicit_opt_in(monkeypatch, value): + if value is not None: + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, value) + with pytest.raises(SecurityError, match=UNSAFE_CODE_EXECUTION_ENV): + require_unsafe_code_execution("test operation") + + +@pytest.mark.parametrize("value", ["1", "true", "YES", " on "]) +def test_explicit_opt_in_is_supported(monkeypatch, value): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, value) + require_unsafe_code_execution("test operation") + + +def test_disabled_processor_does_not_clear_or_write_files(workspace_context, fake_launch): + workspace_context[UNSAFE_CODE_EXECUTION_ENV] = "1" + workspace_context["vars"] = {UNSAFE_CODE_EXECUTION_ENV: "1"} + with pytest.raises(SecurityError, match=UNSAFE_CODE_EXECUTION_ENV): + transformers.code_save_and_run(_code_block("main.py"), workspace_context) + workspace = workspace_context["python_workspace_root"] + assert (workspace / "keep.txt").read_text() == "original" + assert not (workspace / "main.py").exists() + fake_launch.assert_not_called() + + +@pytest.mark.parametrize("filename", [ + "../../escaped.py", "src/../../escaped.py", "src/../main.py", + r"..\..\escaped.py", r"C:\escaped.py", "C:escaped.py", + r"\\server\share\escaped.py", "attachments/changed.py", + r"attachments\changed.py", "attachments", ".", "..", " ", "bad\x00name.py", +]) +def test_processor_validates_all_files_before_clearing( + workspace_context, fake_launch, monkeypatch, filename +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + payload = _code_block("main.py") + "\n" + _code_block(filename) + with pytest.raises(SecurityError): + transformers.code_save_and_run(payload, workspace_context) + workspace = workspace_context["python_workspace_root"] + assert (workspace / "keep.txt").read_text() == "original" + assert (workspace / "attachments/input.txt").read_text() == "attachment" + assert not (workspace / "main.py").exists() + fake_launch.assert_not_called() + + +def test_processor_rejects_absolute_path(workspace_context, tmp_path, fake_launch, monkeypatch): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + target = tmp_path / "outside.py" + with pytest.raises(SecurityError): + transformers.code_save_and_run(_code_block(str(target)), workspace_context) + assert not target.exists() + assert (workspace_context["python_workspace_root"] / "keep.txt").exists() + fake_launch.assert_not_called() + + +@pytest.mark.parametrize("target_kind", ["outside", "attachments"]) +def test_processor_rejects_symlink_targets( + workspace_context, tmp_path, fake_launch, monkeypatch, target_kind +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + workspace = workspace_context["python_workspace_root"] + target = tmp_path / "outside" if target_kind == "outside" else workspace / "attachments" + target.mkdir(exist_ok=True) + (workspace / "alias").symlink_to(target, target_is_directory=True) + with pytest.raises(SecurityError): + transformers.code_save_and_run(_code_block("alias/changed.py"), workspace_context) + assert not (target / "changed.py").exists() + assert (workspace / "keep.txt").exists() + assert (workspace / "alias").is_symlink() + fake_launch.assert_not_called() + + +@pytest.mark.parametrize("filename", [ + "Attachments/input.txt", "ATTACHMENTS/nested/new.py", "aTtAcHmEnTs", +]) +def test_processor_rejects_attachment_case_aliases( + workspace_context, fake_launch, monkeypatch, filename +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + workspace = workspace_context["python_workspace_root"] + attachments = workspace / "attachments" + alias = workspace / "Attachments" + if not alias.exists() or not alias.samefile(attachments): + pytest.skip("Requires a case-insensitive filesystem") + + payload = _code_block("main.py") + "\n" + _code_block(filename, "REVIEW_MARKER") + with pytest.raises(SecurityError, match="attachments"): + transformers.code_save_and_run(payload, workspace_context) + + assert (attachments / "input.txt").read_text() == "attachment" + assert not (attachments / "nested").exists() + assert (workspace / "keep.txt").read_text() == "original" + assert not (workspace / "main.py").exists() + fake_launch.assert_not_called() + + +def test_processor_cleanup_preserves_attachment_directory_with_different_case( + workspace_context, fake_launch, monkeypatch +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + workspace = workspace_context["python_workspace_root"] + attachments = workspace / "attachments" + alias = workspace / "Attachments" + if not alias.exists() or not alias.samefile(attachments): + pytest.skip("Requires a case-insensitive filesystem") + + temporary = workspace / "inputs_to_rename" + attachments.rename(temporary) + temporary.rename(alias) + assert "Attachments" in {item.name for item in workspace.iterdir()} + + transformers.code_save_and_run(_code_block("main.py"), workspace_context) + + assert (attachments / "input.txt").read_text() == "attachment" + assert (workspace / "main.py").exists() + assert not (workspace / "keep.txt").exists() + fake_launch.assert_called_once() + + +def test_processor_allows_distinct_case_sensitive_directory( + workspace_context, fake_launch, monkeypatch +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + workspace = workspace_context["python_workspace_root"] + alias = workspace / "Attachments" + if alias.exists(): + pytest.skip("Requires a case-sensitive filesystem") + + transformers.code_save_and_run(_code_block("Attachments/new.py", "VALUE = 1"), workspace_context) + + assert (alias / "new.py").read_text() == "VALUE = 1" + assert (workspace / "attachments/input.txt").read_text() == "attachment" + fake_launch.assert_called_once() + + +def test_enabled_processor_preserves_nested_files_and_attachments( + workspace_context, tmp_path, fake_launch, monkeypatch +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + workspace = workspace_context["python_workspace_root"] + outside = tmp_path / "outside" + outside.mkdir() + (outside / "keep.txt").write_text("external") + (workspace / "old_link").symlink_to(outside, target_is_directory=True) + payload = _code_block("main.py") + "\n" + _code_block("src/helpers.py", "VALUE = 1") + result = transformers.code_save_and_run(payload, workspace_context) + assert (workspace / "main.py").read_text() == "print('ok')" + assert (workspace / "src/helpers.py").read_text() == "VALUE = 1" + assert not (workspace / "keep.txt").exists() + assert not (workspace / "old_link").is_symlink() + assert (outside / "keep.txt").read_text() == "external" + assert (workspace / "attachments/input.txt").read_text() == "attachment" + assert "executed" in result + args, kwargs = fake_launch.call_args + assert args == (["uv", "run", "main.py"],) + assert kwargs["cwd"] == str(workspace) + assert not kwargs.get("shell", False) + + +@pytest.mark.parametrize("target_kind", ["inside", "outside"]) +def test_processor_rejects_linked_attachments_before_cleanup( + workspace_context, tmp_path, fake_launch, monkeypatch, target_kind +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + workspace = workspace_context["python_workspace_root"] + target = (workspace if target_kind == "inside" else tmp_path) / "inputs" + attachments = workspace / "attachments" + attachments.rename(target) + attachments.symlink_to(target, target_is_directory=True) + with pytest.raises(SecurityError, match="symbolic link"): + transformers.code_save_and_run(_code_block("main.py"), workspace_context) + assert (target / "input.txt").read_text() == "attachment" + assert (workspace / "keep.txt").read_text() == "original" + assert not (workspace / "main.py").exists() + fake_launch.assert_not_called() + + +def _python_executor(workspace, env=None): + context = ExecutionContext( + tool_manager=Mock(), function_manager=Mock(), log_manager=Mock(), + global_state={"python_workspace_root": workspace, "vars": {UNSAFE_CODE_EXECUTION_ENV: "1"}}, + ) + node = Node.from_dict( + {"id": "Runner", "type": "python", "config": {"env": env or {}}}, path="node" + ) + return PythonNodeExecutor(context), node + + +def test_python_node_env_cannot_enable_execution(tmp_path, monkeypatch): + workspace = tmp_path / "workspace" + executor, node = _python_executor(workspace, {UNSAFE_CODE_EXECUTION_ENV: "1"}) + run = Mock(side_effect=AssertionError("Must not start a process")) + monkeypatch.setattr(subprocess, "run", run) + with pytest.raises(SecurityError, match=UNSAFE_CODE_EXECUTION_ENV): + executor.execute(node, [Message(role=MessageRole.USER, content="print('ok')")]) + assert not workspace.exists() + run.assert_not_called() + + +def test_enabled_python_node_executes_harmless_code(tmp_path, monkeypatch): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + executor, node = _python_executor(tmp_path / "workspace") + messages = executor.execute(node, [Message(role=MessageRole.USER, content="print(6 * 7)")]) + assert messages[0].text_content().strip() == "42" + + +def test_execute_code_is_blocked_before_saving_script(tmp_path, monkeypatch): + temp_code_dir = tmp_path / "scripts" + monkeypatch.setenv("TEMP_CODE_DIR", str(temp_code_dir)) + with pytest.raises(SecurityError, match=UNSAFE_CODE_EXECUTION_ENV): + execute_code("print('ok')") + assert not temp_code_dir.exists() + + +def test_execute_code_works_when_enabled(tmp_path, monkeypatch): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + monkeypatch.setenv("TEMP_CODE_DIR", str(tmp_path)) + assert execute_code("print(6 * 7)").strip() == "42" + assert list(tmp_path.iterdir()) == [] + + +@pytest.mark.parametrize("operation", ["run", "install", "init"]) +def test_uv_entry_points_cannot_override_server_policy(workspace_context, monkeypatch, operation): + run = Mock(side_effect=AssertionError("Must not start a process")) + monkeypatch.setattr(uv_related.subprocess, "run", run) + with pytest.raises(SecurityError, match=UNSAFE_CODE_EXECUTION_ENV): + if operation == "run": + uv_related.uv_run( + module="example", env={UNSAFE_CODE_EXECUTION_ENV: "1"}, _context=workspace_context + ) + elif operation == "install": + uv_related.install_python_packages(["example"], _context=workspace_context) + else: + uv_related.init_python_env(_context=workspace_context) + run.assert_not_called() + + +def test_uv_run_is_available_when_enabled(workspace_context, monkeypatch): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + run = Mock(return_value=subprocess.CompletedProcess([], 0, stdout="ok", stderr="")) + monkeypatch.setattr(uv_related.subprocess, "run", run) + result = uv_related.uv_run(module="example", _context=workspace_context) + assert result["returncode"] == 0 + assert run.call_args.args[0] == ["uv", "run", "python", "-m", "example"] + + +def test_manim_is_blocked_before_clearing_media(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + media = tmp_path / "media" + media.mkdir() + (media / "keep.txt").write_text("original") + with pytest.raises(SecurityError, match=UNSAFE_CODE_EXECUTION_ENV): + video.render_manim("unused.py") + assert (media / "keep.txt").read_text() == "original" + + +def test_local_mcp_env_cannot_enable_execution(monkeypatch): + create_client = Mock(side_effect=AssertionError("Must not start MCP")) + monkeypatch.setattr(tool_manager, "_StdioClientWrapper", create_client) + manager = tool_manager.ToolManager() + config = McpLocalConfig.from_dict( + {"command": sys.executable, "env": {UNSAFE_CODE_EXECUTION_ENV: "1"}}, path="tooling" + ) + with pytest.raises(SecurityError, match=UNSAFE_CODE_EXECUTION_ENV): + manager._get_stdio_client(config, config.cache_key()) + create_client.assert_not_called() + + +def test_local_mcp_opt_in_and_cached_client_policy(monkeypatch): + manager = tool_manager.ToolManager() + config = McpLocalConfig.from_dict({"command": sys.executable}, path="tooling") + create_client = Mock() + monkeypatch.setattr(tool_manager, "_StdioClientWrapper", create_client) + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + assert manager._get_stdio_client(config, config.cache_key()) is create_client.return_value + monkeypatch.delenv(UNSAFE_CODE_EXECUTION_ENV) + with pytest.raises(SecurityError): + manager._get_stdio_client(config, config.cache_key()) + create_client.assert_called_once_with(config) + + +def _workflow(kind, code): + graph = { + "id": "execution_policy_test", "start": ["Source"], + "nodes": [ + {"id": "Source", "type": "literal", "config": {"content": code, "role": "assistant"}}, + {"id": "Runner", "type": "python" if kind == "python" else "passthrough", "config": {}}, + ], + "edges": [{"from": "Source", "to": "Runner", "trigger": True, "carry_data": True}], + } + if kind == "processor": + graph["edges"][0]["process"] = {"type": "function", "config": {"name": "code_save_and_run"}} + else: + graph["nodes"][1]["config"]["env"] = {UNSAFE_CODE_EXECUTION_ENV: "1"} + return {"version": "0.4.0", "vars": {UNSAFE_CODE_EXECUTION_ENV: "1"}, "graph": graph} + + +@pytest.fixture +def workflow_api(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + yaml_dir = tmp_path / "yaml_instance" + yaml_dir.mkdir() + for module in (sdk, execute_sync, workflows): + monkeypatch.setattr(module, "YAML_DIR", yaml_dir) + app = FastAPI() + add_exception_handlers(app) + app.include_router(workflows.router) + app.include_router(execute_sync.router) + with TestClient(app) as client: + yield client + + +@pytest.mark.parametrize("kind", ["python", "processor"]) +@pytest.mark.parametrize("accept", ["application/json", "text/event-stream"]) +def test_uploaded_workflow_cannot_enable_execution(workflow_api, tmp_path, kind, accept): + code = _code_block("main.py") if kind == "processor" else "print('ok')" + response = workflow_api.post( + "/api/workflows/upload/content", + json={"filename": "policy.yaml", "content": yaml.safe_dump(_workflow(kind, code))}, + ) + assert response.status_code == 200 + response = workflow_api.post( + "/api/workflow/run", headers={"Accept": accept}, + json={"yaml_file": "policy.yaml", "task_prompt": "test", "session_name": "session_policy"}, + ) + if accept == "application/json": + assert response.status_code == 403 + else: + assert response.status_code == 200 + assert "event: error" in response.text + assert "event: completed" not in response.text + assert UNSAFE_CODE_EXECUTION_ENV in response.text + assert not list((tmp_path / "WareHouse").rglob("*.py")) + + +@pytest.mark.parametrize("accept", ["application/json", "text/event-stream"]) +def test_uploaded_processor_cannot_plant_module_even_when_enabled( + workflow_api, tmp_path, monkeypatch, fake_launch, accept +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + code = _code_block("../../../functions/function_calling/planted.py", "VALUE = 1") + response = workflow_api.post( + "/api/workflows/upload/content", + json={"filename": "traversal.yaml", "content": yaml.safe_dump(_workflow("processor", code))}, + ) + assert response.status_code == 200 + response = workflow_api.post( + "/api/workflow/run", headers={"Accept": accept}, + json={"yaml_file": "traversal.yaml", "task_prompt": "test", "session_name": "session_policy"}, + ) + if accept == "application/json": + assert response.status_code == 403 + else: + assert "event: error" in response.text + assert "event: completed" not in response.text + assert "Invalid code filename" in response.text + assert not (tmp_path / "functions").exists() + fake_launch.assert_not_called() + + +@pytest.mark.parametrize("kind", ["python", "processor"]) +def test_uploaded_workflow_runs_after_server_opt_in( + workflow_api, monkeypatch, fake_launch, kind +): + monkeypatch.setenv(UNSAFE_CODE_EXECUTION_ENV, "1") + code = _code_block("main.py") if kind == "processor" else "print('ok')" + workflow_api.post( + "/api/workflows/upload/content", + json={"filename": "enabled.yaml", "content": yaml.safe_dump(_workflow(kind, code))}, + ) + # subprocess.run uses Popen as a context manager; keep this test deterministic. + if kind == "python": + monkeypatch.setattr(subprocess, "run", Mock(return_value=subprocess.CompletedProcess([], 0, b"ok\n", b""))) + response = workflow_api.post( + "/api/workflow/run", + json={"yaml_file": "enabled.yaml", "task_prompt": "test", "session_name": "session_enabled"}, + ) + assert response.status_code == 200 + assert response.json()["status"] == "completed" + assert "ok" in response.json()["final_message"] or "executed" in response.json()["final_message"] diff --git a/tests/test_session_workspace_security.py b/tests/test_session_workspace_security.py new file mode 100644 index 00000000..57b2b13b --- /dev/null +++ b/tests/test_session_workspace_security.py @@ -0,0 +1,271 @@ +"""Regression coverage for session traversal through uploads and workflow runs.""" + +import asyncio +import io +from pathlib import Path +from unittest.mock import Mock +from urllib.parse import urlencode + +import pytest +from fastapi import FastAPI, UploadFile +from fastapi.testclient import TestClient +from starlette.websockets import WebSocketDisconnect + +from runtime import sdk +from entity.graph_config import GraphConfig +from server import state +from server.routes import execute_sync, uploads, websocket +from server.services.attachment_service import AttachmentService +from server.services.websocket_manager import WebSocketManager +from utils.exceptions import ValidationError +from workflow.graph_context import GraphContext +from workflow.runtime.runtime_builder import RuntimeBuilder + + +TRAVERSAL = "session_../../../functions/function_calling/pocplant" +INVALID_NAMES = [ + TRAVERSAL, + "../outside", + "session_a/../../outside", + "/tmp/session_absolute", + r"session_..\..\outside", + r"C:\outside", + "C:outside", + r"\\server\share", + ".", + "..", + "session_\x00invalid", + "session_\ninvalid", +] +DEMO_YAML = Path(__file__).resolve().parents[1] / "yaml_instance/demo_loop_counter.yaml" + + +@pytest.fixture +def graph_config(tmp_path): + return GraphConfig.from_definition( + sdk.load_config(DEMO_YAML).graph, + name="session_safe", + output_root=tmp_path / "WareHouse", + ) + + +@pytest.fixture +def api(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + manager = WebSocketManager() + monkeypatch.setattr(manager, "_start_gc", lambda: None) + monkeypatch.setattr(state, "websocket_manager", manager) + app = FastAPI() + app.include_router(execute_sync.router) + app.include_router(uploads.router) + app.include_router(websocket.router) + with TestClient(app) as client: + yield client, manager + + +@pytest.mark.parametrize("name", INVALID_NAMES) +def test_graph_rejects_invalid_names_before_creating_directories(graph_config, name): + graph_config.name = name + with pytest.raises(ValidationError): + GraphContext(graph_config) + assert not graph_config.output_root.exists() + + +@pytest.mark.parametrize("name", ["session_ok-123", "demo.v1", "My project", "\u6d4b\u8bd5"]) +def test_graph_preserves_valid_names_and_timestamp_behavior(graph_config, name): + graph_config.name = name + graph = GraphContext(graph_config) + assert graph.directory.parent == graph_config.output_root.resolve() + assert graph.directory.is_dir() + if "session_" in name: + assert graph.directory.name == name + else: + assert graph.directory.name.startswith(name + "_") + assert graph.directory.name[len(name) + 1:].isdigit() + + +def test_fixed_output_directory_is_still_confined(graph_config): + graph_config.name = "task_001" + graph_config.metadata["fixed_output_dir"] = True + assert GraphContext(graph_config).directory == graph_config.output_root / "task_001" + graph_config.name = "../outside" + with pytest.raises(ValidationError): + GraphContext(graph_config) + + +def test_graph_rejects_existing_session_symlink(graph_config, tmp_path): + outside = tmp_path / "functions" + outside.mkdir() + graph_config.output_root.mkdir() + (graph_config.output_root / graph_config.name).symlink_to(outside, target_is_directory=True) + with pytest.raises(ValidationError, match="escapes"): + GraphContext(graph_config) + assert list(outside.iterdir()) == [] + + +@pytest.mark.parametrize("builder", [sdk._build_task_input, execute_sync._build_task_input]) +@pytest.mark.parametrize("linked_part", ["code_workspace", "code_workspace/attachments"]) +def test_attachment_copy_rejects_escaping_symlinks(graph_config, tmp_path, builder, linked_part): + graph = GraphContext(graph_config) + outside = tmp_path / "functions" + outside.mkdir() + link = graph.directory / linked_part + link.parent.mkdir(parents=True, exist_ok=True) + link.symlink_to(outside, target_is_directory=True) + source = tmp_path / "notes.txt" + source.write_text("sample") + with pytest.raises(ValidationError, match="escapes"): + builder(graph, "test", [source]) + assert list(outside.iterdir()) == [] + + +def test_runtime_rejects_workspace_symlink_without_attachments(graph_config, tmp_path): + graph = GraphContext(graph_config) + outside = tmp_path / "functions" + outside.mkdir() + (graph.directory / "code_workspace").symlink_to(outside, target_is_directory=True) + with pytest.raises(ValidationError, match="escapes"): + RuntimeBuilder(graph).build(logger=Mock()) + assert list(outside.iterdir()) == [] + + +@pytest.mark.parametrize("accept", ["application/json", "text/event-stream"]) +@pytest.mark.parametrize("name", INVALID_NAMES) +def test_http_rejects_bad_session_before_starting_worker(api, monkeypatch, accept, name): + client, _ = api + worker = Mock(side_effect=AssertionError("Workflow must not start")) + monkeypatch.setattr(execute_sync, "run_in_threadpool", worker) + monkeypatch.setattr(execute_sync.threading, "Thread", worker) + response = client.post( + "/api/workflow/run", + headers={"Accept": accept}, + json={"yaml_file": str(DEMO_YAML), "task_prompt": "test", "session_name": name}, + ) + assert response.status_code == 400 + assert "session_name" in response.json()["detail"] + worker.assert_not_called() + + +@pytest.mark.parametrize("streaming", [False, True]) +def test_direct_runner_rejects_traversal_before_loading_config(tmp_path, monkeypatch, streaming): + module = execute_sync if streaming else sdk + loader = Mock(side_effect=AssertionError("Config must not load")) + monkeypatch.setattr(module, "load_config", loader) + monkeypatch.setattr(module, "OUTPUT_ROOT", tmp_path / "WareHouse") + kwargs = dict( + yaml_file=DEMO_YAML, + task_prompt="test", + attachments=[], + session_name=TRAVERSAL, + ) + with pytest.raises(ValidationError): + if streaming: + execute_sync._run_workflow_with_logger( + **kwargs, variables=None, log_level=None, log_callback=lambda *_: None + ) + else: + sdk.run_workflow(**kwargs) + loader.assert_not_called() + assert not (tmp_path / "WareHouse").exists() + + +@pytest.mark.parametrize("name", INVALID_NAMES) +def test_websocket_rejects_bad_session_without_registering_connection(api, name): + client, manager = api + with pytest.raises(WebSocketDisconnect) as exc: + with client.websocket_connect("/ws?" + urlencode({"session_id": name})): + pytest.fail("Invalid session must not be accepted") + assert exc.value.code == 1008 + assert not manager.active_connections + + +@pytest.mark.parametrize("name", [TRAVERSAL, r"session_..\..\outside", ".."]) +def test_upload_service_rejects_bad_session_before_reading_file(tmp_path, name): + service = AttachmentService(root=tmp_path / "WareHouse") + upload = UploadFile(filename="sample.py", file=io.BytesIO(b"VALUE = 1\n")) + with pytest.raises(ValidationError): + asyncio.run(service.save_upload_file(name, upload)) + assert upload.file.tell() == 0 + assert list(service.attachments_root.iterdir()) == [] + + +@pytest.mark.parametrize("session_name", [None, " "]) +def test_workflow_still_generates_default_session_name(api, tmp_path, session_name): + client, _ = api + payload = {"yaml_file": str(DEMO_YAML), "task_prompt": "test"} + if session_name is not None: + payload["session_name"] = session_name + response = client.post("/api/workflow/run", json=payload) + assert response.status_code == 200 + output_dir = Path(response.json()["output_dir"]) + assert output_dir.parent == tmp_path / "WareHouse" + assert output_dir.name.startswith("sdk_demo_loop_counter_") + + +def test_generated_websocket_session_can_reconnect(api): + client, manager = api + with client.websocket_connect("/ws") as ws: + session_id = ws.receive_json()["data"]["session_id"] + assert session_id in manager.active_connections + session = manager.session_store.create_session( + session_id=session_id, yaml_file=str(DEMO_YAML), task_prompt="test" + ) + session.append_message({"type": "test", "data": "replayed"}) + with client.websocket_connect("/ws?" + urlencode({"session_id": session_id})) as ws: + assert ws.receive_json()["data"]["session_id"] == session_id + assert ws.receive_json() == {"type": "test", "data": "replayed"} + assert ws.receive_json()["type"] == "session_resumed" + + +@pytest.mark.parametrize("accept", ["application/json", "text/event-stream"]) +def test_upload_then_workflow_round_trip_and_traversal_block(api, tmp_path, accept): + client, _ = api + with client.websocket_connect("/ws?session_id=upload_test") as ws: + assert ws.receive_json()["data"]["session_id"] == "upload_test" + response = client.post( + "/api/uploads/upload_test", + files={"file": ("sample.py", b"VALUE = 1\n", "text/x-python")}, + ) + assert response.status_code == 200 + assert client.get("/api/uploads/upload_test").status_code == 200 + sources = list((tmp_path / "WareHouse/session_upload_test").rglob("sample.py")) + assert len(sources) == 1 + payload = { + "yaml_file": str(DEMO_YAML), + "task_prompt": "test", + "attachments": [str(sources[0])], + "session_name": TRAVERSAL, + } + response = client.post("/api/workflow/run", headers={"Accept": accept}, json=payload) + assert response.status_code == 400 + assert not (tmp_path / "functions").exists() + + payload["session_name"] = "session_safe_run" + response = client.post("/api/workflow/run", headers={"Accept": accept}, json=payload) + assert response.status_code == 200 + if accept == "text/event-stream": + assert "event: completed" in response.text + assert "event: error" not in response.text + else: + assert response.json()["status"] == "completed" + stored = list((tmp_path / "WareHouse/session_safe_run").rglob("sample.py")) + assert len(stored) == 1 + assert stored[0].read_bytes() == b"VALUE = 1\n" + + +@pytest.mark.parametrize("linked_part", ["", "code_workspace", "code_workspace/attachments"]) +def test_upload_endpoints_reject_escaping_symlinks(api, tmp_path, linked_part): + client, _ = api + outside = tmp_path / "functions" + outside.mkdir() + link = tmp_path / "WareHouse/session_upload_test" / linked_part + link.parent.mkdir(parents=True, exist_ok=True) + link.symlink_to(outside, target_is_directory=True) + with client.websocket_connect("/ws?session_id=upload_test") as ws: + ws.receive_json() + assert client.get("/api/uploads/upload_test").status_code == 400 + response = client.post( + "/api/uploads/upload_test", files={"file": ("sample.py", b"VALUE = 1\n")} + ) + assert response.status_code == 400 + assert list(outside.iterdir()) == [] diff --git a/tests/test_websocket_send_message_sync.py b/tests/test_websocket_send_message_sync.py index 0924a8be..eef81020 100644 --- a/tests/test_websocket_send_message_sync.py +++ b/tests/test_websocket_send_message_sync.py @@ -48,8 +48,11 @@ from server.services.websocket_manager import WebSocketManager # noqa: E402 def _make_manager() -> WebSocketManager: """Create a WebSocketManager with minimal mocks.""" + session_store = MagicMock() + session_store.has_session.return_value = False + session_store.get_session.return_value = None return WebSocketManager( - session_store=MagicMock(), + session_store=session_store, session_controller=MagicMock(), attachment_service=MagicMock(), workflow_run_service=MagicMock(), diff --git a/utils/code_execution.py b/utils/code_execution.py new file mode 100644 index 00000000..b03daef1 --- /dev/null +++ b/utils/code_execution.py @@ -0,0 +1,20 @@ +"""Server-controlled opt-in for running workflow-supplied code.""" + +import os + +from utils.exceptions import SecurityError + + +UNSAFE_CODE_EXECUTION_ENV = "MAC_ALLOW_UNSAFE_CODE_EXECUTION" + + +def require_unsafe_code_execution(operation: str) -> None: + # Never consult workflow variables, tool arguments, or child-process env overrides. + enabled = os.environ.get(UNSAFE_CODE_EXECUTION_ENV, "").strip().lower() + if enabled not in {"1", "true", "yes", "on"}: + raise SecurityError( + f"{operation} is disabled. Running workflow-supplied code requires " + f"{UNSAFE_CODE_EXECUTION_ENV}=1 in the server environment. " + "Enable this only for trusted workflows; execution is not sandboxed.", + details={"operation": operation}, + ) diff --git a/utils/workspace_paths.py b/utils/workspace_paths.py new file mode 100644 index 00000000..715abe7e --- /dev/null +++ b/utils/workspace_paths.py @@ -0,0 +1,34 @@ +"""Validate names and confine generated files to their workspace.""" + +import re +from pathlib import Path + +from utils.exceptions import ValidationError + + +def validate_path_component(name: str, *, field: str = "name") -> str: + """Accept a single directory name on both POSIX and Windows.""" + if ( + name.strip() in {"", ".", ".."} + or re.search(r"[/\\:\x00-\x1f\x7f]", name) + ): + raise ValidationError( + f"Invalid {field}: expected a single directory name without path separators, " + "drive prefixes, or control characters", + field=field, + ) + return name + + +def resolve_workspace_path(root: Path | str, *parts: str) -> Path: + """Resolve a child path, rejecting traversal and existing escaping symlinks.""" + for part in parts: + validate_path_component(part) + try: + resolved_root = Path(root).resolve() + candidate = resolved_root.joinpath(*parts).resolve() + except (OSError, RuntimeError) as exc: + raise ValidationError("Unable to resolve workspace path", field="workspace") from exc + if candidate == resolved_root or not candidate.is_relative_to(resolved_root): + raise ValidationError("Workspace path escapes its root directory", field="workspace") + return candidate diff --git a/workflow/graph.py b/workflow/graph.py index 35675ba9..2efef30d 100755 --- a/workflow/graph.py +++ b/workflow/graph.py @@ -12,7 +12,7 @@ from entity.messages import Message, MessageRole from runtime.node.executor.base import ExecutionContext from runtime.node.executor.factory import NodeExecutorFactory from utils.logger import WorkflowLogger -from utils.exceptions import ValidationError, WorkflowExecutionError, WorkflowCancelledError +from utils.exceptions import ValidationError, WorkflowExecutionError, WorkflowCancelledError, SecurityError from utils.structured_logger import get_server_logger from utils.human_prompt import ( CliPromptChannel, @@ -420,6 +420,8 @@ class GraphExecutor: from_node, self.log_manager, ) + except SecurityError: + raise except Exception as exc: # pragma: no cover - defensive logging error_msg = ( f"Edge manager failed for {from_node.id} -> {edge_link.target.id}: {exc}" diff --git a/workflow/graph_context.py b/workflow/graph_context.py index 56fcc3be..7051887e 100755 --- a/workflow/graph_context.py +++ b/workflow/graph_context.py @@ -10,6 +10,7 @@ import yaml from entity.configs import Node from entity.graph_config import GraphConfig +from utils.workspace_paths import resolve_workspace_path, validate_path_component class GraphContext: @@ -60,12 +61,14 @@ class GraphContext: self.cycle_execution_order: List[Dict[str, Any]] = [] # Output directory + validate_path_component(config.name, field="session_name") timestamp = datetime.now().strftime("%Y%m%d%H%M%S") fixed_output_dir = bool(config.metadata.get("fixed_output_dir")) if fixed_output_dir or "session_" in config.name: - self.directory = config.output_root / config.name + directory_name = config.name else: - self.directory = config.output_root / f"{config.name}_{timestamp}" + directory_name = f"{config.name}_{timestamp}" + self.directory = resolve_workspace_path(config.output_root, directory_name) self.directory.mkdir(parents=True, exist_ok=True) # Voting mode flag self.is_majority_voting: bool = config.is_majority_voting diff --git a/workflow/runtime/runtime_builder.py b/workflow/runtime/runtime_builder.py index b7e63401..daeaeda2 100755 --- a/workflow/runtime/runtime_builder.py +++ b/workflow/runtime/runtime_builder.py @@ -9,6 +9,7 @@ from utils.function_manager import EDGE_FUNCTION_DIR, EDGE_PROCESSOR_FUNCTION_DI from utils.log_manager import LogManager from utils.logger import WorkflowLogger from utils.token_tracker import TokenTracker +from utils.workspace_paths import resolve_workspace_path from workflow.graph_context import GraphContext from .runtime_context import RuntimeContext @@ -28,9 +29,9 @@ class RuntimeBuilder: log_manager = LogManager(logger) token_tracker = TokenTracker(workflow_id=self.graph.name) - code_workspace = (self.graph.directory / "code_workspace").resolve() + code_workspace = resolve_workspace_path(self.graph.directory, "code_workspace") + attachments_dir = resolve_workspace_path(self.graph.directory, "code_workspace", "attachments") code_workspace.mkdir(parents=True, exist_ok=True) - attachments_dir = code_workspace / "attachments" attachments_dir.mkdir(parents=True, exist_ok=True) attachment_store = AttachmentStore(attachments_dir)