Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions credentialsd/src/dbus/flow_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +39,8 @@ pub struct UiRequestContext {
/// Client window handle
window_handle: Option<WindowHandle>,
activation_token: Option<String>,
/// UI backend chosen by the portal frontend, or `None` for the default.
ui_backend: Option<OwnedWellKnownName>,
response_channel: oneshot::Sender<Result<CredentialResponse, CredentialServiceError>>,
}

Expand All @@ -53,16 +56,17 @@ pub async fn start_flow_control_service<M: ManageDevice + Debug + Send + Sync +
while let Some(ui_request_ctx) = listener.recv().await {
let svc = svc2.clone();

let ui_control_client = UiControlServiceClient::new(conn.clone());

let UiRequestContext {
request,
app,
window_handle,
activation_token,
ui_backend,
response_channel,
} = ui_request_ctx;

let ui_control_client = UiControlServiceClient::new(conn.clone(), ui_backend);

let response = handle(
svc,
ui_control_client,
Expand Down Expand Up @@ -318,6 +322,7 @@ pub trait CredentialRequestController {
request: CredentialRequest,
window_handle: Option<WindowHandle>,
activation_token: Option<String>,
ui_backend: Option<OwnedWellKnownName>,
) -> Result<CredentialResponse, WebAuthnError>;
}

Expand All @@ -333,6 +338,7 @@ impl CredentialRequestController for CredentialRequestControllerClient {
request: CredentialRequest,
window_handle: Option<WindowHandle>,
activation_token: Option<String>,
ui_backend: Option<OwnedWellKnownName>,
) -> Result<CredentialResponse, WebAuthnError> {
let (tx, rx) = oneshot::channel();
self.initiator
Expand All @@ -341,6 +347,7 @@ impl CredentialRequestController for CredentialRequestControllerClient {
app,
window_handle,
activation_token,
ui_backend,
response_channel: tx,
})
.await
Expand Down
24 changes: 19 additions & 5 deletions credentialsd/src/dbus/ui_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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<OwnedWellKnownName>,
}

impl UiControlServiceClient {
pub fn new(conn: Connection) -> Self {
Self { conn }
pub fn new(conn: Connection, backend: Option<OwnedWellKnownName>) -> Self {
Self { conn, backend }
}
}

Expand All @@ -431,7 +433,19 @@ impl UiController for UiControlServiceClient {
options: PortalBackendOptions,
) -> Result<Ceremony, Box<dyn Error>> {
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
Expand All @@ -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?
}
Expand Down
83 changes: 80 additions & 3 deletions credentialsd/src/gateway/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -78,6 +78,7 @@ impl CredentialPortalGateway {
activation_token,
top_origin,
public_key,
ui_backend,
} = options;

let request_json = match (&cred_type, public_key) {
Expand All @@ -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,
Expand All @@ -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);

Expand All @@ -138,6 +149,7 @@ impl CredentialPortalGateway {
activation_token,
top_origin,
public_key,
ui_backend,
} = options;
let app_validation_result = validate_app_details(
connection,
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -209,6 +231,11 @@ struct CreateCredentialPortalOptions {
/// [PublicKeyCredentialRequestOptions](https://www.w3.org/TR/webauthn-3/#publickeycredential)
/// type.
public_key: Option<String>,

/// 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<String>,
}

#[derive(Debug, DeserializeDict, Type)]
Expand All @@ -225,6 +252,11 @@ struct GetCredentialPortalOptions {
/// [PublicKeyCredentialRequestOptions](https://www.w3.org/TR/webauthn-3/#publickeycredential)
/// type.
public_key: Option<String>,

/// 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<String>,
}

#[allow(clippy::enum_variant_names)]
Expand Down Expand Up @@ -331,6 +363,21 @@ impl<T, E> From<Result<T, E>> for PortalResult<T, E> {
}
}

/// 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<String>) -> Result<Option<OwnedWellKnownName>, 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<'_>,
Expand Down Expand Up @@ -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}");
}
}
}
5 changes: 5 additions & 0 deletions credentialsd/src/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -83,6 +84,7 @@ impl GatewayService {
context: RequestContext,
parent_window: Option<WindowHandle>,
activation_token: Option<String>,
ui_backend: Option<OwnedWellKnownName>,
) -> Result<CreateCredentialResponse, WebAuthnError> {
let request_environment = validate_request(&context)?;

Expand Down Expand Up @@ -115,6 +117,7 @@ impl GatewayService {
cred_request,
parent_window,
activation_token,
ui_backend,
)
.await?;

Expand Down Expand Up @@ -148,6 +151,7 @@ impl GatewayService {
context: RequestContext,
parent_window: Option<WindowHandle>,
activation_token: Option<String>,
ui_backend: Option<OwnedWellKnownName>,
) -> Result<GetCredentialResponse, WebAuthnError> {
let request_environment = validate_request(&context)?;

Expand Down Expand Up @@ -177,6 +181,7 @@ impl GatewayService {
cred_request,
parent_window,
activation_token,
ui_backend,
)
.await?;

Expand Down
21 changes: 19 additions & 2 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ CreateCredentialRequest(
IN options a{sv} {
activation_token: s
top_origin: s
ui_backend: s
<type_specific_fields>
},
IN app_id s,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -323,6 +339,7 @@ GetCredentialRequest (
IN options a{sv} {
activation_token: s
top_origin: s
ui_backend: s
<type_specific_fields>
public_key: s
},
Expand All @@ -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.
Expand Down