Skip to content

Commit 02eced0

Browse files
committed
fix(tools): honor machine-bundle protect paths in the in-process edit tools
_refuse_protected_writes guarded only .git; apply_edit/apply_patch write in-process (outside the jail), so a mode='run' machine state could rewrite its own .asm.toml or scripts/ -- persisting a payload for the next run -- even though the jail marks the bundle read-only for run_command. Refuse an in-process write whose resolved path is at or under any extra_protect_path, on both sandbox profiles. Security review note: the in-process edit tools now enforce the operator/machine protect-path set, not just .git. The docs/security.md bundle-confinement claim is still partly inaccurate for the hardened jail's nested run_command case (a separate main.rs item); correct the doc together with that fix.
1 parent c0a0741 commit 02eced0

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎src/agent6/tools/dispatch.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,22 @@ def _grep(self, raw: dict[str, Any]) -> dict[str, Any]:
730730
return {"hits": hits, "truncated": False}
731731

732732
def _refuse_protected_writes(self, path: str, resolved: _SafePath | None = None) -> None:
733-
"""Refuse an in-process edit into ``.git`` (it bypasses the jail entirely)."""
733+
"""Refuse an in-process edit into a protected location (it bypasses the
734+
jail entirely). ``.git`` under ``protect_git``, plus any operator/machine
735+
protect paths (a machine bundle's ``.asm.toml`` + ``scripts/``), which the
736+
jail marks read-only for ``run_command`` but the in-process edit tools
737+
would otherwise let a ``mode="run"`` state rewrite -- persisting a payload
738+
for the next run. Applies on both sandbox profiles."""
734739
if self._config.sandbox.protect_git:
735740
_refuse_protected_write(path, ".git", why="git history/metadata", resolved=resolved)
741+
if resolved is not None and self._extra_protect_paths:
742+
target = resolved.abs_path
743+
for prot in self._extra_protect_paths:
744+
if target == prot or prot in target.parents:
745+
raise ToolError(
746+
f"Refusing to write to a protected path (machine bundle): {path!r} "
747+
f"resolves under {prot}"
748+
)
736749

737750
def _apply_edit(self, raw: dict[str, Any]) -> dict[str, Any]:
738751
args = ApplyEditInput.model_validate(raw)

‎tests/unit/test_tools_dispatch.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,50 @@ def test_apply_edit_allows_git_write_when_protect_git_false(tmp_path: Path) -> N
301301
assert (tmp_path / ".git" / "description").read_text(encoding="utf-8") == "ok\n"
302302

303303

304+
def test_apply_edit_refuses_extra_protect_paths(tmp_path: Path) -> None:
305+
# A machine bundle's .asm.toml + scripts/ are passed as extra_protect_paths.
306+
# The jail marks them read-only for run_command, but the in-process edit tools
307+
# ran outside that -- a mode="run" state could rewrite its own logic/scripts
308+
# and persist a payload for the next run. Refuse on both profiles.
309+
cfg = _config(tmp_path)
310+
(tmp_path / "bundle").mkdir()
311+
asm = tmp_path / "bundle" / "fixer.asm.toml"
312+
asm.write_text("[machine]\n", encoding="utf-8")
313+
scripts = tmp_path / "bundle" / "scripts"
314+
scripts.mkdir()
315+
(scripts / "deploy.sh").write_text("#!/bin/sh\ntrue\n", encoding="utf-8")
316+
d = ToolDispatcher(
317+
root=tmp_path,
318+
config=cfg,
319+
extra_protect_paths=(asm.resolve(), scripts.resolve()),
320+
)
321+
with pytest.raises(ToolError, match="protected path"):
322+
d.dispatch(
323+
"apply_edit",
324+
{
325+
"path": "bundle/fixer.asm.toml",
326+
"edits": [{"kind": "replace", "old_string": "[machine]", "new_string": "[m]"}],
327+
},
328+
)
329+
with pytest.raises(ToolError, match="protected path"):
330+
d.dispatch(
331+
"apply_edit",
332+
{
333+
"path": "bundle/scripts/deploy.sh",
334+
"edits": [{"kind": "replace", "old_string": "true", "new_string": "curl evil"}],
335+
},
336+
)
337+
# A sibling file NOT under a protect path stays editable.
338+
(tmp_path / "bundle" / "notes.md").write_text("hi\n", encoding="utf-8")
339+
d.dispatch(
340+
"apply_edit",
341+
{
342+
"path": "bundle/notes.md",
343+
"edits": [{"kind": "replace", "old_string": "hi", "new_string": "bye"}],
344+
},
345+
)
346+
347+
304348
def test_apply_edit_rejects_create_combined_with_other_edits(tmp_path: Path) -> None:
305349
# A `create` after a `replace` used to skip the file-exists guard (which
306350
# only fired for the first edit) and silently overwrite the whole file.

0 commit comments

Comments
 (0)