Skip to content

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

fix(profiles): enforce profile-scoped search tool routing

Steven Enamakel3930f003a51b
diff --git a/crates/openhuman-core/src/modules/search/proxy.rs b/crates/openhuman-core/src/modules/search/proxy.rs
index 2256714e1b..7a14852936 100644
--- a/crates/openhuman-core/src/modules/search/proxy.rs
+++ b/crates/openhuman-core/src/modules/search/proxy.rs
@@ -140,6 +140,14 @@ where
}
async fn current_config(config: &Config) -> Result<Config, String> {
+ if config.profile_providers.is_some() {
+ let context = crate::core::runtime::CoreContext::scoped()
+ .ok_or_else(|| "profile search requires its authenticated context".to_owned())?;
+ let current = context.embedder_config().filter(|current| current.config_path == config.config_path)
+ .filter(|_| context.profile().is_some())
+ .ok_or_else(|| "profile search configuration belongs to another context".to_owned())?;
+ return Ok(current.clone());
+ }
crate::config::ops::reload_config_from_paths(&config.config_path, &config.workspace_dir).await
}
@@ -163,13 +171,37 @@ pub async fn refresh_loaded(config: &Config) -> Result<(), String> {
pub async fn list_tools(config: &Config) -> Result<ListToolsResponse, String> {
let current = current_config(config).await?;
with_module_lock(|| async {
- proxy(&current)
- .await?
- .call(names::methods::LIST_TOOLS, ())
- .await
- .map_err(|error| format!("search ListTools failed: {error}"))
- })
- .await
+ let mut listed: ListToolsResponse = proxy(&current).await?
+ .call(names::methods::LIST_TOOLS, ()).await
+ .map_err(|error| format!("search ListTools failed: {error}"))?;
+ if let Some(binding) = &current.profile_providers { binding.present(&mut listed.tools); }
+ Ok(listed)
+ }).await
+}
+
+/// The presentation and route boundary is checked before any module load or call.
+fn canonical_request(config: &Config, mut request: ExecuteToolRequest) -> Result<ExecuteToolRequest, String> {
+ let Some(binding) = &config.profile_providers else { return Ok(request) };
+ let canonical = binding.canonical_role_name(&request.name)
+ .ok_or_else(|| "search tool was not advertised by this profile".to_owned())?;
+ if !super::configured_tool_specs(config).iter().any(|tool| tool.name == request.name) {
+ return Err("search tool is unavailable in this profile".into());
+ }
+ let role = tinysearch_bus::role_for_tool(canonical)
+ .ok_or_else(|| "search alias has no canonical role".to_owned())?;
+ let module = module_config(config);
+ if let Some(provider) = request.arguments.get("provider").and_then(serde_json::Value::as_str) {
+ if !module.presentation.roles.get(&role).is_some_and(|order| order.iter().any(|allowed| allowed == provider)) {
+ return Err("search provider is outside the operator role policy".into());
+ }
+ }
+ if request.arguments.get("depth").and_then(serde_json::Value::as_str).is_some_and(|depth| depth != "standard")
+ || ["api_key", "credential", "endpoint", "base_url", "route", "provider_url", "backend"].into_iter().any(|key| request.arguments.get(key).is_some())
+ {
+ return Err("search arguments cannot override operator routes or capabilities".into());
+ }
+ request.name = canonical.to_owned();
+ Ok(request)
}
pub async fn execute_tool(
@@ -177,6 +209,7 @@ pub async fn execute_tool(
request: ExecuteToolRequest,
) -> Result<ExecuteToolResponse, String> {
let current = current_config(config).await?;
+ let request = canonical_request(&current, request)?;
let tool = request.name.clone();
with_module_lock(|| async {
let proxy = proxy(&current).await?;
diff --git a/crates/openhuman-core/src/profiles/capabilities.rs b/crates/openhuman-core/src/profiles/capabilities.rs
index 235d149ff0..ebc4c36131 100644
--- a/crates/openhuman-core/src/profiles/capabilities.rs
+++ b/crates/openhuman-core/src/profiles/capabilities.rs
@@ -67,6 +67,19 @@ impl ProfileCapabilities {
/// Whether a selected name may reach the host permission decision.
pub fn admits(&self, name: &str) -> bool {
+ let binding = super::provider_settings::ProfileProviderSettings::current();
+ let canonical = match &binding {
+ Some(binding) => match binding.policy_name(name) {
+ Some(canonical) => canonical,
+ None => return false,
+ },
+ None => std::borrow::Cow::Borrowed(name),
+ };
+ self.admits_capability(&canonical)
+ && self.names.as_ref().is_none_or(|names| names.contains(name))
+ }
+
+ fn admits_capability(&self, name: &str) -> bool {
super::tools::admits_with(name, true, &self.groups)
&& !matches!(
crate::tools::ops::tool_group(name),
@@ -82,7 +95,7 @@ impl ProfileCapabilities {
&& !name.starts_with("mcp_")
&& !crate::tools::toolpacks::pack_for_tool(name)
.is_some_and(|pack| pack.id == "workflows")
- && self.names.as_ref().is_none_or(|names| names.contains(name))
+
}
/// An explicitly named operation can pass the supervised gate only with a host callback.
@@ -106,7 +119,9 @@ impl ProfileCapabilities {
if let Some(names) = &self.names {
let mut groups = ToolGroups::none();
for name in names.iter().filter(|name| self.admits(name)) {
- if let Some(pack) = crate::tools::toolpacks::pack_for_tool(name) {
+ let binding = super::provider_settings::ProfileProviderSettings::current();
+ let canonical = binding.as_ref().and_then(|binding| binding.policy_name(name));
+ if let Some(pack) = crate::tools::toolpacks::pack_for_tool(canonical.as_deref().unwrap_or(name)) {
groups = groups.with(pack.id, GroupMode::Advertised);
}
}
@@ -205,7 +220,31 @@ impl ToolHook for ProfilePermission {
);
}
match &self.capabilities.permission {
- Some(permission) => permission.before_tool_decision(context).await,
+ Some(permission) => {
+ let binding = super::provider_settings::ProfileProviderSettings::current();
+ let canonical = binding.as_ref().and_then(|binding| binding.canonical_role_name(&context.tool_name));
+ let mut advertised = context.clone();
+ let mut rewritten = false;
+ if let Some(canonical) = canonical.filter(|name| *name != context.tool_name) {
+ if !self.capabilities.admits_capability(canonical) {
+ return ToolHookDecision::Deny("canonical search capability is denied".into());
+ }
+ let mut original = context.clone();
+ original.tool_name = canonical.to_owned();
+ match permission.before_tool_decision(&original).await {
+ ToolHookDecision::Proceed => {},
+ ToolHookDecision::ProceedWith(arguments) => {
+ advertised.arguments = arguments;
+ rewritten = true;
+ },
+ refusal => return refusal,
+ }
+ }
+ match permission.before_tool_decision(&advertised).await {
+ ToolHookDecision::Proceed if rewritten => ToolHookDecision::ProceedWith(advertised.arguments),
+ decision => decision,
+ }
+ },
None => {
ToolHookDecision::Deny("this profile has no host tool permission callback".into())
}
diff --git a/crates/openhuman-core/src/profiles/provider_settings.rs b/crates/openhuman-core/src/profiles/provider_settings.rs
index aa5460225e..8e0721ffe1 100644
--- a/crates/openhuman-core/src/profiles/provider_settings.rs
+++ b/crates/openhuman-core/src/profiles/provider_settings.rs
@@ -23,6 +23,22 @@ pub struct ProfileProviderSettings {
}
impl ProfileProviderSettings {
+ /// Binding of the authenticated task's own profile, never a process fallback.
+ pub fn current() -> Option<Arc<Self>> {
+ let context = crate::core::runtime::CoreContext::scoped()?;
+ context.profile()?;
+ context.embedder_config()?.profile_providers.clone()
+ }
+
+ /// Canonical capability for a registered name. A renamed canonical role is
+ /// refused rather than being allowed to bypass its advertised scope.
+ pub(crate) fn policy_name<'a>(&self, name: &'a str) -> Option<std::borrow::Cow<'a, str>> {
+ if let Some(canonical) = self.canonical_role_name(name) {
+ return Some(std::borrow::Cow::Borrowed(canonical));
+ }
+ tinysearch_bus::role_for_tool(name).is_none().then(|| std::borrow::Cow::Borrowed(name))
+ }
+
/// Validate operator-owned direct routes and their presentation.
///
/// # Errors
diff --git a/crates/openhuman-embed/src/lib.rs b/crates/openhuman-embed/src/lib.rs
index 1cd17abe2a..3ad39b470c 100644
--- a/crates/openhuman-embed/src/lib.rs
+++ b/crates/openhuman-embed/src/lib.rs
@@ -188,7 +188,7 @@ pub use harness::{
pub use harness::{HttpHeader, McpAuthConfig, McpServer};
#[cfg(feature = "channels")]
pub use profiles::{
- ChatReply, OpenError, ProfileError, ProfileEvents, ProfileHandle, ProfileId, ProfileIdMode,
+ ChatReply, DirectProfileProvider, OpenError, ProfileError, ProfileEvents, ProfileHandle, ProfileId, ProfileIdMode,
ProfileRuntime, ProfileRuntimeBuilder, ProfileSummary, Provisioned, RelayAccepted,
RelayMessage, SaasConfig,
};