mirror of
https://github.com/OpenBMB/ChatDev.git
synced 2026-11-18 07:50:38 +00:00
fix: validate attachment sources and isolate session storage
This commit is contained in:
parent
5f8142d439
commit
5df5af7926
@ -218,6 +218,22 @@ python -m tools.export_design_template \
|
||||
- **HTTP**: `POST /api/workflow/execute` with `session_name`, `graph_path` or `graph_content`, `task_prompt`, optional `attachments`, and `log_level` (defaults to `INFO`, supports `INFO` or `DEBUG`).
|
||||
- **CLI**: `python run.py --path yaml_instance/demo.yaml --name test_run`. Provide `TASK_PROMPT` via env var or respond to the CLI prompt.
|
||||
|
||||
For `POST /api/workflow/run` (JSON or SSE), upload attachments through
|
||||
`/api/uploads/{session_id}` first, then pass that exact ID as `attachment_session_id`
|
||||
and the returned attachment IDs in `attachments`. Existing paths are accepted only
|
||||
when they match registered uploads in that session. Arbitrary server file paths are
|
||||
rejected. `session_name` still names the output run; it is independent of the upload
|
||||
session. Local SDK and CLI file inputs are unchanged.
|
||||
|
||||
Upload, WebSocket execution, and download routes map each raw session ID to
|
||||
`WareHouse/session_<session_id>`. For example, `foo` and `session_foo` use
|
||||
`session_foo` and `session_session_foo`, respectively. Previously shared uploads
|
||||
for prefixed IDs must be uploaded again under the correct session.
|
||||
|
||||
Agent-generated attachment names and node IDs used for storage must be single
|
||||
path components. Local attachment sources must stay within the code workspace;
|
||||
generated destination paths and session downloads cannot follow symbolic links.
|
||||
|
||||
## 10. Debugging Tips
|
||||
- Use the Web UI context snapshots or `WareHouse/<session>/context.json` to inspect node I/O. Note that all node outputs are now standardized as `List[Message]`.
|
||||
- Leverage the Schema API breadcrumbs ([config_schema_contract.md](config_schema_contract.md)) or run `python run.py --inspect-schema` to view field specs quickly.
|
||||
|
||||
@ -241,6 +241,18 @@ python -m tools.export_design_template \
|
||||
- **HTTP**:`POST /api/workflow/execute`,payload 包含 `session_name`, `graph_path` 或 `graph_content`, `task_prompt`、可选的 `attachments`,以及 `log_level`(默认 `INFO`,支持 `INFO` 或 `DEBUG`)。
|
||||
- **CLI**:`python run.py --path yaml_instance/demo.yaml --name test_run`(执行前可设置 `TASK_PROMPT` 环境变量或在 CLI 提示中输入)。
|
||||
|
||||
调用 `POST /api/workflow/run`(JSON 或 SSE)时,先通过 `/api/uploads/{session_id}`
|
||||
上传附件,再把同一个原始会话 ID 填入 `attachment_session_id`,将上传返回的附件 ID
|
||||
放入 `attachments`。兼容该会话中已登记的上传文件路径,但拒绝任意服务器文件路径。
|
||||
`session_name` 仍用于指定运行输出名称,与上传会话独立。本地 SDK 和 CLI 的文件输入不变。
|
||||
|
||||
上传、WebSocket 运行和下载统一映射到 `WareHouse/session_<session_id>`:
|
||||
`foo` 对应 `session_foo`,`session_foo` 对应 `session_session_foo`。
|
||||
旧版本中因前缀合并而共用的附件,需要在正确的会话下重新上传。
|
||||
|
||||
agent 生成附件时,文件名和用于落盘的节点 ID 必须是单个路径分量;本地附件来源必须
|
||||
位于代码工作区内。生成附件的目标路径以及会话下载均不跟随符号链接。
|
||||
|
||||
## 10. 调试建议
|
||||
- 使用 Web UI 的上下文快照或 WareHouse 中的 `context.json` 检查节点输入输出。注意所有节点输出现已统一为 `List[Message]` 结构。
|
||||
- 结合 [config_schema_contract.md](config_schema_contract.md) 的 breadcrumbs 功能,用 CLI `python run.py --inspect-schema` 快速查看字段定义。
|
||||
|
||||
@ -11,6 +11,7 @@ import asyncio
|
||||
import base64
|
||||
import json
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Dict, List, Optional, Sequence
|
||||
|
||||
from entity.configs import Node
|
||||
@ -34,6 +35,8 @@ from runtime.node.agent.memory.memory_base import (
|
||||
from runtime.node.agent import ThinkingPayload
|
||||
from runtime.node.agent import ModelProvider, ProviderRegistry, ModelResponse
|
||||
from runtime.node.agent.skills import AgentSkillManager
|
||||
from utils.exceptions import SecurityError, ValidationError
|
||||
from utils.workspace_paths import resolve_workspace_path, validate_path_component
|
||||
from tenacity import Retrying, retry_if_exception, stop_after_attempt, wait_random_exponential
|
||||
|
||||
|
||||
@ -164,6 +167,8 @@ class AgentNodeExecutor(NodeExecutor):
|
||||
source=node.id,
|
||||
)]
|
||||
|
||||
except SecurityError:
|
||||
raise
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
error_msg = f"[Node: {node.id}] Error calling model: {str(e)}"
|
||||
@ -1079,6 +1084,8 @@ class AgentNodeExecutor(NodeExecutor):
|
||||
continue
|
||||
try:
|
||||
self._persist_single_attachment(store, block, node_id)
|
||||
except SecurityError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
raise RuntimeError(f"Failed to persist attachment '{attachment.name or attachment.attachment_id}': {exc}") from exc
|
||||
|
||||
@ -1086,6 +1093,14 @@ class AgentNodeExecutor(NodeExecutor):
|
||||
attachment = block.attachment
|
||||
if attachment is None:
|
||||
return
|
||||
attachment.mime_type = attachment.mime_type or self._guess_mime_from_data_uri(attachment.data_uri)
|
||||
try:
|
||||
validate_path_component(node_id, field="node_id")
|
||||
if attachment.attachment_id:
|
||||
validate_path_component(attachment.attachment_id, field="attachment_id")
|
||||
filename = validate_path_component(self._make_generated_filename(attachment), field="attachment name")
|
||||
except ValidationError as exc:
|
||||
raise SecurityError(f"Invalid generated attachment path: {exc}") from exc
|
||||
if attachment.remote_file_id and not attachment.data_uri and not attachment.local_path:
|
||||
record = store.register_remote_file(
|
||||
remote_file_id=attachment.remote_file_id,
|
||||
@ -1102,21 +1117,31 @@ class AgentNodeExecutor(NodeExecutor):
|
||||
if workspace_root is None or not node_id:
|
||||
raise RuntimeError("Workspace or node context missing for attachment persistence")
|
||||
|
||||
target_dir = workspace_root / "generated" / node_id
|
||||
target_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
inferred_mime = attachment.mime_type or self._guess_mime_from_data_uri(attachment.data_uri)
|
||||
attachment.mime_type = inferred_mime
|
||||
workspace_root = Path(workspace_root).resolve()
|
||||
try:
|
||||
target_path = resolve_workspace_path(workspace_root, "generated", node_id, filename)
|
||||
current = workspace_root
|
||||
for part in ("generated", node_id, filename):
|
||||
current = current / part
|
||||
if current.is_symlink():
|
||||
raise SecurityError("Generated attachment paths must not contain symbolic links")
|
||||
except (ValidationError, OSError, RuntimeError) as exc:
|
||||
raise SecurityError(f"Invalid generated attachment path: {exc}") from exc
|
||||
|
||||
data_bytes = self._decode_data_uri(attachment.data_uri) if attachment.data_uri else None
|
||||
target_path = None
|
||||
|
||||
if data_bytes is None and attachment.local_path:
|
||||
target_path = target_dir / (attachment.name or self._make_generated_filename(attachment))
|
||||
try:
|
||||
source = Path(attachment.local_path).resolve()
|
||||
if not source.is_relative_to(workspace_root) or not source.is_file():
|
||||
raise SecurityError("Generated attachment source must stay inside the workspace")
|
||||
except (OSError, RuntimeError, ValueError) as exc:
|
||||
raise SecurityError("Invalid generated attachment source") from exc
|
||||
target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
import shutil
|
||||
shutil.copy2(attachment.local_path, target_path)
|
||||
if source != target_path:
|
||||
shutil.copy2(source, target_path)
|
||||
elif data_bytes is not None:
|
||||
target_path = target_dir / (attachment.name or self._make_generated_filename(attachment))
|
||||
target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(target_path, "wb") as handle:
|
||||
handle.write(data_bytes)
|
||||
else:
|
||||
|
||||
@ -17,6 +17,7 @@ class WorkflowRunRequest(BaseModel):
|
||||
yaml_file: str
|
||||
task_prompt: str
|
||||
attachments: Optional[List[str]] = None
|
||||
attachment_session_id: Optional[str] = None
|
||||
session_name: Optional[str] = None
|
||||
variables: Optional[Dict[str, Any]] = None
|
||||
log_level: Optional[Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]] = None
|
||||
|
||||
@ -19,6 +19,7 @@ from runtime.bootstrap.schema import ensure_schema_registry_populated
|
||||
from runtime.sdk import OUTPUT_ROOT, run_workflow
|
||||
from server.models import WorkflowRunRequest
|
||||
from server.settings import YAML_DIR
|
||||
from server.state import get_websocket_manager
|
||||
from utils.attachments import AttachmentStore
|
||||
from utils.exceptions import ValidationError, WorkflowExecutionError, SecurityError
|
||||
from utils.logger import WorkflowLogger
|
||||
@ -167,13 +168,26 @@ async def run_workflow_sync(request: WorkflowRunRequest, http_request: Request):
|
||||
)
|
||||
|
||||
accepts_stream = _SSE_CONTENT_TYPE in (http_request.headers.get("accept") or "")
|
||||
try:
|
||||
attachments = []
|
||||
if request.attachments:
|
||||
if not request.attachment_session_id:
|
||||
raise SecurityError("attachments require attachment_session_id from the upload session")
|
||||
attachments = get_websocket_manager().attachment_service.resolve_uploaded_paths(
|
||||
request.attachment_session_id, request.attachments,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
except SecurityError as exc:
|
||||
raise HTTPException(status_code=403, detail=str(exc))
|
||||
|
||||
if not accepts_stream:
|
||||
try:
|
||||
result = await run_in_threadpool(
|
||||
run_workflow,
|
||||
request.yaml_file,
|
||||
task_prompt=request.task_prompt,
|
||||
attachments=request.attachments,
|
||||
attachments=attachments,
|
||||
session_name=request.session_name,
|
||||
variables=request.variables,
|
||||
log_level=resolved_log_level,
|
||||
@ -222,7 +236,7 @@ async def run_workflow_sync(request: WorkflowRunRequest, http_request: Request):
|
||||
final_message, meta = _run_workflow_with_logger(
|
||||
yaml_file=request.yaml_file,
|
||||
task_prompt=request.task_prompt,
|
||||
attachments=request.attachments,
|
||||
attachments=attachments,
|
||||
session_name=request.session_name,
|
||||
variables=request.variables,
|
||||
log_level=resolved_log_level,
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
@ -8,8 +8,9 @@ from fastapi.responses import FileResponse
|
||||
from starlette.background import BackgroundTask
|
||||
|
||||
from server.settings import WARE_HOUSE_DIR
|
||||
from utils.exceptions import ResourceNotFoundError, ValidationError
|
||||
from utils.exceptions import ResourceNotFoundError, SecurityError, ValidationError
|
||||
from utils.structured_logger import get_server_logger, LogType
|
||||
from utils.workspace_paths import resolve_workspace_path, session_directory_name
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@ -29,8 +30,8 @@ async def download_session(session_id: str):
|
||||
field="session_id",
|
||||
)
|
||||
|
||||
dir_name = f"session_{session_id}"
|
||||
session_path = WARE_HOUSE_DIR / dir_name
|
||||
dir_name = session_directory_name(session_id)
|
||||
session_path = resolve_workspace_path(WARE_HOUSE_DIR, dir_name)
|
||||
|
||||
if not session_path.exists() or not session_path.is_dir():
|
||||
raise ResourceNotFoundError(
|
||||
@ -39,12 +40,22 @@ async def download_session(session_id: str):
|
||||
resource_id=session_id,
|
||||
)
|
||||
|
||||
if (WARE_HOUSE_DIR / dir_name).is_symlink():
|
||||
raise SecurityError("Session downloads must not follow symbolic links")
|
||||
entries = [session_path]
|
||||
for path in session_path.rglob("*"):
|
||||
if path.is_symlink() or not path.resolve().is_relative_to(session_path):
|
||||
raise SecurityError("Session downloads must not follow symbolic links")
|
||||
if path.is_file() or path.is_dir():
|
||||
entries.append(path)
|
||||
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp_file:
|
||||
zip_path = Path(tmp_file.name)
|
||||
|
||||
archive_base = zip_path.with_suffix("")
|
||||
try:
|
||||
shutil.make_archive(str(archive_base), "zip", root_dir=WARE_HOUSE_DIR, base_dir=dir_name)
|
||||
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
|
||||
for path in entries:
|
||||
archive.write(path, arcname=str(Path(dir_name) / path.relative_to(session_path)))
|
||||
except Exception as exc:
|
||||
if zip_path.exists():
|
||||
zip_path.unlink()
|
||||
@ -73,6 +84,8 @@ async def download_session(session_id: str):
|
||||
)
|
||||
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 ResourceNotFoundError:
|
||||
raise HTTPException(status_code=404, detail="Session directory not found")
|
||||
except HTTPException:
|
||||
|
||||
@ -12,7 +12,8 @@ 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
|
||||
from utils.exceptions import SecurityError, ValidationError
|
||||
from utils.workspace_paths import resolve_workspace_path, session_directory_name
|
||||
|
||||
|
||||
class AttachmentService:
|
||||
@ -124,9 +125,49 @@ class AttachmentService:
|
||||
store = self.get_attachment_store(session_id)
|
||||
return store.export_manifest()
|
||||
|
||||
def resolve_uploaded_paths(self, session_id: str, attachments: List[str]) -> List[str]:
|
||||
"""Accept only registered uploads belonging to the specified session."""
|
||||
root = self._session_attachments_path(session_id, create=False)
|
||||
if root is None:
|
||||
raise SecurityError("Attachment upload session does not exist")
|
||||
manifest = root / "attachments_manifest.json"
|
||||
if manifest.is_symlink():
|
||||
raise SecurityError("Attachment manifest must not be a symbolic link")
|
||||
store = AttachmentStore(root)
|
||||
uploaded = {}
|
||||
for attachment_id in store.export_manifest():
|
||||
record = store.get(attachment_id)
|
||||
if (
|
||||
record.extra.get("origin") == "web_upload"
|
||||
and record.extra.get("session_id") == session_id
|
||||
and record.ref.local_path
|
||||
):
|
||||
uploaded[attachment_id] = Path(record.ref.local_path)
|
||||
|
||||
paths = []
|
||||
for value in attachments:
|
||||
try:
|
||||
source = uploaded.get(value)
|
||||
if source is None:
|
||||
requested = Path(value).expanduser().resolve()
|
||||
source = next((path for path in uploaded.values() if path.resolve() == requested), None)
|
||||
if source is None:
|
||||
raise SecurityError("Attachment must reference a registered upload in attachment_session_id")
|
||||
resolved = source.resolve()
|
||||
if source.is_symlink() or not resolved.is_relative_to(root) or not resolved.is_file():
|
||||
raise SecurityError("Attachment source must stay inside its upload session")
|
||||
paths.append(str(resolved))
|
||||
except (OSError, RuntimeError, ValueError) as exc:
|
||||
raise SecurityError("Invalid uploaded attachment source") from exc
|
||||
return paths
|
||||
|
||||
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}"
|
||||
session_dir_name = session_directory_name(session_id)
|
||||
current = self.attachments_root
|
||||
for part in (session_dir_name, "code_workspace", "attachments"):
|
||||
current = current / part
|
||||
if current.is_symlink():
|
||||
raise ValidationError("Upload session paths must not contain symbolic links", field="session_id")
|
||||
path = resolve_workspace_path(
|
||||
self.attachments_root, session_dir_name, "code_workspace", "attachments"
|
||||
)
|
||||
|
||||
@ -15,6 +15,7 @@ from entity.enums import LogLevel
|
||||
from entity.graph_config import GraphConfig
|
||||
from utils.exceptions import ValidationError
|
||||
from utils.task_input import TaskInputBuilder
|
||||
from utils.workspace_paths import resolve_workspace_path, session_directory_name
|
||||
from workflow.graph import GraphExecutor
|
||||
from workflow.graph_context import GraphContext
|
||||
|
||||
@ -160,7 +161,7 @@ class BatchRunService:
|
||||
)
|
||||
|
||||
def _write_batch_outputs(self, session_id: str, result_rows: List[Dict[str, Any]]) -> None:
|
||||
output_root = WARE_HOUSE_DIR / f"session_{session_id}"
|
||||
output_root = resolve_workspace_path(WARE_HOUSE_DIR, session_directory_name(session_id))
|
||||
output_root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
csv_path = output_root / "batch_results.csv"
|
||||
@ -205,7 +206,7 @@ class BatchRunService:
|
||||
details={"yaml_file": yaml_file},
|
||||
)
|
||||
|
||||
output_root = WARE_HOUSE_DIR / f"session_{session_id}"
|
||||
output_root = resolve_workspace_path(WARE_HOUSE_DIR, session_directory_name(session_id))
|
||||
graph_config = GraphConfig.from_definition(
|
||||
design.graph,
|
||||
name=task_dir,
|
||||
|
||||
@ -11,6 +11,7 @@ from entity.enums import LogLevel
|
||||
from utils.exceptions import ValidationError, WorkflowCancelledError
|
||||
from utils.structured_logger import get_server_logger, LogType
|
||||
from utils.task_input import TaskInputBuilder
|
||||
from utils.workspace_paths import session_directory_name
|
||||
from workflow.graph_context import GraphContext
|
||||
|
||||
from server.services.attachment_service import AttachmentService
|
||||
@ -148,7 +149,7 @@ class WorkflowRunService:
|
||||
design = load_config(yaml_path)
|
||||
graph_config = GraphConfig.from_definition(
|
||||
design.graph,
|
||||
name=f"session_{session_id}",
|
||||
name=session_directory_name(session_id),
|
||||
output_root=WARE_HOUSE_DIR,
|
||||
source_path=str(yaml_path),
|
||||
vars=design.vars,
|
||||
|
||||
282
tests/test_attachment_boundary_security.py
Normal file
282
tests/test_attachment_boundary_security.py
Normal file
@ -0,0 +1,282 @@
|
||||
"""Regression tests for HTTP attachment sources and generated attachment paths."""
|
||||
|
||||
import base64
|
||||
import io
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from entity.messages import AttachmentRef, Message, MessageBlock, MessageBlockType, MessageRole
|
||||
from runtime import sdk
|
||||
from runtime.node.agent import ModelResponse, ProviderRegistry
|
||||
from runtime.node.executor.agent_executor import AgentNodeExecutor
|
||||
from runtime.node.executor.base import ExecutionContext
|
||||
from server import state
|
||||
from server.routes import execute_sync, sessions, uploads, websocket, workflows
|
||||
from server.services.websocket_manager import WebSocketManager
|
||||
from utils.attachments import AttachmentStore
|
||||
from utils.error_handler import add_exception_handlers
|
||||
from utils.exceptions import SecurityError
|
||||
|
||||
|
||||
DEMO = Path(__file__).resolve().parents[1] / "yaml_instance/demo_loop_counter.yaml"
|
||||
CONTENT = b"generated attachment content"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api(tmp_path, monkeypatch):
|
||||
monkeypatch.chdir(tmp_path)
|
||||
monkeypatch.setenv("MAC_ALLOW_UNSAFE_CODE_EXECUTION", "0")
|
||||
manager = WebSocketManager()
|
||||
monkeypatch.setattr(state, "websocket_manager", manager)
|
||||
monkeypatch.setattr(sessions, "WARE_HOUSE_DIR", tmp_path / "WareHouse")
|
||||
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)
|
||||
for module in (execute_sync, sessions, uploads, websocket, workflows):
|
||||
app.include_router(module.router)
|
||||
with TestClient(app) as client:
|
||||
yield client, manager
|
||||
|
||||
|
||||
def upload(client, sid, data=b"uploaded input"):
|
||||
with client.websocket_connect("/ws?session_id=" + sid) as ws:
|
||||
ws.receive_json()
|
||||
response = client.post("/api/uploads/" + sid, files={"file": ("input.txt", data)})
|
||||
assert response.status_code == 200, response.text
|
||||
manifest = client.get("/api/uploads/" + sid).json()["attachments"]
|
||||
attachment_id = response.json()["attachment_id"]
|
||||
return attachment_id, Path(manifest[attachment_id]["ref"]["local_path"])
|
||||
|
||||
|
||||
def request_run(client, accept, **overrides):
|
||||
payload = {"yaml_file": str(DEMO), "task_prompt": "test", "session_name": "session_result"}
|
||||
payload.update(overrides)
|
||||
return client.post("/api/workflow/run", headers={"Accept": accept}, json=payload)
|
||||
|
||||
|
||||
def archive_contents(client, sid):
|
||||
response = client.get(f"/api/sessions/{sid}/download")
|
||||
assert response.status_code == 200, response.text
|
||||
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
|
||||
return {name: archive.read(name) for name in archive.namelist()}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("accept", ["application/json", "text/event-stream"])
|
||||
@pytest.mark.parametrize("source_kind", ["absolute", "relative", "symlink", "unregistered", "other_session", "other_id"])
|
||||
def test_http_rejects_untrusted_sources_before_copying(api, tmp_path, accept, source_kind):
|
||||
client, _ = api
|
||||
own_id, own_path = upload(client, "owner")
|
||||
other_id, other_path = upload(client, "other", b"other session secret")
|
||||
secret = tmp_path / "secret.txt"
|
||||
secret.write_text("outside workspace secret")
|
||||
if source_kind == "symlink":
|
||||
own_path.unlink()
|
||||
own_path.symlink_to(secret)
|
||||
unregistered = own_path.parent / "unregistered.txt"
|
||||
unregistered.write_text("not uploaded")
|
||||
source = {"absolute": str(secret), "relative": "secret.txt", "symlink": own_id,
|
||||
"unregistered": str(unregistered), "other_session": str(other_path), "other_id": other_id}[source_kind]
|
||||
response = request_run(client, accept, attachments=[own_id, source], attachment_session_id="owner")
|
||||
assert response.status_code == 403, response.text
|
||||
assert not (tmp_path / "WareHouse/session_result").exists()
|
||||
assert client.get("/api/sessions/result/download").status_code == 404
|
||||
assert secret.read_text() == "outside workspace secret"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("accept", ["application/json", "text/event-stream"])
|
||||
def test_http_paths_without_upload_session_are_rejected(api, tmp_path, accept):
|
||||
client, _ = api
|
||||
secret = tmp_path / "secret.txt"
|
||||
secret.write_text("secret")
|
||||
response = request_run(client, accept, attachments=[str(secret)])
|
||||
assert response.status_code == 403
|
||||
assert "attachment_session_id" in response.text
|
||||
assert not (tmp_path / "WareHouse/session_result").exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("accept", ["application/json", "text/event-stream"])
|
||||
@pytest.mark.parametrize("use_id", [False, True])
|
||||
def test_uploaded_attachment_runs_and_downloads(api, accept, use_id):
|
||||
client, _ = api
|
||||
attachment_id, path = upload(client, "owner")
|
||||
response = request_run(client, accept, attachments=[attachment_id if use_id else str(path)],
|
||||
attachment_session_id="owner")
|
||||
assert response.status_code == 200, response.text
|
||||
assert "Final summary released" in response.text
|
||||
assert "event: error" not in response.text
|
||||
contents = archive_contents(client, "result")
|
||||
assert b"uploaded input" in contents.values()
|
||||
|
||||
|
||||
def test_local_sdk_still_accepts_explicit_file_paths(api, tmp_path):
|
||||
local = tmp_path / "local.txt"
|
||||
local.write_bytes(b"local CLI input")
|
||||
result = sdk.run_workflow(DEMO, task_prompt="test", attachments=[local], session_name="session_sdk")
|
||||
assert result.final_message.text_content() == "Final summary released"
|
||||
assert next(result.meta_info.output_dir.rglob("local.txt")).read_bytes() == b"local CLI input"
|
||||
|
||||
|
||||
def test_download_preserves_empty_directories(api, tmp_path):
|
||||
client, _ = api
|
||||
(tmp_path / "WareHouse/session_empty/nested").mkdir(parents=True)
|
||||
contents = archive_contents(client, "empty")
|
||||
assert "session_empty/" in contents
|
||||
assert "session_empty/nested/" in contents
|
||||
|
||||
|
||||
def test_prefixed_sessions_have_separate_uploads_execution_and_downloads(api, tmp_path):
|
||||
client, manager = api
|
||||
first_id, first_path = upload(client, "foo", b"first")
|
||||
second_id, second_path = upload(client, "session_foo", b"second")
|
||||
assert first_path.is_relative_to(tmp_path / "WareHouse/session_foo")
|
||||
assert second_path.is_relative_to(tmp_path / "WareHouse/session_session_foo")
|
||||
assert set(manager.attachment_service.list_attachment_manifests("foo")) == {first_id}
|
||||
assert set(manager.attachment_service.list_attachment_manifests("session_foo")) == {second_id}
|
||||
for sid, attachment_id, expected in (("foo", first_id, b"first"), ("session_foo", second_id, b"second")):
|
||||
contents = archive_contents(client, sid)
|
||||
assert expected in contents.values()
|
||||
assert (b"second" if expected == b"first" else b"first") not in contents.values()
|
||||
assert manager.attachment_service.build_attachment_blocks(sid, [attachment_id])
|
||||
assert not manager.attachment_service.build_attachment_blocks("foo", [second_id])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kind", ["file", "directory", "session"])
|
||||
def test_download_does_not_follow_links(api, tmp_path, kind):
|
||||
client, _ = api
|
||||
secret_dir = tmp_path / "outside"
|
||||
secret_dir.mkdir()
|
||||
(secret_dir / "secret.txt").write_text("secret")
|
||||
session = tmp_path / "WareHouse/session_download"
|
||||
if kind == "session":
|
||||
session.symlink_to(secret_dir, target_is_directory=True)
|
||||
else:
|
||||
session.mkdir()
|
||||
(session / "link").symlink_to(secret_dir if kind == "directory" else secret_dir / "secret.txt",
|
||||
target_is_directory=kind == "directory")
|
||||
response = client.get("/api/sessions/download/download")
|
||||
assert response.status_code in (400, 403)
|
||||
assert "application/zip" not in response.headers.get("content-type", "")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def agent(tmp_path):
|
||||
workspace = tmp_path / "workspace"
|
||||
store = AttachmentStore(workspace / "attachments")
|
||||
context = ExecutionContext(tool_manager=Mock(), function_manager=Mock(), log_manager=Mock(),
|
||||
global_state={"python_workspace_root": workspace, "attachment_store": store})
|
||||
return AgentNodeExecutor(context), workspace, store
|
||||
|
||||
|
||||
def attachment_message(name="result.txt", attachment_id="generated", local_path=None):
|
||||
ref = AttachmentRef(attachment_id=attachment_id, name=name, mime_type="text/plain",
|
||||
local_path=str(local_path) if local_path else None,
|
||||
data_uri=None if local_path else "data:text/plain;base64," + base64.b64encode(CONTENT).decode())
|
||||
return Message(role=MessageRole.ASSISTANT, content=[MessageBlock(type=MessageBlockType.FILE, attachment=ref)])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field", ["node_id", "name", "attachment_id"])
|
||||
@pytest.mark.parametrize("value", ["../outside", "/absolute", r"..\outside", "C:outside", ".", "..", "bad\x00name"])
|
||||
def test_generated_paths_reject_untrusted_components(agent, tmp_path, field, value):
|
||||
executor, workspace, store = agent
|
||||
node_id = value if field == "node_id" else "Generator"
|
||||
message = attachment_message(**({field: value} if field != "node_id" else {}))
|
||||
with pytest.raises(SecurityError):
|
||||
executor._persist_message_attachments(message, node_id)
|
||||
assert not (workspace / "generated").exists()
|
||||
assert not store.export_manifest()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("part", ["generated", "generated/Generator", "generated/Generator/result.txt"])
|
||||
def test_generated_paths_reject_symlinks(agent, tmp_path, part):
|
||||
executor, workspace, _ = agent
|
||||
outside = tmp_path / "outside"
|
||||
outside.mkdir()
|
||||
sentinel = outside / "result.txt"
|
||||
sentinel.write_text("original")
|
||||
link = workspace / part
|
||||
link.parent.mkdir(parents=True, exist_ok=True)
|
||||
link.symlink_to(sentinel if part.endswith(".txt") else outside, target_is_directory=not part.endswith(".txt"))
|
||||
with pytest.raises(SecurityError):
|
||||
executor._persist_message_attachments(attachment_message(), "Generator")
|
||||
assert sentinel.read_text() == "original"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("linked", [False, True])
|
||||
def test_generated_local_source_cannot_read_outside(agent, tmp_path, linked):
|
||||
executor, workspace, _ = agent
|
||||
secret = tmp_path / "secret.txt"
|
||||
secret.write_text("secret")
|
||||
source = secret
|
||||
if linked:
|
||||
source = workspace / "alias.txt"
|
||||
source.symlink_to(secret)
|
||||
with pytest.raises(SecurityError):
|
||||
executor._persist_message_attachments(attachment_message(local_path=source), "Generator")
|
||||
assert not (workspace / "generated").exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("local", [False, True])
|
||||
def test_generated_attachment_normal_round_trip(agent, local):
|
||||
executor, workspace, store = agent
|
||||
source = None
|
||||
if local:
|
||||
source = workspace / "input.txt"
|
||||
source.write_bytes(CONTENT)
|
||||
message = attachment_message(local_path=source)
|
||||
executor._persist_message_attachments(message, "Image Generator")
|
||||
target = workspace / "generated/Image Generator/result.txt"
|
||||
assert target.read_bytes() == CONTENT
|
||||
assert Path(message.blocks()[0].attachment.local_path) == target
|
||||
assert store.get("generated").ref.local_path == str(target)
|
||||
|
||||
|
||||
def test_generated_image_infers_extension_from_data_uri(agent):
|
||||
executor, workspace, _ = agent
|
||||
message = attachment_message(name=None)
|
||||
ref = message.blocks()[0].attachment
|
||||
ref.mime_type = None
|
||||
ref.data_uri = "data:image/png;base64," + base64.b64encode(CONTENT).decode()
|
||||
executor._persist_message_attachments(message, "Generator")
|
||||
assert (workspace / "generated/Generator/generated.png").read_bytes() == CONTENT
|
||||
assert message.blocks()[0].attachment.mime_type == "image/png"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("accept", ["application/json", "text/event-stream"])
|
||||
@pytest.mark.parametrize("malicious", ["node", "name", "id", "none"])
|
||||
def test_agent_attachment_security_reaches_http_api(api, tmp_path, monkeypatch, accept, malicious):
|
||||
client, _ = api
|
||||
outside = tmp_path / "outside"
|
||||
node_id = str(outside) if malicious == "node" else "Generator"
|
||||
message = attachment_message(name="../../../../escaped.txt" if malicious == "name" else "result.txt",
|
||||
attachment_id="../../escape" if malicious == "id" else "generated")
|
||||
provider = Mock()
|
||||
provider.call_model.return_value = ModelResponse(message=message)
|
||||
monkeypatch.setattr(ProviderRegistry, "get_provider", lambda _: Mock(return_value=provider))
|
||||
definition = {"version": "0.4.0", "graph": {"id": "attachment_test", "start": [node_id], "nodes": [
|
||||
{"id": node_id, "type": "agent", "config": {"provider": "openai", "name": "test-model",
|
||||
"api_key": "local-test", "role": "test"}},
|
||||
], "edges": []}}
|
||||
response = client.post("/api/workflows/upload/content", json={"filename": "agent.yaml", "content": yaml.safe_dump(definition)})
|
||||
assert response.status_code == 200, response.text
|
||||
response = request_run(client, accept, yaml_file="agent.yaml")
|
||||
if malicious == "none":
|
||||
assert response.status_code == 200, response.text
|
||||
assert "event: error" not in response.text
|
||||
assert CONTENT in archive_contents(client, "result").values()
|
||||
elif accept == "text/event-stream":
|
||||
assert response.status_code == 200
|
||||
assert "event: error" in response.text
|
||||
assert "event: completed" not in response.text
|
||||
else:
|
||||
assert response.status_code == 403, response.text
|
||||
assert not outside.exists()
|
||||
assert not (tmp_path / "escaped.txt").exists()
|
||||
@ -234,6 +234,7 @@ def test_upload_then_workflow_round_trip_and_traversal_block(api, tmp_path, acce
|
||||
"yaml_file": str(DEMO_YAML),
|
||||
"task_prompt": "test",
|
||||
"attachments": [str(sources[0])],
|
||||
"attachment_session_id": "upload_test",
|
||||
"session_name": TRAVERSAL,
|
||||
}
|
||||
response = client.post("/api/workflow/run", headers={"Accept": accept}, json=payload)
|
||||
|
||||
@ -32,3 +32,8 @@ def resolve_workspace_path(root: Path | str, *parts: str) -> Path:
|
||||
if candidate == resolved_root or not candidate.is_relative_to(resolved_root):
|
||||
raise ValidationError("Workspace path escapes its root directory", field="workspace")
|
||||
return candidate
|
||||
|
||||
|
||||
def session_directory_name(session_id: str) -> str:
|
||||
"""Map a raw transport session ID to one distinct directory name."""
|
||||
return f"session_{validate_path_component(session_id, field='session_id')}"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user