Skip to content

The agent that grows with you

fix(windows): the icacls fallback refuses every link in the subtree

Teknium5f4defcad075
diff --git a/hermes_dir_acl.py b/hermes_dir_acl.py
index d7ebb8c42f2..4d48a3a8c5a 100644
--- a/hermes_dir_acl.py
+++ b/hermes_dir_acl.py
@@ -166,10 +166,15 @@ def _grant_with_icacls(path: Path, is_dir: bool, expected: str | None = None) ->
if is_dir:
# icacls applies an inheritable ACE through SetNamedSecurityInfo, which also propagates it
# down the existing subtree; refuse when that subtree holds a link it could descend into.
+ # Walk the WHOLE subtree before judging: the generator appends a link it meets after its
+ # last yield, so checking inside the loop admitted a tree whose only link came last. A
+ # hard-linked file is refused too: the inherited ACE lands on its other names outside.
nested: list[Path] = []
- for _ in iter_tree_without_reparse_points(path, nested):
- if nested:
- raise ReparsePointRefused(f"{nested[0]} under {path} is a junction or link (icacls fallback)")
+ linked = [entry for entry, entry_is_dir in iter_tree_without_reparse_points(path, nested)
+ if not entry_is_dir and _hard_linked_file(entry)]
+ if nested or linked:
+ raise ReparsePointRefused(f"{(nested + linked)[0]} under {path} is a junction, symlink or hard "
+ "link (icacls fallback)")
ace = f"*{_whoami_sid()}:(OI)(CI)F" if is_dir else f"*{_whoami_sid()}:F"
# /L: act on a symbolic link or junction itself, not its target (the lstat check already refused
# one; this covers a swap between that check and icacls opening the path). Without pywin32 there
diff --git a/tests/test_hermes_dir_acl.py b/tests/test_hermes_dir_acl.py
index 3dae55ea734..0c677c306a4 100644
--- a/tests/test_hermes_dir_acl.py
+++ b/tests/test_hermes_dir_acl.py
@@ -199,3 +199,40 @@ def test_tree_grant_is_a_noop_unelevated(tmp_path, monkeypatch):
monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: False)
assert hermes_dir_acl.grant_tree_user_access(tmp_path) == (0, [], [])
+
+
[email protected]("shape", ["symlink-only", "symlink-before-a-file", "hardlink-only", "nested-hardlink"])
+def test_icacls_fallback_refuses_any_link_in_the_subtree(tmp_path, monkeypatch, shape):
+ """Review-9 (b): the prewalk checked inside the loop, so a link met after the last yield
+ (a root holding only a link) was admitted, and a hard-linked file was never checked."""
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (outside / "file").write_text("sentinel")
+ root = tmp_path / "home"
+ root.mkdir()
+ if shape.startswith("symlink"):
+ (root / "a-link").symlink_to(outside, target_is_directory=True)
+ if shape == "symlink-before-a-file":
+ (root / "z-file").write_text("x")
+ elif shape == "hardlink-only":
+ (root / "alias").hardlink_to(outside / "file")
+ else:
+ (root / "d").mkdir()
+ (root / "d" / "alias").hardlink_to(outside / "file")
+ monkeypatch.setattr(hermes_dir_acl, "_whoami_sid", lambda: "S-1-5-21-1-2-3-1001")
+ calls = []
+ monkeypatch.setattr(hermes_dir_acl.subprocess, "run", lambda argv, **_k: calls.append(argv))
+ with pytest.raises(hermes_dir_acl.ReparsePointRefused):
+ hermes_dir_acl._grant_with_icacls(root, True, str(root))
+ assert calls == []
+
+
+def test_icacls_fallback_still_grants_a_plain_subtree(tmp_path, monkeypatch):
+ root = tmp_path / "home"
+ (root / "d").mkdir(parents=True)
+ (root / "d" / "f").write_text("x")
+ monkeypatch.setattr(hermes_dir_acl, "_whoami_sid", lambda: "S-1-5-21-1-2-3-1001")
+ calls = []
+ monkeypatch.setattr(hermes_dir_acl.subprocess, "run", lambda argv, **_k: calls.append(argv))
+ hermes_dir_acl._grant_with_icacls(root, True, str(root))
+ assert [c[0] for c in calls] == ["icacls"] and "/L" in calls[0]