Skip to content

The agent that grows with you

test: harden disk cleanup path safety

Benjamin Brumbaughcc2dd7016efb
diff --git a/plugins/disk-cleanup/disk_cleanup.py b/plugins/disk-cleanup/disk_cleanup.py
index c8cd1e0e40f..67f990a81bf 100755
--- a/plugins/disk-cleanup/disk_cleanup.py
+++ b/plugins/disk-cleanup/disk_cleanup.py
@@ -37,7 +37,7 @@ def is_safe_path(path: Path) -> bool:
hermes_home = get_hermes_home()
try:
resolved = path.resolve()
- except OSError:
+ except (OSError, RuntimeError):
return False
try:
resolved.relative_to(hermes_home)
diff --git a/tests/plugins/test_disk_cleanup_plugin.py b/tests/plugins/test_disk_cleanup_plugin.py
index e55d8338e94..ea6a7bb4d3d 100644
--- a/tests/plugins/test_disk_cleanup_plugin.py
+++ b/tests/plugins/test_disk_cleanup_plugin.py
@@ -113,6 +113,45 @@ class TestIsSafePath:
dg = _load_lib()
assert dg.is_safe_path(Path("/etc/passwd")) is False
+ @pytest.mark.skipif(sys.platform == "win32", reason="POSIX /tmp path contract")
+ def test_rejects_tmp_traversal_and_near_prefixes(self, _isolate_env):
+ dg = _load_lib()
+ assert dg.is_safe_path(Path("/tmp/hermes-safe/../not-hermes/file")) is False
+ assert dg.is_safe_path(Path("/tmp-near/hermes-safe/file")) is False
+
+ @pytest.mark.skipif(sys.platform == "win32", reason="POSIX symlink contract")
+ def test_rejects_final_and_nested_symlink_escapes(self, _isolate_env):
+ dg = _load_lib()
+ temporary = Path(tempfile.mkdtemp(prefix="hermes-disk-cleanup-", dir="/tmp"))
+ outside_dir = Path(tempfile.mkdtemp(prefix="disk-cleanup-outside-", dir="/tmp"))
+ outside_file = outside_dir / "file.txt"
+ outside_file.write_text("outside", encoding="utf-8")
+ try:
+ final_link = temporary / "final-link"
+ final_link.symlink_to(outside_file)
+ nested_link = temporary / "nested-link"
+ nested_link.symlink_to(outside_dir, target_is_directory=True)
+
+ assert dg.is_safe_path(final_link) is False
+ assert dg.is_safe_path(nested_link / outside_file.name) is False
+ finally:
+ shutil.rmtree(temporary)
+ shutil.rmtree(outside_dir)
+
+ @pytest.mark.skipif(sys.platform == "win32", reason="POSIX symlink contract")
+ def test_symlink_loop_fails_closed(self, _isolate_env):
+ dg = _load_lib()
+ temporary = Path(tempfile.mkdtemp(prefix="hermes-disk-cleanup-", dir="/tmp"))
+ try:
+ first = temporary / "first"
+ second = temporary / "second"
+ first.symlink_to(second)
+ second.symlink_to(first)
+
+ assert dg.is_safe_path(first) is False
+ finally:
+ shutil.rmtree(temporary)
+
class TestGuessCategory:
def test_test_prefix(self, _isolate_env):