From a279e04881c84434a9cd1c720d8f1b226cb5e6dc Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Tue, 7 Apr 2026 08:22:28 -0600 Subject: [PATCH] feat(credentials): use XDG portal secret for key derivation Add per-app secret from org.freedesktop.portal.Secret as entropy in the AES-256-GCM key derivation. DE-agnostic (works with GNOME, KDE, etc.) and Flatpak-safe (portals auto-accessible in sandbox). Falls back to machine_id + salt when portal is unavailable (headless, old DEs, non-Linux). Cached for session lifetime via OnceLock. Users will need to re-login once on upgrade (key derivation changed). cc --- src-tauri/Cargo.toml | 4 ++-- src-tauri/src/credentials/mod.rs | 40 +++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index acce31188..b3389baa8 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "qbz-nix" -version = "1.2.3" +version = "1.2.4" description = "Native high-fidelity client for Qobuz™ subscribers on Linux with Hi-Fi audio support (uses the Qobuz API; not certified by Qobuz)" authors = ["blitzkriegfc"] edition = "2021" @@ -145,7 +145,7 @@ futures-util = "0.3" # Platform-specific dependencies [target.'cfg(target_os = "linux")'.dependencies] tauri-plugin-dialog = { version = "2.6.0" } -ashpd = { version = "0.13", default-features = false, features = ["tokio", "notification", "inhibit"] } +ashpd = { version = "0.13", default-features = false, features = ["tokio", "notification", "inhibit", "secret"] } mupdf = { version = "0.6", features = ["sys-lib-zlib", "sys-lib-harfbuzz", "sys-lib-freetype"] } [target.'cfg(target_os = "macos")'.dependencies] diff --git a/src-tauri/src/credentials/mod.rs b/src-tauri/src/credentials/mod.rs index 2d295b666..3c9f422cb 100644 --- a/src-tauri/src/credentials/mod.rs +++ b/src-tauri/src/credentials/mod.rs @@ -154,13 +154,51 @@ fn get_machine_id() -> Result, String> { load_or_create_machine_id_fallback() } -/// Derive encryption key from machine ID +/// Retrieve per-app secret from XDG Desktop Portal (cached for session lifetime). +/// Returns None if portal is unavailable (headless, old DEs, non-Linux). +#[cfg(target_os = "linux")] +fn get_portal_secret() -> Option> { + use std::sync::OnceLock; + static PORTAL_SECRET: OnceLock>> = OnceLock::new(); + PORTAL_SECRET + .get_or_init(|| { + let rt = tokio::runtime::Handle::try_current().ok()?; + let (tx, rx) = std::sync::mpsc::channel(); + rt.spawn(async move { + let _ = tx.send(ashpd::desktop::secret::retrieve().await.ok()); + }); + match rx.recv_timeout(std::time::Duration::from_secs(3)) { + Ok(secret) => { + if secret.is_some() { + log::info!("[Credentials] Using XDG portal secret for key derivation"); + } + secret + } + Err(_) => { + log::debug!("[Credentials] XDG portal secret unavailable (timeout/missing)"); + None + } + } + }) + .clone() +} + +/// Derive encryption key from XDG portal secret + machine ID + installation salt. +/// Portal secret adds DE-agnostic, Flatpak-safe entropy when available. fn derive_key() -> Result<[u8; 32], String> { let machine_id = get_machine_id()?; let installation_salt = load_or_create_installation_salt()?; + #[cfg(target_os = "linux")] + let portal_secret = get_portal_secret(); + #[cfg(not(target_os = "linux"))] + let portal_secret: Option> = None; + let mut hasher = Sha256::new(); hasher.update(&installation_salt); + if let Some(ref secret) = portal_secret { + hasher.update(secret); + } hasher.update(&machine_id); hasher.update(&installation_salt);