Skip to content

The agent that grows with you

fix(update): authenticate import health markers

fangliquanflq8236b51878e0
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 657a0c2a9aa..c2b679efe48 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -688,7 +688,9 @@ def _critical_module_import_failures(
"""
from hermes_constants import FIRST_PARTY_MODULE_ROOTS
- marker = "__HERMES_IMPORT_HEALTH__"
+ import secrets
+
+ marker = f"__HERMES_IMPORT_HEALTH_{secrets.token_hex(16)}__"
probe = (
"import importlib, json, sys\n"
"failures = []\n"
@@ -754,9 +756,18 @@ def _critical_module_import_failures(
import json
failures = json.loads(output.rsplit(marker, 1)[1])
+ if not isinstance(failures, list) or any(
+ not isinstance(item, list)
+ or len(item) != 2
+ or not all(isinstance(value, str) for value in item)
+ for item in failures
+ ):
+ raise ValueError("invalid import-health payload")
return {str(module): str(detail) for module, detail in failures}
except (TypeError, ValueError):
- return {}
+ return {
+ "critical-module probe": "reported malformed import health data"
+ }
def _validate_critical_modules_import(
diff --git a/tests/hermes_cli/test_update_import_guard.py b/tests/hermes_cli/test_update_import_guard.py
index 7e45523c14c..5854252f85b 100644
--- a/tests/hermes_cli/test_update_import_guard.py
+++ b/tests/hermes_cli/test_update_import_guard.py
@@ -124,6 +124,42 @@ def test_import_guard_reports_probe_termination_by_default(monkeypatch, tmp_path
assert error == "terminated before reporting import health (exit code 9)"
+def test_import_guard_does_not_accept_forged_static_marker(monkeypatch, tmp_path):
+ """Imported stdout cannot impersonate the per-probe completion marker."""
+ (tmp_path / "consumer.py").write_text(
+ "import os, sys\n"
+ "sys.stdout.write('__HERMES_IMPORT_HEALTH__[]')\n"
+ "sys.stdout.flush()\n"
+ "os._exit(7)\n"
+ )
+ monkeypatch.setattr(update_cmd, "_UPDATE_CRITICAL_MODULES", ("consumer",))
+
+ ok, module, error = hermes_main._validate_critical_modules_import(tmp_path)
+
+ assert ok is False
+ assert module == "critical-module probe"
+ assert error == "terminated before reporting import health (exit code 7)"
+
+
+def test_import_guard_rejects_malformed_health_payload(monkeypatch, tmp_path):
+ class Result:
+ returncode = 0
+ stdout = ""
+
+ def malformed(cmd, **_kwargs):
+ marker = cmd[-1].split("sys.stdout.write('\\n", 1)[1].split("'", 1)[0]
+ Result.stdout = f"{marker}{{}}"
+ return Result()
+
+ monkeypatch.setattr(update_cmd.subprocess, "run", malformed)
+
+ ok, module, error = hermes_main._validate_critical_modules_import(tmp_path)
+
+ assert ok is False
+ assert module == "critical-module probe"
+ assert error == "reported malformed import health data"
+
+
def test_untracked_enumeration_failure_is_visible(monkeypatch, tmp_path, capsys):
class Result:
returncode = 1