Skip to content

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

fix(privacy): refuse a per-request Ollama cloud tag under local-only

M3gA-Mindbd7ef9c8201d
diff --git a/crates/openhuman-core/src/inference/provider/factory/access_gates.rs b/crates/openhuman-core/src/inference/provider/factory/access_gates.rs
index 4760492720..e732725fc1 100644
--- a/crates/openhuman-core/src/inference/provider/factory/access_gates.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/access_gates.rs
@@ -98,6 +98,76 @@ pub(super) fn is_loopback_endpoint(endpoint: &str) -> bool {
}
}
+/// Local-only for the model a request actually names. A caller may override
+/// the route's model per request (a flow node's pinned model, for one), so an
+/// Ollama route built for `llama3` can still be asked for a `*-cloud` tag.
+pub(super) struct LocalOnlyRequestGuard {
+ pub(super) inner: std::sync::Arc<dyn tinyinference_llm::model::ChatModel<()>>,
+}
+
+impl LocalOnlyRequestGuard {
+ fn check(
+ &self,
+ request: &tinyinference_llm::model::ModelRequest,
+ ) -> tinyinference_llm::Result<()> {
+ let Some(model) = request
+ .model
+ .as_deref()
+ .filter(|m| is_ollama_cloud_model(&format!("ollama:{m}")))
+ else {
+ return Ok(());
+ };
+ enforce_local_only_inference("request", &format!("ollama:{model}"))
+ .map_err(|e| tinyinference_llm::Error::Validation(e.to_string()))
+ }
+}
+
+#[async_trait::async_trait]
+impl tinyinference_llm::model::ChatModel<()> for LocalOnlyRequestGuard {
+ fn profile(&self) -> Option<&tinyinference_llm::model::ModelProfile> {
+ self.inner.profile()
+ }
+ fn supports_input(
+ &self,
+ modality: tinyinference_llm::model::InputModality,
+ mime_type: &str,
+ source: tinyinference_llm::model::InputSource,
+ ) -> bool {
+ self.inner.supports_input(modality, mime_type, source)
+ }
+ fn cache_identity(&self) -> Option<String> {
+ self.inner.cache_identity()
+ }
+ fn tracks_physical_attempts(&self) -> bool {
+ self.inner.tracks_physical_attempts()
+ }
+ fn has_pre_invocation_admission_gate(&self) -> bool {
+ self.inner.has_pre_invocation_admission_gate()
+ }
+ async fn invoke(
+ &self,
+ state: &(),
+ request: tinyinference_llm::model::ModelRequest,
+ ) -> tinyinference_llm::Result<tinyinference_llm::model::ModelResponse> {
+ self.check(&request)?;
+ self.inner.invoke(state, request).await
+ }
+ async fn stream(
+ &self,
+ state: &(),
+ request: tinyinference_llm::model::ModelRequest,
+ ) -> tinyinference_llm::Result<tinyinference_llm::model::ModelStream> {
+ self.check(&request)?;
+ self.inner.stream(state, request).await
+ }
+ async fn fetch_deferred(
+ &self,
+ handle: &tinyinference_llm::model::DeferredHandle,
+ ) -> tinyinference_llm::Result<tinyinference_llm::model::DeferredStatus> {
+ self.inner.fetch_deferred(handle).await
+ }
+}
+
/// Privacy Mode `LocalOnly` for a resolved local-runtime endpoint: refuse
/// one that is not loopback (the prompt and the `local_ai` key would go there).
pub(super) fn enforce_local_only_endpoint(role: &str, endpoint: &str) -> anyhow::Result<()> {
diff --git a/crates/openhuman-core/src/inference/provider/factory/local_runtime.rs b/crates/openhuman-core/src/inference/provider/factory/local_runtime.rs
index 618b6e757e..8c748f4276 100644
--- a/crates/openhuman-core/src/inference/provider/factory/local_runtime.rs
+++ b/crates/openhuman-core/src/inference/provider/factory/local_runtime.rs
@@ -4,7 +4,7 @@
use super::*;
use crate::inference::provider::factory::access_gates::{
enforce_local_only_endpoint, is_loopback_endpoint, is_ollama_cloud_model,
- verify_provider_session,
+ verify_provider_session, LocalOnlyRequestGuard,
};
use tinyinference_llm::providers::openai::build_local_runtime_chat_model;
use tinyinference_llm::providers::openai::AuthStyle as CompatAuthStyle;
@@ -167,6 +167,11 @@ fn try_create_local_runtime_chat_model_from_string_with_identity(
resolved.temperature,
resolved.num_ctx,
);
+ let chat: Arc<dyn ChatModel<()>> = if resolved.kind.as_str() == "ollama" {
+ Arc::new(LocalOnlyRequestGuard { inner: chat })
+ } else {
+ chat
+ };
Some(Ok((chat, resolved.model)))
}
diff --git a/crates/openhuman-core/src/inference/provider/factory_local_only_endpoint_tests.rs b/crates/openhuman-core/src/inference/provider/factory_local_only_endpoint_tests.rs
index be5fd3b336..14d40015e1 100644
--- a/crates/openhuman-core/src/inference/provider/factory_local_only_endpoint_tests.rs
+++ b/crates/openhuman-core/src/inference/provider/factory_local_only_endpoint_tests.rs
@@ -1,13 +1,26 @@
-//! Local-only privacy mode means the prompt stays on this machine. Two routes
-//! named "local" break that: an Ollama `-cloud` tag, which the local daemon
-//! proxies to ollama.com, and a local-runtime prefix whose endpoint is another
-//! host (it gets the prompt and the `local_ai` bearer key). Only loopback counts
-//! as local; a LAN address is another machine.
-
use super::*;
+use crate::config::test_env::EnvVarGuard;
use crate::config::PrivacyMode;
use crate::inference::provider::factory::access_gates::local_only_violation;
+// Local-only privacy mode means the prompt stays on this machine. Two routes
+// named "local" break that: an Ollama `-cloud` tag, which the local daemon
+// proxies to ollama.com, and a local-runtime prefix whose endpoint is another
+// host (it gets the prompt and the `local_ai` bearer key). Only loopback counts
+// as local; a LAN address is another machine.
+
+/// Environment overrides that take precedence over `local_ai.base_url`.
+const ENDPOINT_ENV: &[&str] = &[
+ "OPENHUMAN_LOCAL_INFERENCE_URL",
+ "LOCAL_OPENAI_URL",
+ "OPENHUMAN_LM_STUDIO_BASE_URL",
+ "LM_STUDIO_BASE_URL",
+ "OPENHUMAN_OLLAMA_BASE_URL",
+ "OLLAMA_HOST",
+ "MLX_SERVER_URL",
+ "OMLX_SERVER_URL",
+];
+
fn local_ai_at(base_url: &str) -> Config {
let mut config = Config::default();
config.local_ai.base_url = Some(base_url.to_string());
@@ -39,6 +52,7 @@ fn local_only_refuses_ollama_cloud_proxied_models() {
#[test]
fn local_only_refuses_a_local_runtime_pointed_at_another_host() {
let _guard = crate::inference::inference_test_guard();
+ let _env = EnvVarGuard::locked_unset_many(ENDPOINT_ENV);
let _mode = crate::security::live_policy::test_privacy_scope(PrivacyMode::LocalOnly);
for (provider, base_url) in [
("local-openai:m", "https://gpu.example.invalid/v1"),
@@ -75,6 +89,9 @@ async fn a_local_runtime_pointed_at_another_host_is_recorded_as_external_egress(
use crate::security::egress::EgressDescriptor;
use std::time::Duration;
+ let _guard = crate::inference::inference_test_guard_async().await;
+ let _env = EnvVarGuard::locked_unset_many_async(ENDPOINT_ENV).await;
+ let _mode = crate::security::live_policy::test_privacy_scope(PrivacyMode::Standard);
crate::core::bus::init().await.expect("bus init");
let mut rx = BUS.get().unwrap().receiver();
@@ -112,3 +129,29 @@ async fn a_local_runtime_pointed_at_another_host_is_recorded_as_external_egress(
}
}
}
+
+/// A request may name a different model than the route was built for (a flow
+/// node's pinned model): an Ollama route built for a local tag must still
+/// refuse a `-cloud` tag at request time under local-only.
+#[tokio::test]
+async fn local_only_refuses_a_cloud_tag_named_per_request() {
+ use tinyinference_llm::model::ModelRequest;
+ let _guard = crate::inference::inference_test_guard_async().await;
+ let _env = EnvVarGuard::locked_unset_many_async(ENDPOINT_ENV).await;
+ let _mode = crate::security::live_policy::test_privacy_scope(PrivacyMode::LocalOnly);
+ let (model, _) = create_test_chat_model_from_string(
+ "chat",
+ "ollama:llama3",
+ &local_ai_at("http://127.0.0.1:9/v1"),
+ )
+ .expect("a local Ollama tag builds under local-only");
+
+ let error = model
+ .invoke(
+ &(),
+ ModelRequest::new(Vec::new()).with_model("gpt-oss:120b-cloud"),
+ )
+ .await
+ .expect_err("a per-request cloud tag must be refused before dispatch");
+ assert!(error.to_string().contains("Local-only"), "{error}");
+}