Skip to content

The agent that grows with you

fix(buzz): scope owner credentials per profile

Alex P. Günsberg8c09c39530b5
diff --git a/plugins/platforms/buzz/adapter.py b/plugins/platforms/buzz/adapter.py
index 6f91909a327..af3b3d8823b 100644
--- a/plugins/platforms/buzz/adapter.py
+++ b/plugins/platforms/buzz/adapter.py
@@ -330,53 +330,66 @@ def _resolve_cli_path(configured: str = "") -> str:
return str(fallback) if fallback.is_file() else ""
-def _resolve_private_key(extra: Optional[dict] = None) -> str:
- """Resolve the Nostr private key: env first, then a credentials JSON.
-
- NEVER log the return value.
- """
- key = _get_scoped_secret("BUZZ_PRIVATE_KEY", "").strip()
- if key:
- return key
- _creds_raw = _scoped_platform_setting("BUZZ_CREDENTIALS_FILE", extra, "credentials_file")
- configured = str(_creds_raw or "").strip() or (extra or {}).get("credentials_file", "")
+def _credentials_candidates(extra: Optional[dict] = None) -> List[Path]:
+ # Scope-aware read (#98738/#95216): inside a secondary profile scope the
+ # scope is authoritative (a miss falls to the profile's own config extra,
+ # never the default profile's os.environ); unscoped reads keep env
+ # precedence plus the external-secret rung.
+ configured = str(_get_scoped_secret("BUZZ_CREDENTIALS_FILE", "") or "").strip() or str(
+ (extra or {}).get("credentials_file", "") or ""
+ ).strip()
if configured:
- candidates = [Path(configured).expanduser()]
- else:
- try:
- candidates = sorted(_DEFAULT_CREDENTIALS_DIR.glob("*credentials*.json"))
- except OSError:
- candidates = []
- for path in candidates:
+ return [Path(configured).expanduser()]
+ try:
+ return sorted(_DEFAULT_CREDENTIALS_DIR.glob("*credentials*.json"))
+ except OSError:
+ return []
+
+
+def _resolve_credentials_data(extra: Optional[dict] = None) -> dict:
+ """Load the first credential record containing a private key."""
+ for path in _credentials_candidates(extra):
try:
data = json.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError):
continue
if not isinstance(data, dict):
continue
- for field in ("nsec", "private_key_hex", "private_key"):
- value = data.get(field)
- if isinstance(value, str) and value.strip():
- return value.strip()
+ if any(isinstance(data.get(field), str) and data[field].strip() for field in ("nsec", "private_key_hex", "private_key")):
+ return data
+ return {}
+
+
+def _resolve_private_key(extra: Optional[dict] = None) -> str:
+ """Resolve the Nostr private key: scoped secret first, then credentials JSON.
+
+ NEVER log the return value.
+ """
+ key = str(_get_scoped_secret("BUZZ_PRIVATE_KEY", "") or "").strip()
+ if key:
+ return key
+ data = _resolve_credentials_data(extra)
+ for field in ("nsec", "private_key_hex", "private_key"):
+ value = data.get(field)
+ if isinstance(value, str) and value.strip():
+ return value.strip()
return ""
def _resolve_auth_tag(extra: Optional[dict] = None) -> str:
"""Resolve and validate the optional NIP-OA owner-attestation tag."""
- configured = os.getenv("BUZZ_AUTH_TAG", "").strip()
+ configured = str(_get_scoped_secret("BUZZ_AUTH_TAG", "") or "").strip()
if configured:
raw: Any = configured
else:
- credentials_file = os.getenv("BUZZ_CREDENTIALS_FILE", "").strip() or (extra or {}).get(
- "credentials_file", ""
- )
- if not credentials_file:
- return ""
- try:
- data = json.loads(Path(credentials_file).expanduser().read_text(encoding="utf-8"))
- except (OSError, ValueError):
+ credentials_file = str(_get_scoped_secret("BUZZ_CREDENTIALS_FILE", "") or "").strip() or str(
+ (extra or {}).get("credentials_file", "") or ""
+ ).strip()
+ direct_key = str(_get_scoped_secret("BUZZ_PRIVATE_KEY", "") or "").strip()
+ if direct_key and not credentials_file:
return ""
- if not isinstance(data, dict) or "auth_tag" not in data:
+ data = _resolve_credentials_data(extra)
+ if "auth_tag" not in data:
return ""
raw = data["auth_tag"]
diff --git a/tests/gateway/test_buzz_adapter.py b/tests/gateway/test_buzz_adapter.py
index fa2a348a13a..d1f0d0681c1 100644
--- a/tests/gateway/test_buzz_adapter.py
+++ b/tests/gateway/test_buzz_adapter.py
@@ -942,6 +942,34 @@ class TestCredentialResolution:
with pytest.raises(ValueError, match="auth tag"):
_resolve_auth_tag()
+ def test_scoped_credentials_and_tag_do_not_borrow_ambient_profile(self, monkeypatch, tmp_path):
+ from agent import secret_scope as ss
+
+ ambient_tag = ["auth", "a" * 64, "", "d" * 128]
+ scoped_tag = ["auth", "b" * 64, "", "c" * 128]
+ ambient = tmp_path / "ambient.json"
+ scoped = tmp_path / "scoped.json"
+ ambient.write_text(json.dumps({"nsec": "nsec1ambient", "auth_tag": ambient_tag}))
+ scoped.write_text(json.dumps({"nsec": "nsec1scoped", "auth_tag": scoped_tag}))
+ monkeypatch.setenv("BUZZ_CREDENTIALS_FILE", str(ambient))
+ monkeypatch.setenv("BUZZ_AUTH_TAG", json.dumps(ambient_tag))
+ ss.set_multiplex_active(True)
+ token = ss.set_secret_scope({"BUZZ_CREDENTIALS_FILE": str(scoped)})
+ try:
+ assert _resolve_private_key() == "nsec1scoped"
+ assert json.loads(_resolve_auth_tag()) == scoped_tag
+ finally:
+ ss.reset_secret_scope(token)
+ ss.set_multiplex_active(False)
+
+ def test_owner_auth_tag_uses_credentials_autodiscovery(self, monkeypatch, tmp_path):
+ tag = ["auth", "b" * 64, "", "c" * 128]
+ creds = tmp_path / "agent_credentials.json"
+ creds.write_text(json.dumps({"nsec": "nsec1fromfile", "auth_tag": tag}), encoding="utf-8")
+ monkeypatch.setattr(_buzz_mod, "_DEFAULT_CREDENTIALS_DIR", tmp_path)
+ assert _resolve_private_key() == "nsec1fromfile"
+ assert json.loads(_resolve_auth_tag()) == tag
+
# ── Env enablement / registration / standalone send ──────────────────────