Skip to content

The agent that grows with you

fix(backup): fail closed for skipped snapshot auth restore

Lion-Leporidae841ec9605ba2
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index 9e24f2435cc..3ee30a82601 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -1485,9 +1485,13 @@ def _is_trusted_root_auth_alias(path: Path, home: Path) -> bool:
if not path.is_symlink():
return False
try:
- trusted = (get_default_hermes_root(home=home) / "auth.json").resolve(strict=False)
+ root = get_default_hermes_root(home=home).resolve(strict=False)
+ if home.resolve(strict=False) == root:
+ return False
+ trusted = (root / "auth.json").resolve(strict=False)
+ trusted.relative_to(root)
return path.resolve(strict=False) == trusted
- except (OSError, RuntimeError):
+ except (OSError, RuntimeError, ValueError):
return False
@@ -1498,7 +1502,8 @@ def restore_quick_snapshot(
"""Restore state from a quick snapshot.
Overwrites current state files with the snapshot's copies.
- Returns True if at least one file was restored.
+ Returns True if at least one file was restored and the listed auth.json
+ was not refused or skipped.
"""
home = hermes_home or get_hermes_home()
root = _quick_snapshot_root(home)
@@ -1537,6 +1542,8 @@ def restore_quick_snapshot(
src.resolve().relative_to(snap_dir.resolve())
except ValueError:
logger.error("Manifest path traversal blocked: %s", rel)
+ if rel == "auth.json":
+ auth_restore_failed = True
continue
dst = home / rel
@@ -1548,9 +1555,14 @@ def restore_quick_snapshot(
# every other manifest destination outside the profile remains a traversal.
if rel != "auth.json" or not _is_trusted_root_auth_alias(dst, home):
logger.error("Manifest path traversal blocked: %s", rel)
+ if rel == "auth.json":
+ auth_restore_failed = True
continue
if not src.exists():
+ if rel == "auth.json":
+ logger.error("Snapshot auth.json listed in manifest is missing: %s", src)
+ auth_restore_failed = True
continue
dst.parent.mkdir(parents=True, exist_ok=True)
@@ -1580,6 +1592,8 @@ def restore_quick_snapshot(
restored += 1
except (OSError, PermissionError) as exc:
logger.error("Failed to restore %s: %s", rel, exc)
+ if rel == "auth.json":
+ auth_restore_failed = True
logger.info("Restored %d files from snapshot %s", restored, snapshot_id)
return restored > 0 and not auth_restore_failed
diff --git a/tests/hermes_cli/test_snapshot_restore_oauth.py b/tests/hermes_cli/test_snapshot_restore_oauth.py
index 5c135234115..59218ed7a4e 100644
--- a/tests/hermes_cli/test_snapshot_restore_oauth.py
+++ b/tests/hermes_cli/test_snapshot_restore_oauth.py
@@ -556,3 +556,99 @@ def test_auth_refusal_is_not_hidden_by_another_restored_file(tmp_path, monkeypat
assert live_path.read_bytes() == before
assert (home / "config.yaml").read_text(encoding="utf-8") == "model: snapshot\n"
assert result is False
+
+
+ "refusal", ["missing-source", "source-alias", "untrusted-destination"]
+)
+def test_skipped_auth_member_is_not_hidden_by_restored_config(tmp_path, monkeypatch, refusal):
+ """A manifest's auth.json must count as failed when a guard skips it."""
+ from hermes_cli.backup import restore_quick_snapshot
+
+ root = tmp_path / "root"
+ home = root / "profiles" / "work"
+ snap_dir = home / "state-snapshots" / "partial"
+ snap_dir.mkdir(parents=True)
+ monkeypatch.setenv("HERMES_HOME", str(home))
+
+ live_path = home / "auth.json"
+ live_path.write_text(
+ json.dumps({"providers": {}, "sentinel": "live"}), encoding="utf-8"
+ )
+ before = live_path.read_bytes()
+ snapshot_auth = snap_dir / "auth.json"
+ if refusal == "source-alias":
+ external = tmp_path / "outside" / "snapshot-auth.json"
+ external.parent.mkdir()
+ external.write_text(json.dumps({"providers": {}}), encoding="utf-8")
+ try:
+ snapshot_auth.symlink_to(external)
+ except (OSError, NotImplementedError) as exc:
+ pytest.skip(f"symlink unavailable: {exc}")
+ elif refusal == "untrusted-destination":
+ external = tmp_path / "outside" / "auth.json"
+ external.parent.mkdir()
+ external.write_bytes(before)
+ live_path.unlink()
+ try:
+ live_path.symlink_to(external)
+ except (OSError, NotImplementedError) as exc:
+ pytest.skip(f"symlink unavailable: {exc}")
+
+ (home / "config.yaml").write_text("model: live\n", encoding="utf-8")
+ (snap_dir / "config.yaml").write_text("model: snapshot\n", encoding="utf-8")
+ (snap_dir / "manifest.json").write_text(
+ json.dumps({"files": {"auth.json": 1, "config.yaml": 1}}),
+ encoding="utf-8",
+ )
+
+ assert restore_quick_snapshot("partial", hermes_home=home) is False
+ assert live_path.read_bytes() == before
+ assert (home / "config.yaml").read_text(encoding="utf-8") == "model: snapshot\n"
+
+
[email protected]("topology", ["default", "named-through-root"])
+def test_root_auth_alias_outside_home_is_never_a_trusted_restore_target(
+ tmp_path, monkeypatch, topology
+):
+ """The named-profile exception must not authorize a root link outside the root."""
+ from hermes_cli.backup import restore_quick_snapshot
+
+ root = tmp_path / "root"
+ root.mkdir()
+ external = tmp_path / "outside" / "auth.json"
+ external.parent.mkdir()
+ external.write_text(
+ json.dumps({"providers": {}, "sentinel": "external"}), encoding="utf-8"
+ )
+ before = external.read_bytes()
+ root_auth = root / "auth.json"
+ try:
+ root_auth.symlink_to(external)
+ except (OSError, NotImplementedError) as exc:
+ pytest.skip(f"symlink unavailable: {exc}")
+
+ home = root
+ if topology == "named-through-root":
+ home = root / "profiles" / "work"
+ home.mkdir(parents=True)
+ try:
+ (home / "auth.json").symlink_to(root_auth)
+ except (OSError, NotImplementedError) as exc:
+ pytest.skip(f"symlink unavailable: {exc}")
+ monkeypatch.setenv("HERMES_HOME", str(home))
+
+ snap_dir = home / "state-snapshots" / "partial"
+ snap_dir.mkdir(parents=True)
+ (snap_dir / "auth.json").write_text(
+ json.dumps({"providers": {}, "sentinel": "snapshot"}), encoding="utf-8"
+ )
+ (snap_dir / "config.yaml").write_text("model: snapshot\n", encoding="utf-8")
+ (snap_dir / "manifest.json").write_text(
+ json.dumps({"files": {"auth.json": 1, "config.yaml": 1}}),
+ encoding="utf-8",
+ )
+
+ result = restore_quick_snapshot("partial", hermes_home=home)
+ assert external.read_bytes() == before
+ assert result is False