strix/tests/test_cli_resume.py
LuckVd ce580989ef fix(resume): re-validate persisted mounts with the mount guard
--resume rebuilt sandbox bind mounts from run.json but skipped
check_mountable_dir for workspace_mount and repository cloned_repo_path
(local_code paths were already re-checked), and workspace_files sources
were not guarded at all. run.json lives on disk under the scanned tree and
must be treated as untrusted: a tampered record could mount '/' or $HOME
writable, or copy credential files into the sandbox.

Apply the same guard on resume:
- workspace_mount and cloned_repo_path now pass through check_mountable_dir
- workspace_files sources pass through the new check_mountable_file, which
  applies the same system/home/credential checks to a single file
- split the shared root/tree/credential sets into _mount_guard_sets used by
  both guards

Adds unit coverage for check_mountable_file and resume integration tests
for the three bypass paths.
2026-10-24 04:27:43 +08:00

133 lines
3.9 KiB
Python

"""--resume re-validates persisted mounts against the mount guard.
run.json lives on disk under the scanned tree and is treated as untrusted on
resume: workspace_mount, repository cloned_repo_path and workspace_files
sources must all pass the same checks a fresh invocation would apply.
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import pytest
from strix.interface.cli_args import ResumeError, load_resume_state
def _resume_args(**overrides: object) -> argparse.Namespace:
defaults: dict[str, object] = {
"resume": "old-run",
"targets_info": [],
"instruction": None,
"user_instruction": None,
"local_sources": [],
"workspace_mount": None,
"workspace_files": None,
"diff_scope": None,
"scan_mode": "quick",
}
defaults.update(overrides)
return argparse.Namespace(**defaults)
def _write_run(tmp_path: Path, record: dict[str, object]) -> None:
run_dir = tmp_path / "strix_runs" / "old-run"
(run_dir / ".state").mkdir(parents=True)
(run_dir / "run.json").write_text(json.dumps(record), encoding="utf-8")
# A run that got far enough to resume always has a first agent snapshot.
(run_dir / ".state" / "agents.json").write_text("{}", encoding="utf-8")
def _patch_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
home = tmp_path / "home"
home.mkdir()
monkeypatch.setenv("HOME", str(home))
monkeypatch.setattr(Path, "home", classmethod(lambda _cls: home))
return home
def test_resume_rejects_forbidden_workspace_mount(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
home = _patch_home(tmp_path, monkeypatch)
monkeypatch.chdir(tmp_path)
_write_run(
tmp_path,
{"targets_info": [], "workspace_mount": str(home), "workspace_files": []},
)
with pytest.raises(ResumeError, match="Refusing to mount"):
load_resume_state(_resume_args())
def test_resume_rejects_system_workspace_mount(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
_write_run(
tmp_path,
{"targets_info": [], "workspace_mount": "/etc", "workspace_files": []},
)
with pytest.raises(ResumeError, match="Refusing to mount"):
load_resume_state(_resume_args())
def test_resume_rejects_forbidden_cloned_repo_path(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
_write_run(
tmp_path,
{
"targets_info": [{"type": "repository", "details": {"cloned_repo_path": "/etc"}}],
"workspace_mount": None,
"workspace_files": [],
},
)
with pytest.raises(ResumeError, match="Refusing to mount"):
load_resume_state(_resume_args())
def test_resume_rejects_credential_workspace_file(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
home = _patch_home(tmp_path, monkeypatch)
ssh = home / ".ssh"
ssh.mkdir()
key = ssh / "id_rsa"
key.write_text("key", encoding="utf-8")
monkeypatch.chdir(tmp_path)
_write_run(
tmp_path,
{
"targets_info": [],
"user_instruction": "scan the target",
"workspace_mount": None,
"workspace_files": [{"source_path": str(key), "workspace_path": "id_rsa"}],
},
)
with pytest.raises(ResumeError, match="holds credentials"):
load_resume_state(_resume_args())
def test_resume_accepts_a_clean_record(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
_write_run(
tmp_path,
{
"targets_info": [],
"user_instruction": "scan the target",
"workspace_mount": None,
"workspace_files": [],
},
)
args = _resume_args()
load_resume_state(args)
assert args.user_instruction == "scan the target"