Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(security): move bearer endpoint guard into security module

Steven Enamakel7221e5a1f735
diff --git a/crates/openhuman-core/src/inference/provider/openhuman_backend_model.rs b/crates/openhuman-core/src/inference/provider/openhuman_backend_model.rs
index 68b0f023a5..2e81060d4d 100644
--- a/crates/openhuman-core/src/inference/provider/openhuman_backend_model.rs
+++ b/crates/openhuman-core/src/inference/provider/openhuman_backend_model.rs
@@ -5,47 +5,11 @@
//! [`PROVIDER_OPENHUMAN`](super::factory::PROVIDER_OPENHUMAN)
//! ([`crate::inference::registry`]); it lives with that provider.
//!
-//! The two `is_*_managed_*` endpoint guards below are transitional: they remain
-//! only until the last core callers (integrations, voice, socket URL, backend
-//! client) move onto the provider crate's own copies.
-
use serde_json::Value;
use tinyinference_llm::model::ModelRequest;
pub const PROVIDER_LABEL: &str = "OpenHuman";
-fn is_loopback_host(url: &url::Url) -> bool {
- match url.host() {
- Some(url::Host::Domain("localhost")) => true,
- Some(url::Host::Ipv4(address)) => address.is_loopback(),
- Some(url::Host::Ipv6(address)) => address.is_loopback(),
- _ => false,
- }
-}
-
-/// Whether `endpoint` is safe to carry the TinyHumans API key as a bearer.
-///
-/// `https://` always qualifies; plain `http://` only for loopback, matching
-/// `openhuman_embed::turn::is_safe_endpoint_for_bearer`'s allowance for local
-/// testing against a dev server. Anything else — a plaintext non-loopback
-/// endpoint — would put the key on the wire in the clear (CWE-319), so
-/// the managed model refuses before it gets there.
-/// Deliberately narrow to the managed-key bearer path: `normalize_api_base_url`
-/// itself must stay permissive, because a library host's BYOK/local `api_url`
-/// can legitimately be plain HTTP.
-pub(crate) fn is_safe_endpoint_for_managed_bearer(endpoint: &str) -> bool {
- let Ok(url) = url::Url::parse(endpoint) else {
- return false;
- };
- if url.scheme() == "https" {
- return true;
- }
- if url.scheme() != "http" {
- return false;
- }
- is_loopback_host(&url)
-}
-
/// Request-metadata key a caller sets (to `true`) to ask for no reasoning on
/// a call — see [`without_reasoning`].
pub const REASONING_OFF_METADATA_KEY: &str = "openhuman_reasoning_off";
diff --git a/crates/openhuman-core/src/platform/socket/url.rs b/crates/openhuman-core/src/platform/socket/url.rs
index 398c3ff132..fde3f2f343 100644
--- a/crates/openhuman-core/src/platform/socket/url.rs
+++ b/crates/openhuman-core/src/platform/socket/url.rs
@@ -13,9 +13,7 @@ pub(super) fn is_safe_socket_endpoint(endpoint: &str) -> bool {
if url.set_scheme("http").is_err() {
return false;
}
- crate::inference::provider::openhuman_backend_model::is_safe_endpoint_for_managed_bearer(
- url.as_str(),
- )
+ crate::security::transport::is_safe_endpoint_for_bearer(url.as_str())
}
_ => false,
}
diff --git a/crates/openhuman-core/src/security/mod.rs b/crates/openhuman-core/src/security/mod.rs
index 6cf0486e44..949a34c14a 100644
--- a/crates/openhuman-core/src/security/mod.rs
+++ b/crates/openhuman-core/src/security/mod.rs
@@ -21,6 +21,7 @@ pub mod policy;
pub mod prompt_injection;
pub mod scrub;
pub mod secret_storage;
+pub(crate) mod transport;
pub(crate) mod secure_open;
pub mod turn_origin;
diff --git a/crates/openhuman-core/src/security/transport.rs b/crates/openhuman-core/src/security/transport.rs
new file mode 100644
index 0000000000..540166a090
--- /dev/null
+++ b/crates/openhuman-core/src/security/transport.rs
@@ -0,0 +1,27 @@
+//! Credential transport checks shared by host-owned network adapters.
+
+/// Whether an HTTP endpoint can safely carry a bearer credential.
+///
+/// HTTPS endpoints are allowed. Plain HTTP is limited to localhost and IP
+/// loopback addresses so local development servers remain usable without
+/// sending credentials over a network in cleartext.
+pub(crate) fn is_safe_endpoint_for_bearer(endpoint: &str) -> bool {
+ let Ok(url) = url::Url::parse(endpoint) else {
+ return false;
+ };
+
+ match url.scheme() {
+ "https" => true,
+ "http" => match url.host() {
+ Some(url::Host::Domain("localhost")) => true,
+ Some(url::Host::Ipv4(address)) => address.is_loopback(),
+ Some(url::Host::Ipv6(address)) => address.is_loopback(),
+ _ => false,
+ },
+ _ => false,
+ }
+}
+
+#[cfg(test)]
+#[path = "transport_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/security/transport_tests.rs b/crates/openhuman-core/src/security/transport_tests.rs
new file mode 100644
index 0000000000..f3c6734671
--- /dev/null
+++ b/crates/openhuman-core/src/security/transport_tests.rs
@@ -0,0 +1,15 @@
+use super::*;
+
+#[test]
+fn bearer_transport_allows_tls_and_only_http_loopback() {
+ assert!(is_safe_endpoint_for_bearer("https://api.example.test/v1"));
+ assert!(is_safe_endpoint_for_bearer("http://localhost:8080/v1"));
+ assert!(is_safe_endpoint_for_bearer("http://[::1]:8080/v1"));
+ assert!(is_safe_endpoint_for_bearer("http://127.0.0.1:8080/v1"));
+ assert!(is_safe_endpoint_for_bearer("http://127.200.10.20:8080/v1"));
+
+ assert!(!is_safe_endpoint_for_bearer("http://192.168.1.10:8080/v1"));
+ assert!(!is_safe_endpoint_for_bearer("http://127.attacker.example/v1"));
+ assert!(!is_safe_endpoint_for_bearer("ftp://localhost/v1"));
+ assert!(!is_safe_endpoint_for_bearer("not a url"));
+}