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): restore user scope secret access
Steven Enamakel635aa3278fb6
diff --git a/crates/openhuman-core/src/security/credentials/ops/secrets.rs b/crates/openhuman-core/src/security/credentials/ops/secrets.rsnew file mode 100644index 0000000000..eb1c0df611--- /dev/null+++ b/crates/openhuman-core/src/security/credentials/ops/secrets.rs@@ -0,0 +1,31 @@+//! Encrypt / decrypt arbitrary secrets with the config's `SecretStore`.++use crate::config::Config;+use crate::rpc::RpcOutcome;+use crate::security::keyring::SecretStore;++pub(crate) fn secret_store_for_config(config: &Config) -> SecretStore {+ let data_dir = config+ .config_path+ .parent()+ .map_or_else(|| std::path::PathBuf::from("."), std::path::PathBuf::from);+ SecretStore::new(&data_dir, true)+}++pub async fn encrypt_secret(+ config: &Config,+ plaintext: &str,+) -> Result<RpcOutcome<String>, String> {+ let store = secret_store_for_config(config);+ let ciphertext = store.encrypt(plaintext).map_err(|e| e.to_string())?;+ Ok(RpcOutcome::single_log(ciphertext, "secret encrypted"))+}++pub async fn decrypt_secret(+ config: &Config,+ ciphertext: &str,+) -> Result<RpcOutcome<String>, String> {+ let store = secret_store_for_config(config);+ let plaintext = store.decrypt(ciphertext).map_err(|e| e.to_string())?;+ Ok(RpcOutcome::single_log(plaintext, "secret decrypted"))+}diff --git a/crates/openhuman-core/src/security/credentials/ops/user_scope.rs b/crates/openhuman-core/src/security/credentials/ops/user_scope.rsnew file mode 100644index 0000000000..126cc66d25--- /dev/null+++ b/crates/openhuman-core/src/security/credentials/ops/user_scope.rs@@ -0,0 +1,139 @@+//! The per-user directory and the process globals that follow it.+//!+//! A credential names a user; the core keeps that user's state under+//! `~/.openhuman/users/<id>/` and points every process-global store+//! (config, core context / memory binding, conversation persistence, cron+//! seeds) at it. These helpers move those pointers when a credential is+//! installed or removed. They know nothing about *how* the credential was+//! obtained — that is the host's business.++use crate::config::{+ default_root_openhuman_dir, pre_login_user_dir, read_active_user_id, user_openhuman_dir,+ write_active_user_id, Config,+};+use crate::memory::conversations;++use super::gated_services::is_embedder_host;++const LOG_PREFIX: &str = "[credentials][user-scope]";++/// Whether this process may touch the operator's global+/// `~/.openhuman/active_user.toml` / `users/` tree. An embedder host (the+/// library `Harness`) keeps session state under its own `config_path` scope+/// and must never change which user the operator's real install believes is+/// active purely by virtue of running a library call.+pub(super) fn operator_user_activation_allowed() -> bool {+ !is_embedder_host()+}++/// Activate `~/.openhuman/users/<user_id>/` as the current user directory.+///+/// On the very first activation (no previous `active_user.toml`) the+/// anonymous pre-login conversation store is purged so a fresh account never+/// inherits demo or scratch threads from the pre-login bucket (#1157).+/// Returns human-readable log lines for the RPC outcome.+pub(super) fn activate_user_scope(user_id: &str) -> Vec<String> {+ let mut logs = Vec::new();+ if !operator_user_activation_allowed() {+ log::debug!("{LOG_PREFIX} embedder host; skipping global user activation for {user_id}");+ return logs;+ }+ let Ok(root_dir) = default_root_openhuman_dir() else {+ return logs;+ };+ // Snapshot before overwriting `active_user.toml` so first activation can be+ // told apart from an in-place account switch.+ let previous_active = read_active_user_id(&root_dir);+ let user_dir = user_openhuman_dir(&root_dir, user_id);+ if let Err(e) = std::fs::create_dir_all(&user_dir) {+ tracing::warn!(user_id = %user_id, error = %e, "{LOG_PREFIX} failed to create user directory");+ return logs;+ }+ if let Err(e) = write_active_user_id(&root_dir, user_id) {+ tracing::warn!(user_id = %user_id, error = %e, "{LOG_PREFIX} failed to write active_user.toml");+ return logs;+ }+ logs.push(format!("user directory activated for {user_id}"));+ tracing::info!(user_id = %user_id, user_dir = %user_dir.display(), "{LOG_PREFIX} user-scoped directory activated");++ if previous_active.is_none() {+ // Shares `memory::conversations`' process-wide mutex with+ // `list_threads` / `purge_threads` on any workspace, so purge and+ // concurrent thread RPC in this process cannot interleave.+ let pre_ws = pre_login_user_dir(&root_dir).join("workspace");+ let pre_ws_log = pre_ws.display().to_string();+ match conversations::purge_threads(pre_ws) {+ Ok(stats) => {+ tracing::info!(+ pre_login_workspace = %pre_ws_log,+ threads = stats.thread_count,+ messages = stats.message_count,+ "{LOG_PREFIX} purged pre-login conversation threads after first activation"+ );+ logs.push(format!(+ "purged pre-login conversation history (threads={}, messages={})",+ stats.thread_count, stats.message_count+ ));+ }+ Err(e) => tracing::debug!(+ error = %e,+ pre_login_workspace = %pre_ws_log,+ "{LOG_PREFIX} pre-login conversation purge skipped (non-fatal)"+ ),+ }+ }+ logs+}++/// Clear `active_user.toml` so subsequent config loads fall back to the+/// default (unauthenticated) openhuman directory.+pub(super) fn deactivate_user_scope() {+ if !operator_user_activation_allowed() {+ return;+ }+ if let Ok(root_dir) = default_root_openhuman_dir() {+ if let Err(e) = crate::config::clear_active_user(&root_dir) {+ tracing::warn!(error = %e, "{LOG_PREFIX} failed to clear active_user.toml");+ }+ }+}++/// The config that reflects the current `active_user.toml` — reloaded after an+/// activation so credentials, keys and workspaces land in the user-scoped+/// location — or `fallback` when the reload fails.+pub(super) async fn reload_config_or(fallback: &Config) -> Config {+ match crate::config::load_config_with_timeout().await {+ Ok(config) => config,+ Err(error) => {+ tracing::warn!(error = %error, "{LOG_PREFIX} config reload failed; keeping caller config");+ fallback.clone()+ }+ }+}++/// Point every process-global store at `config`'s workspace after a+/// credential change: cron seeds, the core context (which carries the memory+/// binding — see `CoreContext::memory_binding`, #5560), and conversation+/// persistence. Returns log lines for the RPC outcome.+pub(super) fn rebind_after_credential_change(config: &Config, reason: &str) -> Vec<String> {+ let mut logs = Vec::new();+ if let Err(error) = crate::cron::seed::prune_retired_jobs(config) {+ tracing::warn!(error = %error, "{LOG_PREFIX} failed to prune retired cron jobs ({reason})");+ }+ match crate::core::runtime::context::CoreContext::rebind_default_workspace(+ &config.workspace_dir,+ config.subsystems.memory.clone(),+ ) {+ Ok(_) => logs.push(format!(+ "core context bound to workspace {}",+ config.workspace_dir.display()+ )),+ Err(e) => {+ tracing::warn!(error = %e, "{LOG_PREFIX} failed to rebind core context ({reason})");+ logs.push(format!("core context bind warning: {e}"));+ }+ }+ conversations::register_conversation_persistence_subscriber(config.workspace_dir.clone());+ logs.push("conversation persistence bound to active workspace".to_string());+ logs+}