Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(security): resolve the workspace root before comparing containment
trohitg8418bcdeedca
diff --git a/server/nodes/filesystem/_backend.py b/server/nodes/filesystem/_backend.pyindex 2daa6efb..e31ad548 100644--- a/server/nodes/filesystem/_backend.py+++ b/server/nodes/filesystem/_backend.py@@ -557,15 +557,33 @@ def resolve_within(root: os.PathLike[str] | str, key: str) -> Path:``services.media`` and the workspace HTTP route both need it withoutpaying for a backend instance (whose ``__init__`` mkdirs, copies theenvironment, and mints a sandbox id).++ **The root is resolved, not merely made absolute.** Comparing a fully+ resolved candidate against an unresolved root is not like-for-like: if+ any component of the root is itself a symlink (a macOS ``/tmp``, a+ symlinked home or data directory), every legitimate path under it+ resolves to a prefix the root does not have and is refused. That failed+ closed rather than open, so it was never a hole -- but it rejected valid+ files. ``WorkspaceBackend`` already resolves its own root, so this makes+ the two entry points agree."""- root_path = Path(os.path.abspath(os.fspath(root)))+ root_path = Path(os.path.abspath(os.fspath(root))).resolve(strict=False)virtual = _validate_virtual_path(key)candidate = root_path / virtual.lstrip("/")try:resolved = candidate.resolve(strict=False)- resolved.relative_to(root_path)- except (OSError, RuntimeError, ValueError) as exc:- raise ValueError(f"Path '{key}' resolves outside workspace root") from exc+ except (OSError, RuntimeError) as exc:+ # A symlink loop or an unreadable component. Indistinguishable from+ # an escape as far as the caller is concerned, so it translates the+ # same way.+ raise ValueError(f"Path '{key}' could not be resolved") from exc++ # An explicit conditional rather than leaning on ``relative_to`` raising:+ # containment is the point of this function, so it should read as a+ # check, and a check that controls a branch is one a static analyser can+ # actually see.+ if not resolved.is_relative_to(root_path):+ raise ValueError(f"Path '{key}' resolves outside workspace root")return resolveddiff --git a/server/services/media/workspace.py b/server/services/media/workspace.pyindex 8483e4c9..9b8f81c5 100644--- a/server/services/media/workspace.py+++ b/server/services/media/workspace.py@@ -124,12 +124,18 @@ def resolve_media(# Tolerated for back-compat with nodes that stored absolute paths# before AudioRef existed, but still contained: an absolute path# outside the workspace is refused rather than read.- try:- key = candidate.resolve(strict=False).relative_to(root).as_posix()- except ValueError as exc:+ #+ # Both sides are resolved before comparing, for the reason spelled+ # out in ``resolve_within``: a resolved candidate measured against an+ # unresolved root refuses valid files whenever the root sits under a+ # symlink.+ root_resolved = root.resolve(strict=False)+ resolved_candidate = candidate.resolve(strict=False)+ if not resolved_candidate.is_relative_to(root_resolved):raise NodeUserError(f"'{candidate}' is outside this workflow's workspace."- ) from exc+ )+ key = resolved_candidate.relative_to(root_resolved).as_posix()try:return resolve_within(root, key)