Skip to content

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

fix(security): drop revoked devices when a pairing session is pending

Steven Enamakel2106dbd455d2
diff --git a/crates/openhuman-core/src/security/devices/owner.rs b/crates/openhuman-core/src/security/devices/owner.rs
index 20ed1124c3..738366f698 100644
--- a/crates/openhuman-core/src/security/devices/owner.rs
+++ b/crates/openhuman-core/src/security/devices/owner.rs
@@ -71,6 +71,17 @@ async fn has_live_device(channel_id: &str) -> Result<bool, String> {
.map_err(|error| error.to_string())
}
+/// Whether the current scope holds `channel_id` as a revoked device (a missing
+/// row is not revoked).
+async fn is_revoked(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. A channel no scope holds live but whose session
@@ -87,12 +98,34 @@ pub(super) async fn owner_of(
channel_id: &str,
pending: Option<&PairingSession>,
) -> Result<Option<String>, OwnerLookupFailed> {
- // A pending pairing names its agent only until the handshake completes;
- // once this process holds the channel's session cipher the device is
- // paired, and the store (not a leftover pairing session) decides whether
- // it is still live — it may have been revoked by another process.
- if let Some(session) = pending.filter(|_| !has_active_cipher(channel_id)) {
- return Ok(session.agent.clone());
+ // A pending pairing names its agent. Once this process holds the channel's
+ // session cipher the handshake has completed, and the session may be a
+ // leftover: another process sharing the backend can have revoked the
+ // device since. The session's own scope then decides — a revoked row drops
+ // the frame; no row yet (the first frame can race the row being persisted
+ // right after the handshake ACK) keeps the session's agent.
+ if let Some(session) = pending {
+ if !has_active_cipher(channel_id) {
+ return Ok(session.agent.clone());
+ }
+ let revoked =
+ crate::storage::agents::within_agent(session.agent.as_deref(), is_revoked(channel_id))
+ .await;
+ return match revoked {
+ Some(Ok(false)) => Ok(session.agent.clone()),
+ Some(Ok(true)) => Err(OwnerLookupFailed {
+ agent: session.agent.clone(),
+ error: "device is revoked".to_string(),
+ }),
+ Some(Err(error)) => Err(OwnerLookupFailed {
+ agent: session.agent.clone(),
+ error,
+ }),
+ None => Err(OwnerLookupFailed {
+ agent: session.agent.clone(),
+ error: "no context can act for the pairing agent".to_string(),
+ }),
+ };
}
if let Some(owner) = cached(channel_id) {
match crate::storage::agents::within_agent(owner.as_deref(), has_live_device(channel_id))
diff --git a/crates/openhuman-core/src/security/devices/owner_tests.rs b/crates/openhuman-core/src/security/devices/owner_tests.rs
index 254b5b6ce1..383b779ee6 100644
--- a/crates/openhuman-core/src/security/devices/owner_tests.rs
+++ b/crates/openhuman-core/src/security/devices/owner_tests.rs
@@ -163,22 +163,22 @@ async fn a_revoked_device_with_a_live_cipher_is_dropped() {
}
/// A pairing session left over after the handshake does not vouch for a device
-/// another process has since revoked: with the cipher live, the store decides.
+/// another process has since revoked, but a device whose row is not persisted
+/// yet (the first frame racing the handshake ACK) still belongs to the
+/// session's agent.
#[tokio::test]
async fn a_leftover_pairing_session_does_not_outlive_a_revocation() {
let tmp = tempfile::tempdir().unwrap();
+ let config = scoped_config(tmp.path());
let context = crate::core::runtime::CoreContext::for_test_with_config(
crate::core::runtime::DomainSet::full(),
- scoped_config(tmp.path()),
+ config.clone(),
);
- let pending = session(Some("agent-7"));
+ let pending = session(None);
// Before the handshake the session names the agent.
assert_eq!(
- owner_of("owner-test-leftover", Some(&pending))
- .await
- .unwrap()
- .as_deref(),
- Some("agent-7")
+ owner_of("owner-test-leftover", Some(&pending)).await,
+ Ok(None)
);
super::super::rpc::ACTIVE_CIPHERS.lock().unwrap().insert(
"owner-test-leftover".to_string(),
@@ -186,6 +186,17 @@ async fn a_leftover_pairing_session_does_not_outlive_a_revocation() {
super::super::crypto::TunnelCipher::new(&[9u8; 32]),
)),
);
+ // Cipher live, row not persisted yet: still the session's agent.
+ let racing = crate::core::runtime::CoreContext::scope(
+ std::sync::Arc::clone(&context),
+ owner_of("owner-test-leftover", Some(&pending)),
+ )
+ .await;
+ assert_eq!(racing, Ok(None));
+ // Cipher live, row revoked elsewhere: dropped.
+ super::super::store::insert_device(&config, "owner-test-leftover", "label", "pk", "hash")
+ .unwrap();
+ super::super::store::revoke_device(&config, "owner-test-leftover").unwrap();
let owner = crate::core::runtime::CoreContext::scope(
context,
owner_of("owner-test-leftover", Some(&pending)),