From 20008f179ca579b3d4ab87bd2380021325172cc5 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Fri, 3 Jul 2026 22:59:21 +0800 Subject: [PATCH] fix: preserve absolute custom workspace paths --- astrbot/core/workspace.py | 12 ++++++------ tests/unit/test_chatui_project_service.py | 20 +++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/astrbot/core/workspace.py b/astrbot/core/workspace.py index 822f6da04..00ff5c5f0 100644 --- a/astrbot/core/workspace.py +++ b/astrbot/core/workspace.py @@ -95,24 +95,24 @@ def workspace_path_to_root(path: str) -> Path: Args: path: Stored workspace path. Relative values are rooted under AstrBot - workspaces. Absolute values must also point inside AstrBot workspaces. + workspaces. Absolute values are allowed and resolved as provided. Returns: Absolute resolved path. Raises: - ValueError: If the path escapes or targets the AstrBot workspaces + ValueError: If a relative path escapes or targets the AstrBot workspaces root. """ workspaces_root = Path(get_astrbot_workspaces_path()).resolve(strict=False) candidate = Path(path).expanduser() if candidate.is_absolute(): - resolved = candidate.resolve(strict=False) - else: - resolved = (workspaces_root / candidate).resolve(strict=False) + return candidate.resolve(strict=False) + + resolved = (workspaces_root / candidate).resolve(strict=False) if resolved == workspaces_root or not resolved.is_relative_to(workspaces_root): raise ValueError( - "Custom workspace path must stay within a subdirectory of AstrBot workspaces" + "Relative workspace path must stay within a subdirectory of AstrBot workspaces" ) return resolved diff --git a/tests/unit/test_chatui_project_service.py b/tests/unit/test_chatui_project_service.py index 7de03f0d4..3aaacb5e3 100644 --- a/tests/unit/test_chatui_project_service.py +++ b/tests/unit/test_chatui_project_service.py @@ -118,10 +118,10 @@ def test_custom_workspace_rejects_workspaces_root(tmp_path, monkeypatch): ) -def test_custom_workspace_rejects_absolute_path_outside_workspaces( +def test_custom_workspace_accepts_absolute_path_outside_workspaces( tmp_path, monkeypatch ): - """Absolute custom workspace paths must not escape AstrBot workspaces.""" + """Absolute custom workspace paths may point outside AstrBot workspaces.""" outside_workspace = tmp_path / "outside" workspaces_root = tmp_path / "workspaces" outside_workspace.mkdir() @@ -131,10 +131,12 @@ def test_custom_workspace_rejects_absolute_path_outside_workspaces( lambda: str(workspaces_root), ) - with pytest.raises(ChatUIProjectServiceError, match="must stay within"): - ChatUIProjectService._normalize_workspace_config( - { - "workspace_type": "custom", - "workspace_path": str(outside_workspace), - } - ) + workspace_type, workspace_path = ChatUIProjectService._normalize_workspace_config( + { + "workspace_type": "custom", + "workspace_path": str(outside_workspace), + } + ) + + assert workspace_type == "custom" + assert workspace_path == str(outside_workspace)