From 3bce605eaa3df68370701ef40fa9e73ac33f71f1 Mon Sep 17 00:00:00 2001 From: Jeff Zhang Date: Tue, 3 Nov 2026 01:25:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(host):=20=E4=B8=8A=E6=8A=A5=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=A3=81=E7=BA=B8=EF=BC=8C=E8=A1=A5=E9=BD=90=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=8D=A1=E7=89=87=E7=BC=A9=E7=95=A5=E5=9B=BE=20(#153)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(host): report system wallpaper for device cards * fix(host): handle quoted wallpaper paths and KDE defaults * docs: align wallpaper guidance and disclose default uploads * fix(host): validate active KDE plugin for stored wallpapers * fix(host): support Plasma 5 wallpaper config queries * fix(host): exclude Plasma panels from wallpaper reporting * fix(host): follow image symlinks in KDE wallpaper packages --------- Co-authored-by: Jeff Zhang <2854276+ferstar@users.noreply.github.com> --- README.en.md | 7 + README.md | 3 + docs/wallpaper-protocol.md | 43 ++++ src/main.rs | 4 + src/wallpaper.rs | 427 +++++++++++++++++++++++++++++++++++++ 5 files changed, 484 insertions(+) create mode 100644 docs/wallpaper-protocol.md create mode 100644 src/wallpaper.rs diff --git a/README.en.md b/README.en.md index 8700da9..43d2d7b 100644 --- a/README.en.md +++ b/README.en.md @@ -25,6 +25,12 @@ ## Appearance / 外观 +Signed-in graphical hosts report the system wallpaper for disconnected device cards in official clients +(no desktop-window screenshot). GNOME/Cinnamon, KDE and Xfce are supported; checks run every 60 seconds +and unchanged content is not uploaded again. Other desktops can set `UU_WALLPAPER=/absolute/path/image.png` +(PNG/JPEG/WebP). Set `UU_WALLPAPER_UPLOAD=0` to disable; guest and headless hosts skip reporting. +See [protocol evidence and limitations](docs/wallpaper-protocol.md). + Choose **System / Light / Dark** (default: System) in the viewer's **Settings → General → Appearance**, or via the gear settings button in host / tool windows. The choice is saved to `$XDG_CONFIG_HOME/uuremote/theme` (default `~/.config/uuremote/theme`). @@ -458,6 +464,7 @@ flowchart LR ## Security and privacy +- By default, signed-in graphical hosts upload the current wallpaper image to UU's file service for device-card backgrounds. Set `UU_WALLPAPER_UPLOAD=0` to disable this upload. - Logs never contain keystrokes, terminal text, clipboard contents, file contents, passwords or tokens. - For auto unlock the password is stored in the **viewer** (the official client). The host only checks it once with PAM; it is never written to disk or logs. diff --git a/README.md b/README.md index c595a85..cedbb30 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ ## 外观 / Appearance +已登录的图形主机会上报系统壁纸,供官方客户端的设备卡片在未连接时显示背景图(不是桌面窗口截图)。支持 GNOME/Cinnamon、KDE、Xfce;每 60 秒检查一次,内容不变则不重复上传。其他桌面可用 `UU_WALLPAPER=/绝对路径/图片.png` 指定 PNG/JPEG/WebP;`UU_WALLPAPER_UPLOAD=0` 关闭。访客和无桌面模式跳过上报。协议依据与限制见 [设备壁纸上报](docs/wallpaper-protocol.md)。 + 在控制端的 **设置 → 常规 → 外观**,或主机 / 工具窗口的齿轮设置按钮中选择 **跟随系统 / 浅色 / 深色**(默认跟随系统)。 选择保存在 `$XDG_CONFIG_HOME/uuremote/theme`(默认 `~/.config/uuremote/theme`)。 @@ -402,6 +404,7 @@ flowchart LR ## 安全与隐私 +- 已登录的图形主机默认会将当前壁纸图片上传到 UU 的文件服务,供设备卡片显示背景;可用 `UU_WALLPAPER_UPLOAD=0` 关闭上传。 - 日志不记录键盘输入、终端内容、剪贴板内容、文件内容、密码和令牌。 - 自动解锁的密码保存在**控制端**(官方客户端)里,被控端只拿来做一次 PAM 校验,不落盘、不写日志。 - 能连上这台机器的,只有登录了同一个 UU 账号的设备。请保管好你的 UU 账号。 diff --git a/docs/wallpaper-protocol.md b/docs/wallpaper-protocol.md new file mode 100644 index 0000000..951ec60 --- /dev/null +++ b/docs/wallpaper-protocol.md @@ -0,0 +1,43 @@ +# Device-card wallpaper reporting + +The official Windows host publishes the system wallpaper for device cards. This is separate from +the Linux viewer's local last-frame cache and does not require capturing desktop windows on disconnect. + +## Static protocol evidence + +Read-only analysis of the locally installed `GameViewerServer.exe` (SHA-256 +`af3a15c3887e06bfa9506971e01cd8ec58f41efe8ac9eb5831e31dae1fc1cef8`) found: + +- `ApplicationManager::updateWallpaper`, `getWallpaperPath`, `uploadWallpaper`, and log strings + `wallpaper check`, `wallpaper is not changed, the same md5`, `wallpaper file upload succeeded`. + The source path recorded in the executable is `server_core/business/apps/application_manager.cpp`. +- `0x140ade430`: GET `/api/v1/tool/fp/token?filetype=1`. Response parser `0x140ae3410` + reads `token`, `req_url`, `ttl`, optional `max_size` from API data. +- `0x140aeb8c0`: POST raw file bytes to `req_url`, with `Authorization` set directly to the + file token (no Bearer prefix, multipart wrapper, or account credentials). +- Upload callback `0x140ad1010` requires HTTP 200; parser `0x140ae1550` reads top-level + `fsize`, `url`, `md5`, `mime`. The wallpaper callback `0x1404206c0` uses `url`. +- `0x140b48d80`: POST `/api/v1/device/wallpaper`, JSON `{"wallpaper_url":""}`. + Device response parsers and the official viewer also contain `wallpaper_url`. + +Addresses are preferred virtual addresses for that executable only. No official binary, assets, +account data, upload tokens, desktop pictures, or account URLs are included here. +This establishes the wallpaper flow statically; it does not prove that every official client's +last-frame display uses this flow or establish the official timer interval. + +## Linux behavior + +Signed-in graphical hosts check the configured wallpaper on startup and every 60 seconds, upload +only changed supported image bytes, and retry failures on a later check. Guest and headless hosts skip +reporting. Account renewal replaces the reporting task; stopping the host cancels it. Network work +does not block signaling or alter peer connections. Images are bounded to 10 MiB and the file service's +advertised limit. HTTPS is required and upload redirects are disabled. + +GNOME/Unity/Cinnamon use GSettings (GNOME dark preference included); KDE reads the first image +wallpaper containment, including image packages, or the installed look-and-feel's default wallpaper +when no explicit wallpaper is configured; Xfce reads the first `last-image`. Multi-monitor selection, slideshow/XML +wallpapers, compositor-specific wallpaper daemons, and unsupported formats are not inferred. +`UU_WALLPAPER=/absolute/path/image.png` selects a file explicitly; `UU_WALLPAPER_UPLOAD=0` disables +reporting. PNG, JPEG, and WebP are supported. No desktop screenshot fallback is used. + +Only success/failure is logged. Paths, hashes, URLs, response bodies and credentials stay out of logs. diff --git a/src/main.rs b/src/main.rs index 611ab89..7c3908e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod ws_transport; mod engine_plugin; mod doctor; mod wol; +mod wallpaper; mod desktop_backend; mod desktop_mode; mod desktop_session; @@ -775,6 +776,7 @@ async fn serve(mut capture: Arc, input: Arc, shutd client }; + let mut wallpaper_task = AbortOnDrop(wallpaper::start(auth.clone())); // One peer per viewer connection (desktop, file transfer, terminal...), keyed by ice_id. let mut next_capture_type: Option<(u64,crate::session_ice::SessionIce)> = None; let mut capture_types: HashMap = HashMap::new(); @@ -812,6 +814,8 @@ async fn serve(mut capture: Arc, input: Arc, shutd log::error!("UU login is invalid or expired; a new QR login is required"); let stale = config::read_fresh().ok().flatten().and_then(|f| f.get("token").and_then(|t| t.as_str()).map(str::to_owned)); auth = Arc::new(UUAuthClient::new(login::wait_for_credentials(stale).await)); + wallpaper_task.0.abort(); + wallpaper_task = AbortOnDrop(wallpaper::start(auth.clone())); continue; } Err(e) => { diff --git a/src/wallpaper.rs b/src/wallpaper.rs new file mode 100644 index 0000000..42ea937 --- /dev/null +++ b/src/wallpaper.rs @@ -0,0 +1,427 @@ +//! Official device-card background reporting. Protocol evidence: docs/wallpaper-protocol.md. +use std::{path::PathBuf, sync::Arc, time::Duration}; +use sha2::{Digest, Sha256}; +use tokio::io::AsyncReadExt; +use crate::auth::UUAuthClient; + +const LIMIT: usize = 10 * 1024 * 1024; +const TIMEOUT: Duration = Duration::from_secs(20); + +pub fn start(auth: Arc) -> tokio::task::AbortHandle { + tokio::spawn(async move { + if auth.creds.user_id.is_empty() || crate::desktop_backend::headless() + || std::env::var("UU_WALLPAPER_UPLOAD").as_deref() == Ok("0") { return; } + let mut previous = None; + loop { + if let Some(path) = source().await { + match read_image(&path).await { + Ok(image) => { + let hash = Sha256::digest(&image).to_vec(); + if previous.as_ref() != Some(&hash) { + match publish(&auth, image).await { + Ok(()) => { previous = Some(hash); log::info!("Device wallpaper synchronized"); } + Err(stage) => log::warn!("Device wallpaper synchronization failed: stage={stage}; will retry"), + } + } + } + Err(()) => log::debug!("Device wallpaper unavailable or unsupported"), + } + } + tokio::time::sleep(Duration::from_secs(60)).await; + } + }).abort_handle() +} + +async fn command(program: &str, args: &[&str]) -> Option { + let output = tokio::time::timeout(Duration::from_secs(3), tokio::process::Command::new(program) + .args(args).kill_on_drop(true).output()).await.ok()?.ok()?; + output.status.success().then(|| String::from_utf8(output.stdout).ok()).flatten() +} + +async fn kde_config(programs: &[&str], args: &[&str]) -> Option { + for program in programs { + if let Some(value) = command(program, args).await { return Some(value); } + } + None +} + +async fn source() -> Option { + if let Some(path) = std::env::var_os("UU_WALLPAPER") { + let path = PathBuf::from(path); return path.is_absolute().then_some(path); + } + let desktop = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().to_lowercase(); + if desktop.contains("gnome") || desktop.contains("unity") || desktop.contains("cinnamon") { + let schema = if desktop.contains("cinnamon") { "org.cinnamon.desktop.background" } else { "org.gnome.desktop.background" }; + if schema == "org.gnome.desktop.background" && command("gsettings", &["get", "org.gnome.desktop.interface", "color-scheme"]) + .await.is_some_and(|v| v.contains("prefer-dark")) { + if let Some(path) = command("gsettings", &["get", schema, "picture-uri-dark"]).await.and_then(|v| gsettings_path(&v)) { return Some(path); } + } + return command("gsettings", &["get", schema, "picture-uri"]).await.and_then(|v| gsettings_path(&v)); + } + if desktop.contains("kde") { + let config = std::env::var_os("XDG_CONFIG_HOME").map(PathBuf::from) + .filter(|p| p.is_absolute()).or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".config")))?; + let text = tokio::fs::read_to_string(config.join("plasma-org.kde.plasma.desktop-appletsrc")).await.unwrap_or_default(); + if let Some(path) = kde_image(&text) { return package_image(path).await; } + // Plasma omits Image for the look-and-feel's default wallpaper. + if !kde_default_allowed(&text) { return None; } + let readers = ["kreadconfig6", "kreadconfig5"]; + let theme = kde_config(&readers, &["--file", "kdeglobals", "--group", "KDE", "--key", "LookAndFeelPackage"]) + .await.unwrap_or_default(); + let theme = if theme.trim().is_empty() { "org.kde.breeze.desktop" } else { theme.trim() }; + if theme.contains('/') || theme.contains("..") { return None; } + let defaults = format!("/usr/share/plasma/look-and-feel/{theme}/contents/defaults"); + let image = kde_config(&readers, &["--file", &defaults, "--group", "Wallpaper", "--key", "Image"]).await?; + let image = image.trim(); + if image.is_empty() || image.contains('/') || image.contains("..") { return None; } + return package_image(PathBuf::from("/usr/share/wallpapers").join(image)).await; + } + if desktop.contains("xfce") { + let text = command("xfconf-query", &["-c", "xfce4-desktop", "-lv"]).await?; + for line in text.lines() { + if let Some((key, value)) = line.trim().split_once(char::is_whitespace) { + if key.ends_with("/last-image") { if let Some(path) = local_path(value) { return Some(path); } } + } + } + } + None +} + +async fn package_image(path: PathBuf) -> Option { + if tokio::fs::metadata(&path).await.ok()?.is_file() { return Some(path); } + let mut entries = tokio::fs::read_dir(path.join("contents/images")).await.ok()?; + let mut candidates = Vec::new(); + while let Some(entry) = entries.next_entry().await.ok()? { + if !tokio::fs::metadata(entry.path()).await.is_ok_and(|meta| meta.is_file()) { continue; } + let path = entry.path(); + let Some((w,h)) = path.file_stem().and_then(|s| s.to_str()).and_then(|s| s.split_once('x')) + .and_then(|(w,h)| Some((w.parse::().ok()?,h.parse::().ok()?))) else { continue; }; + candidates.push(((w < h, u64::from(w) * u64::from(h)), path)); + } + candidates.sort_by_key(|(score, _)| *score); + candidates.into_iter().next().map(|(_, path)| path) +} + +fn kde_image(text: &str) -> Option { + let plugins = kde_plugins(text); + let mut image_section = false; + for line in text.lines().map(str::trim) { + if line.starts_with('[') { + image_section = line.strip_prefix("[Containments][").and_then(|s| s.split_once(']')) + .is_some_and(|(id, section)| section == "[Wallpaper][org.kde.image][General]" + && plugins.get(id).map_or(true, |plugin| *plugin == KdeWallpaper::Image)); + } + if image_section { if let Some(value) = line.strip_prefix("Image=") { if let Some(path) = local_path(value) { return Some(path); } } } + } + None +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum KdeWallpaper { Image, Unsupported } + +fn kde_plugins(text: &str) -> std::collections::HashMap<&str, KdeWallpaper> { + let mut containment = None; + let mut plugins = std::collections::HashMap::new(); + let mut panels = Vec::new(); + for line in text.lines().map(str::trim) { + if line.starts_with('[') { + containment = line.strip_prefix("[Containments][").and_then(|s| s.split_once(']')) + .filter(|(_, section)| section.is_empty()).map(|(id, _)| id); + } + if let Some(id) = containment { + plugins.entry(id).or_insert(KdeWallpaper::Image); + if line.strip_prefix("plugin=").is_some_and(|plugin| plugin.trim() == "org.kde.panel") { panels.push(id); } + if let Some(plugin) = line.strip_prefix("wallpaperplugin=") { + plugins.insert(id, if plugin.trim() == "org.kde.image" { KdeWallpaper::Image } else { KdeWallpaper::Unsupported }); + } + } + } + // Panels can keep an image wallpaper setting, but do not supply a desktop background. + for id in panels { plugins.insert(id, KdeWallpaper::Unsupported); } + plugins +} + +fn kde_default_allowed(text: &str) -> bool { + let plugins = kde_plugins(text); + if !plugins.is_empty() { return plugins.values().any(|plugin| *plugin == KdeWallpaper::Image); } + !text.contains("[Wallpaper][") || text.contains("[Wallpaper][org.kde.image]") +} + +fn gsettings_path(value: &str) -> Option { + // gsettings prints a GVariant string, with either quote style and C/Unicode escapes. + // https://docs.gtk.org/glib/gvariant-text-format.html#strings + let mut chars = value.trim().chars(); + let quote = chars.next()?; + if quote != '\'' && quote != '"' { return None; } + let mut decoded = String::new(); + while let Some(ch) = chars.next() { + if ch == quote { return chars.as_str().is_empty().then(|| local_path(&decoded)).flatten(); } + if ch != '\\' { decoded.push(ch); continue; } + let escaped = chars.next()?; + decoded.push(match escaped { + 'a' => '\x07', 'b' => '\x08', 'f' => '\x0c', 'n' => '\n', + 'r' => '\r', 't' => '\t', 'v' => '\x0b', '\\' => '\\', '\'' => '\'', '"' => '"', + 'u' | 'U' => { + let mut code = 0u32; + for _ in 0..if escaped == 'u' { 4 } else { 8 } { code = u32::checked_add(code.checked_mul(16)?, chars.next()?.to_digit(16)?)?; } + char::from_u32(code).filter(|c| *c != '\0')? + } + '\n' => continue, + _ => escaped, + }); + } + None +} + +fn local_path(value: &str) -> Option { + let value = value.trim(); + let path = if value.starts_with("file:") { + let url = reqwest::Url::parse(value).ok()?; + if url.query().is_some() || url.fragment().is_some() { return None; } + url.to_file_path().ok()? + } else { PathBuf::from(value) }; + path.is_absolute().then_some(path) +} + +async fn read_image(path: &std::path::Path) -> Result, ()> { + if !tokio::fs::metadata(path).await.map_err(|_| ())?.is_file() { return Err(()); } + let file = tokio::fs::File::open(path).await.map_err(|_| ())?; + if !file.metadata().await.map_err(|_| ())?.is_file() { return Err(()); } + let mut image = Vec::new(); + file.take((LIMIT + 1) as u64).read_to_end(&mut image).await.map_err(|_| ())?; + if image.len() > LIMIT || image_type(&image).is_none() { return Err(()); } + Ok(image) +} + +fn image_type(image: &[u8]) -> Option<&'static str> { + if image.starts_with(b"\x89PNG\r\n\x1a\n") { Some("image/png") } + else if image.starts_with(b"\xff\xd8\xff") { Some("image/jpeg") } + else if image.starts_with(b"RIFF") && image.get(8..12) == Some(b"WEBP") { Some("image/webp") } + else { None } +} + +fn api_data(status: u16, body: &serde_json::Value) -> Result<&serde_json::Value, ()> { + if status != 200 || body.get("code").and_then(|v| v.as_i64()) != Some(0) { return Err(()); } + body.get("data").ok_or(()) +} + +async fn publish(auth: &UUAuthClient, image: Vec) -> Result<(), &'static str> { + let http = crate::auth::http_client_builder().redirect(reqwest::redirect::Policy::none()).build().map_err(|_| "http-client")?; + publish_to(auth, image, crate::auth::BASE_URL, &http).await +} + +async fn json(request: reqwest::RequestBuilder) -> Result<(u16, serde_json::Value), ()> { + let mut response = request.timeout(TIMEOUT).send().await.map_err(|_| ())?; + let status = response.status().as_u16(); + let mut bytes = Vec::new(); + while let Some(chunk) = response.chunk().await.map_err(|_| ())? { + if bytes.len() + chunk.len() > 65536 { return Err(()); } + bytes.extend_from_slice(&chunk); + } + Ok((status, serde_json::from_slice(&bytes).map_err(|_| ())?)) +} + +async fn publish_to(auth: &UUAuthClient, image: Vec, base: &str, http: &reqwest::Client) -> Result<(), &'static str> { + let path = "/api/v1/tool/fp/token?filetype=1"; + let result = json(http.get(format!("{base}{path}")).headers(auth.build_headers("GET", path, ""))).await.map_err(|_| "token-request")?; + let data = api_data(result.0, &result.1).map_err(|_| "token-response")?; + let token = data.get("token").and_then(|v| v.as_str()).filter(|v| !v.is_empty()).ok_or("file-token")?; + let url = data.get("req_url").and_then(|v| v.as_str()).ok_or("upload-url")?; + let url = https_url(url).map_err(|_| "upload-url")?; + let max = data.get("max_size").and_then(|v| v.as_u64()).filter(|n| *n > 0).unwrap_or(LIMIT as u64); + if image.len() as u64 > max { return Err("file-size"); } + // Filepicker takes raw bytes and its own Authorization value, never account headers. + let response = json(http.post(url).header("Authorization", token) + .header("Content-Type", image_type(&image).ok_or("file-type")?).body(image) + ).await.map_err(|_| "file-upload")?; + if response.0 != 200 { return Err("file-upload-status"); } + let url = response.1.get("url").and_then(|v| v.as_str()).ok_or("file-response")?; + https_url(url).map_err(|_| "file-response-url")?; + let path = "/api/v1/device/wallpaper"; + let body = serde_json::json!({"wallpaper_url": url}).to_string(); + let result = json(http.post(format!("{base}{path}")).headers(auth.build_headers("POST", path, &body)).body(body)).await.map_err(|_| "report-request")?; + if result.0 != 200 || result.1.get("code").and_then(|v| v.as_i64()) != Some(0) { return Err("report-response"); } + Ok(()) +} + +fn https_url(value: &str) -> Result { + let url = reqwest::Url::parse(value).map_err(|_| ())?; + if url.scheme() != "https" || url.host_str().is_none() || !url.username().is_empty() || url.password().is_some() { return Err(()); } + Ok(url) +} + +#[cfg(test)] +mod tests { + use super::*; + use tokio::io::AsyncWriteExt; + #[tokio::test] + async fn kde_config_uses_plasma5_when_plasma6_reader_is_unavailable() { + use std::os::unix::fs::PermissionsExt; + let root = std::env::temp_dir().join(format!("uu-kreadconfig-test-{}-{}", std::process::id(), rand::random::())); + std::fs::create_dir(&root).unwrap(); + let five = root.join("kreadconfig5"); let six = root.join("kreadconfig6"); + std::fs::write(&five, "#!/bin/sh\ncase \"$*\" in *LookAndFeelPackage*) echo org.kde.breeze.desktop;; *Image*) echo Next;; esac\n").unwrap(); + std::fs::set_permissions(&five, std::fs::Permissions::from_mode(0o700)).unwrap(); + let readers = [six.to_str().unwrap(), five.to_str().unwrap()]; + assert_eq!(kde_config(&readers, &["--key", "LookAndFeelPackage"]).await.as_deref(), Some("org.kde.breeze.desktop\n")); + assert_eq!(kde_config(&readers, &["--key", "Image"]).await.as_deref(), Some("Next\n")); + std::fs::write(&six, "#!/bin/sh\necho Plasma6\n").unwrap(); + std::fs::set_permissions(&six, std::fs::Permissions::from_mode(0o700)).unwrap(); + assert_eq!(kde_config(&readers, &["--key", "Image"]).await.as_deref(), Some("Plasma6\n")); + std::fs::remove_dir_all(root).unwrap(); + } + #[tokio::test] + async fn kde_packages_select_landscape_and_image_reads_are_bounded() { + let root = std::env::temp_dir().join(format!("uu-wallpaper-test-{}-{}", std::process::id(), rand::random::())); + let images = root.join("contents/images"); + std::fs::create_dir_all(&images).unwrap(); + for name in ["1440x2960.png", "5120x2880.png", "7680x2160.png"] { + std::fs::write(images.join(name), b"\x89PNG\r\n\x1a\n").unwrap(); + } + let image = package_image(root.clone()).await.unwrap(); + assert_eq!(image.file_name().unwrap(), "5120x2880.png"); + assert_eq!(package_image(image.clone()).await, Some(image.clone())); + assert!(read_image(&image).await.is_ok()); + std::fs::write(&image, b"private text is not an image").unwrap(); + assert!(read_image(&image).await.is_err()); + std::fs::OpenOptions::new().write(true).open(&image).unwrap().set_len((LIMIT + 1) as u64).unwrap(); + assert!(read_image(&image).await.is_err()); + assert!(read_image(&root).await.is_err()); + std::fs::remove_dir_all(root).unwrap(); + } + #[tokio::test] + async fn kde_packages_follow_image_symlinks_and_skip_invalid_targets() { + use std::os::unix::fs::symlink; + let root = std::env::temp_dir().join(format!("uu-wallpaper-links-test-{}-{}", std::process::id(), rand::random::())); + let images = root.join("contents/images"); + std::fs::create_dir_all(&images).unwrap(); + let target = root.join("shared.png"); + std::fs::write(&target, b"\x89PNG\r\n\x1a\n").unwrap(); + let image = images.join("1920x1080.png"); + symlink("../../shared.png", &image).unwrap(); + symlink("../../missing.png", images.join("640x480.png")).unwrap(); + symlink(&root, images.join("800x600.png")).unwrap(); + assert_eq!(package_image(root.clone()).await, Some(image.clone())); + assert!(read_image(&image).await.is_ok()); + std::fs::remove_dir_all(root).unwrap(); + } + #[tokio::test] + async fn reporting_protocol_keeps_file_token_separate_and_stops_on_failures() { + use tokio_rustls::{rustls, TlsAcceptor}; + use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer}; + let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap(); + let der = cert.serialize_der().unwrap(); + let http = reqwest::Client::builder().no_proxy().redirect(reqwest::redirect::Policy::none()) + .add_root_certificate(reqwest::Certificate::from_der(&der).unwrap()).build().unwrap(); + let config = rustls::ServerConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider())) + .with_safe_default_protocol_versions().unwrap().with_no_client_auth() + .with_single_cert(vec![CertificateDer::from(der)], PrivateKeyDer::Pkcs8( + PrivatePkcs8KeyDer::from(cert.serialize_private_key_der()))).unwrap(); + let acceptor = TlsAcceptor::from(Arc::new(config)); + let auth = UUAuthClient::new(crate::auth::Credentials { + token: "fixture-account-token".into(), csrf: "fixture-csrf".into(), + sign_key: "fixture-key".into(), ..Default::default() + }); + for scenario in ["success", "token-failed", "too-large", "upload-failed", "report-failed"] { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("https://localhost:{}", listener.local_addr().unwrap().port()); + let server_base = base.clone(); let acceptor = acceptor.clone(); + let steps = match scenario { "token-failed" | "too-large" => 1, "upload-failed" => 2, _ => 3 }; + let server = tokio::spawn(async move { + for step in 0..steps { + let (socket, _) = listener.accept().await.unwrap(); + let mut tls = acceptor.accept(socket).await.unwrap(); + let mut bytes = Vec::new(); let mut byte = [0u8; 1]; + while !bytes.ends_with(b"\r\n\r\n") { + tls.read_exact(&mut byte).await.unwrap(); bytes.push(byte[0]); assert!(bytes.len() < 16384); + } + let headers = String::from_utf8(bytes).unwrap().to_lowercase(); + let length: usize = headers.lines().find_map(|l| l.strip_prefix("content-length:") + .and_then(|s| s.trim().parse().ok())).unwrap_or(0); + let mut body = vec![0; length]; tls.read_exact(&mut body).await.unwrap(); + let (status, reply) = match step { + 0 => { + assert!(headers.starts_with("get /api/v1/tool/fp/token?filetype=1 ")); + assert!(headers.contains("authorization: bearer fixture-account-token\r\n")); + assert!(headers.contains("x-param-sign:")); assert!(body.is_empty()); + (200, serde_json::json!({"code":if scenario=="token-failed" {1120}else{0}, + "data":{"token":"fixture-file-token","req_url":format!("{server_base}/upload"),"ttl":60, + "max_size":if scenario=="too-large" {1}else{1024}}})) + } + 1 => { + assert!(headers.starts_with("post /upload ")); + assert!(headers.contains("authorization: fixture-file-token\r\n")); + for secret in ["fixture-account-token", "fixture-csrf", "cookie:", "x-param-"] { assert!(!headers.contains(secret)); } + assert_eq!(body, b"\x89PNG\r\n\x1a\n"); + (if scenario=="upload-failed" {500}else{200}, serde_json::json!({"url":"https://example.invalid/wallpaper.png","fsize":8,"md5":"fixture","mime":"image/png"})) + } + _ => { + assert!(headers.starts_with("post /api/v1/device/wallpaper ")); + assert!(headers.contains("authorization: bearer fixture-account-token\r\n")); + assert!(!headers.contains("fixture-file-token")); + assert_eq!(serde_json::from_slice::(&body).unwrap(), serde_json::json!({"wallpaper_url":"https://example.invalid/wallpaper.png"})); + (200, serde_json::json!({"code":if scenario=="report-failed" {1120}else{0}})) + } + }; + let reply = reply.to_string(); + tls.write_all(format!("HTTP/1.1 {status} Fixture\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{reply}", reply.len()).as_bytes()).await.unwrap(); + tls.shutdown().await.unwrap(); + } + }); + let result = tokio::time::timeout(Duration::from_secs(5), publish_to(&auth, b"\x89PNG\r\n\x1a\n".to_vec(), &base, &http)).await.unwrap(); + assert_eq!(result.is_ok(), scenario == "success", "{scenario}"); + tokio::time::timeout(Duration::from_secs(5), server).await.unwrap().unwrap(); + } + } + #[test] fn wallpaper_sources_are_local_and_scoped() { + assert_eq!(gsettings_path("'file:///usr/share/a%20b.png'"), Some(PathBuf::from("/usr/share/a b.png"))); + for value in ["", "relative.png", "https://example.com/a.png", "file://remote/a.png", "file:///tmp/a.png?token=secret"] { assert!(local_path(value).is_none()); } + assert_eq!(kde_image("[Other]\nImage=/wrong.png\n[Containments][2][Wallpaper][org.kde.image][General]\nImage=file:///right.png"), Some(PathBuf::from("/right.png"))); + } + #[test] fn gsettings_decodes_quotes_backslashes_and_unicode() { + assert_eq!(gsettings_path("\"file:///tmp/it's.png\""), Some(PathBuf::from("/tmp/it's.png"))); + assert_eq!(gsettings_path(r#"'file:///tmp/it\'s.png'"#), Some(PathBuf::from("/tmp/it's.png"))); + assert_eq!(gsettings_path(r#"'/tmp/a\\b\t\u00e9\U0001f600.png'"#), Some(PathBuf::from("/tmp/a\\b\té😀.png"))); + for value in ["'unterminated", "'/tmp/a' trailing", r#"'/tmp/\uD800'"#, r#"'/tmp/\Uffffffff'"#] { assert!(gsettings_path(value).is_none()); } + assert_eq!(gsettings_path(r#"'/tmp/\x41'"#), Some(PathBuf::from("/tmp/x41"))); + assert_eq!(local_path("/tmp/it's.png"), Some(PathBuf::from("/tmp/it's.png"))); + } + #[test] fn kde_default_survives_saved_image_settings_but_not_other_plugins() { + let image = "[Containments][1]\nwallpaperplugin=org.kde.image\n[Containments][1][Wallpaper][org.kde.image][General]\nFillMode=1\n"; + assert!(kde_image(image).is_none()); assert!(kde_default_allowed(image)); + assert!(kde_default_allowed("[Containments][1][Wallpaper][org.kde.image][General]\nFillMode=1")); + assert!(kde_default_allowed("")); + assert!(!kde_default_allowed(&image.replace("wallpaperplugin=org.kde.image", "wallpaperplugin=org.kde.slideshow"))); + assert!(!kde_default_allowed("[Containments][1][Wallpaper][org.kde.slideshow][General]\nSlidePaths=/tmp")); + } + #[test] fn kde_stored_images_belong_to_the_same_active_image_containment() { + let stale = "[Containments][1]\nwallpaperplugin=org.kde.slideshow\n[Containments][1][Wallpaper][org.kde.image][General]\nImage=/tmp/previous.png\n"; + assert!(kde_image(stale).is_none()); + assert!(!kde_default_allowed(stale)); + let mixed = format!("{stale}[Containments][2]\nwallpaperplugin=org.kde.image\n[Containments][2][Wallpaper][org.kde.image][General]\nImage=/tmp/current.png\n"); + assert_eq!(kde_image(&mixed), Some(PathBuf::from("/tmp/current.png"))); + assert_eq!(kde_image(&stale.replace("org.kde.slideshow", "org.kde.image")), Some(PathBuf::from("/tmp/previous.png"))); + } + #[test] fn kde_panels_do_not_enable_default_wallpaper_or_supply_saved_images() { + let panel = "[Containments][2]\nwallpaperplugin=org.kde.image\nplugin=org.kde.panel\n[Containments][2][Wallpaper][org.kde.image][General]\nImage=/tmp/panel.png\n"; + let slideshow = "[Containments][1]\nplugin=org.kde.plasma.folder\nwallpaperplugin=org.kde.slideshow\n"; + assert!(!kde_default_allowed(slideshow)); + assert!(!kde_default_allowed(&format!("{slideshow}{panel}"))); + assert!(kde_image(&format!("{slideshow}{panel}")).is_none()); + assert!(!kde_default_allowed(panel)); + let desktop = "[Containments][1]\nplugin=org.kde.plasma.folder\n"; + assert!(kde_default_allowed(&format!("{desktop}{panel}"))); + } + #[test] fn only_supported_image_files_are_uploaded() { + assert_eq!(image_type(b"\x89PNG\r\n\x1a\n"), Some("image/png")); + assert_eq!(image_type(b"\xff\xd8\xff"), Some("image/jpeg")); + assert_eq!(image_type(b"RIFF1234WEBP"), Some("image/webp")); + assert_eq!(image_type(b"private text"), None); + } + #[test] fn failed_api_responses_and_unsafe_upload_urls_are_rejected() { + assert!(api_data(200, &serde_json::json!({"code":0,"data":{}})).is_ok()); + assert!(api_data(422, &serde_json::json!({"code":1120})).is_err()); + assert!(api_data(200, &serde_json::json!({"data":{}})).is_err()); + for url in ["http://example.com", "file:///tmp/a", "https://secret@example.com"] { assert!(https_url(url).is_err()); } + } +}