Skip to content

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

test(security): add approval gate tenant isolation tests

Steven Enamakelc4d352479850
diff --git a/crates/openhuman-core/src/security/approval/gate_tenant_tests.rs b/crates/openhuman-core/src/security/approval/gate_tenant_tests.rs
new file mode 100644
index 0000000000..a6e9c69d94
--- /dev/null
+++ b/crates/openhuman-core/src/security/approval/gate_tenant_tests.rs
@@ -0,0 +1,104 @@
+//! Approval routes key on the tenant: two SaaS profiles' default agents (no
+//! agent id) parked on one thread id never share a route, and the desktop's
+//! keys stay byte-identical.
+
+use super::*;
+use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet, Tenant};
+
+fn tenant(profile: Option<&str>, agent: Option<&str>) -> Tenant {
+ Tenant {
+ profile: profile.map(str::to_owned),
+ agent: agent.map(str::to_owned),
+ }
+}
+
+/// A profile's context as the profile host derives it: a profile, no agent.
+fn profile_ctx(profile: &str) -> Arc<CoreContext> {
+ let parent = CoreContext::for_test(DomainSet::full(), Some(std::env::temp_dir()));
+ parent.derive_with(
+ ContextOverlay::new(
+ Config::default(),
+ DomainSet::kernel(),
+ crate::tools::toolpacks::ToolGroups::none(),
+ )
+ .profile(profile),
+ )
+}
+
+#[test]
+fn desktop_route_keys_are_unchanged() {
+ assert_eq!(thread_route_key(&tenant(None, None), "t-1"), "t-1");
+ assert_eq!(
+ thread_route_key(&tenant(None, Some("alpha")), "t-1"),
+ "alpha\u{1f}t-1"
+ );
+}
+
+#[test]
+fn profile_route_keys_are_distinct_per_profile_and_from_the_desktop() {
+ let a = thread_route_key(&tenant(Some("alice"), None), "t-1");
+ let b = thread_route_key(&tenant(Some("bob"), None), "t-1");
+ assert_ne!(a, b);
+ assert_ne!(a, "t-1");
+ assert_ne!(a, thread_route_key(&tenant(Some("alice"), Some("alice")), "t-1"));
+ assert!(a.starts_with('\u{1e}'), "a profile key is marked: {a:?}");
+}
+
+fn park_as(
+ gate: &Arc<ApprovalGate>,
+ ctx: Arc<CoreContext>,
+) -> tokio::task::JoinHandle<(GateOutcome, Option<String>)> {
+ let gate = Arc::clone(gate);
+ tokio::spawn(CoreContext::scope(
+ ctx,
+ turn_origin::with_origin(
+ web_origin(),
+ APPROVAL_CHAT_CONTEXT.scope(chat_ctx(), async move {
+ gate.intercept_audited("shell", "run it", serde_json::json!({}))
+ .await
+ }),
+ ),
+ ))
+}
+
+async fn parked_for(gate: &ApprovalGate, tenant: &Tenant) -> String {
+ for _ in 0..200 {
+ if let Some(request_id) = gate.pending_for_tenant_thread(tenant, "t-test") {
+ return request_id;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ panic!("{tenant:?} never parked");
+}
+
+#[tokio::test]
+async fn two_profiles_default_agents_park_on_one_thread_id_apart() {
+ let (gate, _dir) = test_gate();
+ let gate = Arc::new(gate);
+ let (alice, bob) = (profile_ctx("alice"), profile_ctx("bob"));
+
+ let alice_turn = park_as(&gate, Arc::clone(&alice));
+ let bob_turn = park_as(&gate, Arc::clone(&bob));
+ let alice_request = parked_for(&gate, &tenant(Some("alice"), None)).await;
+ let bob_request = parked_for(&gate, &tenant(Some("bob"), None)).await;
+ assert_ne!(alice_request, bob_request);
+
+ // Each profile's own lookup finds only its own request.
+ let seen_by = |ctx: Arc<CoreContext>| {
+ let gate = Arc::clone(&gate);
+ CoreContext::scope(ctx, async move { gate.pending_for_thread("t-test") })
+ };
+ assert_eq!(seen_by(Arc::clone(&alice)).await, Some(alice_request.clone()));
+ assert_eq!(seen_by(Arc::clone(&bob)).await, Some(bob_request.clone()));
+ // The process's own (unprofiled) route for the thread stays empty.
+ assert!(gate.pending_for_agent_thread(None, "t-test").is_none());
+
+ gate.decide(&alice_request, ApprovalDecision::Deny).unwrap();
+ gate.decide(&bob_request, ApprovalDecision::Deny).unwrap();
+ let (alice_outcome, _) = alice_turn.await.unwrap();
+ let (bob_outcome, _) = bob_turn.await.unwrap();
+ assert!(matches!(alice_outcome, GateOutcome::Deny { .. }), "{alice_outcome:?}");
+ assert!(matches!(bob_outcome, GateOutcome::Deny { .. }), "{bob_outcome:?}");
+ assert!(seen_by(alice).await.is_none(), "a decision clears the route");
+ assert!(seen_by(bob).await.is_none());
+}
diff --git a/crates/openhuman-core/src/security/approval/gate_tests.rs b/crates/openhuman-core/src/security/approval/gate_tests.rs
index 3704352a6d..9ed13415a6 100644
--- a/crates/openhuman-core/src/security/approval/gate_tests.rs
+++ b/crates/openhuman-core/src/security/approval/gate_tests.rs
@@ -210,3 +210,5 @@ mod origin_intercept_tests;
mod triage_tests;
#[path = "gate_ttl_and_triage_tests.rs"]
mod ttl_and_triage_tests;
+#[path = "gate_tenant_tests.rs"]
+mod tenant_tests;