|
11 | 11 | import pytest |
12 | 12 |
|
13 | 13 | from agents.sandbox import Manifest, SandboxPathGrant |
| 14 | +from agents.sandbox.errors import ExecNonZeroError, WorkspaceArchiveWriteError |
| 15 | +from agents.sandbox.session import SandboxSession |
14 | 16 | from agents.sandbox.session.base_sandbox_session import BaseSandboxSession |
15 | 17 |
|
16 | 18 | from . import _docker_removal_helpers as removal_helpers |
17 | 19 |
|
18 | 20 | service = removal_helpers.service |
19 | 21 |
|
20 | 22 |
|
| 23 | +def _removal_session( |
| 24 | + backend: str, manifest: Manifest, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 25 | +) -> SandboxSession: |
| 26 | + module = pytest.importorskip("agents.sandbox.sandboxes.unix_local", exc_type=ImportError) |
| 27 | + from agents.sandbox.snapshot import NoopSnapshot |
| 28 | + |
| 29 | + from .test_runtime_helpers import _install_resolve_helper |
| 30 | + from .test_snapshot import _ResumeTrackingSession |
| 31 | + |
| 32 | + if backend == "local": |
| 33 | + session = module.UnixLocalSandboxSession( |
| 34 | + state=module.UnixLocalSandboxSessionState( |
| 35 | + manifest=manifest, snapshot=NoopSnapshot(id="recursive-grants") |
| 36 | + ) |
| 37 | + ) |
| 38 | + else: |
| 39 | + session = _ResumeTrackingSession(workspace_root=Path(manifest.root)) |
| 40 | + session.state.manifest = manifest |
| 41 | + # Execute the shipped resolver and real rm against disposable filesystem data. |
| 42 | + monkeypatch.setattr( |
| 43 | + session, |
| 44 | + "_ensure_runtime_helper_installed", |
| 45 | + AsyncMock(return_value=_install_resolve_helper(tmp_path)), |
| 46 | + ) |
| 47 | + monkeypatch.setattr(session, "_validate_path_access", session._validate_remote_path_access) |
| 48 | + return SandboxSession(session) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.asyncio |
| 52 | +@pytest.mark.parametrize("backend", ["local", "remote"]) |
| 53 | +@pytest.mark.parametrize("alias", ["none", "ancestor", "grant"]) |
| 54 | +async def test_recursive_remove_preserves_nested_read_only_grant( |
| 55 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, backend: str, alias: str |
| 56 | +) -> None: |
| 57 | + workspace = tmp_path / "workspace" |
| 58 | + workspace.mkdir() |
| 59 | + external = tmp_path / "external" |
| 60 | + target = external / "tree" |
| 61 | + protected = target / "protected" |
| 62 | + protected.mkdir(parents=True) |
| 63 | + sentinel = protected / "config" |
| 64 | + sentinel.write_bytes(b"protected") |
| 65 | + writable = target / "writable" |
| 66 | + writable.mkdir() |
| 67 | + sibling = writable / "data" |
| 68 | + sibling.write_bytes(b"allowed") |
| 69 | + grant_path = protected |
| 70 | + remove_path = target |
| 71 | + if alias == "ancestor": |
| 72 | + link = workspace / "link" |
| 73 | + link.symlink_to(external, target_is_directory=True) |
| 74 | + remove_path = link / "tree" |
| 75 | + elif alias == "grant": |
| 76 | + grant_path = tmp_path / "protected-alias" |
| 77 | + grant_path.symlink_to(protected, target_is_directory=True) |
| 78 | + session = _removal_session( |
| 79 | + backend, |
| 80 | + Manifest( |
| 81 | + root=str(workspace), |
| 82 | + extra_path_grants=( |
| 83 | + SandboxPathGrant(path=str(external)), |
| 84 | + SandboxPathGrant(path=str(grant_path), read_only=True), |
| 85 | + ), |
| 86 | + ), |
| 87 | + tmp_path, |
| 88 | + monkeypatch, |
| 89 | + ) |
| 90 | + |
| 91 | + with pytest.raises(WorkspaceArchiveWriteError) as direct: |
| 92 | + await session.rm(sentinel) |
| 93 | + assert direct.value.context["reason"] == "read_only_extra_path_grant" |
| 94 | + with pytest.raises((WorkspaceArchiveWriteError, ExecNonZeroError)): |
| 95 | + await session.rm(remove_path) |
| 96 | + assert sentinel.read_bytes() == b"protected" |
| 97 | + with pytest.raises(WorkspaceArchiveWriteError) as recursive: |
| 98 | + await session.rm(remove_path, recursive=True) |
| 99 | + assert recursive.value.context["reason"] == "read_only_extra_path_grant" |
| 100 | + assert sentinel.read_bytes() == b"protected" |
| 101 | + assert sibling.read_bytes() == b"allowed" |
| 102 | + |
| 103 | + await session.rm(writable, recursive=True) |
| 104 | + assert not writable.exists() |
| 105 | + assert sentinel.read_bytes() == b"protected" |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio |
| 109 | +@pytest.mark.parametrize("backend", ["local", "remote"]) |
| 110 | +@pytest.mark.parametrize("precedence", ["workspace", "first-grant", "nested-writable"]) |
| 111 | +async def test_recursive_remove_preserves_writable_precedence( |
| 112 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, backend: str, precedence: str |
| 113 | +) -> None: |
| 114 | + workspace = tmp_path / "workspace" |
| 115 | + workspace.mkdir() |
| 116 | + external = tmp_path / "external" |
| 117 | + target = (workspace if precedence == "workspace" else external) / "tree" |
| 118 | + child = target / "child" |
| 119 | + child.mkdir(parents=True) |
| 120 | + (child / "data").write_bytes(b"allowed") |
| 121 | + grants = ( |
| 122 | + SandboxPathGrant(path=str(external)), |
| 123 | + SandboxPathGrant(path=str(child), read_only=True), |
| 124 | + ) |
| 125 | + if precedence == "first-grant": |
| 126 | + grants = (SandboxPathGrant(path=str(child)), *grants) |
| 127 | + elif precedence == "nested-writable": |
| 128 | + grants = ( |
| 129 | + SandboxPathGrant(path=str(external), read_only=True), |
| 130 | + SandboxPathGrant(path=str(target)), |
| 131 | + ) |
| 132 | + session = _removal_session( |
| 133 | + backend, Manifest(root=str(workspace), extra_path_grants=grants), tmp_path, monkeypatch |
| 134 | + ) |
| 135 | + |
| 136 | + await session.rm(target, recursive=True) |
| 137 | + |
| 138 | + assert not target.exists() |
| 139 | + assert workspace.exists() |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.asyncio |
| 143 | +async def test_remote_recursive_remove_unlinks_leaf_alias_only( |
| 144 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 145 | +) -> None: |
| 146 | + workspace = tmp_path / "workspace" |
| 147 | + workspace.mkdir() |
| 148 | + external = tmp_path / "external" |
| 149 | + protected = external / "protected" |
| 150 | + protected.mkdir(parents=True) |
| 151 | + sentinel = protected / "data" |
| 152 | + sentinel.write_bytes(b"protected") |
| 153 | + link = workspace / "link" |
| 154 | + link.symlink_to(external, target_is_directory=True) |
| 155 | + session = _removal_session( |
| 156 | + "remote", |
| 157 | + Manifest( |
| 158 | + root=str(workspace), |
| 159 | + extra_path_grants=( |
| 160 | + SandboxPathGrant(path=str(external)), |
| 161 | + SandboxPathGrant(path=str(protected), read_only=True), |
| 162 | + ), |
| 163 | + ), |
| 164 | + tmp_path, |
| 165 | + monkeypatch, |
| 166 | + ) |
| 167 | + |
| 168 | + await session.rm(link, recursive=True) |
| 169 | + |
| 170 | + assert not link.is_symlink() |
| 171 | + assert sentinel.read_bytes() == b"protected" |
| 172 | + |
| 173 | + |
| 174 | +@pytest.mark.asyncio |
| 175 | +async def test_unix_local_recursive_remove_as_user_preserves_nested_grant( |
| 176 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 177 | +) -> None: |
| 178 | + from agents.sandbox.types import ExecResult |
| 179 | + |
| 180 | + workspace = tmp_path / "workspace" |
| 181 | + workspace.mkdir() |
| 182 | + external = tmp_path / "external" |
| 183 | + protected = external / "protected" |
| 184 | + protected.mkdir(parents=True) |
| 185 | + sentinel = protected / "data" |
| 186 | + sentinel.write_bytes(b"protected") |
| 187 | + session = _removal_session( |
| 188 | + "local", |
| 189 | + Manifest( |
| 190 | + root=str(workspace), |
| 191 | + extra_path_grants=( |
| 192 | + SandboxPathGrant(path=str(external)), |
| 193 | + SandboxPathGrant(path=str(protected), read_only=True), |
| 194 | + ), |
| 195 | + ), |
| 196 | + tmp_path, |
| 197 | + monkeypatch, |
| 198 | + ) |
| 199 | + # Only the OS permission probe is simulated; use the real local deletion path. |
| 200 | + permission_probe = AsyncMock(return_value=ExecResult(stdout=b"", stderr=b"", exit_code=0)) |
| 201 | + monkeypatch.setattr(session._inner, "exec", permission_probe) |
| 202 | + |
| 203 | + with pytest.raises(WorkspaceArchiveWriteError) as error: |
| 204 | + await session.rm(external, recursive=True, user="example-user") |
| 205 | + |
| 206 | + assert error.value.context["reason"] == "read_only_extra_path_grant" |
| 207 | + assert permission_probe.await_args is not None |
| 208 | + assert permission_probe.await_args.kwargs["user"] == "example-user" |
| 209 | + assert sentinel.read_bytes() == b"protected" |
| 210 | + |
| 211 | + |
21 | 212 | @pytest.mark.asyncio |
22 | 213 | async def test_default_remote_start_restores_with_unrelated_read_only_grant( |
23 | 214 | tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
|
0 commit comments