Skip to content

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

fix(app): don't forward Linux deep links to another user's socket

M3gA-Mind04ae40ee8910
diff --git a/crates/openhuman-app/src/deep_link_ipc.rs b/crates/openhuman-app/src/deep_link_ipc.rs
index 423cd77900..3d904d0422 100644
--- a/crates/openhuman-app/src/deep_link_ipc.rs
+++ b/crates/openhuman-app/src/deep_link_ipc.rs
@@ -107,6 +107,27 @@ pub(crate) fn try_forward_deep_links() -> ForwardResult {
}
fn try_forward_urls(urls: &[String], path: &Path) -> ForwardResult {
+ try_forward_urls_as(urls, path, nix::unistd::getuid().as_raw())
+}
+
+/// Whether the socket file at `path` belongs to `uid`. The /tmp fallback path
+/// is in a shared directory, so another local user can pre-create it to
+/// receive forwarded `openhuman://auth?token=…` links; only our own socket is
+/// trusted.
+fn socket_owned_by(path: &Path, uid: u32) -> bool {
+ use std::os::unix::fs::MetadataExt;
+ std::fs::symlink_metadata(path).is_ok_and(|meta| meta.uid() == uid)
+}
+
+fn try_forward_urls_as(urls: &[String], path: &Path, uid: u32) -> ForwardResult {
+ if path.exists() && !socket_owned_by(path, uid) {
+ log::warn!(
+ "[deep-link-ipc] secondary: socket at {} is owned by another user; \
+ not forwarding deep links to it",
+ path.display()
+ );
+ return ForwardResult::NoPrimary;
+ }
log::info!(
"[deep-link-ipc] secondary: found {} deep-link URL(s), trying socket at {}",
urls.len(),
@@ -209,6 +230,15 @@ pub(crate) fn bind_and_listen() -> 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.
+ if !socket_owned_by(&path, nix::unistd::getuid().as_raw()) {
+ log::warn!(
+ "[deep-link-ipc] socket {} is owned by another user; \
+ deep-link forwarding disabled for this session",
+ path.display()
+ );
+ return None;
+ }
// A socket file already exists. Probe whether a live primary
// is behind it before deciding to unlink.
match UnixStream::connect(&path) {
diff --git a/crates/openhuman-app/src/deep_link_ipc_tests.rs b/crates/openhuman-app/src/deep_link_ipc_tests.rs
index 33fa096fa5..6904752731 100644
--- a/crates/openhuman-app/src/deep_link_ipc_tests.rs
+++ b/crates/openhuman-app/src/deep_link_ipc_tests.rs
@@ -146,3 +146,23 @@ fn round_trip_bind_connect_forward() {
assert_eq!(got.len(), 1);
assert_eq!(got[0], "openhuman://auth?token=testtoken123");
}
+
+#[test]
+fn a_socket_owned_by_another_user_receives_nothing() {
+ let tmp = tempfile::TempDir::new().unwrap();
+ let sock_path = tmp.path().join("squatted-deeplink.sock");
+ let listener = UnixListener::bind(&sock_path).unwrap();
+ listener.set_nonblocking(true).unwrap();
+ let me = nix::unistd::getuid().as_raw();
+ assert!(socket_owned_by(&sock_path, me));
+
+ // Forwarding as a different user: our socket is "foreign" to them.
+ let urls = vec!["openhuman://auth?token=secret".to_string()];
+ let result = try_forward_urls_as(&urls, &sock_path, me.wrapping_add(1));
+
+ assert!(matches!(result, ForwardResult::NoPrimary));
+ assert!(
+ listener.accept().is_err(),
+ "nothing may connect to a socket owned by someone else"
+ );
+}