Agent Zero AI framework
Limit runtime secret redaction to credentials
Alessandrofd795bda8216
diff --git a/helpers/secrets.py b/helpers/secrets.pyindex 8bd6f58f2..ba038aa21 100644--- a/helpers/secrets.py+++ b/helpers/secrets.py@@ -17,6 +17,12 @@ if TYPE_CHECKING:# New alias-based placeholder format §§secret(KEY)ALIAS_PATTERN = r"§§secret\(([A-Za-z_][A-Za-z0-9_]*)\)"DEFAULT_SECRETS_FILE = "usr/secrets.env"+_RUNTIME_CREDENTIAL_KEYS = {+ dotenv.KEY_AUTH_LOGIN,+ dotenv.KEY_AUTH_PASSWORD,+ dotenv.KEY_RFC_PASSWORD,+ dotenv.KEY_ROOT_PASSWORD,+}def alias_for_key(key: str, placeholder: str = "§§secret({key})") -> str:@@ -160,6 +166,14 @@ class SecretsManager:content = ""self._raw_snapshots[path] = content+ if path == dotenv.get_dotenv_file_path():+ content = "\n".join(+ line.raw+ for line in self.parse_env_lines(content)+ if line.type != "pair"+ or (line.key or "").upper().startswith("API_KEY_")+ or (line.key or "").upper() in _RUNTIME_CREDENTIAL_KEYS+ )parts.append(content)combined = "\n".join(parts)diff --git a/helpers/secrets.py.dox.md b/helpers/secrets.py.dox.mdindex e0db5017b..e22f58b14 100644--- a/helpers/secrets.py.dox.md+++ b/helpers/secrets.py.dox.md@@ -29,12 +29,12 @@- `get_secrets_manager(context: 'AgentContext|None'=...) -> SecretsManager`- `get_project_secrets_manager(project_name: str, merge_with_global: bool=...) -> SecretsManager`- `get_default_secrets_manager() -> SecretsManager`-- Notable constants/configuration names: `ALIAS_PATTERN`, `DEFAULT_SECRETS_FILE`.+- Notable constants/configuration names: `ALIAS_PATTERN`, `DEFAULT_SECRETS_FILE`, `_RUNTIME_CREDENTIAL_KEYS`.## Runtime Contracts- Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together.-- The agent-facing `get_secrets_manager` masks and unpacks values from `usr/.env`, the global `usr/secrets.env`, and the active project's `secrets.env`; `get_default_secrets_manager` remains scoped to the single writable `usr/secrets.env` file.+- The agent-facing `get_secrets_manager` masks and unpacks `API_KEY_*` and login/password credentials from `usr/.env`, every value from the global `usr/secrets.env`, and every value from the active project's `secrets.env`; ordinary runtime settings are not treated as secrets. `get_default_secrets_manager` remains scoped to the single writable `usr/secrets.env` file.- Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change.- Observed side-effect areas: filesystem reads, filesystem writes, filesystem deletion, WebSocket state, settings/state persistence, secret handling.- Imported dependency areas include: `dataclasses`, `dotenv.parser`, `helpers`, `helpers.errors`, `helpers.extension`, `io`, `os`, `re`, `threading`, `time`, `typing`.@@ -54,6 +54,7 @@- Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers.- Related tests observed by source search:+ - `tests/test_secrets.py`- `tests/test_plugin_scan_prompt.py`- `tests/test_print_style.py`- `tests/test_time_travel.py`diff --git a/tests/test_secrets.py b/tests/test_secrets.pyindex a351dfcb8..c18f4a493 100644--- a/tests/test_secrets.py+++ b/tests/test_secrets.py@@ -6,18 +6,32 @@ class _Context:return None-def test_agent_secret_manager_masks_runtime_dotenv_values(monkeypatch):+def test_agent_secret_manager_masks_runtime_credentials_only(monkeypatch):monkeypatch.setattr(secrets.SecretsManager, "_instances", {})monkeypatch.setattr(secrets.dotenv, "get_dotenv_file_path", lambda: "usr/.env")contents = {"usr/secrets.env": "PROJECT_SECRET=project-value\n",- "usr/.env": "LLM_API_KEY=llm-secret-value\n",+ "usr/.env": (+ "API_KEY_OPENAI=llm-secret-value\n"+ "ANONYMIZED_TELEMETRY=false\n"+ "DEFAULT_USER_TIMEZONE=Europe/Rome\n"+ ),}monkeypatch.setattr(secrets.files, "read_file", contents.__getitem__)manager = secrets.get_secrets_manager(_Context())- assert manager.mask_values("key=llm-secret-value") == (- "key=§§secret(LLM_API_KEY)"+ assert manager.mask_values(+ "project-value; key=llm-secret-value; avoid falsely accusing a utility"+ ) == (+ "§§secret(PROJECT_SECRET); key=§§secret(API_KEY_OPENAI); "+ "avoid falsely accusing a utility")+ assert "ANONYMIZED_TELEMETRY" not in manager.get_secrets_for_prompt()++ stream_filter = manager.create_streaming_filter()+ assert stream_filter.process_chunk("avoid falsely accusing a utility") == (+ "avoid falsely accusing a utility"+ )+ assert stream_filter.finalize() == ""