The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(app): verify the deep-link peer on the connected socket
M3gA-Mind7aa06fda8f19
diff --git a/crates/openhuman-app/Cargo.toml b/crates/openhuman-app/Cargo.tomlindex c90b419fcf..6f314bd5c8 100644--- a/crates/openhuman-app/Cargo.toml+++ b/crates/openhuman-app/Cargo.toml@@ -222,7 +222,7 @@ async-trait = "0.1"# rather than reaching for a heavier image stack.[target.'cfg(unix)'.dependencies]-nix = { version = "0.29", default-features = false, features = ["hostname", "signal", "user"] }+nix = { version = "0.29", default-features = false, features = ["hostname", "signal", "socket", "user"] }[target.'cfg(target_os = "macos")'.dependencies]objc2 = "0.6"diff --git a/crates/openhuman-app/src/deep_link_ipc.rs b/crates/openhuman-app/src/deep_link_ipc.rsindex 3d904d0422..7419fbf5ec 100644--- a/crates/openhuman-app/src/deep_link_ipc.rs+++ b/crates/openhuman-app/src/deep_link_ipc.rs@@ -110,24 +110,24 @@ 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 {+/// The owner uid of the socket file at `path`, or `None` when it does not+/// exist. The /tmp fallback path is in a shared directory, so another local+/// user can pre-create it.+fn socket_owner(path: &Path) -> Option<u32> {use std::os::unix::fs::MetadataExt;- std::fs::symlink_metadata(path).is_ok_and(|meta| meta.uid() == uid)+ std::fs::symlink_metadata(path).ok().map(|meta| meta.uid())+}++/// The uid of the process on the other end of a connected socket+/// (`SO_PEERCRED`). Checked on the stream itself, so a socket swapped in at the+/// path between any file check and `connect` cannot receive the URLs.+fn peer_uid(stream: &UnixStream) -> Option<u32> {+ nix::sys::socket::getsockopt(stream, nix::sys::socket::sockopt::PeerCredentials)+ .ok()+ .map(|credentials| credentials.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(),@@ -136,6 +136,15 @@ fn try_forward_urls_as(urls: &[String], path: &Path, uid: u32) -> ForwardResultmatch UnixStream::connect(path) {Ok(mut stream) => {+ // Only our own primary may receive `openhuman://auth?token=…`.+ if peer_uid(&stream) != Some(uid) {+ log::warn!(+ "[deep-link-ipc] secondary: socket at {} is held by another user; \+ not forwarding deep links to it",+ path.display()+ );+ return ForwardResult::NoPrimary;+ }stream.set_write_timeout(Some(Duration::from_secs(2))).ok();let result = forward_connected_stream(&mut stream, urls);if matches!(result, ForwardResult::Forwarded) {@@ -231,7 +240,9 @@ pub(crate) fn bind_and_listen() -> Option<DeepLinkSocketGuard> {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()) {+ // 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()) {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.rsindex 6904752731..9b4844ea53 100644--- a/crates/openhuman-app/src/deep_link_ipc_tests.rs+++ b/crates/openhuman-app/src/deep_link_ipc_tests.rs@@ -148,21 +148,23 @@ fn round_trip_bind_connect_forward() {}#[test]-fn a_socket_owned_by_another_user_receives_nothing() {+fn a_socket_held_by_another_user_receives_nothing() {+ use std::io::Read;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));+ assert_eq!(socket_owner(&sock_path), Some(me));+ assert_eq!(socket_owner(&tmp.path().join("missing.sock")), None);- // Forwarding as a different user: our socket is "foreign" to them.+ // Forwarding as a different user: our listener 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"- );+ // The peer check ran on the connected stream, so nothing was written.+ let (mut accepted, _) = listener.accept().unwrap();+ let mut received = String::new();+ accepted.read_to_string(&mut received).unwrap();+ assert!(received.is_empty(), "token leaked: {received:?}");}