diff --git a/CHANGELOG.md b/CHANGELOG.md index 406bd146..61b63c19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - ui: Display restart reason in case of premature failure - daemon: Stop retrying a transport after 5 consecutive failures before user interaction; other transports keep running. +- daemon: Show the UI through the backend that xdg-desktop-portal selects, passed in the new `ui_backend` option - daemon: Treat Debian's Firefox ESR (app ID `firefox-esr`) as a privileged client, like the other Firefox builds - daemon: Build the list of devices offered to the user from libwebauthn's `available_transports()`, the same source discovery uses (#32) - ui: Pass the caller's activation token to the compositor when showing the dialog, so it can take focus when the requesting window is in the background (#152) diff --git a/credentialsd/src/dbus/flow_control.rs b/credentialsd/src/dbus/flow_control.rs index 3e4c7d3f..0446c613 100644 --- a/credentialsd/src/dbus/flow_control.rs +++ b/credentialsd/src/dbus/flow_control.rs @@ -18,6 +18,7 @@ use tokio::sync::oneshot; use tokio::sync::{Mutex as AsyncMutex, mpsc::Sender}; use tokio::task::AbortHandle; use zbus::connection::Connection; +use zbus::names::OwnedWellKnownName; use zbus::zvariant::OwnedObjectPath; use crate::dbus::UiControlServiceClient; @@ -38,6 +39,8 @@ pub struct UiRequestContext { /// Client window handle window_handle: Option, activation_token: Option, + /// UI backend chosen by the portal frontend, or `None` for the default. + ui_backend: Option, response_channel: oneshot::Sender>, } @@ -53,16 +56,17 @@ pub async fn start_flow_control_service, activation_token: Option, + ui_backend: Option, ) -> Result; } @@ -333,6 +338,7 @@ impl CredentialRequestController for CredentialRequestControllerClient { request: CredentialRequest, window_handle: Option, activation_token: Option, + ui_backend: Option, ) -> Result { let (tx, rx) = oneshot::channel(); self.initiator @@ -341,6 +347,7 @@ impl CredentialRequestController for CredentialRequestControllerClient { app, window_handle, activation_token, + ui_backend, response_channel: tx, }) .await diff --git a/credentialsd/src/dbus/ui_control.rs b/credentialsd/src/dbus/ui_control.rs index 43f0b1c9..d2fee9e5 100644 --- a/credentialsd/src/dbus/ui_control.rs +++ b/credentialsd/src/dbus/ui_control.rs @@ -10,7 +10,7 @@ use tokio_stream::StreamExt; use zbus::{ Connection, MatchRule, MessageStream, fdo::{self, DBusProxy}, - names::{OwnedUniqueName, WellKnownName}, + names::{OwnedUniqueName, OwnedWellKnownName, WellKnownName}, proxy, zvariant::{ObjectPath, Optional, OwnedFd, OwnedObjectPath}, }; @@ -410,11 +410,13 @@ trait CeremonySession { #[derive(Debug)] pub struct UiControlServiceClient { conn: Connection, + /// Backend to show the UI with; the default service if `None`. + backend: Option, } impl UiControlServiceClient { - pub fn new(conn: Connection) -> Self { - Self { conn } + pub fn new(conn: Connection, backend: Option) -> Self { + Self { conn, backend } } } @@ -431,7 +433,19 @@ impl UiController for UiControlServiceClient { options: PortalBackendOptions, ) -> Result> { let (from_ui_tx, from_ui_rx) = mpsc::channel(32); - let backend_proxy = UiControlServiceProxy::new(&self.conn).await?; + let backend_proxy = match &self.backend { + Some(backend) => { + UiControlServiceProxy::builder(&self.conn) + .destination(backend.clone())? + .build() + .await? + } + None => UiControlServiceProxy::new(&self.conn).await?, + }; + tracing::debug!( + backend = %backend_proxy.as_ref().destination(), + "Launching UI" + ); let dbus_proxy = DBusProxy::new(&self.conn).await?; // Calling `create_session()` below would activate the service automatically, // but we want to subscribe to UI-events before creating a session, and filter @@ -446,7 +460,7 @@ impl UiController for UiControlServiceClient { Ok(sender) => sender, Err(fdo::Error::NameHasNoOwner(_)) => { let service_name = WellKnownName::try_from(destination.clone()) - .expect("UiControl destination is a well-known name"); + .expect("UI backend destination is a well-known name"); dbus_proxy.start_service_by_name(service_name, 0).await?; dbus_proxy.get_name_owner(destination).await? } diff --git a/credentialsd/src/gateway/dbus.rs b/credentialsd/src/gateway/dbus.rs index a39f63d5..bc1dbb66 100644 --- a/credentialsd/src/gateway/dbus.rs +++ b/credentialsd/src/gateway/dbus.rs @@ -10,7 +10,7 @@ use tokio::sync::Mutex as AsyncMutex; use zbus::{ Connection, DBusError, interface, message::Header, - names::{BusName, UniqueName}, + names::{BusName, OwnedWellKnownName, UniqueName}, zvariant::{DeserializeDict, Optional, Type, Value}, }; @@ -78,6 +78,7 @@ impl CredentialPortalGateway { activation_token, top_origin, public_key, + ui_backend, } = options; let request_json = match (&cred_type, public_key) { @@ -100,6 +101,10 @@ impl CredentialPortalGateway { Ok(context) => context, Err(err) => return Err(err).into(), }; + let ui_backend = match parse_ui_backend(ui_backend) { + Ok(ui_backend) => ui_backend, + Err(err) => return Err(err).into(), + }; tracing::debug!( ?context, @@ -117,7 +122,13 @@ impl CredentialPortalGateway { .gateway_service .lock() .await - .handle_create_credential(request, context, parent_window.into(), activation_token) + .handle_create_credential( + request, + context, + parent_window.into(), + activation_token, + ui_backend, + ) .await .map_err(Error::from); @@ -138,6 +149,7 @@ impl CredentialPortalGateway { activation_token, top_origin, public_key, + ui_backend, } = options; let app_validation_result = validate_app_details( connection, @@ -157,6 +169,10 @@ impl CredentialPortalGateway { Ok(context) => context, Err(err) => return Err(err).into(), }; + let ui_backend = match parse_ui_backend(ui_backend) { + Ok(ui_backend) => ui_backend, + Err(err) => return Err(err).into(), + }; tracing::trace!( ?context, @@ -173,7 +189,13 @@ impl CredentialPortalGateway { .gateway_service .lock() .await - .handle_get_credential(request, context, parent_window.into(), activation_token) + .handle_get_credential( + request, + context, + parent_window.into(), + activation_token, + ui_backend, + ) .await .map_err(Error::from); response.into() @@ -209,6 +231,11 @@ struct CreateCredentialPortalOptions { /// [PublicKeyCredentialRequestOptions](https://www.w3.org/TR/webauthn-3/#publickeycredential) /// type. public_key: Option, + + /// D-Bus name of the `org.freedesktop.impl.portal.experimental.Credential` + /// backend to show the UI with, as chosen by the portal configuration. + /// If omitted, the default UI is used. + ui_backend: Option, } #[derive(Debug, DeserializeDict, Type)] @@ -225,6 +252,11 @@ struct GetCredentialPortalOptions { /// [PublicKeyCredentialRequestOptions](https://www.w3.org/TR/webauthn-3/#publickeycredential) /// type. public_key: Option, + + /// D-Bus name of the `org.freedesktop.impl.portal.experimental.Credential` + /// backend to show the UI with, as chosen by the portal configuration. + /// If omitted, the default UI is used. + ui_backend: Option, } #[allow(clippy::enum_variant_names)] @@ -331,6 +363,21 @@ impl From> for PortalResult { } } +/// Validates the UI backend name passed by the portal frontend. Call this only +/// after `validate_app_details`: only trusted callers get that far, so any valid +/// well-known name is accepted. +fn parse_ui_backend(ui_backend: Option) -> Result, Error> { + ui_backend + .filter(|name| !name.is_empty()) + .map(|name| { + OwnedWellKnownName::try_from(name.as_str()).map_err(|err| { + tracing::warn!(%err, "Invalid UI backend name passed: {name}"); + Error::SecurityError + }) + }) + .transpose() +} + async fn validate_app_details( connection: &Connection, header: &Header<'_>, @@ -429,3 +476,33 @@ async fn query_peer_pidfd( ) .ok() } + +#[cfg(test)] +mod test { + use super::parse_ui_backend; + + #[test] + fn test_ui_backend_defaults_when_missing_or_empty() { + assert_eq!(parse_ui_backend(None).unwrap(), None); + assert_eq!(parse_ui_backend(Some(String::new())).unwrap(), None); + } + + #[test] + fn test_ui_backend_accepts_well_known_name() { + let backend = parse_ui_backend(Some("org.freedesktop.impl.portal.desktop.gnome".into())) + .unwrap() + .unwrap(); + assert_eq!( + backend.as_str(), + "org.freedesktop.impl.portal.desktop.gnome" + ); + } + + #[test] + fn test_ui_backend_rejects_invalid_names() { + // Unique names would bypass activation and the owner checks. + for name in [":1.42", "not a name", "nodots", "/org/freedesktop"] { + assert!(parse_ui_backend(Some(name.into())).is_err(), "{name}"); + } + } +} diff --git a/credentialsd/src/gateway/mod.rs b/credentialsd/src/gateway/mod.rs index e88c5ffd..7c66e570 100644 --- a/credentialsd/src/gateway/mod.rs +++ b/credentialsd/src/gateway/mod.rs @@ -16,6 +16,7 @@ use credentialsd_common::model::WindowHandle; use tokio::sync::Mutex as AsyncMutex; use zbus::{ Connection, + names::OwnedWellKnownName, zvariant::{DeserializeDict, NoneValue, OwnedValue, SerializeDict, Type}, }; @@ -83,6 +84,7 @@ impl GatewayService { context: RequestContext, parent_window: Option, activation_token: Option, + ui_backend: Option, ) -> Result { let request_environment = validate_request(&context)?; @@ -115,6 +117,7 @@ impl GatewayService { cred_request, parent_window, activation_token, + ui_backend, ) .await?; @@ -148,6 +151,7 @@ impl GatewayService { context: RequestContext, parent_window: Option, activation_token: Option, + ui_backend: Option, ) -> Result { let request_environment = validate_request(&context)?; @@ -177,6 +181,7 @@ impl GatewayService { cred_request, parent_window, activation_token, + ui_backend, ) .await?; diff --git a/doc/api.md b/doc/api.md index b1812c93..571f75d2 100644 --- a/doc/api.md +++ b/doc/api.md @@ -205,6 +205,7 @@ CreateCredentialRequest( IN options a{sv} { activation_token: s top_origin: s + ui_backend: s }, IN app_id s, @@ -240,6 +241,21 @@ suffix, as defined by the [Public Suffix List][PSL]. [web-origins]: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-tuple [PSL]: https://github.com/publicsuffix/list +#### UI backend + +`options.ui_backend` is the D-Bus name of the +`org.freedesktop.impl.portal.experimental.Credential` backend that shows the UI +for this request, for example `org.freedesktop.impl.portal.desktop.gnome`. +xdg-desktop-portal sets it to the backend chosen by the portal configuration +(`portals.conf` and the `.portal` files); applications can't set it. If it is +omitted or empty, the daemon uses `xyz.iinuwa.credentialsd.UiControl`. It must +be a well-known name, otherwise the request is rejected. + +A backend such as a desktop's portal keeps its name for the whole session, so +the daemon can't tell from the bus that its dialog was closed, only that the +backend crashed. The backend must report a closed dialog itself, by sending +`Closed` for the session. + #### Credential Request Types ##### WebAuthn Credential Request @@ -323,6 +339,7 @@ GetCredentialRequest ( IN options a{sv} { activation_token: s top_origin: s + ui_backend: s public_key: s }, @@ -342,8 +359,8 @@ there is no `type` field, and credential types are specified using the optional #### Request Context -The `GetCredential()` `origin` and `options.top_origin` have the same semantics and -restrictions as in `CreateCredential()` described above. +The `GetCredential()` `origin`, `options.top_origin` and `options.ui_backend` have +the same semantics and restrictions as in `CreateCredential()` described above. When multiple credential types are specified, the request context applies to all credentials.