Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions crates/openhuman-core/src/config/schema/load/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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
}

Expand Down
178 changes: 178 additions & 0 deletions crates/openhuman-core/src/config/schema/load/secrets_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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:
Expand Down Expand Up @@ -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);
}
Loading