Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(security): keep tokens out of logged tracebacks (#789)

Ruben Lütke653c5f1778ee
diff --git a/dodaios/__init__.py b/dodaios/__init__.py
index 9f81656179..c080f88951 100644
--- a/dodaios/__init__.py
+++ b/dodaios/__init__.py
@@ -2,17 +2,28 @@
__version__ = "4.0.1"
+import os as _os
+
+# loguru's "diagnose" mode prints every local variable of every frame in a
+# logged traceback, tokens and API keys included, and it is on by default for
+# its stderr sink and every logger.add() that does not say otherwise. Turn it
+# off for the whole process (and the helper processes, which inherit the
+# environment) before anything imports loguru. A developer can still opt in
+# by setting LOGURU_DIAGNOSE=YES.
+_os.environ.setdefault("LOGURU_DIAGNOSE", "NO")
+del _os
+
# A variable written as ``DODAIOS_<NAME>`` is moved to the ``JARVIS_<NAME>``
# spelling the code reads. Done here, on the first import of the package, so
# every entry point (the app, the CLIs, the MCP servers, each helper process)
# sees it before any reader runs. A dictionary pass; see
# dodaios/core/env_prefix.py for the precedence.
-from dodaios.core.env_prefix import adopt_environment as _adopt_environment
+from dodaios.core.env_prefix import adopt_environment as _adopt_environment # noqa: E402
# Every httpx client in the process shares one parsed CA bundle instead of
# re-parsing it (~200 ms of CPU) per client. Patches httpx lazily, the moment
# it is first imported; importing ``jarvis`` itself still loads nothing heavy.
-from dodaios.core.shared_tls import install_when_imported as _install_shared_tls
+from dodaios.core.shared_tls import install_when_imported as _install_shared_tls # noqa: E402
_adopt_environment()
_install_shared_tls()
diff --git a/dodaios/agent_chat/turn_host.py b/dodaios/agent_chat/turn_host.py
index f086c4e1a7..e7d6216be8 100644
--- a/dodaios/agent_chat/turn_host.py
+++ b/dodaios/agent_chat/turn_host.py
@@ -597,7 +597,14 @@ def _configure_logging(log_path: Path | None) -> None:
if log_path is None:
return
log_path.parent.mkdir(parents=True, exist_ok=True)
- logger.add(str(log_path), level="INFO", rotation="2 MB", retention=2, encoding="utf-8")
+ logger.add(
+ str(log_path),
+ level="INFO",
+ rotation="2 MB",
+ retention=2,
+ encoding="utf-8",
+ diagnose=False, # never dump frame locals (tokens, keys) into the log
+ )
def main(argv: list[str] | None = None) -> int:
diff --git a/dodaios/self_build/code/github.py b/dodaios/self_build/code/github.py
index f285d910a9..37e90d0567 100644
--- a/dodaios/self_build/code/github.py
+++ b/dodaios/self_build/code/github.py
@@ -267,11 +267,31 @@ def _first_error(payload: dict[str, Any]) -> str:
return ""
+class _Secret:
+ """Holds the token so a logged traceback or ``repr`` never shows it."""
+
+ __slots__ = ("_value",)
+
+ def __init__(self, value: str) -> None:
+ self._value = value
+
+ def reveal(self) -> str:
+ return self._value
+
+ def __repr__(self) -> str:
+ return "<token redacted>"
+
+ __str__ = __repr__
+
+
class GitHubApi:
""":class:`GitHub` over GraphQL with the person's token."""
def __init__(self, token: str) -> None:
- self._token = token
+ self._token = _Secret(token)
+
+ def __repr__(self) -> str:
+ return "GitHubApi(<token redacted>)"
@classmethod
def connect(cls) -> GitHubApi | None:
@@ -279,7 +299,7 @@ class GitHubApi:
return cls(cred.token) if cred is not None else None
def _q(self, query: str, variables: dict[str, Any] | None = None) -> dict[str, Any]:
- return github_link.graphql(self._token, query, variables or {})
+ return github_link.graphql(self._token.reveal(), query, variables or {})
def viewer(self) -> Viewer:
data = (self._q(_VIEWER).get("data") or {}).get("viewer") or {}
diff --git a/dodaios/terminal/pty_host.py b/dodaios/terminal/pty_host.py
index ec28f8246f..b4baaf1d9f 100644
--- a/dodaios/terminal/pty_host.py
+++ b/dodaios/terminal/pty_host.py
@@ -498,6 +498,7 @@ def _configure_logging(log_path: Path | None) -> None:
retention=2,
encoding="utf-8",
enqueue=False,
+ diagnose=False, # never dump frame locals (tokens, keys) into the log
)
diff --git a/tests/unit/core/test_log_secret_redaction.py b/tests/unit/core/test_log_secret_redaction.py
new file mode 100644
index 0000000000..fa11e62de0
--- /dev/null
+++ b/tests/unit/core/test_log_secret_redaction.py
@@ -0,0 +1,61 @@
+"""A logged traceback never prints a token held in a local variable."""
+
+from __future__ import annotations
+
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+from dodaios.self_build.code import github
+
+_REPO = Path(__file__).resolve().parents[3]
+
+_SCRIPT = """
+import dodaios # must come first: it switches loguru's diagnose mode off
+from loguru import logger
+
+def graphql(token, query):
+ raise RuntimeError("GitHub could not be reached.")
+
+def call_github():
+ token = "ghp_" + "S3CRET" * 6
+ graphql(token, "query { viewer { login } }")
+
+try:
+ call_github()
+except RuntimeError:
+ logger.opt(exception=True).warning("pass failed")
+"""
+
+
+def test_logged_traceback_hides_frame_locals() -> None:
+ env = {k: v for k, v in os.environ.items() if k != "LOGURU_DIAGNOSE"}
+ env["PYTHONPATH"] = str(_REPO)
+ result = subprocess.run( # noqa: S603 - fixed interpreter and script
+ [sys.executable, "-c", _SCRIPT],
+ capture_output=True,
+ text=True,
+ env=env,
+ cwd=str(_REPO),
+ timeout=120,
+ )
+ assert "pass failed" in result.stderr
+ assert "RuntimeError" in result.stderr
+ assert "S3CRET" not in result.stderr
+
+
+def test_github_api_never_shows_its_token(monkeypatch) -> None:
+ seen: list[str] = []
+
+ def fake_graphql(token: str, query: str, variables: dict) -> dict:
+ seen.append(token)
+ return {"data": {"viewer": {"login": "me", "organizations": {"nodes": []}}}}
+
+ monkeypatch.setattr(github.github_link, "graphql", fake_graphql)
+ api = github.GitHubApi("ghp_realtoken")
+ assert "ghp_realtoken" not in repr(api)
+ assert "ghp_realtoken" not in repr(vars(api) if hasattr(api, "__dict__") else api._token)
+ assert "ghp_realtoken" not in str(api._token)
+ api.viewer()
+ assert seen == ["ghp_realtoken"]