`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.
Parallel directories for one source package, folded into the mirror:
tests/acp -> tests/acp_adapter (its __init__/conftest move with it)
tests/cli -> tests/hermes_cli (prompt_toolkit fixture merged into
hermes_cli/conftest.py)
tests/run_agent -> tests/agent (backoff fixture becomes
agent/conftest.py)
tests/relay -> tests/gateway/relay
tests/state -> tests/hermes_state
246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.
Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.
Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).
Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
173 lines
5.8 KiB
Python
173 lines
5.8 KiB
Python
"""Security-focused integration tests for CLI worktree setup."""
|
|
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
def _can_symlink():
|
|
"""Check if we can create symlinks (needs admin/dev-mode on Windows)."""
|
|
import tempfile
|
|
try:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
src = Path(d) / "src"
|
|
src.write_text("x")
|
|
lnk = Path(d) / "lnk"
|
|
lnk.symlink_to(src)
|
|
return True
|
|
except OSError:
|
|
return False
|
|
|
|
|
|
@pytest.fixture
|
|
def git_repo(tmp_path):
|
|
"""Create a temporary git repo for testing real cli._setup_worktree behavior."""
|
|
repo = tmp_path / "test-repo"
|
|
repo.mkdir()
|
|
subprocess.run(["git", "init"], cwd=repo, check=True, capture_output=True)
|
|
subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=repo, check=True, capture_output=True)
|
|
subprocess.run(["git", "config", "user.name", "Test"], cwd=repo, check=True, capture_output=True)
|
|
(repo / "README.md").write_text("# Test Repo\n")
|
|
subprocess.run(["git", "add", "."], cwd=repo, check=True, capture_output=True)
|
|
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=repo, check=True, capture_output=True)
|
|
return repo
|
|
|
|
|
|
def _force_remove_worktree(info: dict | None) -> None:
|
|
if not info:
|
|
return
|
|
subprocess.run(
|
|
["git", "worktree", "remove", info["path"], "--force"],
|
|
cwd=info["repo_root"],
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
subprocess.run(
|
|
["git", "branch", "-D", info["branch"]],
|
|
cwd=info["repo_root"],
|
|
capture_output=True,
|
|
check=False,
|
|
)
|
|
|
|
|
|
class TestWorktreeIncludeSecurity:
|
|
def test_rejects_parent_directory_file_traversal(self, git_repo):
|
|
import cli as cli_mod
|
|
|
|
outside_file = git_repo.parent / "sensitive.txt"
|
|
outside_file.write_text("SENSITIVE DATA")
|
|
(git_repo / ".worktreeinclude").write_text("../sensitive.txt\n")
|
|
|
|
info = None
|
|
try:
|
|
info = cli_mod._setup_worktree(str(git_repo))
|
|
assert info is not None
|
|
|
|
wt_path = Path(info["path"])
|
|
assert not (wt_path.parent / "sensitive.txt").exists()
|
|
assert not (wt_path / "../sensitive.txt").resolve().exists()
|
|
finally:
|
|
_force_remove_worktree(info)
|
|
|
|
def test_rejects_parent_directory_directory_traversal(self, git_repo):
|
|
import cli as cli_mod
|
|
|
|
outside_dir = git_repo.parent / "outside-dir"
|
|
outside_dir.mkdir()
|
|
(outside_dir / "secret.txt").write_text("SENSITIVE DIR DATA")
|
|
(git_repo / ".worktreeinclude").write_text("../outside-dir\n")
|
|
|
|
info = None
|
|
try:
|
|
info = cli_mod._setup_worktree(str(git_repo))
|
|
assert info is not None
|
|
|
|
wt_path = Path(info["path"])
|
|
escaped_dir = wt_path.parent / "outside-dir"
|
|
assert not escaped_dir.exists()
|
|
assert not escaped_dir.is_symlink()
|
|
finally:
|
|
_force_remove_worktree(info)
|
|
|
|
@pytest.mark.skipif(not _can_symlink(), reason="Symlinks need elevated privileges")
|
|
def test_rejects_symlink_that_resolves_outside_repo(self, git_repo):
|
|
import cli as cli_mod
|
|
|
|
outside_file = git_repo.parent / "linked-secret.txt"
|
|
outside_file.write_text("LINKED SECRET")
|
|
(git_repo / "leak.txt").symlink_to(outside_file)
|
|
(git_repo / ".worktreeinclude").write_text("leak.txt\n")
|
|
|
|
info = None
|
|
try:
|
|
info = cli_mod._setup_worktree(str(git_repo))
|
|
assert info is not None
|
|
|
|
assert not (Path(info["path"]) / "leak.txt").exists()
|
|
finally:
|
|
_force_remove_worktree(info)
|
|
|
|
def test_allows_valid_file_include(self, git_repo):
|
|
import cli as cli_mod
|
|
|
|
(git_repo / ".env").write_text("SECRET=***\n")
|
|
(git_repo / ".worktreeinclude").write_text(".env\n")
|
|
|
|
info = None
|
|
try:
|
|
info = cli_mod._setup_worktree(str(git_repo))
|
|
assert info is not None
|
|
|
|
copied = Path(info["path"]) / ".env"
|
|
assert copied.exists()
|
|
assert copied.read_text() == "SECRET=***\n"
|
|
finally:
|
|
_force_remove_worktree(info)
|
|
|
|
@pytest.mark.skipif(not _can_symlink(), reason="Symlinks need elevated privileges")
|
|
def test_allows_valid_directory_include(self, git_repo):
|
|
import cli as cli_mod
|
|
|
|
assets_dir = git_repo / ".venv" / "lib"
|
|
assets_dir.mkdir(parents=True)
|
|
(assets_dir / "marker.txt").write_text("venv marker")
|
|
(git_repo / ".worktreeinclude").write_text(".venv\n")
|
|
|
|
info = None
|
|
try:
|
|
info = cli_mod._setup_worktree(str(git_repo))
|
|
assert info is not None
|
|
|
|
linked_dir = Path(info["path"]) / ".venv"
|
|
assert linked_dir.is_symlink()
|
|
assert (linked_dir / "lib" / "marker.txt").read_text() == "venv marker"
|
|
finally:
|
|
_force_remove_worktree(info)
|
|
|
|
|
|
class TestWorktreeIncludeEncoding:
|
|
"""The include list and .gitignore are UTF-8 files; reading them with the
|
|
locale default breaks Windows (cp1251/GBK mojibake or UnicodeDecodeError,
|
|
swallowed at DEBUG so no include is copied), and a Notepad BOM glues to
|
|
the first line on every platform."""
|
|
|
|
|
|
def test_non_ascii_worktreeinclude_entry_copied(self, git_repo):
|
|
import cli as cli_mod
|
|
|
|
secret = git_repo / "секреты.env"
|
|
secret.write_text("SECRET=***\n", encoding="utf-8")
|
|
(git_repo / ".worktreeinclude").write_bytes(
|
|
"# ключи агента\nсекреты.env\n".encode("utf-8")
|
|
)
|
|
|
|
info = None
|
|
try:
|
|
info = cli_mod._setup_worktree(str(git_repo))
|
|
assert info is not None
|
|
assert (Path(info["path"]) / "секреты.env").exists()
|
|
finally:
|
|
_force_remove_worktree(info)
|
|
|