Skip to content

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

fix(security): fail closed when a device owner has no acting context

Steven Enamakeld2a725b913ed
diff --git a/crates/openhuman-core/src/flows/ops/run_management.rs b/crates/openhuman-core/src/flows/ops/run_management.rs
index 7d7171a0ce..1781aa533a 100644
--- a/crates/openhuman-core/src/flows/ops/run_management.rs
+++ b/crates/openhuman-core/src/flows/ops/run_management.rs
@@ -306,15 +306,17 @@ pub(crate) enum BootSweepPlan {
}
/// The boot sweep's scopes. On a backend other processes share (MongoDB) a
-/// `running` row below the boot floor can belong to a run another replica is
-/// still driving, and sweeping it would drop that run's checkpoint, so the
-/// agent scopes are left alone there, as the agent run reaper does — and in
-/// SaaS mode, where every scope is an agent's, nothing is swept.
+/// `running` row below the boot floor — `local` or an agent's — can belong to
+/// a run another replica is still driving, and sweeping it would drop that
+/// run's checkpoint, so nothing is swept there, as the agent run reaper does.
pub(crate) fn boot_sweep_plan(shared: bool, saas: bool) -> BootSweepPlan {
- match (shared, saas) {
- (false, _) => BootSweepPlan::EveryScope,
- (true, false) => BootSweepPlan::LocalOnly,
- (true, true) => BootSweepPlan::Nothing,
+ // `saas` is kept for the call site's sake: a shared backend is skipped
+ // in either mode, since another replica can own even a `local` run.
+ let _ = saas;
+ if shared {
+ BootSweepPlan::Nothing
+ } else {
+ BootSweepPlan::EveryScope
}
}
diff --git a/crates/openhuman-core/src/security/devices/bus.rs b/crates/openhuman-core/src/security/devices/bus.rs
index d2dc9ef608..5c422da9a7 100644
--- a/crates/openhuman-core/src/security/devices/bus.rs
+++ b/crates/openhuman-core/src/security/devices/bus.rs
@@ -94,11 +94,18 @@ impl EventHandler<DomainEvent> for DeviceTunnelSubscriber {
.cloned();
match super::owner::owner_of(channel_id, pending.as_ref()).await {
Ok(owner) => {
- crate::storage::agents::within_agent(
+ let handled = crate::storage::agents::within_agent(
owner.as_deref(),
handle_tunnel_frame(channel_id, payload_b64),
)
.await;
+ // Fail closed: no context can act for the owner.
+ if handled.is_none() {
+ log::warn!(
+ "[devices/bus] dropping tunnel frame channel_id={channel_id}: \
+ its owner has no context to act under"
+ );
+ }
}
// Fail closed: an unknown owner must not become `local`.
Err(failed) => log::warn!(
diff --git a/crates/openhuman-core/src/security/devices/owner.rs b/crates/openhuman-core/src/security/devices/owner.rs
index 569401dca0..4867abc1cb 100644
--- a/crates/openhuman-core/src/security/devices/owner.rs
+++ b/crates/openhuman-core/src/security/devices/owner.rs
@@ -38,19 +38,37 @@ fn cached(channel_id: &str) -> Option<Option<String>> {
.cloned()
}
-/// A scope's device lookup failed, and no other scope has the device, so its
-/// owner is unknown. The frame is dropped rather than handled as `local`:
-/// guessing could run a paired device's RPCs as the wrong agent.
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub(super) struct OwnerLookupFailed {
- /// The scope whose lookup failed (`None` = `local`) and why.
- pub(super) agent: Option<String>,
- pub(super) error: String,
+fn forget(channel_id: &str) {
+ OWNERS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .remove(channel_id);
+}
+
+/// A failed owner lookup (`crate::storage::agents::LookupFailed`): no scope
+/// has the device and a scope's lookup failed, so its owner is unknown. The
+/// frame is dropped rather than handled as `local`: guessing could run a
+/// paired device's RPCs as the wrong agent.
+pub(super) type OwnerLookupFailed = crate::storage::agents::LookupFailed;
+
+/// Whether the current scope holds `channel_id` as a live (not revoked)
+/// paired device. The configuration is loaded inside the scope: in SaaS mode
+/// loading it needs an acting agent, which the tunnel task does not have.
+async fn has_live_device(channel_id: &str) -> Result<bool, String> {
+ let config = crate::config::rpc::load_config_with_timeout()
+ .await
+ .map_err(|error| format!("load config: {error}"))?;
+ super::store::get_device(&config, channel_id)
+ .map(|device| device.is_some_and(|device| !device.revoked))
+ .map_err(|error| error.to_string())
}
/// The agent `channel_id` belongs to, resolved as described above: `None`
/// for `local`, which is also where a channel no scope knows yet (a handshake
-/// still in flight) is handled.
+/// still in flight, or a revoked device) is handled.
+///
+/// A remembered owner is re-checked in its scope each time, so a revoked,
+/// deleted or re-paired channel is resolved afresh.
///
/// # Errors
///
@@ -64,17 +82,16 @@ pub(super) async fn owner_of(
return Ok(session.agent.clone());
}
if let Some(owner) = cached(channel_id) {
- return Ok(owner);
- }
- // The configuration is loaded inside each scope: in SaaS mode loading it
- // needs an acting agent, which this tunnel task does not have.
- let lookups = crate::storage::agents::for_each_scope("device owner", || async {
- let config = crate::config::rpc::load_config_with_timeout()
+ match crate::storage::agents::within_agent(owner.as_deref(), has_live_device(channel_id))
.await
- .map_err(|error| format!("load config: {error}"))?;
- super::store::get_device(&config, channel_id)
- .map(|device| device.is_some())
- .map_err(|error| error.to_string())
+ {
+ Some(Ok(true)) => return Ok(owner),
+ Some(Err(error)) => return Err(OwnerLookupFailed { agent: owner, error }),
+ Some(Ok(false)) | None => forget(channel_id),
+ }
+ }
+ let lookups = crate::storage::agents::for_each_scope("device owner", || {
+ has_live_device(channel_id)
})
.await;
let owner = decide(lookups)?;
@@ -88,26 +105,11 @@ pub(super) async fn owner_of(
Ok(owner.flatten())
}
-/// The owner from each scope's lookup: the first scope that has the device
-/// (`Some(Some(agent))` / `Some(None)` for `local`); `None` when none has it
-/// and every lookup succeeded; an error when none has it and one failed.
+/// The owner from each scope's lookup (`crate::storage::agents::decide`).
fn decide(
lookups: Vec<(Option<String>, Result<bool, String>)>,
) -> Result<Option<Option<String>>, OwnerLookupFailed> {
- let mut failure = None;
- for (agent, lookup) in lookups {
- match lookup {
- Ok(true) => return Ok(Some(agent)),
- Ok(false) => {}
- Err(error) => {
- failure.get_or_insert(OwnerLookupFailed { agent, error });
- }
- }
- }
- match failure {
- Some(failure) => Err(failure),
- None => Ok(None),
- }
+ crate::storage::agents::decide(lookups)
}
#[cfg(test)]