diff --git a/Cargo.toml b/Cargo.toml index f6fa2fe..556f8f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,9 @@ tokio = { version = "1", features = ["net", "test-util"] } workspace = true [workspace.lints.rust] -unsafe_code = "forbid" +# `deny` rather than `forbid`, so the one start-up environment write in +# `src/main.rs` can state its reason with `expect`. +unsafe_code = "deny" ambiguous_negative_literals = "deny" explicit_outlives_requirements = "deny" ffi_unwind_calls = "deny" diff --git a/src/credentials.rs b/src/credentials.rs index 486e439..6216238 100644 --- a/src/credentials.rs +++ b/src/credentials.rs @@ -143,6 +143,38 @@ pub async fn remove(origin: &Origin) -> Result { } } +/// The session bus address for the Secret Service keychain when the +/// environment names no bus. The keychain library finds the bus only through +/// `DBUS_SESSION_BUS_ADDRESS` or `$XDG_RUNTIME_DIR/bus`, and some coding agents +/// start MCP servers with neither variable. systemd and elogind serve the user +/// bus at `/run/user//bus`. +#[cfg(target_os = "linux")] +pub fn default_session_bus() -> Option { + use std::os::unix::fs::MetadataExt as _; + + let named = |name: &str| std::env::var_os(name).is_some_and(|value| !value.is_empty()); + let environment_names_bus = named("DBUS_SESSION_BUS_ADDRESS") || named("XDG_RUNTIME_DIR"); + // `/proc/self` belongs to the effective user, who owns the session bus. + let uid = std::fs::metadata("/proc/self").ok()?.uid(); + let bus = std::path::Path::new("/run/user") + .join(uid.to_string()) + .join("bus"); + user_bus_address(environment_names_bus, &bus) +} + +/// `unix:path=` when the environment names no bus and `bus` is a socket. +/// A numeric user bus path needs no D-Bus address escaping. +#[cfg(target_os = "linux")] +fn user_bus_address(environment_names_bus: bool, bus: &std::path::Path) -> Option { + use std::os::unix::fs::FileTypeExt as _; + + if environment_names_bus { + return None; + } + let socket = std::fs::metadata(bus).ok()?.file_type().is_socket(); + socket.then(|| format!("unix:path={}", bus.display())) +} + /// Keychain calls block on the platform's IPC, so they run off the runtime. async fn blocking( call: impl FnOnce() -> keyring::Result + Send + 'static, @@ -151,3 +183,30 @@ async fn blocking( .await .unwrap_or_else(|join| Err(keyring::Error::PlatformFailure(Box::new(join)))) } + +#[cfg(all(test, target_os = "linux"))] +mod tests { + use super::*; + + #[test] + fn user_bus_is_used_only_when_the_environment_names_no_bus() { + let directory = tempfile::tempdir().unwrap(); + let bus = directory.path().join("bus"); + let _listener = std::os::unix::net::UnixListener::bind(&bus).unwrap(); + + assert_eq!( + user_bus_address(false, &bus), + Some(format!("unix:path={}", bus.display())) + ); + // An address or runtime directory in the environment stays in charge. + assert_eq!(user_bus_address(true, &bus), None); + + let file = directory.path().join("file"); + std::fs::write(&file, b"").unwrap(); + assert_eq!(user_bus_address(false, &file), None); + assert_eq!( + user_bus_address(false, &directory.path().join("missing")), + None + ); + } +} diff --git a/src/main.rs b/src/main.rs index 6bb62f6..63a5fd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use proofline_cli::{Cli, Verdict, run}; use secrecy::SecretString; fn main() -> ExitCode { + use_user_session_bus(); proofline_cli::update::on_invocation(); let cli = match Cli::try_parse() { Ok(cli) => cli, @@ -56,3 +57,25 @@ fn main() -> ExitCode { } } } + +/// Points the keychain at the user's session bus when the environment names +/// none, as when a coding agent starts `proofline mcp` with a reduced +/// environment. The keychain library reads the bus address only from the +/// environment. +#[cfg(target_os = "linux")] +#[expect( + unsafe_code, + reason = "the keychain library takes its bus address only from the environment" +)] +fn use_user_session_bus() { + if let Some(address) = proofline_cli::credentials::default_session_bus() { + // SAFETY: `main` calls this before it starts any thread, so nothing + // reads or writes the environment concurrently. + unsafe { + std::env::set_var("DBUS_SESSION_BUS_ADDRESS", address); + } + } +} + +#[cfg(not(target_os = "linux"))] +fn use_user_session_bus() {}