Skip to content

The agent that grows with you

fix(windows): media capture grants keep the home provenance of the store

Teknium026088848e83
diff --git a/gateway/session_ingress_media.py b/gateway/session_ingress_media.py
index 5d147866c9f..6d32b2b8008 100644
--- a/gateway/session_ingress_media.py
+++ b/gateway/session_ingress_media.py
@@ -61,15 +61,36 @@ def drop_capture_lease(paths=None):
del _leases[path]
+def _native_inputs_dir():
+ """The store's path as the profile names it, before ``resolve()`` (links still visible)."""
+ from gateway.platforms.base import get_document_cache_dir
+ return get_document_cache_dir() / 'native-inputs'
+
+
def native_inputs_root():
"""This profile's content-addressed store of retained native inputs (``<root>/<sha256>/<name>``)."""
- from gateway.platforms.base import get_document_cache_dir
- return get_document_cache_dir().resolve() / 'native-inputs'
+ return _native_inputs_dir().resolve()
_media_root = native_inputs_root
+def _grant_path(path):
+ """*path* (under the resolved store root) re-rooted on the unresolved root for ``make_user_dir``.
+
+ ``resolve()`` erases a link planted below the Hermes home (``cache/documents`` -> elsewhere), and
+ an elevated grant on the resolved path would then land outside the home with no home fence. The
+ unresolved form keeps that provenance, so the grant refuses the link. A test-pinned root has no
+ unresolved form and is used as is."""
+ if _media_root is not native_inputs_root:
+ return path
+ lexical = _native_inputs_dir()
+ try:
+ return lexical / path.relative_to(lexical.resolve())
+ except ValueError:
+ return path
+
+
# Public ``prompt.submit`` attachments: a local client stages image bytes in the
# profile image cache (where messaging adapters stage downloads), the authority
# commits them as immutable bytes at admission so no later mutation or cache
@@ -245,7 +266,7 @@ def capture_native_media(paths):
root = target.parent.parent
if target.parent.resolve() != target.parent:
raise RuntimeStoreError('invalid_params')
- make_user_dir(target.parent, mode=0o700, exist_ok=True)
+ make_user_dir(_grant_path(target.parent), mode=0o700, exist_ok=True)
with _lease_lock:
scope = _lease_scope.get()
if scope is not None:
@@ -274,7 +295,7 @@ def _capture_file(path, limit, total, references, staged):
root = _media_root()
if root.resolve() != root:
raise RuntimeStoreError('invalid_params')
- make_user_dir(root, mode=0o700, parents=True, exist_ok=True)
+ make_user_dir(_grant_path(root), mode=0o700, parents=True, exist_ok=True)
fd, name = tempfile.mkstemp(prefix='.capture-', dir=root)
temporary = Path(name)
staged.append(temporary)
diff --git a/hermes_dir_acl.py b/hermes_dir_acl.py
index d49537d5390..d7ebb8c42f2 100644
--- a/hermes_dir_acl.py
+++ b/hermes_dir_acl.py
@@ -158,6 +158,11 @@ def _whoami_sid() -> str:
def _grant_with_icacls(path: Path, is_dir: bool, expected: str | None = None) -> None:
+ 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:
# 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.
diff --git a/tests/gateway/test_native_media_acl_provenance.py b/tests/gateway/test_native_media_acl_provenance.py
new file mode 100644
index 00000000000..64553396de4
--- /dev/null
+++ b/tests/gateway/test_native_media_acl_provenance.py
@@ -0,0 +1,64 @@
+"""Review-9 (a): native media capture keeps the Hermes-home provenance of its store for elevated grants.
+
+``native_inputs_root()`` resolves the store, which erases a link planted below the home
+(``cache/documents`` -> elsewhere). An elevated grant on that resolved path landed outside the home
+with no home fence (``expected=None``). Only the elevated-Windows branch selection and the native
+writer are faked; capture, mkdir and the links are real disk operations.
+"""
+import os
+from pathlib import Path
+
+import pytest
+
+import hermes_dir_acl
+from gateway import session_ingress_media as media
+
+
+def _elevated(monkeypatch, home):
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr(hermes_dir_acl.sys, "platform", "win32")
+ monkeypatch.setattr(hermes_dir_acl, "process_is_elevated", lambda: True)
+ monkeypatch.setattr(hermes_dir_acl, "_granted", set())
+ grants = []
+ monkeypatch.setattr(hermes_dir_acl, "_grant_with_pywin32",
+ lambda path, is_dir, expected=None: grants.append((os.path.realpath(path), expected)))
+ return grants
+
+
+def test_capture_never_grants_through_a_link_below_the_home(tmp_path, monkeypatch):
+ home, outside = tmp_path / "home", tmp_path / "outside"
+ (home / "cache").mkdir(parents=True)
+ (outside / "native-inputs").mkdir(parents=True)
+ (home / "cache" / "documents").symlink_to(outside, target_is_directory=True)
+ source = tmp_path / "sample.txt"
+ source.write_text("native-media sentinel")
+ grants = _elevated(monkeypatch, home)
+
+ references = media.capture_native_media([source])
+
+ assert Path(references[0]["path"]).is_file() # capture itself still works (the user's link)
+ escaped = [g for g in grants if Path(g[0]).is_relative_to(outside)]
+ assert escaped == [], f"an elevated grant followed the link out of the home: {escaped}"
+
+
+def test_capture_grants_the_real_store_with_a_home_fence(tmp_path, monkeypatch):
+ home = tmp_path / "home"
+ home.mkdir()
+ source = tmp_path / "sample.txt"
+ source.write_text("bytes")
+ grants = _elevated(monkeypatch, home)
+
+ references = media.capture_native_media([source])
+
+ digest_dir = Path(references[0]["path"]).parent
+ granted = dict(grants)
+ assert str(digest_dir) in granted and granted[str(digest_dir)] == str(digest_dir)
+
+
+def test_icacls_fallback_refuses_a_grant_with_no_trusted_root(tmp_path, monkeypatch):
+ 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(tmp_path, True, None)
+ assert calls == []