Skip to content
2 changes: 2 additions & 0 deletions crates/openhuman-core/src/channels/routes_provider_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async fn handle_runtime_command_routes_configured_provider_slug_and_alias() {
endpoint: "https://provider.example.test/v1".into(),
auth_style: AuthStyle::Bearer,
default_model: None,
headers: Default::default(),
},
CloudProviderCreds {
id: "p_managed_123".into(),
Expand All @@ -33,6 +34,7 @@ async fn handle_runtime_command_routes_configured_provider_slug_and_alias() {
endpoint: "https://api.openhuman.ai/v1".into(),
auth_style: AuthStyle::OpenhumanJwt,
default_model: None,
headers: Default::default(),
},
];
ctx.config = Some(Arc::new(config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ async fn process_channel_message_handles_models_command_without_llm_call() {
Arc::make_mut(config)
.cloud_providers
.push(crate::config::schema::CloudProviderCreds {
headers: Default::default(),
id: "p_managed_models_test".to_string(),
slug: "openhuman".to_string(),
label: "Managed Backend".to_string(),
Expand Down
71 changes: 70 additions & 1 deletion crates/openhuman-core/src/config/ops/cloud_providers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! RPC input conversion for cloud-provider settings.

use crate::config::schema::cloud_providers::{
builtin_cloud_provider, generate_provider_id, is_slug_reserved, AuthStyle, CloudProviderCreds,
builtin_cloud_provider, generate_provider_id, is_slug_reserved, validate_provider_headers,
AuthStyle, CloudProviderCreds, REDACTED_HEADER_VALUE,
};
use serde::Deserialize;

use super::model::model_byok_route::same_endpoint;

#[derive(Debug, Deserialize)]
pub struct CloudProviderUpdate {
pub(crate) id: Option<String>,
Expand All @@ -16,6 +19,11 @@ pub struct CloudProviderUpdate {
pub(crate) auth_style: Option<String>,
#[serde(default)]
pub(crate) ca_cert_pem: Option<String>,
/// Extra request headers for this provider (`{name: value}`). Omitted
/// (or empty) keeps the headers already stored for the entry, unless its
/// endpoint changes.
#[serde(default)]
pub(crate) headers: Option<std::collections::BTreeMap<String, String>>,
// keep until v0.66 (review after 2027-01-11): incoming RPC discriminator
// remains accepted until shipped-client parameter coverage exists.
#[serde(rename = "type", default)]
Expand Down Expand Up @@ -84,6 +92,8 @@ pub fn build_cloud_provider_creds(
.label
.filter(|value| !value.trim().is_empty())
.unwrap_or_else(|| slug.clone());
let headers = entry.headers.unwrap_or_default();
validate_provider_headers(&slug, &headers)?;

Ok(CloudProviderCreds {
id,
Expand All @@ -92,11 +102,70 @@ pub fn build_cloud_provider_creds(
endpoint,
auth_style,
default_model: entry.default_model,
headers,
})
})
.collect()
}

/// Settings-save merge for provider `headers` (#7865). An entry arriving
/// without `headers` keeps the ones stored for it (matched by id, else slug):
/// the settings panel echoes every entry back on save and has no editor for
/// headers, so a table set in `config.toml` must survive an unrelated edit.
/// Sending a non-empty map replaces them.
///
/// Stored headers belong to the endpoint they were set for, because a value
/// may be a credential: an entry whose endpoint changed inherits nothing, so
/// the new host only receives what the save itself supplies. A value equal to the redaction
/// placeholder the client snapshot shows ([`REDACTED_HEADER_VALUE`]) is an
/// echo of a stored secret, not a new value, so it keeps the stored one; with
/// nothing stored behind it the header is dropped rather than sent.
pub(crate) fn carry_stored_headers(
incoming: Vec<CloudProviderCreds>,
stored: &[CloudProviderCreds],
) -> Vec<CloudProviderCreds> {
incoming
.into_iter()
.map(|mut entry| {
let previous = stored
.iter()
.find(|e| e.id == entry.id)
.or_else(|| stored.iter().find(|e| e.slug == entry.slug))
.filter(|previous| {
let unchanged = same_endpoint(&previous.endpoint, &entry.endpoint);
if !unchanged && !previous.headers.is_empty() {
log::debug!(
"[config] cloud provider '{}': endpoint changed, not carrying {} \
stored header(s) over",
entry.slug,
previous.headers.len()
);
}
unchanged
});
if entry.headers.is_empty() {
if let Some(previous) = previous {
entry.headers = previous.headers.clone();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
return entry;
}
entry.headers.retain(|name, value| {
if value != REDACTED_HEADER_VALUE {
return true;
}
match previous.and_then(|p| p.headers.get(name)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Some(stored_value) => {
*value = stored_value.clone();
true
}
None => false,
}
});
entry
})
.collect()
}

#[cfg(test)]
#[path = "cloud_providers_tests.rs"]
mod tests;
32 changes: 32 additions & 0 deletions crates/openhuman-core/src/config/ops/cloud_providers_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn update(slug: &str) -> CloudProviderUpdate {
endpoint: String::new(),
auth_style: None,
ca_cert_pem: None,
headers: None,
legacy_type: None,
default_model: None,
}
Expand Down Expand Up @@ -107,3 +108,34 @@ fn builder_rejects_empty_slug() {
"cloud provider slug must not be empty"
);
}

#[test]
fn builder_carries_headers_and_rejects_malformed_ones_naming_the_provider() {
let mut entry = update("gateway");
entry.endpoint = "https://gateway.example/v1".to_string();
entry.headers = Some([("x-tenant".to_string(), "team-a".to_string())].into());
let providers = build_cloud_provider_creds(vec![entry]).expect("valid headers build");
assert_eq!(
providers[0].headers.get("x-tenant").map(String::as_str),
Some("team-a")
);

let mut bad_name = update("gateway");
bad_name.endpoint = "https://gateway.example/v1".to_string();
bad_name.headers = Some([("x tenant".to_string(), "team-a".to_string())].into());
let error = build_cloud_provider_creds(vec![bad_name]).expect_err("space in a name");
assert!(
error.contains("'gateway'") && error.contains("'x tenant'"),
"{error}"
);

let mut bad_value = update("gateway");
bad_value.endpoint = "https://gateway.example/v1".to_string();
bad_value.headers = Some([("x-tenant".to_string(), "secret\nInjected: 1".to_string())].into());
let error = build_cloud_provider_creds(vec![bad_value]).expect_err("newline in a value");
assert!(error.contains("'x-tenant'"), "{error}");
assert!(
!error.contains("secret"),
"the value never reaches the error: {error}"
);
}
12 changes: 11 additions & 1 deletion crates/openhuman-core/src/config/ops/loader/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub async fn snapshot_config_json(config: &Config) -> Result<serde_json::Value,
}

/// Credentials the config holds outside the sealed list: MCP server auth,
/// environment, arguments and endpoint, MCP client capabilities, and proxy
/// environment, arguments and endpoint, cloud provider headers, MCP client
/// capabilities, and proxy
/// URLs, which may carry userinfo.
fn mask_unsealed_credentials(config: &mut Config) {
use crate::config::schema::storage::redact_url;
Expand Down Expand Up @@ -74,6 +75,12 @@ fn mask_unsealed_credentials(config: &mut Config) {
_ => server.auth = McpAuthConfig::None,
}
}
// Any provider header can hold a key (`Authorization`, `x-tenant`), so
// every value goes; the names stay.
for provider in &mut config.cloud_providers {
provider.headers =
crate::config::schema::cloud_providers::redacted_headers(&provider.headers);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
// Client-declared metadata with free-form keys (an `Authorization` entry
// has no credential-shaped name), and nothing displays it.
blank_strings(&mut config.mcp_client.client_identity.capabilities);
Expand Down Expand Up @@ -237,6 +244,9 @@ pub async fn client_config_json(config: &Config) -> serde_json::Value {
"endpoint": c.endpoint,
"auth_style": c.auth_style.as_str(),
"ca_cert_pem": config.cloud_provider_ca_certs.get(&c.slug),
// Names only: header values may be secrets and this snapshot
// goes to the UI.
"headers": crate::config::schema::cloud_providers::redacted_headers(&c.headers),
})
})
.collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ fn config_with_every_secret() -> Config {
}))
})
.collect();
config.cloud_providers = vec![crate::config::schema::cloud_providers::CloudProviderCreds {
slug: "gateway".into(),
endpoint: "https://gateway.example/v1".into(),
headers: [
("Authorization".to_string(), format!("Bearer {SENTINEL}")),
("x-tenant".to_string(), SENTINEL.to_string()),
]
.into(),
..Default::default()
}];
config.mcp_client.registry_auth.smithery_api_key = Some(SENTINEL.into());
config.mcp_client.registry_auth.mcp_official_token = Some(SENTINEL.into());
config.mcp_client.client_identity.capabilities = json!({
Expand Down Expand Up @@ -130,6 +140,13 @@ async fn snapshot_carries_no_credential() {
assert_eq!(server["env"]["AWS_SECRET_ACCESS_KEY"], "");
assert_eq!(server["args"], json!(["", "", ""]));
assert_eq!(server["endpoint"], "https://***@mcp.example/rpc?***");
// Provider headers keep their names.
let headers = &snapshot["config"]["cloud_providers"][0]["headers"];
assert_eq!(
headers["x-tenant"],
crate::config::schema::cloud_providers::REDACTED_HEADER_VALUE
);
assert!(headers.get("Authorization").is_some());
assert_eq!(
snapshot["config"]["proxy"]["http_proxy"],
"http://***@proxy.example:8080"
Expand Down
7 changes: 5 additions & 2 deletions crates/openhuman-core/src/config/ops/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::core::Outcome;
use super::loader::snapshot_config_json;

#[path = "model_byok_route.rs"]
mod model_byok_route;
pub(super) mod model_byok_route;
#[cfg(test)]
use model_byok_route::BYOK_INFERENCE_SLUG;
use model_byok_route::{complete_byok_route, ExplicitRolePins};
Expand Down Expand Up @@ -188,7 +188,10 @@ pub async fn apply_model_settings(
"[config] apply_model_settings: preserving {} reserved cloud provider(s) before overwrite",
preserved.len()
);
config.cloud_providers = providers;
// Entries echoed back without `headers` (or with redacted values)
// keep the stored ones (#7865).
let stored = std::mem::take(&mut config.cloud_providers);
config.cloud_providers = super::cloud_providers::carry_stored_headers(providers, &stored);
let before_reinject = config.cloud_providers.len();
for entry in preserved {
let preserved_slug = entry.slug.trim();
Expand Down
3 changes: 2 additions & 1 deletion crates/openhuman-core/src/config/ops/model_byok_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(super) struct ExplicitRolePins {
pub(super) const BYOK_INFERENCE_SLUG: &str = "byok-inference";

/// Two endpoints are the same route if they differ only by trailing slashes.
fn same_endpoint(a: &str, b: &str) -> bool {
pub(in crate::config::ops) fn same_endpoint(a: &str, b: &str) -> bool {
a.trim().trim_end_matches('/') == b.trim().trim_end_matches('/')
}

Expand Down Expand Up @@ -112,6 +112,7 @@ pub(super) fn complete_byok_route(config: &mut Config, explicit: &ExplicitRolePi
endpoint: endpoint.clone(),
auth_style: AuthStyle::Bearer,
default_model: Some(model.clone()),
headers: Default::default(),
});
BYOK_INFERENCE_SLUG.to_string()
}
Expand Down
1 change: 1 addition & 0 deletions crates/openhuman-core/src/config/ops/model_byok_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn entry(slug: &str, endpoint: &str) -> CloudProviderCreds {
label: slug.to_string(),
endpoint: endpoint.to_string(),
auth_style: AuthStyle::Bearer,
headers: Default::default(),
default_model: None,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn register_test_hosted_policy() {

fn provider(id: &str, slug: &str, endpoint: &str, auth_style: AuthStyle) -> CloudProviderCreds {
CloudProviderCreds {
headers: Default::default(),
id: id.to_string(),
slug: slug.to_string(),
label: slug.to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ async fn apply_model_settings_preserves_existing_reserved_slug_cloud_providers()
label: "OpenHuman".into(),
endpoint: "https://api.tinyhumans.ai".into(),
auth_style: AuthStyle::OpenhumanJwt,
headers: Default::default(),
default_model: Some("reasoning-v1".into()),
},
CloudProviderCreds {
Expand All @@ -221,6 +222,7 @@ async fn apply_model_settings_preserves_existing_reserved_slug_cloud_providers()
label: "My OpenAI".into(),
endpoint: "https://api.openai.com".into(),
auth_style: AuthStyle::Bearer,
headers: Default::default(),
default_model: Some("gpt-4o".into()),
},
];
Expand All @@ -236,6 +238,7 @@ async fn apply_model_settings_preserves_existing_reserved_slug_cloud_providers()
label: "My OpenAI (edited)".into(),
endpoint: "https://api.openai.com/v1".into(),
auth_style: AuthStyle::Bearer,
headers: Default::default(),
default_model: Some("gpt-4o-mini".into()),
}]),
..Default::default()
Expand Down Expand Up @@ -277,6 +280,7 @@ async fn apply_model_settings_does_not_double_add_reserved_entries() {
label: "OpenHuman (stored)".into(),
endpoint: "https://api.tinyhumans.ai".into(),
auth_style: AuthStyle::OpenhumanJwt,
headers: Default::default(),
default_model: Some("reasoning-v1".into()),
}];

Expand All @@ -287,6 +291,7 @@ async fn apply_model_settings_does_not_double_add_reserved_entries() {
label: "OpenHuman (from patch)".into(),
endpoint: "https://api.tinyhumans.ai".into(),
auth_style: AuthStyle::OpenhumanJwt,
headers: Default::default(),
default_model: Some("reasoning-v1".into()),
}]),
..Default::default()
Expand Down
Loading
Loading