Skip to content

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

feat(security): add approval gate for tool call interception

Steven Enamakel2ba07c974843
diff --git a/crates/openhuman-core/src/security/approval/gate.rs b/crates/openhuman-core/src/security/approval/gate.rs
index 5ce2fc4b04..7ab192b7c7 100644
--- a/crates/openhuman-core/src/security/approval/gate.rs
+++ b/crates/openhuman-core/src/security/approval/gate.rs
@@ -68,15 +68,28 @@ pub enum ApprovalError {
WrongAgent { request_id: String },
}
-/// The key `thread_to_request` files a parked chat thread under: the thread
-/// id alone for the process's own sessions, else the agent and thread.
-pub(crate) fn thread_route_key(agent_id: Option<&str>, thread_id: &str) -> String {
- match agent_id {
- Some(agent) => format!("{agent}\u{1f}{thread_id}"),
- None => thread_id.to_string(),
+/// The key `thread_to_request` files a parked chat thread under, keyed by
+/// the tenant that parked it: the thread id alone for the process's own
+/// sessions, `<agent>\x1f<thread>` for an embedded agent (both unchanged),
+/// and the [`tenant_key`](crate::core::runtime::tenant_key) of the thread for
+/// a SaaS profile, so two profiles' default agents (which have no agent id)
+/// never share a route. A profile key starts with `\x1e`, which no agent id
+/// contains, and the desktop and SaaS modes never share a process.
+pub(crate) fn thread_route_key(tenant: &crate::core::runtime::Tenant, thread_id: &str) -> String {
+ match (&tenant.profile, &tenant.agent) {
+ (Some(_), _) => crate::core::runtime::tenant_key(tenant, thread_id),
+ (None, Some(agent)) => format!("{agent}\u{1f}{thread_id}"),
+ (None, None) => thread_id.to_string(),
}
}
+/// The tenant whose approvals the calling task parks and answers. A SaaS
+/// task with no scope gets a tenant nothing else shares, so it can neither
+/// find nor clear another user's route.
+pub(crate) fn current_route_tenant() -> crate::core::runtime::Tenant {
+ crate::core::runtime::tenant::current_tenant_or_isolated("approval_gate")
+}
+
/// How long the gate will park a future before timing out and
/// returning `Deny`. 10 minutes matches the default `expires_at`
/// written into the persisted row.
@@ -222,6 +235,9 @@ pub(crate) struct RequestRoute {
pub(crate) tool_call_id: Option<String>,
pub(crate) forced: bool,
pub(crate) agent_id: Option<String>,
+ /// The [`thread_route_key`] the request was parked under, so a decision
+ /// made outside the parking task's scope clears the right route.
+ pub(crate) thread_key: Option<String>,
}
/// Coordinator for pending approvals.
diff --git a/crates/openhuman-core/src/security/approval/gate_intercept.rs b/crates/openhuman-core/src/security/approval/gate_intercept.rs
index f41fb32837..cbb5e1b7e9 100644
--- a/crates/openhuman-core/src/security/approval/gate_intercept.rs
+++ b/crates/openhuman-core/src/security/approval/gate_intercept.rs
@@ -399,10 +399,13 @@ impl ApprovalGate {
let request_id = uuid::Uuid::new_v4().to_string();
let now = chrono::Utc::now();
- let agent_id = crate::core::runtime::agent_scope::current_agent_id();
+ // Routes key on the tenant (profile and agent), so two SaaS profiles'
+ // default agents, which carry no agent id, never share one.
+ let route_tenant = super::gate::current_route_tenant();
+ let agent_id = route_tenant.agent.clone();
let thread_key = chat_thread_id
.as_deref()
- .map(|thread_id| thread_route_key(agent_id.as_deref(), thread_id));
+ .map(|thread_id| thread_route_key(&route_tenant, thread_id));
// Resolve the clamped park TTL up front so the persisted `expires_at`
// and the actual wait below (see `resolve_park_ttl` further down)
// use the same value — see `Self::resolve_park_ttl` and the
@@ -480,6 +483,7 @@ impl ApprovalGate {
tool_call_id: tool_call_id.map(str::to_string),
forced,
agent_id: agent_id.clone(),
+ thread_key: thread_key.clone(),
},
);
if let Err(err) = store::insert_pending(&self.config, &pending, &self.session_id) {
diff --git a/crates/openhuman-core/src/security/approval/gate_state.rs b/crates/openhuman-core/src/security/approval/gate_state.rs
index b89655d2e5..c4d6c0cd82 100644
--- a/crates/openhuman-core/src/security/approval/gate_state.rs
+++ b/crates/openhuman-core/src/security/approval/gate_state.rs
@@ -132,11 +132,8 @@ impl ApprovalGate {
let _ = tx.send(ApprovalDecision::Deny);
}
let route = self.take_request_route(&decided.request_id);
- if let Some(thread_id) = route.as_ref().and_then(|r| r.thread_id.as_deref()) {
- self.clear_thread_route_if_owned(
- &thread_route_key(Some(agent), thread_id),
- &decided.request_id,
- );
+ if let Some(thread_key) = route.as_ref().and_then(|r| r.thread_key.as_deref()) {
+ self.clear_thread_route_if_owned(thread_key, &decided.request_id);
}
BUS.publish(DomainEvent::ApprovalDecided {
request_id: decided.request_id.clone(),
@@ -304,19 +301,31 @@ impl ApprovalGate {
}
/// The request_id of the approval currently parked on `thread_id` by the
- /// ambient context's agent, if any. Used by the web channel to route an
- /// inbound yes/no reply to a decision.
+ /// ambient tenant (SaaS profile and/or embedded agent), if any. Used by
+ /// the web channel to route an inbound yes/no reply to a decision.
pub fn pending_for_thread(&self, thread_id: &str) -> Option<String> {
- let agent = crate::core::runtime::agent_scope::current_agent_id();
- self.pending_for_agent_thread(agent.as_deref(), thread_id)
+ self.pending_for_tenant_thread(&super::gate::current_route_tenant(), thread_id)
}
/// The request_id `agent` (`None`: the process) has parked on
- /// `thread_id`, if any.
+ /// `thread_id` outside any SaaS profile, if any.
pub fn pending_for_agent_thread(&self, agent: Option<&str>, thread_id: &str) -> Option<String> {
+ let tenant = crate::core::runtime::Tenant {
+ profile: None,
+ agent: agent.map(str::to_owned),
+ };
+ self.pending_for_tenant_thread(&tenant, thread_id)
+ }
+
+ /// The request_id `tenant` has parked on `thread_id`, if any.
+ pub fn pending_for_tenant_thread(
+ &self,
+ tenant: &crate::core::runtime::Tenant,
+ thread_id: &str,
+ ) -> Option<String> {
self.thread_to_request
.lock()
- .get(&thread_route_key(agent, thread_id))
+ .get(&thread_route_key(tenant, thread_id))
.cloned()
}