Skip to content

The agent that grows with you

fix(redact): recognize quoted HERMES_HOME config reads

KoNit-K8ab9e79d0a73
diff --git a/agent/redact.py b/agent/redact.py
index c4f49b1f3a1..f18cb31831b 100644
--- a/agent/redact.py
+++ b/agent/redact.py
@@ -764,8 +764,35 @@ _TEXT_FILE_READ_COMMANDS = frozenset({"grep", "awk", "sed"})
def _command_segments(command: str) -> list[str]:
- """Pipeline/sequence segments of a shell command, stripped, empties dropped."""
- return [seg.strip() for seg in re.split(r"[|;&]+", command) if seg.strip()]
+ """Pipeline/sequence segments, split only on unquoted ``| ; &``.
+
+ Quote-aware so ``awk '{print $1; print $2}'`` / ``grep 'foo|bar'`` stay
+ one segment. Backslash is not an escape (Windows ``C:\\Users\\...``).
+ """
+ segments: list[str] = []
+ buf: list[str] = []
+ quote: str | None = None
+ for ch in command:
+ if quote:
+ buf.append(ch)
+ if ch == quote:
+ quote = None
+ continue
+ if ch in "'\"":
+ quote = ch
+ buf.append(ch)
+ continue
+ if ch in "|;&":
+ seg = "".join(buf).strip()
+ if seg:
+ segments.append(seg)
+ buf = []
+ continue
+ buf.append(ch)
+ seg = "".join(buf).strip()
+ if seg:
+ segments.append(seg)
+ return segments
def _command_reads_env_file(command: str | None) -> bool:
@@ -787,9 +814,18 @@ def _command_reads_env_file(command: str | None) -> bool:
return False
+_HERMES_HOME_PREFIXES = ("$HERMES_HOME/", "${HERMES_HOME}/")
+
+
def _is_secret_bearing_file_arg(arg: str) -> bool:
"""Recognize explicit Hermes config and standard shell startup paths."""
path = arg.strip("\"'").replace("\\", "/")
+ hermes_home = False
+ for prefix in _HERMES_HOME_PREFIXES:
+ if path.startswith(prefix):
+ path = path[len(prefix):]
+ hermes_home = True
+ break
if "$" in path:
return False
parts = [part.lower() for part in path.split("/") if part]
@@ -797,7 +833,9 @@ def _is_secret_bearing_file_arg(arg: str) -> bool:
return False
if parts[-1] in _SECRET_BEARING_FILE_BASENAMES:
return True
- return parts[-1] == "config.yaml" and ".hermes" in parts[:-1]
+ if parts[-1] != "config.yaml":
+ return False
+ return hermes_home or ".hermes" in parts[:-1]
def _command_reads_secret_bearing_file(command: str | None) -> bool:
diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.py
index 6c13d61a7ed..e86bbe9701d 100644
--- a/tests/agent/test_redact.py
+++ b/tests/agent/test_redact.py
@@ -985,6 +985,26 @@ class TestTerminalOutputRedaction:
"profileSecret456",
),
("sed -n '1,20p' ~/.zprofile", "api_key: zprofileSecret789", "zprofileSecret789"),
+ (
+ 'cat "$HERMES_HOME/config.yaml"',
+ "SERVICE_TOKEN=variablePathSecret123456789",
+ "variablePathSecret123456789",
+ ),
+ (
+ 'cat "${HERMES_HOME}/config.yaml"',
+ "SERVICE_TOKEN=variablePathSecret123456789",
+ "variablePathSecret123456789",
+ ),
+ (
+ "awk '{print $1; print $2}' ~/.bashrc",
+ "export SERVICE_TOKEN=awkQuotedSecret123",
+ "awkQuotedSecret123",
+ ),
+ (
+ "grep 'foo|bar' ~/.hermes/config.yaml",
+ "SERVICE_TOKEN=grepQuotedSecret456",
+ "grepQuotedSecret456",
+ ),
],
)
def test_secret_bearing_file_commands_mask_assignments(self, command, output, secret):
@@ -1000,7 +1020,7 @@ class TestTerminalOutputRedaction:
"cat ~/.hermes/config.example.yaml",
"cat ~/.hermes/config.template.yaml",
"cat ~/.bashrc.example",
- 'cat "$HERMES_HOME/config.yaml"',
+ 'cat "$OTHER/config.yaml"',
"grep TOKEN app.py",
"awk '/TOKEN/' settings.yaml",
"sed -n '1,20p' template.yaml",