Skip to content

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

test(app): cover refusing another user's deep-link socket at bind

M3gA-Mind10dd16869ed8
diff --git a/crates/openhuman-app/src/deep_link_ipc.rs b/crates/openhuman-app/src/deep_link_ipc.rs
index 7419fbf5ec..e4861fc0d7 100644
--- a/crates/openhuman-app/src/deep_link_ipc.rs
+++ b/crates/openhuman-app/src/deep_link_ipc.rs
@@ -234,15 +234,17 @@ impl Drop for DeepLinkSocketGuard {
/// unconditionally removes a live primary's socket file: we only remove the
/// file when we can confirm it is stale (connect fails).
pub(crate) fn bind_and_listen() -> Option<DeepLinkSocketGuard> {
- let path = socket_path();
+ bind_and_listen_as(socket_path(), nix::unistd::getuid().as_raw())
+}
+fn bind_and_listen_as(path: PathBuf, uid: u32) -> Option<DeepLinkSocketGuard> {
let listener = match UnixListener::bind(&path) {
Ok(l) => l,
Err(e) if e.kind() == std::io::ErrorKind::AddrInUse => {
// Another user's socket is neither our primary nor ours to unlink.
// One that vanished since `bind` (an old instance exiting) falls
// through to the rebind below.
- if socket_owner(&path).is_some_and(|owner| owner != nix::unistd::getuid().as_raw()) {
+ if socket_owner(&path).is_some_and(|owner| owner != uid) {
log::warn!(
"[deep-link-ipc] socket {} is owned by another user; \
deep-link forwarding disabled for this session",
diff --git a/crates/openhuman-app/src/deep_link_ipc_tests.rs b/crates/openhuman-app/src/deep_link_ipc_tests.rs
index 9b4844ea53..67e3bd7954 100644
--- a/crates/openhuman-app/src/deep_link_ipc_tests.rs
+++ b/crates/openhuman-app/src/deep_link_ipc_tests.rs
@@ -168,3 +168,19 @@ fn a_socket_held_by_another_user_receives_nothing() {
accepted.read_to_string(&mut received).unwrap();
assert!(received.is_empty(), "token leaked: {received:?}");
}
+
+#[test]
+fn another_users_socket_is_neither_bound_over_nor_unlinked() {
+ let tmp = tempfile::TempDir::new().unwrap();
+ let sock_path = tmp.path().join("foreign-deeplink.sock");
+ let listener = UnixListener::bind(&sock_path).unwrap();
+ let me = nix::unistd::getuid().as_raw();
+
+ // Binding as a different user: the existing socket is "foreign" to them.
+ assert!(bind_and_listen_as(sock_path.clone(), me.wrapping_add(1)).is_none());
+
+ // The owner's socket is still there and still serves.
+ assert!(sock_path.exists());
+ let _client = UnixStream::connect(&sock_path).unwrap();
+ listener.accept().unwrap();
+}