fix: preserve absolute custom workspace paths

This commit is contained in:
Soulter
2026-07-03 22:59:21 +08:00
parent 8a31cf01f2
commit 20008f179c
2 changed files with 17 additions and 15 deletions

View File

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

View File

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