The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(windows): one TokenElevation probe for grants, refusal naming and the service installer
Tekniumb497e61e681d
diff --git a/gateway/runtime_bootstrap_windows.py b/gateway/runtime_bootstrap_windows.pyindex 86a7288d78d..450af5936b2 100644--- a/gateway/runtime_bootstrap_windows.py+++ b/gateway/runtime_bootstrap_windows.py@@ -394,37 +394,10 @@ def _session_of(pid: int) -> int | None:return session.value if to_session(pid, ctypes.byref(session)) else None-def _elevation_of(pid: int) -> bool | None:- k = ctypes.WinDLL('kernel32', use_last_error=True)- a = ctypes.WinDLL('advapi32', use_last_error=True)- open_process = _api(k, 'OpenProcess', wintypes.HANDLE, [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD])- close = _api(k, 'CloseHandle', wintypes.BOOL, [wintypes.HANDLE])- process = open_process(0x1000, False, pid) # PROCESS_QUERY_LIMITED_INFORMATION- if not process:- return None- token = wintypes.HANDLE()- try:- open_token = _api(a, 'OpenProcessToken', wintypes.BOOL,- [wintypes.HANDLE, wintypes.DWORD, ctypes.POINTER(wintypes.HANDLE)])- if not open_token(process, 8, ctypes.byref(token)): # TOKEN_QUERY- return None- token_info = _api(a, 'GetTokenInformation', wintypes.BOOL,- [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD,- ctypes.POINTER(wintypes.DWORD)])- elevated, size = wintypes.DWORD(), wintypes.DWORD()- if not token_info(token, 20, ctypes.byref(elevated), ctypes.sizeof(elevated), ctypes.byref(size)):- return None- return bool(elevated.value)- finally:- if token:- close(token)- close(process)--def probe_refused_owner(pid: int) -> tuple[str, str] | None:"""``(reason_code, remedy)`` naming why *pid*'s gateway refused this client, or None."""- from hermes_dir_acl import process_is_elevated+ from hermes_dir_acl import process_elevation, process_is_elevatedown_session, owner_session = _session_of(os.getpid()), _session_of(pid)reason = classify_refused_owner(own_session=own_session, owner_session=owner_session,- own_elevated=process_is_elevated(), owner_elevated=_elevation_of(pid))+ own_elevated=process_is_elevated(), owner_elevated=process_elevation(pid))return (reason, refused_owner_remedy(reason, pid, owner_session)) if reason else Nonediff --git a/hermes_cli/gateway_windows.py b/hermes_cli/gateway_windows.pyindex 37fb18cd56e..16640969295 100644--- a/hermes_cli/gateway_windows.py+++ b/hermes_cli/gateway_windows.py@@ -223,12 +223,11 @@ def _is_access_denied(detail: str) -> bool:def _is_running_as_admin() -> bool:- """Return True when the current Windows process is elevated."""+ """Return True when the current Windows process is elevated (the shared ``TokenElevation``+ probe; ``IsUserAnAdmin`` is deprecated and answered the same question a second way)."""_assert_windows()- try:- return bool(ctypes.windll.shell32.IsUserAnAdmin())- except Exception:- return False+ from hermes_dir_acl import process_is_elevated+ return process_is_elevated()def _current_profile_cli_args() -> list[str]:diff --git a/hermes_dir_acl.py b/hermes_dir_acl.pyindex b3e823e07f2..88149ddd3d5 100644--- a/hermes_dir_acl.py+++ b/hermes_dir_acl.py@@ -69,32 +69,53 @@ def is_reparse_point(path: str | Path) -> bool:return bool(isjunction and isjunction(path))[email protected]_cache(maxsize=1)-def process_is_elevated() -> bool:- """True for an elevated (UAC full-token) Windows process; False elsewhere or on any error."""- if sys.platform != "win32":- return False+_PROCESS_QUERY_LIMITED_INFORMATION = 0x1000+++def process_elevation(pid: int | None = None) -> bool | None:+ """``TokenElevation`` of *pid*'s token (default: this process). The ONE elevation probe: the+ grants below, the refused-owner naming in ``gateway.runtime_bootstrap_windows`` and the+ Windows service installer all read it. None when the process or its token cannot be opened+ (across an elevation boundary that refusal is itself an answer the caller interprets)."""import ctypesfrom ctypes import wintypesadvapi = ctypes.WinDLL("advapi32", use_last_error=True)kernel = ctypes.WinDLL("kernel32", use_last_error=True)kernel.GetCurrentProcess.restype = wintypes.HANDLE+ kernel.OpenProcess.restype = wintypes.HANDLE+ kernel.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]advapi.OpenProcessToken.argtypes = [wintypes.HANDLE, wintypes.DWORD, ctypes.POINTER(wintypes.HANDLE)]advapi.GetTokenInformation.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD,ctypes.POINTER(wintypes.DWORD)]kernel.CloseHandle.argtypes = [wintypes.HANDLE]+ own = pid is None+ process = kernel.GetCurrentProcess() if own else kernel.OpenProcess(_PROCESS_QUERY_LIMITED_INFORMATION,+ False, pid)+ if not process:+ return Nonetoken = wintypes.HANDLE()- if not advapi.OpenProcessToken(kernel.GetCurrentProcess(), _TOKEN_QUERY, ctypes.byref(token)):- return Falsetry:+ if not advapi.OpenProcessToken(process, _TOKEN_QUERY, ctypes.byref(token)):+ return Noneelevated, size = wintypes.DWORD(), wintypes.DWORD()if not advapi.GetTokenInformation(token, _TOKEN_ELEVATION, ctypes.byref(elevated),ctypes.sizeof(elevated), ctypes.byref(size)):- return False+ return Nonereturn bool(elevated.value)finally:- kernel.CloseHandle(token)+ if token:+ kernel.CloseHandle(token)+ if not own: # the pseudo-handle of GetCurrentProcess is never closed+ kernel.CloseHandle(process)++[email protected]_cache(maxsize=1)+def process_is_elevated() -> bool:+ """True for an elevated (UAC full-token) Windows process; False elsewhere or on any error."""+ if sys.platform != "win32":+ return False+ return bool(process_elevation())def _strip_win32_prefix(path: str) -> str:diff --git a/tests/gateway/test_refused_owner_windows.py b/tests/gateway/test_refused_owner_windows.pyindex 94ca03643c0..ca46ef0a5a6 100644--- a/tests/gateway/test_refused_owner_windows.py+++ b/tests/gateway/test_refused_owner_windows.py@@ -18,11 +18,13 @@ pytestmark = pytest.mark.platforms("windows")def test_own_session_and_elevation_are_read_from_the_real_token():from gateway import runtime_bootstrap_windows as native- from hermes_dir_acl import process_is_elevated+ from hermes_dir_acl import process_elevation, process_is_elevatedimport osassert isinstance(native._session_of(os.getpid()), int)- assert native._elevation_of(os.getpid()) is process_is_elevated()+ assert process_elevation(os.getpid()) is process_elevation() is process_is_elevated()+ from hermes_cli.gateway_windows import _is_running_as_admin+ assert _is_running_as_admin() is process_is_elevated()def test_a_same_token_owner_is_not_misnamed():