From 290c9d3f63bc7a4898fc64116e6647a9d65cabc2 Mon Sep 17 00:00:00 2001 From: Ghost Scripter Date: Thu, 12 Nov 2026 11:04:38 +0530 Subject: [PATCH] fix(config): seal the channel credentials the secret list missed `secret_slots` drives save, load and the UI snapshot, but it skipped `email.password`, `yuanbao.app_secret`, `yuanbao.token`, `linq.webhook_secret` and `relay.upgrade_secret`. The connect flow keeps the email and Yuanbao secrets in the credentials store, but a value in `config.toml` still wins, so with `secrets.encrypt = true` these stayed in plaintext on disk and a sealed value reached the channel as `enc2:` ciphertext. They are now in the list. Tests pin that each one is sealed on save and opened on load, and that every listed credential round-trips through save and the snapshot reload (`load_from_config_path`). Refs #312 Co-Authored-By: Claude Opus 5.5 --- .../ops/loader_snapshot_secrets_tests.rs | 4 +- .../src/config/schema/load/secrets.rs | 16 ++ .../src/config/schema/load/secrets_tests.rs | 178 ++++++++++++++++++ 3 files changed, 197 insertions(+), 1 deletion(-) diff --git a/crates/openhuman-core/src/config/ops/loader_snapshot_secrets_tests.rs b/crates/openhuman-core/src/config/ops/loader_snapshot_secrets_tests.rs index 47abb0eeecc..a72e00d7cd0 100644 --- a/crates/openhuman-core/src/config/ops/loader_snapshot_secrets_tests.rs +++ b/crates/openhuman-core/src/config/ops/loader_snapshot_secrets_tests.rs @@ -46,6 +46,8 @@ fn config_with_every_secret() -> Config { password: SENTINEL.into(), ..Default::default() }); + channels.yuanbao = Some(channel(json!({ "app_key": "yb", "app_secret": "" }))); + channels.relay = Some(Default::default()); config.mcp_client.servers = [ json!({ "kind": "bearer_token", "token": SENTINEL }), json!({ "kind": "basic", "username": "mcp-user", "password": SENTINEL }), @@ -96,7 +98,7 @@ fn config_with_every_secret() -> Config { } filled += 1; } - assert_eq!(filled, 33, "every sealed credential field must be set"); + assert_eq!(filled, 38, "every sealed credential field must be set"); config } diff --git a/crates/openhuman-core/src/config/schema/load/secrets.rs b/crates/openhuman-core/src/config/schema/load/secrets.rs index d86fe82acb1..5bc221685ad 100644 --- a/crates/openhuman-core/src/config/schema/load/secrets.rs +++ b/crates/openhuman-core/src/config/schema/load/secrets.rs @@ -226,6 +226,7 @@ pub(crate) fn secret_slots(config: &mut Config) -> Vec<(String, SecretSlot<'_>)> } if let Some(lq) = ch.linq.as_mut() { slots.push(("linq.api_token".into(), Str(&mut lq.api_token))); + slots.push(("linq.webhook_secret".into(), Opt(&mut lq.webhook_secret))); } if let Some(irc) = ch.irc.as_mut() { slots.push(("irc.server_password".into(), Opt(&mut irc.server_password))); @@ -249,6 +250,21 @@ pub(crate) fn secret_slots(config: &mut Config) -> Vec<(String, SecretSlot<'_>)> if let Some(qq) = ch.qq.as_mut() { slots.push(("qq.app_secret".into(), Str(&mut qq.app_secret))); } + // The connect flow keeps these two in the credentials store, but a value + // written to `config.toml` by hand still wins and must not sit in plaintext. + if let Some(email) = ch.email.as_mut() { + slots.push(("email.password".into(), Str(&mut email.password))); + } + if let Some(yb) = ch.yuanbao.as_mut() { + slots.push(("yuanbao.app_secret".into(), Str(&mut yb.app_secret))); + slots.push(("yuanbao.token".into(), Str(&mut yb.token))); + } + if let Some(relay) = ch.relay.as_mut() { + slots.push(( + "relay.upgrade_secret".into(), + Opt(&mut relay.upgrade_secret), + )); + } slots } diff --git a/crates/openhuman-core/src/config/schema/load/secrets_tests.rs b/crates/openhuman-core/src/config/schema/load/secrets_tests.rs index 05a8d4e1e5a..294a38f85ce 100644 --- a/crates/openhuman-core/src/config/schema/load/secrets_tests.rs +++ b/crates/openhuman-core/src/config/schema/load/secrets_tests.rs @@ -78,6 +78,119 @@ async fn a_failed_secret_encryption_reports_the_field_and_the_reason() { assert!(!shown.contains("sk-must-not-leak"), "{shown}"); } +// ── channel credentials outside the connect flow (#312) ──────────────────── + +/// Channel credentials that the connect flow keeps in the credentials store, +/// but that `config.toml` can still carry when written by hand. +const CHANNEL_SECRETS: &[&str] = &[ + "email.password", + "yuanbao.app_secret", + "yuanbao.token", + "linq.webhook_secret", + "relay.upgrade_secret", +]; + +/// A config holding only the channel credential `field`, set to `value`. +fn config_with_channel_secret(dir: &Path, field: &str, value: &str) -> Config { + let mut config = Config::default(); + config.config_path = dir.join("config.toml"); + config.modules.enabled = false; + config.secrets.encrypt = true; + let ch = &mut config.channels_config; + match field { + "email.password" => { + ch.email = Some(crate::config::EmailConfig { + password: value.into(), + ..Default::default() + }) + } + "yuanbao.app_secret" | "yuanbao.token" => { + let mut yb: crate::config::YuanbaoConfig = + serde_json::from_value(serde_json::json!({ "app_key": "yb", "app_secret": "" })) + .unwrap(); + if field == "yuanbao.token" { + yb.token = value.into(); + } else { + yb.app_secret = value.into(); + } + ch.yuanbao = Some(yb); + } + "linq.webhook_secret" => { + ch.linq = Some( + serde_json::from_value(serde_json::json!({ + "api_token": "", "from_phone": "+15550100", "webhook_secret": value, + })) + .unwrap(), + ) + } + "relay.upgrade_secret" => { + ch.relay = Some(tinychannels_bus::config::RelayRuntimeConfig { + upgrade_secret: Some(value.into()), + ..Default::default() + }) + } + other => panic!("no fixture for {other}"), + } + config +} + +fn channel_secret(config: &Config, field: &str) -> Option { + let ch = &config.channels_config; + match field { + "email.password" => ch.email.as_ref().map(|e| e.password.clone()), + "yuanbao.app_secret" => ch.yuanbao.as_ref().map(|y| y.app_secret.clone()), + "yuanbao.token" => ch.yuanbao.as_ref().map(|y| y.token.clone()), + "linq.webhook_secret" => ch.linq.as_ref().and_then(|l| l.webhook_secret.clone()), + "relay.upgrade_secret" => ch.relay.as_ref().and_then(|r| r.upgrade_secret.clone()), + other => panic!("no fixture for {other}"), + } +} + +/// A save with `secrets.encrypt` must seal each channel credential, so with no +/// cipher it fails naming the field rather than writing it in plaintext. +#[tokio::test] +async fn a_channel_credential_is_never_saved_in_plaintext() { + #[cfg(feature = "security-module")] + let _absent = crate::modules::security::test_host::absent(); + for field in CHANNEL_SECRETS { + let dir = tempfile::tempdir().unwrap(); + let mut config = config_with_channel_secret(dir.path(), field, "sentinel-plaintext"); + + let error = encrypt_config_secrets(&mut config).await.err(); + + let shown = error.map(|e| format!("{e:#}")).unwrap_or_default(); + assert!( + shown.contains(&format!("Failed to encrypt {field}")), + "{field} must be sealed on save, not written in plaintext (error: {shown:?})" + ); + assert_eq!( + channel_secret(&config, field).as_deref(), + Some("sentinel-plaintext") + ); + } +} + +/// The load path opens each channel credential; one it cannot open is cleared +/// rather than handed to the channel as `enc2:` ciphertext. +#[tokio::test] +async fn a_sealed_channel_credential_never_reaches_callers_as_ciphertext() { + let sealed = format!("enc2:{}", "00".repeat(32)); + for field in CHANNEL_SECRETS { + let dir = tempfile::tempdir().unwrap(); + let mut config = config_with_channel_secret(dir.path(), field, &sealed); + + decrypt_config_secrets(&mut config, dir.path()) + .await + .unwrap(); + + let opened = channel_secret(&config, field).unwrap_or_default(); + assert!( + !crate::security::secret_storage::SecretStore::is_encrypted(&opened), + "{field} reached the caller sealed: {opened}" + ); + } +} + // ── secrets.unattended_unlock on a headless host ─────────────────────────── /// The two headless-host tests share the process gate and the admitted module: @@ -222,3 +335,68 @@ async fn without_unattended_unlock_the_secret_grant_still_parks_for_a_person() { .expect("save task") .expect("the save succeeds once a person approves"); } + +/// Each sealed credential's name and value, in list order. +#[cfg(feature = "security-module")] +fn sealed_values(config: &Config) -> Vec<(String, String)> { + let mut config = config.clone(); + secret_slots(&mut config) + .into_iter() + .map(|(name, slot)| match slot { + SecretSlot::Opt(value) => (name, value.clone().unwrap_or_default()), + SecretSlot::Str(value) => (name, value.clone()), + }) + .collect() +} + +/// Every credential in the sealed list, channel ones included, is sealed on +/// save and opened again by the snapshot reload (`load_from_config_path`), so +/// no caller of that path is handed `enc2:` ciphertext. +#[cfg(feature = "security-module")] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn every_sealed_credential_round_trips_through_save_and_reload() { + if !native_module_staged() { + return; + } + // The test host answers the unlock grant, as a person at a booted host + // does; holding this keeps the headless tests from taking that over. + let _serial = HEADLESS_HOST.lock().await; + let dir = tempfile::tempdir().unwrap(); + let mut config = native_cipher_fixture(dir.path(), false); + for field in CHANNEL_SECRETS { + let fixture = config_with_channel_secret(dir.path(), field, ""); + let (ch, from) = (&mut config.channels_config, fixture.channels_config); + ch.email = ch.email.take().or(from.email); + ch.yuanbao = ch.yuanbao.take().or(from.yuanbao); + ch.linq = ch.linq.take().or(from.linq); + ch.relay = ch.relay.take().or(from.relay); + } + for (name, slot) in secret_slots(&mut config) { + let sentinel = format!("sentinel-{name}"); + match slot { + SecretSlot::Opt(value) => *value = Some(sentinel), + SecretSlot::Str(value) => *value = sentinel, + } + } + let plain = sealed_values(&config); + for field in CHANNEL_SECRETS { + assert!( + plain.iter().any(|(name, _)| name == field), + "{field} unlisted" + ); + } + config + .save() + .await + .expect("save with every credential sealed"); + let at_rest = std::fs::read_to_string(&config.config_path).unwrap(); + assert!( + !at_rest.contains("sentinel-"), + "plaintext credential at rest:\n{at_rest}" + ); + + let reloaded = Config::load_from_config_path(&config.config_path, &config.workspace_dir) + .await + .expect("reload the saved config"); + assert_eq!(sealed_values(&reloaded), plain); +}