Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/tools/test_file_write_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
Based on PR #1085 by ismoilh (salvaged).
"""

import importlib
import os
from pathlib import Path

import pytest

import tools.file_operations as file_operations
from tools.file_operations import _is_write_denied

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On current main this fixture reloads only the compatibility wrapper, while enforcement is in the already-imported agent.file_safety module. Port these regressions to exercise agent.file_safety.is_write_denied() so they prove the salvaged implementation.


@pytest.fixture
def windows_casefolded_file_operations():
with pytest.MonkeyPatch.context() as mp:
mp.setattr(os.path, "normcase", lambda value: str(value).lower())
yield importlib.reload(file_operations)
importlib.reload(file_operations)


class TestStaticDenyList:
"""Basic sanity checks for the static write deny list."""

Expand Down Expand Up @@ -78,6 +88,33 @@ def test_safe_root_does_not_override_static_deny(self, tmp_path: Path, monkeypat
monkeypatch.setenv("HERMES_WRITE_SAFE_ROOT", os.path.expanduser("~"))
assert _is_write_denied(os.path.expanduser("~/.ssh/id_rsa")) is True

def test_safe_root_comparison_is_case_insensitive_on_windows(
self,
tmp_path: Path,
monkeypatch,
windows_casefolded_file_operations,
):
safe_root = tmp_path / "Workspace"
child = safe_root / "subdir" / "file.txt"
os.makedirs(child.parent, exist_ok=True)

monkeypatch.setenv("HERMES_WRITE_SAFE_ROOT", str(safe_root).upper())
assert windows_casefolded_file_operations._is_write_denied(str(child)) is False


class TestWindowsCaseInsensitiveComparisons:
def test_static_deny_list_matches_case_insensitively_on_windows(
self,
windows_casefolded_file_operations,
):
mixed_case_key = os.path.join(
windows_casefolded_file_operations._HOME.upper(),
".SSH",
"ID_RSA",
)

assert windows_casefolded_file_operations._is_write_denied(mixed_case_key) is True


class TestCheckSensitivePathMacOSBypass:
"""Verify _check_sensitive_path blocks /private/etc paths (issue #8734)."""
Expand Down
14 changes: 10 additions & 4 deletions tools/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@

_HOME = str(Path.home())


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current main moved the live predicate to agent/file_safety.py; tools/file_operations._is_write_denied() now delegates there. Please place this normalizer in the shared module and apply it to its candidate, deny-list, protected-subtree, and safe-root comparisons so both file tools and ACP writes are covered.

def _normalize_write_path(path: str) -> str:
"""Normalize a path for deny-list and safe-root comparisons."""
return os.path.normcase(os.path.realpath(os.path.expanduser(str(path))))


WRITE_DENIED_PATHS = {
os.path.realpath(p) for p in [
_normalize_write_path(p) for p in [
os.path.join(_HOME, ".ssh", "authorized_keys"),
os.path.join(_HOME, ".ssh", "id_rsa"),
os.path.join(_HOME, ".ssh", "id_ed25519"),
Expand All @@ -65,7 +71,7 @@
}

WRITE_DENIED_PREFIXES = [
os.path.realpath(p) + os.sep for p in [
_normalize_write_path(p) + os.sep for p in [
os.path.join(_HOME, ".ssh"),
os.path.join(_HOME, ".aws"),
os.path.join(_HOME, ".gnupg"),
Expand All @@ -91,14 +97,14 @@ def _get_safe_write_root() -> Optional[str]:
if not root:
return None
try:
return os.path.realpath(os.path.expanduser(root))
return _normalize_write_path(root)
except Exception:
return None


def _is_write_denied(path: str) -> bool:
"""Return True if path is on the write deny list."""
resolved = os.path.realpath(os.path.expanduser(str(path)))
resolved = _normalize_write_path(path)

# 1) Static deny list
if resolved in WRITE_DENIED_PATHS:
Expand Down
Loading