Skip to content

Commit 0da7889

Browse files
fix: reject ambiguous canonical Docker removal paths (#5221)
1 parent 5b50815 commit 0da7889

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

‎src/agents/sandbox/sandboxes/docker_removal.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
user before binding, as normal session startup does.
1515
Path-only grant roots must exist at binding time;
1616
the client does not create unrelated grant directories.
17+
Canonical workspace/grant roots and removal targets must not contain backslashes.
1718
Read-only host bind mounts are supported outside the private workspace. Writable
1819
shared mounts, additional capabilities, user namespaces, and missing grant roots are excluded.
1920
The application must exclusively own container lifecycle and Docker API access;
@@ -322,6 +323,11 @@ def _bind_new(
322323
paths=[manifest.root, *(grant.path for grant in manifest.extra_path_grants)],
323324
)
324325
paths = result["paths"]
326+
# Linux canonical names must not be reinterpreted as caller path syntax.
327+
if any("\\" in path for path in paths):
328+
raise ValueError(
329+
"Docker removal does not support canonical paths containing backslashes"
330+
)
325331
root = coerce_posix_path(paths[0])
326332
for grant, path in zip(manifest.extra_path_grants, paths[1:], strict=True):
327333
mounted = coerce_posix_path(path)
@@ -398,6 +404,12 @@ def remove(
398404
)
399405
target = inspection["path"]
400406
if target:
407+
# Policy coercion would treat literal Linux backslashes as separators.
408+
if "\\" in target:
409+
raise WorkspaceArchiveWriteError(
410+
path=posix_path_for_error(original),
411+
context={"reason": "docker_removal_canonical_path"},
412+
)
401413
selected = coerce_posix_path(target)
402414
root = binding.policy.normalize_sandbox_path(".")
403415
live_roots = (

‎tests/sandbox/test_docker_removal.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,50 @@
4040
service = removal_helpers.service
4141

4242

43+
@pytest.mark.parametrize("bound_path", ["/workspace", "/external", "/grant-alias"])
44+
def test_binding_rejects_canonical_backslashes(service: Any, bound_path: str) -> None:
45+
manager, container, worker = service
46+
configured = manifest()
47+
worker.aliases[bound_path] = r"/private/\root"
48+
with pytest.raises(ValueError, match="canonical paths containing backslashes"):
49+
manager.bind_new(container, configured)
50+
assert manager._bindings == {}
51+
assert worker.removed == []
52+
assert container.events == ["pause", "close", "unpause"]
53+
54+
55+
@pytest.mark.asyncio
56+
@pytest.mark.parametrize("is_directory", [True, False])
57+
async def test_removal_rejects_canonical_backslashes(
58+
service: Any, monkeypatch: pytest.MonkeyPatch, is_directory: bool
59+
) -> None:
60+
manager, container, worker = service
61+
configured = Manifest(
62+
root="/workspace",
63+
extra_path_grants=(SandboxPathGrant(path="/protected", read_only=True),),
64+
)
65+
manager.bind_new(container, configured)
66+
worker.aliases["/workspace/link/data"] = r"/protected/\..\..\workspace/data"
67+
original_request = worker.request
68+
69+
def request(**data: Any) -> dict[str, Any]:
70+
result = original_request(**data)
71+
if data["operation"] == "inspect":
72+
result["is_directory"] = is_directory
73+
return result
74+
75+
monkeypatch.setattr(worker, "request", request)
76+
current = session(manager, container, configured)
77+
with pytest.raises(WorkspaceArchiveWriteError) as caught:
78+
await current.rm("link/data", recursive=True)
79+
assert caught.value.context["reason"] == "docker_removal_canonical_path"
80+
assert [call["operation"] for call in worker.calls] == ["bind", "inspect"]
81+
assert worker.removed == []
82+
assert not container.attrs["State"]["Paused"]
83+
await current.rm("build", recursive=True)
84+
assert worker.removed == ["/workspace/build"]
85+
86+
4387
@pytest.mark.asyncio
4488
@pytest.mark.parametrize(("bound", "change_grants"), [(True, True), (True, False), (False, True)])
4589
async def test_live_manifest_update_preserves_removal_authority(

‎tests/sandbox/test_docker_removal_worker.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,45 @@
3535
)
3636

3737

38+
@pytest.mark.asyncio
39+
async def test_removal_preserves_protected_literal_backslash_tree(
40+
service: Any, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
41+
) -> None:
42+
manager, container, worker = service
43+
workspace = tmp_path.resolve() / "workspace"
44+
workspace.mkdir()
45+
protected = tmp_path.resolve() / "protected"
46+
target = protected / r"\..\workspace" / "data"
47+
target.mkdir(parents=True)
48+
sentinel = target / "sentinel.txt"
49+
sentinel.write_text("protected contents")
50+
(workspace / "link").symlink_to(target.parent, target_is_directory=True)
51+
configured = Manifest(
52+
root=workspace.as_posix(),
53+
extra_path_grants=(SandboxPathGrant(path=protected.as_posix(), read_only=True),),
54+
)
55+
manager.bind_new(container, configured)
56+
original_request = worker.request
57+
selected_paths: list[str] = []
58+
59+
def request(**data: Any) -> dict[str, Any]:
60+
result = original_request(**data)
61+
if data["operation"] == "inspect":
62+
selected, is_directory = worker_code._selected_path(data["path"])
63+
selected_paths.append(selected)
64+
result.update(path=selected, is_directory=is_directory)
65+
return result
66+
67+
monkeypatch.setattr(worker, "request", request)
68+
with pytest.raises(WorkspaceArchiveWriteError) as caught:
69+
await session(manager, container, configured).rm("link/data", recursive=True)
70+
assert caught.value.context["reason"] == "docker_removal_canonical_path"
71+
assert selected_paths == [target.as_posix()]
72+
assert worker.removed == []
73+
assert sentinel.read_text() == "protected contents"
74+
assert not container.attrs["State"]["Paused"]
75+
76+
3877
def test_empty_directory_needs_no_search_of_its_contents(monkeypatch: pytest.MonkeyPatch) -> None:
3978
monkeypatch.setattr(
4079
worker_code.os, "lstat", lambda _, **kwargs: SimpleNamespace(st_mode=0o040000)

0 commit comments

Comments
 (0)