diff --git a/docs/user_guide/en/workflow_authoring.md b/docs/user_guide/en/workflow_authoring.md index 026fa11a..f8ce12d8 100755 --- a/docs/user_guide/en/workflow_authoring.md +++ b/docs/user_guide/en/workflow_authoring.md @@ -232,7 +232,9 @@ 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. +generated destination paths cannot follow symbolic links. Session downloads skip +symbolic links inside the session (including virtual environment interpreter links) +and still reject a symbolic link used as the session root. ## 10. Debugging Tips - Use the Web UI context snapshots or `WareHouse//context.json` to inspect node I/O. Note that all node outputs are now standardized as `List[Message]`. diff --git a/docs/user_guide/zh/workflow_authoring.md b/docs/user_guide/zh/workflow_authoring.md index 6129622b..c5d5c64f 100755 --- a/docs/user_guide/zh/workflow_authoring.md +++ b/docs/user_guide/zh/workflow_authoring.md @@ -251,7 +251,8 @@ python -m tools.export_design_template \ 旧版本中因前缀合并而共用的附件,需要在正确的会话下重新上传。 agent 生成附件时,文件名和用于落盘的节点 ID 必须是单个路径分量;本地附件来源必须 -位于代码工作区内。生成附件的目标路径以及会话下载均不跟随符号链接。 +位于代码工作区内。生成附件的目标路径不跟随符号链接。会话下载跳过会话内的符号链接 +(包括虚拟环境的解释器链接),但仍拒绝以符号链接作为会话根目录。 ## 10. 调试建议 - 使用 Web UI 的上下文快照或 WareHouse 中的 `context.json` 检查节点输入输出。注意所有节点输出现已统一为 `List[Message]` 结构。 diff --git a/server/routes/sessions.py b/server/routes/sessions.py index 04bce5d2..c003cbb9 100755 --- a/server/routes/sessions.py +++ b/server/routes/sessions.py @@ -44,7 +44,9 @@ async def download_session(session_id: str): 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): + if path.is_symlink(): + continue + if 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) diff --git a/tests/test_attachment_boundary_security.py b/tests/test_attachment_boundary_security.py index 9b9385c2..4ea9bc2e 100644 --- a/tests/test_attachment_boundary_security.py +++ b/tests/test_attachment_boundary_security.py @@ -2,6 +2,7 @@ import base64 import io +import sys import zipfile from pathlib import Path from unittest.mock import Mock @@ -149,24 +150,51 @@ def test_prefixed_sessions_have_separate_uploads_execution_and_downloads(api, tm 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): +@pytest.mark.parametrize("kind", ["file", "directory", "dangling", "loop"]) +def test_download_skips_links_and_preserves_regular_files(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") + session.mkdir() + (session / "run.log").write_bytes(b"workflow log") + target = {"file": secret_dir / "secret.txt", "directory": secret_dir, + "dangling": secret_dir / "missing", "loop": session / "link"}[kind] + (session / "link").symlink_to(target, target_is_directory=kind == "directory") + contents = archive_contents(client, "download") + assert contents["session_download/run.log"] == b"workflow log" + assert not any("link" in Path(name).parts for name in contents) + assert b"secret" not in contents.values() + + +def test_download_still_rejects_linked_session_root(api, tmp_path): + client, _ = api + secret_dir = tmp_path / "outside" + secret_dir.mkdir() + (secret_dir / "secret.txt").write_text("secret") + (tmp_path / "WareHouse/session_download").symlink_to(secret_dir, target_is_directory=True) response = client.get("/api/sessions/download/download") assert response.status_code in (400, 403) assert "application/zip" not in response.headers.get("content-type", "") +def test_download_with_venv_interpreter_links(api, tmp_path): + client, _ = api + session = tmp_path / "WareHouse/session_venv" + venv = session / "code_workspace/.venv" + (venv / "bin").mkdir(parents=True) + (venv / "bin/python").symlink_to(sys.executable) + (venv / "bin/python3").symlink_to("python") + (venv / "pyvenv.cfg").write_bytes(b"include-system-site-packages = false\n") + (session / "run.log").write_bytes(b"workflow completed") + contents = archive_contents(client, "venv") + assert contents["session_venv/run.log"] == b"workflow completed" + assert contents["session_venv/code_workspace/.venv/pyvenv.cfg"] == (venv / "pyvenv.cfg").read_bytes() + assert "session_venv/code_workspace/.venv/bin/python" not in contents + assert "session_venv/code_workspace/.venv/bin/python3" not in contents + + @pytest.fixture def agent(tmp_path): workspace = tmp_path / "workspace"