Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ impl OpenHumanSecurityGate {
.unwrap_or_else(|| self.policy.clone())
}

/// Hand the answers TinySecurity gave for `evaluated` to the constructor
/// policy, which the session's tools hold, when the two are different
/// instances. The tools' sync gates read only their own cached answers.
///
/// `false` when `evaluated` sets the tools' questions differently (the
/// autonomy settings changed during the session): the tools would check
/// paths against settings no longer in force, so the caller refuses.
fn share_answers(&self, evaluated: &Arc<SecurityPolicy>) -> bool {
if Arc::ptr_eq(evaluated, &self.policy) {
return true;
}
if !self.policy.decides_tools_like(evaluated) {
tracing::warn!(
target: "tinyagents",
"[tinyagents::host::security] the autonomy settings changed during this session; refusing the call"
);
return false;
}
self.policy.adopt_answers_from(evaluated);
true
}

/// The origin of the turn this gate authorizes for: the run's own context
/// when it carries one, else the calling task's turn scope.
fn turn_origin(&self) -> Option<crate::core::runtime::turn_origin::AgentTurnOrigin> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use super::*;

/// Why a call is refused when the session's tools hold settings no longer in
/// force. A new conversation builds its tools from the current settings.
const STALE_SESSION_SETTINGS: &str = "[policy-blocked] The autonomy settings changed during this conversation. Start a new conversation to use the new settings.";

impl OpenHumanSecurityGate {
pub(super) async fn authorize_tool_with_facts(
&self,
Expand Down Expand Up @@ -203,6 +207,9 @@ impl OpenHumanSecurityGate {
// (`crate::security::switch`) skips the same stages: there is no
// module to classify the action or keep the budget.
if policy.unchecked() {
if !self.share_answers(&policy) {
return Ok(GateDecision::deny(STALE_SESSION_SETTINGS));
}
return Ok(self.settled(channel_approved));
}

Expand Down Expand Up @@ -268,6 +275,9 @@ impl OpenHumanSecurityGate {
if let Err(reason) = budget {
return Ok(GateDecision::deny(reason));
}
if !self.share_answers(&policy) {
return Ok(GateDecision::deny(STALE_SESSION_SETTINGS));
}
channel_approved = approved;

tracing::debug!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,37 @@ async fn prompt_screening_uses_the_bound_dispatch_configuration() {
})
.await;
}

/// TS-E2E-025: the session's tools hold the constructor policy, but the gate
/// answers against the live one. When the live settings no longer match the
/// session's (here the policy was switched off after the session was built),
/// the tools would validate paths against settings no longer in force, so the
/// call is refused and nothing is handed to the tools.
#[cfg(feature = "security-module")]
#[tokio::test]
async fn a_call_is_refused_when_the_autonomy_settings_changed_during_the_session() {
let session = Arc::new(SecurityPolicy {
enabled: true,
..SecurityPolicy::default()
});
let live = Arc::new(SecurityPolicy {
enabled: false,
..SecurityPolicy::default()
});
let _live = crate::security::live_policy::install_scoped(
live,
std::path::PathBuf::new(),
std::path::PathBuf::new(),
);

let decision = OpenHumanSecurityGate::new(session.clone(), registry())
.authorize_tool(&req("write_file", json!({ "path": "notes.md" })))
.await
.unwrap();

assert!(
matches!(&decision, GateDecision::Deny { reason } if reason.contains("settings changed")),
"stale session settings must be refused: {decision:?}"
);
assert!(!session.can_act(), "a refused call hands the tools nothing");
}
64 changes: 64 additions & 0 deletions crates/openhuman-core/src/security/policy/enforcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,70 @@ impl SecurityPolicy {
)
}

/// Whether `other` sets what the tools' own gates act on (the tier, path
/// scope, forbidden paths and privacy) the same way. When it does not, the
/// autonomy settings changed after this instance was built, and tools
/// holding it would validate against settings no longer in force.
///
/// Trusted roots are left out: the session's own policy adds the working
/// folder and the projects home as roots, so they differ legitimately.
pub(crate) fn decides_tools_like(&self, other: &SecurityPolicy) -> bool {
self.enabled == other.enabled
&& self.autonomy == other.autonomy
&& self.privacy_mode == other.privacy_mode
&& self.workspace_only == other.workspace_only
&& self.forbidden_paths == other.forbidden_paths
}

/// Take `governing`'s cached TinySecurity answers (budget and class gates),
/// so the sync tool gates holding this instance read the decision the host
/// gate made on `governing`. When `governing` is unchecked it allows
/// everything, which is what this instance's tools are told too.
///
/// Each [`from_config`](Self::from_config) owns its cells, so the session
/// policy the tools hold and the live or agent policy the host gate answers
/// against are separate. Without this, an enabled tool policy never gets an
/// answer and refuses as read-only.
pub(crate) fn adopt_answers_from(&self, governing: &SecurityPolicy) {
use super::types::{CommandClass, GateDecision};
// Read before writing, so cells shared by a clone never lock twice.
let (budget, gates) = if governing.unchecked() {
let all = [
CommandClass::Read,
CommandClass::Write,
CommandClass::Network,
CommandClass::Install,
CommandClass::Destructive,
];
(
Some(tinysecurity_bus::BudgetDecision2::Allow),
all.map(|class| (class, GateDecision::Allow)).to_vec(),
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} else {
let budget = governing
.budget_snapshot
.lock()
.map(|snapshot| snapshot.clone())
.unwrap_or(None);
let gates = governing
.class_gate_snapshot
.lock()
.map(|snapshot| snapshot.clone())
.unwrap_or_default();
Comment thread
M3gA-Mind marked this conversation as resolved.
Resolved
(budget, gates)
};
log::debug!(
"[openhuman:policy] tool policy adopted the host gate's answers: budget={budget:?} classes={}",
gates.len()
);
if let Ok(mut snapshot) = self.budget_snapshot.lock() {
*snapshot = budget;
}
if let Ok(mut snapshot) = self.class_gate_snapshot.lock() {
*snapshot = gates;
Comment thread
M3gA-Mind marked this conversation as resolved.
Resolved
}
}

/// Cached class decision from TinySecurity for synchronous tool adapters.
/// The async host gate refreshes it before the tool is dispatched; an
/// enabled policy with no answer fails closed.
Expand Down
68 changes: 68 additions & 0 deletions crates/openhuman-core/src/security/policy/policy_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,71 @@ fn an_unchecked_host_action_is_classed_from_its_permission_level() {
Class::Destructive
);
}

/// TS-E2E-025: a policy takes another instance's cached TinySecurity answers,
/// so the tools read what the host gate was told. Adopting from a clone, which
/// shares the cells, must not deadlock.
#[cfg(feature = "security-module")]
#[test]
fn a_policy_adopts_the_answers_another_instance_was_given() {
let governing = default_policy();
*governing.budget_snapshot.lock().unwrap() = Some(tinysecurity_bus::BudgetDecision2::Allow);
governing
.class_gate_snapshot
.lock()
.unwrap()
.push((CommandClass::Write, GateDecision::Prompt));
let tools = default_policy();
assert!(!tools.can_act());

tools.adopt_answers_from(&governing);
assert!(tools.can_act());
assert_eq!(
tools.gate_decision(CommandClass::Write),
GateDecision::Prompt
);
assert_eq!(
tools.gate_decision(CommandClass::Install),
GateDecision::Block
);

let shared = tools.clone();
tools.adopt_answers_from(&shared);
assert!(tools.can_act());
}

/// TS-E2E-025: the settings the tools act on decide whether a live policy's
/// answers may be handed to a session's own instance.
#[test]
fn tool_settings_match_only_when_tier_scope_forbidden_paths_and_privacy_agree() {
let session = default_policy();
assert!(session.decides_tools_like(&default_policy()));

let forbidden = SecurityPolicy {
forbidden_paths: vec!["/srv/secret".into()],
..default_policy()
};
assert!(
!session.decides_tools_like(&forbidden),
"a newly forbidden path"
);
let disabled = SecurityPolicy {
enabled: false,
..default_policy()
};
assert!(
!session.decides_tools_like(&disabled),
"the policy switched off"
);
let rooted = SecurityPolicy {
trusted_roots: vec![TrustedRoot {
path: "/work".into(),
access: crate::security::policy::TrustedAccess::ReadWrite,
}],
..default_policy()
};
assert!(
session.decides_tools_like(&rooted),
"a session's own working-folder root is not a settings change"
);
}
Loading