Skip to content

The agent that grows with you

fix(agent): unescape quoted .env values in secret_scope.load_env_file

spfcraze55abf206bff4
diff --git a/agent/secret_scope.py b/agent/secret_scope.py
index 1e4b30e3a70..92ae59361cb 100644
--- a/agent/secret_scope.py
+++ b/agent/secret_scope.py
@@ -181,9 +181,11 @@ def load_env_file(env_path: Path) -> Dict[str, str]:
"""Parse a ``.env`` file into a plain dict WITHOUT touching ``os.environ``.
Used to load a profile's secrets into an isolated mapping for
- ``set_secret_scope``. Mirrors python-dotenv's basic parsing (KEY=VALUE,
- ``export`` prefix, ``#`` comments, optional matching quotes) but never
- mutates the process environment — that isolation is the whole point.
+ ``set_secret_scope``. Parses the small KEY=VALUE subset Hermes writes
+ itself (``export`` prefix, ``#`` comments, matching quotes with the
+ writer's ``\\"``/``\\\\`` escapes reversed — the same semantics as
+ ``hermes_cli.config._parse_env_value``) but never mutates the process
+ environment — that isolation is the whole point.
Encoding is ``utf-8-sig`` so a leading UTF-8 BOM (Windows Notepad /
PowerShell ``Set-Content -Encoding UTF8``) does not prefix the first
@@ -195,6 +197,14 @@ def load_env_file(env_path: Path) -> Dict[str, str]:
except (FileNotFoundError, OSError, UnicodeDecodeError):
return secrets
+ # Parse values with the canonical Hermes parser: save_env_value
+ # escapes " and \ inside double quotes, and every other reader
+ # (load_env, python-dotenv) reverses those escapes. Stripping only
+ # the outer quotes here would corrupt credentials containing "
+ # or \ — they work interactively but fail in scoped (cron /
+ # multiplex) resolution.
+ from hermes_cli.config import _parse_env_value
+
for raw in text.splitlines():
line = raw.strip()
if not line or line.startswith("#"):
@@ -207,10 +217,7 @@ def load_env_file(env_path: Path) -> Dict[str, str]:
key = key.strip()
if not key:
continue
- value = value.strip()
- if len(value) >= 2 and value[0] == value[-1] and value[0] in ("'", '"'):
- value = value[1:-1]
- secrets[key] = value
+ secrets[key] = _parse_env_value(value)
return secrets
diff --git a/tests/agent/test_secret_scope.py b/tests/agent/test_secret_scope.py
index 29f82a334db..770921cbde6 100644
--- a/tests/agent/test_secret_scope.py
+++ b/tests/agent/test_secret_scope.py
@@ -113,6 +113,30 @@ class TestScopeIsolation:
class TestEnvFileParsing:
"""load_env_file parses without mutating os.environ."""
+ def test_load_env_file_unescapes_quoted_values(self, tmp_path):
+ """Values written by save_env_value must round-trip byte-exactly.
+
+ Regression: load_env_file stripped only the outer quotes, leaving
+ the writer's \\" and \\\\ escapes literal — credentials containing
+ '\"' or '\\' worked interactively but were corrupted under scoped
+ (cron / multiplex) resolution.
+ """
+ from hermes_cli.config import _quote_env_value
+
+ original = 'tok"en\\with spaces'
+ (tmp_path / ".env").write_text(f"MY_TOKEN={_quote_env_value(original)}\n")
+ assert ss.load_env_file(tmp_path / ".env") == {"MY_TOKEN": original}
+
+ def test_load_env_file_single_quotes_and_plain_values(self, tmp_path):
+ (tmp_path / ".env").write_text(
+ "PLAIN=abc123\nQUOTED='single quoted'\nEMPTY=\n"
+ )
+ assert ss.load_env_file(tmp_path / ".env") == {
+ "PLAIN": "abc123",
+ "QUOTED": "single quoted",
+ "EMPTY": "",
+ }
+