Skip to content

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

fix(security): enforce approval gate on intercepted tool calls

Steven Enamakele680d8955b14
diff --git a/crates/openhuman-core/src/security/approval/gate.rs b/crates/openhuman-core/src/security/approval/gate.rs
index b7a0cb8cd7..ca5825ed94 100644
--- a/crates/openhuman-core/src/security/approval/gate.rs
+++ b/crates/openhuman-core/src/security/approval/gate.rs
@@ -104,7 +104,7 @@ const DEFAULT_APPROVAL_TTL: Duration = Duration::from_secs(60 * 10);
/// still re-trigger the run from the Runs rail). Scoped by
/// [`ApprovalTurnContext::copilot_stream`]. Deliberately NOT applied to
/// main-chat `WebChat` parks — only `flows::ops::flows_build`'s streaming
-/// branch scopes the task-local below.
+/// branch sets the flag.
const COPILOT_APPROVAL_TTL: Duration = Duration::from_secs(180);
/// Shorter park window for approvals raised inside a sub-agent run — a
diff --git a/crates/openhuman-core/src/security/approval/gate_intercept.rs b/crates/openhuman-core/src/security/approval/gate_intercept.rs
index 3087ce7b3e..a7bc3e7621 100644
--- a/crates/openhuman-core/src/security/approval/gate_intercept.rs
+++ b/crates/openhuman-core/src/security/approval/gate_intercept.rs
@@ -15,6 +15,7 @@ impl ApprovalGate {
#[inline(never)]
fn intercept_audited_inner<'a>(
&'a self,
+ turn: &'a ApprovalTurnContext,
tool_name: &'a str,
action_summary: &'a str,
args_redacted: serde_json::Value,
@@ -24,6 +25,7 @@ impl ApprovalGate {
forced: bool,
) -> futures::future::BoxFuture<'a, (GateOutcome, Option<String>)> {
Box::pin(self.intercept_audited_inner_body(
+ turn,
tool_name,
action_summary,
args_redacted,
@@ -37,6 +39,7 @@ impl ApprovalGate {
#[allow(clippy::too_many_arguments)]
async fn intercept_audited_inner_body(
&self,
+ turn: &ApprovalTurnContext,
tool_name: &str,
action_summary: &str,
args_redacted: serde_json::Value,
@@ -108,7 +111,7 @@ impl ApprovalGate {
..
}
),
- chat_context = APPROVAL_CHAT_CONTEXT.try_with(|c| c.clone()).is_ok(),
+ chat_context = turn.chat.is_some(),
"[approval::gate] evaluating approval request"
);
@@ -262,9 +265,7 @@ impl ApprovalGate {
// chat turn never completes.
// Forced approvals route from the validated WebChat origin itself. A
// stray task-local must not redirect the decision to another thread.
- let chat_ctx = (!forced)
- .then(|| APPROVAL_CHAT_CONTEXT.try_with(|c| c.clone()).ok())
- .flatten();
+ let chat_ctx = (!forced).then(|| turn.chat.clone()).flatten();
let origin_chat_route = match &origin {
AgentTurnOrigin::WebChat {
thread_id,
@@ -287,7 +288,7 @@ impl ApprovalGate {
if detached {
tracing::debug!(
tool = tool_name,
- "[approval::gate] APPROVAL_CHAT_CONTEXT absent on a WebChat turn — routing the \
+ "[approval::gate] no chat context on a WebChat turn — routing the \
approval via the origin's thread/client (async-delegated sub-agent path, #5499); \
marking the card detached so it survives the parent turn's end"
);
@@ -310,7 +311,7 @@ impl ApprovalGate {
// Copilot-streaming context — set by `flows::ops::flows_build` around
// the streaming `run_single` call. Presence alone clamps the park
// window to `COPILOT_APPROVAL_TTL`; see that task-local's doc.
- let copilot_stream = !forced && APPROVAL_COPILOT_STREAM_CONTEXT.try_with(|_| ()).is_ok();
+ let copilot_stream = !forced && turn.copilot_stream;
// Branch by origin. Web chat parks for an in-app approval; external
// channel persists an audit row and TTL-denies (no routable approval
@@ -475,13 +476,14 @@ impl ApprovalGate {
AgentTurnOrigin::TrustedAutomation {
source: TrustedAutomationSource::Workflow { .. },
job_id: flow_id,
- } => APPROVAL_FLOW_RUN_CONTEXT
- .try_with(|ctx| ApprovalSourceContext::Flow {
+ } => turn
+ .flow_run
+ .as_ref()
+ .map(|ctx| ApprovalSourceContext::Flow {
flow_id: flow_id.clone(),
run_id: ctx.run_id.clone(),
node_id: None,
- })
- .ok(),
+ }),
_ => None,
};
diff --git a/crates/openhuman-core/src/security/approval/gate_setup.rs b/crates/openhuman-core/src/security/approval/gate_setup.rs
index eab85b3d7a..6585b81738 100644
--- a/crates/openhuman-core/src/security/approval/gate_setup.rs
+++ b/crates/openhuman-core/src/security/approval/gate_setup.rs
@@ -129,13 +129,14 @@ impl ApprovalGate {
/// rows when both sides report in. See #2135.
pub async fn intercept(
&self,
+ turn: &ApprovalTurnContext,
tool_name: &str,
action_summary: &str,
args_redacted: serde_json::Value,
) -> GateOutcome {
// Drop the request_id; callers using the legacy entry point
// don't record execution.
- self.intercept_audited(tool_name, action_summary, args_redacted)
+ self.intercept_audited(turn, tool_name, action_summary, args_redacted)
.await
.0
}
@@ -150,7 +151,11 @@ impl ApprovalGate {
args_redacted: serde_json::Value,
) -> GateOutcome {
let mut _park_bound_elapsed = false;
+ // A forced approval routes from the validated WebChat origin alone and
+ // never reads a chat context, a copilot flag or a flow run, so it takes
+ // none.
self.intercept_audited_inner(
+ &ApprovalTurnContext::default(),
tool_name,
action_summary,
args_redacted,
@@ -178,11 +183,12 @@ impl ApprovalGate {
/// whether to invoke `record_execution`.
pub async fn intercept_audited(
&self,
+ turn: &ApprovalTurnContext,
tool_name: &str,
action_summary: &str,
args_redacted: serde_json::Value,
) -> (GateOutcome, Option<String>) {
- self.intercept_audited_for_call(tool_name, action_summary, args_redacted, None)
+ self.intercept_audited_for_call(turn, tool_name, action_summary, args_redacted, None)
.await
}
@@ -194,6 +200,7 @@ impl ApprovalGate {
/// not driven through the tinyagents harness).
pub async fn intercept_audited_for_call(
&self,
+ turn: &ApprovalTurnContext,
tool_name: &str,
action_summary: &str,
args_redacted: serde_json::Value,
@@ -204,6 +211,7 @@ impl ApprovalGate {
// path, so the out-flag stays `false` and is discarded here.
let mut _park_bound_elapsed = false;
self.intercept_audited_inner(
+ turn,
tool_name,
action_summary,
args_redacted,
@@ -234,6 +242,7 @@ impl ApprovalGate {
/// [`Self::intercept_audited`] and always returns `Some`.
pub async fn intercept_audited_bounded(
&self,
+ turn: &ApprovalTurnContext,
tool_name: &str,
action_summary: &str,
args_redacted: serde_json::Value,
@@ -242,6 +251,7 @@ impl ApprovalGate {
let mut park_bound_elapsed = false;
let resolved = self
.intercept_audited_inner(
+ turn,
tool_name,
action_summary,
args_redacted,