fix: skip symlinks when downloading session archives

This commit is contained in:
NA-Wen 2026-10-16 23:48:50 +08:00
parent 5df5af7926
commit 1970de9875
4 changed files with 44 additions and 11 deletions

View File

@ -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/<session>/context.json` to inspect node I/O. Note that all node outputs are now standardized as `List[Message]`.

View File

@ -251,7 +251,8 @@ python -m tools.export_design_template \
旧版本中因前缀合并而共用的附件,需要在正确的会话下重新上传。
agent 生成附件时,文件名和用于落盘的节点 ID 必须是单个路径分量;本地附件来源必须
位于代码工作区内。生成附件的目标路径以及会话下载均不跟随符号链接。
位于代码工作区内。生成附件的目标路径不跟随符号链接。会话下载跳过会话内的符号链接
(包括虚拟环境的解释器链接),但仍拒绝以符号链接作为会话根目录。
## 10. 调试建议
- 使用 Web UI 的上下文快照或 WareHouse 中的 `context.json` 检查节点输入输出。注意所有节点输出现已统一为 `List[Message]` 结构。

View File

@ -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)

View File

@ -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"