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(secrets): harden the security-CLI vault after review — stderr redaction, refresh-before-mutate, wiring test

Personal Jarvis Maintainerb85dec30b776
diff --git a/jarvis/core/keychain_bundle.py b/jarvis/core/keychain_bundle.py
index d2a88b08cf..62e4334e32 100644
--- a/jarvis/core/keychain_bundle.py
+++ b/jarvis/core/keychain_bundle.py
@@ -75,6 +75,15 @@ _SAFE_CLI_TOKEN = re.compile(r"^[A-Za-z0-9._-]+$")
# The written payload is standard base64 (never starts with ``-``, contains no
# whitespace/quotes), so it is safe on a ``security -i`` line after ``-w``.
_SAFE_CLI_VALUE = re.compile(r"^[A-Za-z0-9+/=]+$")
+# Any base64-ish run long enough to be (part of) a secret payload. Applied to
+# CLI stderr before it may enter an exception message: the write command's
+# input line carries the WHOLE encoded vault, and a CLI parser echoing its
+# failing input into stderr must never let that reach a log line (AP-2/AP-12).
+_REDACT_PATTERN = re.compile(r"[A-Za-z0-9+/=]{16,}")
+
+
+def _redact(text: str) -> str:
+ return _REDACT_PATTERN.sub("<redacted>", text)
class SecurityCliVaultError(RuntimeError):
@@ -133,7 +142,8 @@ class SecurityCliVault:
if proc.returncode == _SECURITY_NOT_FOUND_EXIT:
return None
raise SecurityCliVaultError(
- f"security CLI read exited {proc.returncode}: {proc.stderr.strip()!r}"
+ f"security CLI read exited {proc.returncode}: "
+ f"{_redact(proc.stderr.strip())!r}"
)
def write(self, service: str, account: str, value: str) -> None:
@@ -150,6 +160,11 @@ class SecurityCliVault:
"vault payload is not clean base64; refusing CLI interpolation"
)
self.delete(service, account)
+ # ``-U`` is deliberate defense-in-depth for the case where the delete
+ # above silently failed to remove a stuck item: the in-place update
+ # may then need the OLD item's ACL consent (one dialog) or fail
+ # cleanly into the inner-backend fallback — both beat a hard
+ # duplicate-item error.
command = (
f"add-generic-password -U -A -s {service} -a {account} -w {value}\n"
)
@@ -166,7 +181,8 @@ class SecurityCliVault:
raise SecurityCliVaultError(f"security CLI write failed: {exc}") from exc
if proc.returncode != 0:
raise SecurityCliVaultError(
- f"security CLI write exited {proc.returncode}: {proc.stderr.strip()!r}"
+ f"security CLI write exited {proc.returncode}: "
+ f"{_redact(proc.stderr.strip())!r}"
)
# ``security -i`` can swallow a failing sub-command's exit status;
# trust only a verified read-back.
@@ -290,15 +306,24 @@ class DarwinBundleKeyringBackend(_KeyringBackendBase): # type: ignore[valid-typ
return None, False
return (parsed, True) if isinstance(parsed, dict) else (None, False)
- def _load_locked(self, service: str) -> dict[str, str]:
+ def _load_locked(self, service: str, *, refresh: bool = False) -> dict[str, str]:
"""Return the parsed vault dict for *service*, loading it once.
Caller must hold ``self._lock``. On a JSON-decode error, sets
``self._bundle_unusable`` and returns an empty dict without touching
the stored item.
+
+ ``refresh=True`` bypasses the cache and re-reads the stored item:
+ every mutation path uses it so a concurrent WRITE from another
+ process (main app, worker, CLI) is picked up before this process's
+ read-modify-write, shrinking the cross-process lost-update window
+ from "since this process first read the vault" to milliseconds.
+ There is still no cross-process lock — two writes landing inside the
+ same window remain last-writer-wins, the same residual race the
+ pre-vault per-item store had for a single slot.
"""
cached = self._cache.get(service)
- if cached is not None:
+ if cached is not None and not refresh:
return cached
raw: str | None = None
used_cli = False
@@ -331,10 +356,12 @@ class DarwinBundleKeyringBackend(_KeyringBackendBase): # type: ignore[valid-typ
return {}
bundle = {str(k): str(v) for k, v in parsed.items()}
self._cache[service] = bundle
- if used_cli and not was_base64:
+ if used_cli and not was_base64 and not refresh:
# A keyring-written legacy vault, just read through the CLI (the
# one-time consent dialog, if any, already happened). Rewrite it
# with the any-application ACL so no process ever prompts again.
+ # Skipped on a refresh load: those callers persist right after
+ # anyway, which performs the same format upgrade.
try:
self._save_locked(service, bundle)
except Exception: # noqa: BLE001 -- upgrade is best-effort
@@ -415,7 +442,7 @@ class DarwinBundleKeyringBackend(_KeyringBackendBase): # type: ignore[valid-typ
def _migrate_legacy(self, service: str, key: str, value: str) -> None:
with self._lock:
- bundle = self._load_locked(service)
+ bundle = self._load_locked(service, refresh=True)
if self._bundle_unusable:
# Nothing to migrate into; leave the legacy item alone.
return
@@ -437,7 +464,7 @@ class DarwinBundleKeyringBackend(_KeyringBackendBase): # type: ignore[valid-typ
self._inner.set_password(service, key, value)
return
with self._lock:
- bundle = self._load_locked(service)
+ bundle = self._load_locked(service, refresh=True)
if self._bundle_unusable:
self._inner.set_password(service, key, value)
return
@@ -454,7 +481,7 @@ class DarwinBundleKeyringBackend(_KeyringBackendBase): # type: ignore[valid-typ
removed_from_bundle = False
with self._lock:
- bundle = self._load_locked(service)
+ bundle = self._load_locked(service, refresh=True)
if self._bundle_unusable:
self._inner.delete_password(service, key)
return
diff --git a/tests/unit/core/test_keychain_bundle.py b/tests/unit/core/test_keychain_bundle.py
index 3e75a4c834..0fcf1d6bef 100644
--- a/tests/unit/core/test_keychain_bundle.py
+++ b/tests/unit/core/test_keychain_bundle.py
@@ -440,6 +440,85 @@ def test_security_cli_vault_rejects_non_base64_payloads() -> None:
# ---------------------------------------------------------------------------
+def test_cli_error_messages_redact_base64_payload_runs() -> None:
+ """The write command's input line carries the WHOLE encoded vault; a CLI
+ error echoing its input must never leak it into an exception/log line."""
+ from jarvis.core.keychain_bundle import _redact
+
+ secret_b64 = base64.b64encode(b'{"anthropic_api_key": "sk-ant-tops3cret"}')
+ stderr = f"security: parse error near {secret_b64.decode()} on line 1"
+ assert secret_b64.decode() not in _redact(stderr)
+ assert "<redacted>" in _redact(stderr)
+
+
+def test_mutations_refresh_from_store_so_concurrent_writes_survive(
+ inner: FakeInnerBackend, cli: FakeSecurityCli
+) -> None:
+ """Two wrapper instances (two processes) writing DIFFERENT keys: the
+ second write must pick up the first one's key from the store instead of
+ clobbering the vault with its own stale snapshot."""
+ first = DarwinBundleKeyringBackend(inner, cli=cli)
+ second = DarwinBundleKeyringBackend(inner, cli=cli)
+
+ # Both processes load the (empty) vault first.
+ assert first.get_password(SERVICE, "anthropic_api_key") is None
+ assert second.get_password(SERVICE, "groq_api_key") is None
+
+ first.set_password(SERVICE, "anthropic_api_key", "sk-ant-1")
+ second.set_password(SERVICE, "groq_api_key", "gsk-2")
+
+ decoded = json.loads(
+ base64.b64decode(cli.items[(SERVICE, VAULT_ACCOUNT)], validate=True)
+ )
+ assert decoded == {"anthropic_api_key": "sk-ant-1", "groq_api_key": "gsk-2"}, (
+ "the second writer must refresh-load before mutating, not overwrite "
+ "the vault with its stale process-local snapshot"
+ )
+
+
+def test_ensure_keyring_backend_wires_the_cli_store_on_darwin(
+ inner: FakeInnerBackend, cli: FakeSecurityCli, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """The production wiring itself: on darwin, ``_ensure_keyring_backend``
+ must install the wrapper WITH the security-CLI store. Dropping the
+ ``cli=`` kwarg (or the factory silently returning ``None``) would
+ reopen BUG-103 while every direct-construction test stays green."""
+ import sys
+
+ import keyring
+ import keyring.backend
+
+ from jarvis.core import config as cfg
+ from jarvis.core import keychain_bundle as kb
+
+ class _FakePlatformBackend(keyring.backend.KeyringBackend):
+ priority = 5 # type: ignore[assignment]
+
+ def get_password(self, service: str, key: str) -> str | None:
+ return inner.get_password(service, key)
+
+ def set_password(self, service: str, key: str, value: str) -> None:
+ inner.set_password(service, key, value)
+
+ def delete_password(self, service: str, key: str) -> None:
+ inner.delete_password(service, key)
+
+ original = keyring.get_keyring()
+ try:
+ keyring.set_keyring(_FakePlatformBackend())
+ monkeypatch.setattr(sys, "platform", "darwin")
+ monkeypatch.setattr(kb, "darwin_security_cli_vault", lambda: cli)
+ monkeypatch.setattr(cfg, "_KEYRING_BACKEND_READY", False)
+
+ cfg._ensure_keyring_backend()
+
+ installed = keyring.get_keyring()
+ assert isinstance(installed, DarwinBundleKeyringBackend)
+ assert installed._cli is cli
+ finally:
+ keyring.set_keyring(original)
+
+
def test_wrapper_is_accepted_by_keyring_set_keyring(
inner: FakeInnerBackend,
) -> None: