Skip to content

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

fix(devices): ignore stale pairing sessions once the cipher is live

Steven Enamakel03a1a96cea4e
diff --git a/crates/openhuman-core/src/integrations/task_sources/bus_tests.rs b/crates/openhuman-core/src/integrations/task_sources/bus_tests.rs
index eab7292570..c86e59feeb 100644
--- a/crates/openhuman-core/src/integrations/task_sources/bus_tests.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/bus_tests.rs
@@ -60,9 +60,10 @@ async fn an_unreadable_scope_is_skipped() {
fire_for_connection(&config, ProviderSlug::Github, "github", "conn-1").await;
}
-/// The subscriber handles a connection event through each scope's own
-/// configuration: toolkits that are not task sources are ignored, and a
-/// scope whose configuration disables task sources fires nothing.
+/// Smoke test: the subscriber handles connection events through the scope's
+/// own configuration without panicking or hanging — a non-task-source toolkit
+/// and an unrelated event are ignored, a task-source toolkit is handled. (The
+/// firing itself is asserted by the test above.)
#[tokio::test]
async fn a_connection_event_is_handled_per_scope() {
let _lock = crate::config::TEST_ENV_LOCK.lock().await;
diff --git a/crates/openhuman-core/src/security/devices/owner.rs b/crates/openhuman-core/src/security/devices/owner.rs
index 27446f08cb..20ed1124c3 100644
--- a/crates/openhuman-core/src/security/devices/owner.rs
+++ b/crates/openhuman-core/src/security/devices/owner.rs
@@ -38,6 +38,14 @@ fn cached(channel_id: &str) -> Option<Option<String>> {
.cloned()
}
+/// Whether this process holds a post-handshake session cipher for the channel.
+fn has_active_cipher(channel_id: &str) -> bool {
+ super::rpc::ACTIVE_CIPHERS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .contains_key(channel_id)
+}
+
fn forget(channel_id: &str) {
OWNERS
.lock()
@@ -79,7 +87,11 @@ pub(super) async fn owner_of(
channel_id: &str,
pending: Option<&PairingSession>,
) -> Result<Option<String>, OwnerLookupFailed> {
- if let Some(session) = pending {
+ // 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());
}
if let Some(owner) = cached(channel_id) {
@@ -113,16 +125,10 @@ pub(super) async fn owner_of(
// session cipher: it was revoked or removed elsewhere (another process
// sharing the backend). Dropping the frame, rather than handling it as
// `local`, keeps the revoked device from gaining the operator's scope.
- None if super::rpc::ACTIVE_CIPHERS
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .contains_key(channel_id) =>
- {
- Err(OwnerLookupFailed {
- agent: None,
- error: "device is revoked or no longer paired".to_string(),
- })
- }
+ None if has_active_cipher(channel_id) => Err(OwnerLookupFailed {
+ agent: None,
+ error: "device is revoked or no longer paired".to_string(),
+ }),
None => Ok(None),
}
}
diff --git a/crates/openhuman-core/src/security/devices/owner_tests.rs b/crates/openhuman-core/src/security/devices/owner_tests.rs
index bc55687a7b..254b5b6ce1 100644
--- a/crates/openhuman-core/src/security/devices/owner_tests.rs
+++ b/crates/openhuman-core/src/security/devices/owner_tests.rs
@@ -161,3 +161,39 @@ async fn a_revoked_device_with_a_live_cipher_is_dropped() {
.remove("owner-test-cipher");
assert!(owner.is_err(), "{owner:?}");
}
+
+/// 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.
+#[tokio::test]
+async fn a_leftover_pairing_session_does_not_outlive_a_revocation() {
+ let tmp = tempfile::tempdir().unwrap();
+ let context = crate::core::runtime::CoreContext::for_test_with_config(
+ crate::core::runtime::DomainSet::full(),
+ scoped_config(tmp.path()),
+ );
+ let pending = session(Some("agent-7"));
+ // Before the handshake the session names the agent.
+ assert_eq!(
+ owner_of("owner-test-leftover", Some(&pending))
+ .await
+ .unwrap()
+ .as_deref(),
+ Some("agent-7")
+ );
+ super::super::rpc::ACTIVE_CIPHERS.lock().unwrap().insert(
+ "owner-test-leftover".to_string(),
+ std::sync::Arc::new(std::sync::Mutex::new(
+ super::super::crypto::TunnelCipher::new(&[9u8; 32]),
+ )),
+ );
+ let owner = crate::core::runtime::CoreContext::scope(
+ context,
+ owner_of("owner-test-leftover", Some(&pending)),
+ )
+ .await;
+ super::super::rpc::ACTIVE_CIPHERS
+ .lock()
+ .unwrap()
+ .remove("owner-test-leftover");
+ assert!(owner.is_err(), "{owner:?}");
+}