The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(security): redact secrets from config file reads
KoNit-K0997a23e5719
diff --git a/agent/redact.py b/agent/redact.pyindex 5d0f9f2c6e7..c4f49b1f3a1 100644--- a/agent/redact.py+++ b/agent/redact.py@@ -756,6 +756,11 @@ _FILE_READ_COMMANDS = frozenset({"cat", "head", "tail", "type", "bat", "less", "more", "nl","zcat", "tac", "view", "batcat",})+_SECRET_BEARING_FILE_BASENAMES = frozenset({+ ".bashrc", ".bash_profile", ".bash_login", ".profile",+ ".zshrc", ".zprofile", ".zlogin", ".zshenv",+})+_TEXT_FILE_READ_COMMANDS = frozenset({"grep", "awk", "sed"})def _command_segments(command: str) -> list[str]:@@ -782,6 +787,39 @@ def _command_reads_env_file(command: str | None) -> bool:return False+def _is_secret_bearing_file_arg(arg: str) -> bool:+ """Recognize explicit Hermes config and standard shell startup paths."""+ path = arg.strip("\"'").replace("\\", "/")+ if "$" in path:+ return False+ parts = [part.lower() for part in path.split("/") if part]+ if not parts:+ return False+ if parts[-1] in _SECRET_BEARING_FILE_BASENAMES:+ return True+ return parts[-1] == "config.yaml" and ".hermes" in parts[:-1]+++def _command_reads_secret_bearing_file(command: str | None) -> bool:+ """True for direct stdout reads of known secret-bearing config files."""+ if not command or not isinstance(command, str):+ return False+ for seg in _command_segments(command):+ tokens = seg.split() # preserve Windows path separators; see _command_reads_env_file+ if not tokens:+ continue+ reader = tokens[0].rsplit("/", 1)[-1].lower()+ if reader in _FILE_READ_COMMANDS:+ if any(_is_secret_bearing_file_arg(arg) for arg in tokens[1:] if not arg.startswith("-")):+ return True+ continue+ if reader in _TEXT_FILE_READ_COMMANDS:+ positional = [arg for arg in tokens[1:] if not arg.startswith("-")]+ if any(_is_secret_bearing_file_arg(arg) for arg in positional[1:]):+ return True+ return False++def is_env_dump_command(command: str | None) -> bool:"""True if any pipeline/sequence segment starts with an _ENV_DUMP_COMMANDStoken. Conservative: unrecognized → False (callers fall back to code_file=True)."""@@ -821,11 +859,15 @@ def redact_for_egress(text: str) -> str:def redact_terminal_output(output: str, command: str | None = None, *, force: bool = False) -> str:"""Single redaction policy for ALL terminal-output surfaces: the ENV-assignment- pass runs only when ``command`` is an env dump or reads a ``.env`` file+ pass runs when ``command`` is an env dump or reads a secret-bearing file(otherwise code_file=True avoids false positives on source/config dumps)."""if not output:return output- code_file = not (is_env_dump_command(command) or _command_reads_env_file(command))+ code_file = not (+ is_env_dump_command(command)+ or _command_reads_env_file(command)+ or _command_reads_secret_bearing_file(command)+ )return redact_sensitive_text(output, force=force, code_file=code_file)diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.pyindex 16a4e132cd5..6c13d61a7ed 100644--- a/tests/agent/test_redact.py+++ b/tests/agent/test_redact.py@@ -968,6 +968,53 @@ class TestTerminalOutputRedaction:assert "abc123secret" not in redassert "export MISTRAL_API_KEY=*** # prod key" in red+ @pytest.mark.parametrize(+ ("command", "output", "secret"),+ [+ ("cat ~/.hermes/config.yaml", "api_key: hermesConfigSecret123", "hermesConfigSecret123"),+ (+ "head ~/.hermes/profiles/work/config.yaml",+ "provider.token=profileConfigSecret456",+ "profileConfigSecret456",+ ),+ ("tail ~/.bashrc", "export SERVICE_TOKEN=bashRcSecret789", "bashRcSecret789"),+ ("grep TOKEN ~/.zshrc", "SERVICE_TOKEN=zshRcSecret123", "zshRcSecret123"),+ (+ "awk -F= '/TOKEN/ {print $2}' ~/.profile",+ "SERVICE_TOKEN=profileSecret456",+ "profileSecret456",+ ),+ ("sed -n '1,20p' ~/.zprofile", "api_key: zprofileSecret789", "zprofileSecret789"),+ ],+ )+ def test_secret_bearing_file_commands_mask_assignments(self, command, output, secret):+ from agent.redact import redact_terminal_output++ assert secret not in redact_terminal_output(output, command)++ @pytest.mark.parametrize(+ "command",+ [+ "cat config.yaml",+ "cat /project/config.yaml",+ "cat ~/.hermes/config.example.yaml",+ "cat ~/.hermes/config.template.yaml",+ "cat ~/.bashrc.example",+ 'cat "$HERMES_HOME/config.yaml"',+ "grep TOKEN app.py",+ "awk '/TOKEN/' settings.yaml",+ "sed -n '1,20p' template.yaml",+ ],+ )+ def test_secret_bearing_file_detection_preserves_fail_open_controls(self, command):+ from agent.redact import redact_terminal_output++ output = "SERVICE_TOKEN=placeholder_value_here"+ assert redact_terminal_output(output, command) == output+ assert "realEnvSecret123" not in redact_terminal_output(+ "SERVICE_TOKEN=realEnvSecret123", "cat .env"+ )+def test_disabled_passes_through(self, monkeypatch):