The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(security): make the credential_ref gate order structural and name-free
Maciej Myszkiewicz05c982e75d88
diff --git a/src/openhuman/security/credentials/credential_ref.rs b/src/openhuman/security/credentials/credential_ref.rsindex 5d934752d5..a733a5ec15 100644--- a/src/openhuman/security/credentials/credential_ref.rs+++ b/src/openhuman/security/credentials/credential_ref.rs@@ -23,16 +23,29 @@//! 1. **Consent.** [`keyring_consent::policy::check_secret_access`] must return//! [`PolicyDecision::Proceed`]. A prompt that has not been answered is not a//! missing credential, and conflating the two would train an operator to-//! "fix" a pending consent dialog by editing config.+//! "fix" a pending consent dialog by editing config. An *unanswered* prompt+//! and a *declined* one are also reported apart+//! ([`CredentialRefError::ConsentPending`] vs+//! [`CredentialRefError::ConsentDenied`]): telling an operator who already+//! said no to wait for a dialog describes a state that will never arrive.//! 2. **Availability.** A host with no usable keychain backend reports that as//! itself rather than as a missing entry — the same order//! `web3::wallet`'s `keychain_load_mnemonic` uses.//! 3. **Absence.** Only then is a `None` from the keychain a genuine//! "not configured".//!+//! The order is enforced structurally, not by convention: [`preflight`] takes+//! availability as a **closure**, so a refused consent decision returns before+//! the probe can run. That matters because the probe is a real backend+//! round-trip on its first call (`keyring::ops`' availability cache starts+//! empty) — evaluating it eagerly would touch the keychain on exactly the paths+//! this gate exists to stop.+//!//! ## Nothing here may reach an operator-facing string//!-//! [`CredentialRefError`] carries **no name and no secret**, and+//! [`CredentialRefError`] carries **no name and no secret** — not the entry+//! name, and not the scheme half either, which is operator-typed text rather+//! than fixed vocabulary (`sk-abc:123` parses as the scheme `sk-abc`). And//! [`CredentialRef`]'s `Debug` redacts the name. That is load-bearing rather//! than decorative: `MemoryDriverConfig`'s own doc requires the credential//! reference to stay out of `Debug`/error output (plan-memory.md §7, Tier 3),@@ -40,11 +53,19 @@//! `subsystems_status` — pinned by//! `fallback_reason_never_contains_credential_ref_or_endpoint`.//!-//! The sharp edge is [`KeyringError`], whose own `Display` interpolates the-//! key: `"OS keychain error for key '{key}': {source}"`. Propagating one-//! verbatim into a bind failure would leak the very name this module exists to-//! keep out of that string, so backend failures are deliberately mapped to a-//! name-free variant and the detail is logged instead.+//! The sharp edge is [`KeyringError`](keyring::KeyringError), whose own+//! `Display` interpolates the key (`"OS keychain error for key '{key}':+//! {source}"`) — and so does its `diagnostic()`, which is `{self:?}`.+//! Propagating or logging one verbatim would leak the very name this module+//! exists to keep out of that string, so backend failures are mapped to a+//! name-free variant and logged through [`keyring_error_kind`], which yields a+//! fixed label and nothing else.+//!+//! **Scope of that promise.** It covers this module's errors and this module's+//! logs. `keyring::get` itself logs its `key=` argument at `debug`, as the+//! whole keyring surface has always done for its fixed-name callers; narrowing+//! that is a shared-kernel decision affecting every subsystem in kernel.md §5,+//! not something to settle here.use zeroize::Zeroizing;@@ -78,23 +99,31 @@ pub enum CredentialRefError {/// The scheme is not one this build understands.///- /// The scheme itself is a fixed vocabulary word, not user data, so it is- /// safe to name; the entry name after it is not and is never included.- #[error("credential_ref scheme '{scheme}' is not supported (expected \"{KEYCHAIN_SCHEME}\")")]- UnsupportedScheme {- /// The offending scheme, lowercased.- scheme: String,- },+ /// The offending scheme is deliberately **not** carried. It is whatever+ /// text precedes the first colon in a hand-edited `config.toml`, so a+ /// mistake such as pasting a raw secret (`sk-abc:123`) would otherwise put+ /// its first fragment into an operator-facing string. With exactly one+ /// scheme defined, naming the expected value is the actionable half.+ #[error("credential_ref scheme is not supported (expected \"{KEYCHAIN_SCHEME}:<name>\")")]+ UnsupportedScheme,/// The scheme was valid but the name after it was empty.#[error("credential_ref has an empty name after \"{KEYCHAIN_SCHEME}:\"")]EmptyName,/// Keychain access is gated behind a consent prompt that has not been- /// answered. Distinct from [`Self::Unavailable`] on purpose.+ /// answered. Distinct from [`Self::Unavailable`] on purpose, and from+ /// [`Self::ConsentDenied`]: this one resolves itself once the operator+ /// answers.#[error("keychain access is pending user consent")]ConsentPending,+ /// The operator declined keychain access. Distinct from+ /// [`Self::ConsentPending`] because there is no dialog left to answer —+ /// reporting it as "pending" describes a state that will never arrive.+ #[error("keychain access was declined by the user")]+ ConsentDenied,+/// No usable keychain backend on this host.#[error("no keychain backend is available on this host")]Unavailable,@@ -112,25 +141,65 @@ pub enum CredentialRefError {/// The two gates that run *before* the keychain is touched, as a pure/// function of what they observed.///-/// Split out for the same reason `memory::binding::admit` is pure: the rule-/// worth pinning is the **order** — a pending consent prompt must be reported-/// as consent, not as an unavailable backend, and neither may be reported as a-/// missing entry. That ordering is what stops an operator "fixing" an-/// unanswered dialog by editing `config.toml`, and it is testable here without-/// a keychain, a consent store, or a booted core.+/// Split out for the same reason `memory::binding::admit` is: the rule worth+/// pinning is the **order** — a consent refusal must be reported as consent,+/// not as an unavailable backend, and neither may be reported as a missing+/// entry. That ordering is what stops an operator "fixing" an unanswered dialog+/// by editing `config.toml`, and it is testable here without a keychain, a+/// consent store, or a booted core.+///+/// `keychain_available` is a **closure, not a `bool`**, so the order is a+/// property of the code rather than of the call site. Passing the probe's+/// result would evaluate it before this function is entered, and+/// `keyring::is_available`'s first call is a real backend round-trip — the+/// keychain would be touched on precisely the paths consent just refused.+///+/// The decision is matched exhaustively rather than compared against+/// `Proceed`, so a variant added to [`PolicyDecision`] upstream becomes a+/// compile error here instead of being silently folded into "pending".////// Returns `None` when the caller may proceed to the lookup.#[must_use]-pub fn preflight(decision: PolicyDecision, keychain_available: bool) -> Option<CredentialRefError> {- if decision != PolicyDecision::Proceed {- return Some(CredentialRefError::ConsentPending);+pub fn preflight(+ decision: PolicyDecision,+ keychain_available: impl FnOnce() -> bool,+) -> Option<CredentialRefError> {+ match decision {+ PolicyDecision::ConsentRequired => return Some(CredentialRefError::ConsentPending),+ PolicyDecision::Declined => return Some(CredentialRefError::ConsentDenied),+ PolicyDecision::Proceed => {}}- if !keychain_available {+ if !keychain_available() {return Some(CredentialRefError::Unavailable);}None}+/// A fixed, name-free label for a [`KeyringError`](keyring::KeyringError).+///+/// Both that type's `Display` and its `diagnostic()` interpolate the key, so+/// neither may be logged from a path whose key is a credential reference. This+/// keeps the failure classified — which is the part worth having in a log —+/// while carrying nothing operator-typed.+///+/// The match is exhaustive so an upstream variant is a compile error here+/// rather than a silent fall-through to a wrong label. The five folded into+/// `"backend"` cannot arise from a `get`: they belong to `set`, migration and+/// random generation.+fn keyring_error_kind(e: &keyring::KeyringError) -> &'static str {+ use keyring::KeyringError as E;+ match e {+ E::Os { .. } => "os-backend",+ E::InvalidUtf8 { .. } => "invalid-utf8",+ E::Crypto(_) => "crypto",+ E::VerifyFailed { .. }+ | E::RandomGeneration(_)+ | E::Backend(_)+ | E::MigrationReadFailed { .. }+ | E::MigrationDeleteFailed { .. } => "backend",+ }+}+/// The scheme half of a parsed reference.#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]#[non_exhaustive]@@ -185,7 +254,8 @@ impl CredentialRef {////// [`CredentialRefError::Empty`], [`CredentialRefError::MissingScheme`],/// [`CredentialRefError::UnsupportedScheme`] or- /// [`CredentialRefError::EmptyName`]. None of them carry the name.+ /// [`CredentialRefError::EmptyName`]. None of them carry any part of the+ /// input — neither the name nor the scheme.pub fn parse(raw: &str) -> Result<Self, CredentialRefError> {let raw = raw.trim();if raw.is_empty() {@@ -198,13 +268,13 @@ impl CredentialRef {return Err(CredentialRefError::MissingScheme);};- let scheme_norm = scheme_raw.trim().to_ascii_lowercase();- let scheme = if scheme_norm == KEYCHAIN_SCHEME {+ // `eq_ignore_ascii_case` rather than lowercasing into a `String`: the+ // normalised scheme is never carried anywhere now, so materialising it+ // would only produce a value that must not be allowed to escape.+ let scheme = if scheme_raw.trim().eq_ignore_ascii_case(KEYCHAIN_SCHEME) {CredentialRefScheme::Keychain} else {- return Err(CredentialRefError::UnsupportedScheme {- scheme: scheme_norm,- });+ return Err(CredentialRefError::UnsupportedScheme);};let name = name_raw.trim();@@ -245,9 +315,11 @@ impl CredentialRef {////// # Errors///- /// [`CredentialRefError::ConsentPending`], [`CredentialRefError::Unavailable`],- /// [`CredentialRefError::NotFound`] or [`CredentialRefError::Backend`], in- /// that order of checking. No variant carries the name or the secret.+ /// [`CredentialRefError::ConsentPending`],+ /// [`CredentialRefError::ConsentDenied`],+ /// [`CredentialRefError::Unavailable`], [`CredentialRefError::NotFound`] or+ /// [`CredentialRefError::Backend`], in that order of checking. No variant+ /// carries the name or the secret.pub fn resolve(&self, user_id: &str) -> Result<Zeroizing<String>, CredentialRefError> {match self.scheme {CredentialRefScheme::Keychain => self.resolve_keychain(user_id),@@ -255,11 +327,13 @@ impl CredentialRef {}fn resolve_keychain(&self, user_id: &str) -> Result<Zeroizing<String>, CredentialRefError> {- if let Some(refusal) = preflight(policy::check_secret_access(), keyring::is_available()) {+ // `keyring::is_available` is passed unevaluated: a consent refusal must+ // return before the availability probe, whose first call is a real+ // backend round-trip. See `preflight`.+ if let Some(refusal) = preflight(policy::check_secret_access(), keyring::is_available) {log::debug!("{LOG_PREFIX} keychain access refused before lookup user_id={user_id} \- refusal={refusal} backend={}",- keyring::backend_name()+ refusal={refusal}");return Err(refusal);}@@ -273,11 +347,16 @@ impl CredentialRef {log::debug!("{LOG_PREFIX} no keychain entry for credential_ref user_id={user_id}");Err(CredentialRefError::NotFound)}- // Logged, not propagated: `KeyringError`'s Display interpolates the- // key, and this error's Display is allowed into operator-facing- // strings. See the module docs.+ // Classified, never rendered: `KeyringError`'s `Display` — and its+ // `diagnostic()`, which is `{self:?}` — both interpolate the key.+ // Formatting either here would write the credential name to a+ // retained warn-level log, which is the leak this module's+ // redaction exists to prevent. See the module docs.Err(e) => {- log::warn!("{LOG_PREFIX} keychain lookup failed user_id={user_id}: {e}");+ log::warn!(+ "{LOG_PREFIX} keychain lookup failed user_id={user_id} kind={}",+ keyring_error_kind(&e)+ );Err(CredentialRefError::Backend)}}diff --git a/src/openhuman/security/credentials/credential_ref_tests.rs b/src/openhuman/security/credentials/credential_ref_tests.rsindex 5141e33701..2babcbd0cb 100644--- a/src/openhuman/security/credentials/credential_ref_tests.rs+++ b/src/openhuman/security/credentials/credential_ref_tests.rs@@ -57,18 +57,8 @@ fn rejects_malformed_references() {("supermemory", MissingScheme),("keychain:", EmptyName),("keychain: ", EmptyName),- (- "vault:supermemory",- UnsupportedScheme {- scheme: "vault".to_string(),- },- ),- (- "ENV:SUPERMEMORY",- UnsupportedScheme {- scheme: "env".to_string(),- },- ),+ ("vault:supermemory", UnsupportedScheme),+ ("ENV:SUPERMEMORY", UnsupportedScheme),];for (raw, expected) in cases {let err = CredentialRef::parse(raw).expect_err(&format!("{raw:?} must not parse"));@@ -77,19 +67,34 @@ fn rejects_malformed_references() {}#[test]-fn an_unsupported_scheme_is_named_but_the_entry_name_is_not() {- // The scheme is fixed vocabulary and is useful to report; everything after- // it is operator data and must not travel with the error.- let err = CredentialRef::parse(&format!("vault:{SECRET_NAME}")).expect_err("vault is unknown");- let rendered = err.to_string();- assert!(- rendered.contains("vault"),- "scheme should be named: {rendered}"- );- assert!(- !rendered.contains(SECRET_NAME),- "entry name leaked into the error: {rendered}"- );+fn an_unsupported_scheme_echoes_back_no_part_of_the_input() {+ // The scheme half is operator-typed text, not fixed vocabulary: a config+ // holding a pasted secret rather than a handle splits into a "scheme" that+ // is a fragment of that secret. Neither half may travel with the error,+ // which is rendered into `subsystems_status`.+ let secret_shaped = "sk-live-abcdef123456";+ for raw in [+ format!("vault:{SECRET_NAME}"),+ format!("{secret_shaped}:tail"),+ ] {+ let rendered = CredentialRef::parse(&raw)+ .expect_err("scheme is not `keychain`")+ .to_string();+ assert!(+ !rendered.contains(SECRET_NAME) && !rendered.contains(secret_shaped),+ "input leaked into the error for {raw:?}: {rendered}"+ );+ assert!(+ !rendered.contains("vault"),+ "the offending scheme is not echoed back: {rendered}"+ );+ // Still actionable: there is exactly one valid scheme, so naming the+ // expectation is the half that helps.+ assert!(+ rendered.contains(KEYCHAIN_SCHEME),+ "the expected scheme should be named: {rendered}"+ );+ }}#[test]@@ -121,11 +126,10 @@ fn no_error_display_can_carry_a_name_or_a_secret() {let all = [CredentialRefError::Empty,CredentialRefError::MissingScheme,- CredentialRefError::UnsupportedScheme {- scheme: "vault".to_string(),- },+ CredentialRefError::UnsupportedScheme,CredentialRefError::EmptyName,CredentialRefError::ConsentPending,+ CredentialRefError::ConsentDenied,CredentialRefError::Unavailable,CredentialRefError::NotFound,CredentialRefError::Backend,@@ -142,30 +146,77 @@ fn no_error_display_can_carry_a_name_or_a_secret() {#[test]fn preflight_reports_consent_before_availability() {- // Order is the whole point. With consent pending AND no backend, the+ // Order is the whole point. With consent unresolved AND no backend, the// operator must be told about consent — telling them the keychain is// unavailable would send them to fix the wrong thing.assert_eq!(- preflight(PolicyDecision::ConsentRequired, false),+ preflight(PolicyDecision::ConsentRequired, || false),Some(CredentialRefError::ConsentPending));assert_eq!(- preflight(PolicyDecision::Declined, false),- Some(CredentialRefError::ConsentPending)+ preflight(PolicyDecision::Declined, || false),+ Some(CredentialRefError::ConsentDenied)+ );+}++#[test]+fn a_declined_prompt_is_not_reported_as_a_pending_one() {+ // `Declined` is an answer, not the absence of one. Reporting it as+ // "pending" tells an operator to wait for a dialog that will never appear.+ let denied = preflight(PolicyDecision::Declined, || true);+ assert_eq!(denied, Some(CredentialRefError::ConsentDenied));+ assert_ne!(denied, Some(CredentialRefError::ConsentPending));++ let pending = preflight(PolicyDecision::ConsentRequired, || true);+ assert_eq!(pending, Some(CredentialRefError::ConsentPending));++ // And the two must not read alike to whoever ends up seeing them.+ assert_ne!(+ denied.map(|e| e.to_string()),+ pending.map(|e| e.to_string()),+ "the declined and pending messages are indistinguishable");}+#[test]+fn preflight_does_not_probe_the_keychain_once_consent_has_refused() {+ // The real probe's first call is a backend round-trip, so evaluating it+ // eagerly would touch the keychain on exactly the paths consent refused.+ // Taking it as a closure makes that structural; this pins it.+ for decision in [PolicyDecision::ConsentRequired, PolicyDecision::Declined] {+ let probed = std::cell::Cell::new(false);+ let refusal = preflight(decision, || {+ probed.set(true);+ true+ });+ assert!(refusal.is_some(), "{decision:?} must refuse");+ assert!(+ !probed.get(),+ "availability was probed despite {decision:?} refusing first"+ );+ }++ // Conversely, a granted decision must reach the probe — otherwise the gate+ // would be vacuously "ordered" by never running.+ let probed = std::cell::Cell::new(false);+ let _ = preflight(PolicyDecision::Proceed, || {+ probed.set(true);+ true+ });+ assert!(probed.get(), "availability was never probed after Proceed");+}+#[test]fn preflight_reports_unavailability_only_once_consent_is_granted() {assert_eq!(- preflight(PolicyDecision::Proceed, false),+ preflight(PolicyDecision::Proceed, || false),Some(CredentialRefError::Unavailable));}#[test]fn preflight_allows_the_lookup_when_both_gates_pass() {- assert_eq!(preflight(PolicyDecision::Proceed, true), None);+ assert_eq!(preflight(PolicyDecision::Proceed, || true), None);}#[test]@@ -180,7 +231,7 @@ fn preflight_never_reports_a_missing_entry() {] {for available in [true, false] {assert_ne!(- preflight(decision, available),+ preflight(decision, || available),Some(CredentialRefError::NotFound),"preflight invented a NotFound for {decision:?}/available={available}");@@ -188,6 +239,55 @@ fn preflight_never_reports_a_missing_entry() {}}+#[test]+fn keyring_error_kinds_are_fixed_labels_that_carry_no_key() {+ // The classifier exists so a backend failure can be logged without+ // formatting a `KeyringError`, whose `Display` *and* `diagnostic()` both+ // interpolate the key. Every label must therefore be free of it.+ use crate::openhuman::security::keyring::KeyringError;++ let invalid_utf8 = String::from_utf8(vec![0xff]).expect_err("0xff is not utf-8");+ let cases: Vec<(KeyringError, &str)> = vec![+ (+ KeyringError::Os {+ key: SECRET_NAME.to_string(),+ source: ::keyring::Error::NoEntry,+ },+ "os-backend",+ ),+ (+ KeyringError::InvalidUtf8 {+ key: SECRET_NAME.to_string(),+ source: invalid_utf8,+ },+ "invalid-utf8",+ ),+ (KeyringError::Crypto(SECRET_NAME.to_string()), "crypto"),+ (+ KeyringError::VerifyFailed {+ key: SECRET_NAME.to_string(),+ },+ "backend",+ ),+ ];++ for (err, expected) in cases {+ let kind = keyring_error_kind(&err);+ assert_eq!(kind, expected);+ assert!(+ !kind.contains(SECRET_NAME),+ "classification leaked the key: {kind}"+ );+ // Guard the premise: these labels are worth having only because the+ // error's own renderings are unusable here.+ assert!(+ err.to_string().contains(SECRET_NAME) || err.diagnostic().contains(SECRET_NAME),+ "premise broken — {err:?} no longer carries the key, so re-check \+ whether the classifier is still needed"+ );+ }+}+#[test]fn scheme_round_trips_through_its_wire_spelling() {assert_eq!(CredentialRefScheme::Keychain.as_str(), KEYCHAIN_SCHEME);