Skip to content

The agent that grows with you

security(state): harden async_delegation's direct state.db writer

joaomarcos3966e5de9460
diff --git a/tests/tools/test_async_delegation.py b/tests/tools/test_async_delegation.py
index 361e5df93f9..8437c61c41d 100644
--- a/tests/tools/test_async_delegation.py
+++ b/tests/tools/test_async_delegation.py
@@ -1126,3 +1126,27 @@ print(json.dumps(q.get_nowait(), sort_keys=True))
assert by_index[1]["status"] == "unknown"
assert "1/2 child results were recorded" in evt["error"]
assert "done: fast member" in format_process_notification(evt)
+
+
[email protected](sys.platform.startswith("win"), reason="POSIX mode bits not enforced on Windows")
+def test_connect_creates_state_db_0o600_under_permissive_umask(tmp_path, monkeypatch):
+ """``_connect`` shares state.db with hermes_state.SessionDB -- a fresh
+ HERMES_HOME must land the file (and its WAL sidecar, if created) at 0o600
+ even under a permissive process umask, not the SessionDB-only path."""
+ import stat
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ old_umask = os.umask(0o022)
+ try:
+ conn = ad._connect()
+ conn.close()
+ finally:
+ os.umask(old_umask)
+
+ db_path = tmp_path / "state.db"
+ assert stat.S_IMODE(db_path.stat().st_mode) == 0o600
+
+ for suffix in ("-wal", "-shm"):
+ sidecar = tmp_path / f"state.db{suffix}"
+ if sidecar.exists():
+ assert stat.S_IMODE(sidecar.stat().st_mode) == 0o600
diff --git a/tools/async_delegation.py b/tools/async_delegation.py
index 25d1f7176f4..df2a9ffec6d 100644
--- a/tools/async_delegation.py
+++ b/tools/async_delegation.py
@@ -85,12 +85,18 @@ def _db_path():
def _connect() -> sqlite3.Connection:
path = _db_path()
path.parent.mkdir(parents=True, exist_ok=True)
+ # Same state.db as hermes_state.SessionDB -- reuse its owner-only (0600)
+ # hardening so this writer doesn't create/leave the file (and its WAL
+ # sidecars) at the process umask. See hermes_state._secure_state_db_files.
+ from hermes_state import _secure_state_db_files
+ _secure_state_db_files(path, create_main=True)
conn = sqlite3.connect(path, timeout=10)
try:
_initialize_schema(conn)
except Exception:
conn.close() # don't leak the connection on PRAGMA/DDL failure
raise
+ _secure_state_db_files(path)
return conn