Skip to content

The agent that grows with you

fix(windows): repairing a left-behind dir also repairs what is inside it; the icacls tree fallback walks once

Teknium2b53b7c9333c
diff --git a/hermes_dir_acl.py b/hermes_dir_acl.py
index 42087b2f810..b3e823e07f2 100644
--- a/hermes_dir_acl.py
+++ b/hermes_dir_acl.py
@@ -13,7 +13,8 @@ or in a non-elevated process, it is exactly ``Path.mkdir``. In an elevated Windo
an explicit, inheriting full-control ACE for the token's user SID to the directory, so the directory
and every child created later stay usable by that user after elevation ends. An elevated call on an
existing directory re-grants the same ACE, which repairs a directory an earlier elevated run left
-behind. A non-elevated process cannot change such a DACL; callers that hit one report it once with
+behind; the pywin32 write does not propagate, so when it had to write, the entries already inside
+(an old token lock) are granted too, once, never through a link. A non-elevated process cannot change such a DACL; callers that hit one report it once with
:func:`unusable_dir_remedy`.
An elevated grant never goes through a junction, symlink or other reparse point (nor a hard-linked
@@ -102,7 +103,9 @@ def _strip_win32_prefix(path: str) -> str:
return path.removeprefix("\\\\?\\")
-def _grant_with_pywin32(path: Path, is_dir: bool, expected: str | None = None) -> None:
+def _grant_with_pywin32(path: Path, is_dir: bool, expected: str | None = None) -> bool:
+ """Write the ACE on the handle; True when it was written (False: already present). The write
+ does NOT propagate (``SetKernelObjectSecurity``), so a written dir's existing children are owed."""
import ntsecuritycon
import win32api
import win32file
@@ -136,13 +139,14 @@ def _grant_with_pywin32(path: Path, is_dir: bool, expected: str | None = None) -
if (ace_type == win32security.ACCESS_ALLOWED_ACE_TYPE and sid == user_sid
and mask & ntsecuritycon.FILE_ALL_ACCESS == ntsecuritycon.FILE_ALL_ACCESS
and flags & inherit == inherit and not flags & win32security.INHERIT_ONLY_ACE):
- return # already granted: never grow the DACL on repeat calls
+ return False # already granted: never grow the DACL on repeat calls
else:
dacl = win32security.ACL()
dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION_DS, inherit, ntsecuritycon.FILE_ALL_ACCESS, user_sid)
descriptor.SetSecurityDescriptorDacl(True, dacl, False)
# The handle form of SetFileSecurity: same semantics (no propagation walk), no name resolution.
win32security.SetKernelObjectSecurity(handle, win32security.DACL_SECURITY_INFORMATION, descriptor)
+ return True
finally:
handle.Close()
@@ -157,13 +161,17 @@ def _whoami_sid() -> str:
return sid
-def _grant_with_icacls(path: Path, is_dir: bool, expected: str | None = None) -> None:
+def _grant_with_icacls(path: Path, is_dir: bool, expected: str | None = None,
+ subtree_has_link: bool | None = None) -> None:
+ """*subtree_has_link*: the caller's own walk already answered it (None: walk here)."""
if expected is None:
# icacls opens by name, so nothing pins the object between the checks and the write. Inside
# the home the components were lstat-checked against a known root; with no expected root
# (off-home, or a path whose home provenance was lost) there is nothing to check: refuse.
raise ReparsePointRefused(f"{path} has no trusted root to check (icacls fallback, no pywin32)")
- if is_dir:
+ if is_dir and subtree_has_link:
+ raise ReparsePointRefused(f"{path} holds a junction, symlink or hard link (icacls fallback)")
+ if is_dir and subtree_has_link is None:
# 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
@@ -191,16 +199,21 @@ def _hard_linked_file(path: Path) -> bool:
return not stat.S_ISDIR(st.st_mode) and st.st_nlink > 1
-def _grant(path: Path, is_dir: bool = True, expected: str | None = None) -> None:
+def _grant(path: Path, is_dir: bool = True, expected: str | None = None,
+ subtree_has_link: bool | None = None) -> bool:
"""Grant the token's user full control on *path* itself; raise :class:`ReparsePointRefused`
for a reparse point or hard-linked file (or, with *expected*, a handle that resolves anywhere
- else) and anything else on failure."""
+ else) and anything else on failure.
+
+ Returns True when the existing children are still owed: the pywin32 write does not propagate,
+ while the icacls fallback (``SetNamedSecurityInfo``) does, and refuses a dir holding a link."""
if is_reparse_point(path) or _hard_linked_file(path):
raise ReparsePointRefused(f"{path} is a junction, symlink, other reparse point or hard link")
try:
- _grant_with_pywin32(path, is_dir, expected)
+ return _grant_with_pywin32(path, is_dir, expected) and is_dir
except ImportError:
- _grant_with_icacls(path, is_dir, expected)
+ _grant_with_icacls(path, is_dir, expected, subtree_has_link)
+ return False
def _components_below_home(path: Path) -> list[Path]:
@@ -238,12 +251,16 @@ def _elevated(elevated: bool | None) -> bool:
return process_is_elevated() if elevated is None else elevated
-def grant_user_access(path: str | Path, *, elevated: bool | None = None) -> bool:
+def grant_user_access(path: str | Path, *, elevated: bool | None = None, repair_children: bool = False) -> bool:
"""Give the token's user an inheriting full-control ACE on *path* when this process is elevated.
True when a grant was attempted and did not fail; False off Windows, when not elevated, when
*path* is a junction/symlink/reparse point (refused, one warning), or when the grant failed
- (logged; the directory itself still exists). *elevated* defaults to the probed host fact."""
+ (logged; the directory itself still exists). *elevated* defaults to the probed host fact.
+
+ With *repair_children* (an existing dir an earlier elevated run may have filled), a write that
+ does not propagate is followed by the same grant on everything already below *path*, never
+ through a link; children are best-effort and never change the result."""
if not _elevated(elevated):
return False
target = Path(path)
@@ -251,7 +268,8 @@ def grant_user_access(path: str | Path, *, elevated: bool | None = None) -> bool
for component in _components_below_home(target):
if is_reparse_point(component):
raise ReparsePointRefused(f"{component} is a junction, symlink or other reparse point")
- _grant(target, expected=_expected_final(target))
+ expected = _expected_final(target)
+ children_owed = _grant(target, expected=expected)
except ReparsePointRefused as exc:
_warn_refused(target, exc)
return False
@@ -260,6 +278,14 @@ def grant_user_access(path: str | Path, *, elevated: bool | None = None) -> bool
"non-elevated Hermes may not be able to use it. To repair, from an elevated prompt run: "
'icacls "%s" /reset /t', target, exc, target)
return False
+ if repair_children and children_owed:
+ real = expected or os.path.realpath(target)
+ skipped: list[Path] = []
+ entries = [(p, d) for p, d in iter_tree_without_reparse_points(target, skipped) if p != target]
+ _, _, failed = _grant_entries(target, real, entries, skipped)
+ if failed:
+ log.debug("Granting the existing entries under %s failed for %d, e.g. %s: %s",
+ target, len(failed), *failed[0])
return True
@@ -306,21 +332,39 @@ def grant_tree_user_access(root: str | Path, *, pin_root: bool = False,
if not _elevated(elevated):
return 0, [], []
root = Path(os.path.realpath(root) if pin_root else os.path.abspath(root))
- real_root = os.path.realpath(root)
- granted, skipped, failed = 0, [], []
- for path, is_dir in iter_tree_without_reparse_points(root, skipped):
+ skipped: list[Path] = []
+ entries = list(iter_tree_without_reparse_points(root, skipped))
+ granted, skipped, failed = _grant_entries(root, os.path.realpath(root), entries, skipped)
+ if skipped:
+ log.warning("Elevated ACL repair skipped %d junction/symlink/hard-link entr%s under %s (never "
+ "granted through): %s", len(skipped), "y" if len(skipped) == 1 else "ies", root,
+ ", ".join(str(p) for p in skipped[:5]) + (" ..." if len(skipped) > 5 else ""))
+ return granted, skipped, failed
+
+
+def _grant_entries(root: Path, real_root: str, entries: list[tuple[Path, bool]],
+ skipped: list[Path]) -> tuple[int, list[Path], list[tuple[Path, str]]]:
+ """Grant each walked entry on its own (the pywin32 write does not propagate). The ONE walk
+ that produced *entries* and *skipped* also decides which dirs hold a link, so the icacls
+ fallback never re-walks a subtree per directory (it refuses exactly those dirs, as before)."""
+ linked = {root, *skipped} # a skipped link, or the root itself when it was skipped
+ linked.update(p for p, is_dir in entries if not is_dir and _hard_linked_file(p))
+ holds_link: set[Path] = set()
+ for bad in linked - {root}:
+ holds_link.update(parent for parent in bad.parents if parent == root or root in parent.parents)
+ if root in skipped:
+ holds_link.add(root)
+ granted, failed = 0, []
+ for path, is_dir in entries:
try:
- _grant(path, is_dir, os.path.join(real_root, *path.relative_to(root).parts))
+ _grant(path, is_dir, os.path.join(real_root, *path.relative_to(root).parts),
+ subtree_has_link=is_dir and path in holds_link)
except ReparsePointRefused:
skipped.append(path)
except Exception as exc: # health: allow BLE001 -- pywintypes.error is not an OSError; keep going like /C
failed.append((path, str(exc) or type(exc).__name__))
else:
granted += 1
- if skipped:
- log.warning("Elevated ACL repair skipped %d junction/symlink/hard-link entr%s under %s (never "
- "granted through): %s", len(skipped), "y" if len(skipped) == 1 else "ies", root,
- ", ".join(str(p) for p in skipped[:5]) + (" ..." if len(skipped) > 5 else ""))
return granted, skipped, failed
@@ -385,7 +429,7 @@ def _grant_dirs(target: Path, to_grant: list[Path], fresh: set[Path] = frozenset
key = _identity(directory)
if key is None or (directory not in fresh and key in _granted):
continue
- if grant_user_access(directory, elevated=True):
+ if grant_user_access(directory, elevated=True, repair_children=directory not in fresh):
_granted.add(key)
diff --git a/tests/test_hermes_dir_acl.py b/tests/test_hermes_dir_acl.py
index 0a2cae5d5a4..f17b30bfff5 100644
--- a/tests/test_hermes_dir_acl.py
+++ b/tests/test_hermes_dir_acl.py
@@ -277,3 +277,71 @@ def test_media_release_then_recapture_grants_the_recreated_digest_dir(tmp_path,
finally:
db.close()
assert grants.count(directory) == 2
+
+
+def test_repairing_an_existing_dir_also_grants_what_is_already_inside_it(tmp_path, monkeypatch):
+ """Review #140490 (pastels minor 1): the pywin32 write (SetKernelObjectSecurity) does not
+ propagate, so an old token lock already inside a dir an earlier elevated run left stayed
+ Administrators-only after make_user_dir repaired the dir. A write on the dir now walks its
+ existing children (never through a link); an already-granted dir (no write) walks nothing."""
+ home = tmp_path / "home"
+ locks = home / "gateway-locks"
+ (locks / "old").mkdir(parents=True)
+ (locks / "telegram-bot-token-x.lock").write_text("{}")
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (locks / "jn").symlink_to(outside, target_is_directory=True)
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(hermes_dir_acl, "_granted", set())
+ writes = []
+
+ def native(path, is_dir, expected=None):
+ writes.append((Path(path), expected))
+ return True # the DACL lacked the ACE: written, not propagated
+
+ monkeypatch.setattr(hermes_dir_acl, "_grant_with_pywin32", native)
+ hermes_dir_acl.make_user_dir(locks, parents=True, exist_ok=True, elevated=True)
+ touched = sorted(p.relative_to(home).as_posix() for p, _ in writes)
+ assert touched == ["gateway-locks", "gateway-locks/old", "gateway-locks/telegram-bot-token-x.lock"]
+ assert all(expected == os.path.join(os.path.realpath(home), *p.relative_to(home).parts) for p, expected in writes)
+
+ writes.clear()
+ monkeypatch.setattr(hermes_dir_acl, "_granted", set())
+ monkeypatch.setattr(hermes_dir_acl, "_grant_with_pywin32", lambda p, *_a, **_k: writes.append(p) and False)
+ hermes_dir_acl.make_user_dir(locks, exist_ok=True, elevated=True)
+ assert writes == [locks] # already granted: no write, so no child walk
+
+
+def test_icacls_tree_fallback_walks_the_tree_once(tmp_path, monkeypatch):
+ """Review #140490 (pastels minor 2): without pywin32 the tree repair re-walked the whole subtree
+ of every directory it granted (quadratic). One walk now decides which directories hold a link;
+ those are skipped as before, and every other entry gets its own icacls call (propagation alone
+ misses children whose DACL is protected, which is exactly what mkdir(0o700) makes on 3.13+)."""
+ home = tmp_path / "home"
+ for d in ("a/b/c", "dirty/sub"):
+ (home / d).mkdir(parents=True)
+ for f in ("a/f1", "a/b/f2", "a/b/c/f3", "dirty/f4", "dirty/sub/f5"):
+ (home / f).write_text("x")
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ (home / "dirty" / "jn").symlink_to(outside, target_is_directory=True)
+ monkeypatch.setattr(hermes_dir_acl, "_grant_with_pywin32", _no_pywin32)
+ 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(Path(argv[1])))
+ walks = []
+ real_iter = hermes_dir_acl.iter_tree_without_reparse_points
+ monkeypatch.setattr(hermes_dir_acl, "iter_tree_without_reparse_points",
+ lambda root, skipped: walks.append(root) or real_iter(root, skipped))
+
+ granted, skipped, failed = hermes_dir_acl.grant_tree_user_access(home, elevated=True)
+
+ assert walks == [home], "one walk, not one per directory"
+ clean = ["a", "a/b", "a/b/c", "a/b/c/f3", "a/b/f2", "a/f1", "dirty/f4", "dirty/sub", "dirty/sub/f5"]
+ assert sorted(p.relative_to(home).as_posix() for p in calls) == clean
+ assert sorted(p.relative_to(tmp_path).as_posix() for p in skipped) == ["home", "home/dirty", "home/dirty/jn"]
+ assert failed == [] and granted == len(clean)
+
+
+def _no_pywin32(*_a, **_k):
+ raise ImportError("pywin32 missing")
diff --git a/tests/test_hermes_dir_acl_windows.py b/tests/test_hermes_dir_acl_windows.py
index 5223150727c..ce980563220 100644
--- a/tests/test_hermes_dir_acl_windows.py
+++ b/tests/test_hermes_dir_acl_windows.py
@@ -185,6 +185,30 @@ def test_elevated_rerun_repairs_a_dir_an_earlier_elevated_run_left(reporters_par
assert after["usable"] is True, acl
+def test_elevated_repair_also_reaches_an_old_lock_already_inside_the_dir(reporters_parent):
+ """Review #140490: the pywin32 write does not propagate, so a token lock an earlier elevated run
+ left INSIDE the dir kept the reporters' DACL after the dir itself was repaired."""
+ from hermes_dir_acl import make_user_dir
+
+ left = reporters_parent / "gateway-locks"
+ left.mkdir()
+ old = left / "telegram-bot-token-x.lock"
+ old.write_text("{}") # inherits SYSTEM + Administrators only
+ _require_faithful_probe(reporters_parent, left)
+ make_user_dir(left, parents=True, exist_ok=True)
+ after = _child("""
+ try:
+ with open(sys.argv[1], "r+", encoding="utf-8") as handle:
+ handle.write("{}")
+ ok = True
+ except PermissionError:
+ ok = False
+ print(json.dumps({"old_lock_writable": ok}))
+ """, str(old))
+ acl = subprocess.run(["icacls", str(old)], capture_output=True, text=True, check=True).stdout
+ assert after["old_lock_writable"] is True, acl
+
+
def test_token_lock_on_an_unusable_lock_dir_parks_with_one_named_warning(tmp_path):
"""W2: the gateway meets a ``gateway-locks`` it may not create in.