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
This commit is contained in:
vicrodh committed 2026-04-07 08:22:28 -06:00
1 parent 6da5092605
commit a279e04881
2 files changed
+41 -3

No files matched your search

+2 -2
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "qbz-nix" 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)" 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"] authors = ["blitzkriegfc"]
edition = "2021" edition = "2021"
@@ -145,7 +145,7 @@ futures-util = "0.3"
# Platform-specific dependencies # Platform-specific dependencies
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
tauri-plugin-dialog = { version = "2.6.0" } 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"] } mupdf = { version = "0.6", features = ["sys-lib-zlib", "sys-lib-harfbuzz", "sys-lib-freetype"] }
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
+39 -1
View File
@@ -154,13 +154,51 @@ fn get_machine_id() -> Result<Vec<u8>, String> {
load_or_create_machine_id_fallback() 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<Vec<u8>> {
use std::sync::OnceLock;
static PORTAL_SECRET: OnceLock<Option<Vec<u8>>> = 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> { fn derive_key() -> Result<[u8; 32], String> {
let machine_id = get_machine_id()?; let machine_id = get_machine_id()?;
let installation_salt = load_or_create_installation_salt()?; 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<Vec<u8>> = None;
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();
hasher.update(&installation_salt); hasher.update(&installation_salt);
if let Some(ref secret) = portal_secret {
hasher.update(secret);
}
hasher.update(&machine_id); hasher.update(&machine_id);
hasher.update(&installation_salt); hasher.update(&installation_salt);