From dad98b4558b805dd9f0eff974aba44357304e63f Mon Sep 17 00:00:00 2001 From: hauke Date: Fri, 6 Nov 2026 15:40:15 +0100 Subject: [PATCH] fix #14740: guard the backend url and the backend key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BACKEND_URL must never end with "/" (a stored key's base_url is compared against it byte-for-byte): a unit test now asserts both RELEASE_BACKEND_URL and DEV_BACKEND_URL have no trailing slash and start with "https://", and AppState trims it defensively where it is stored (an earlier commit already carries that trim). set_api_key/has_api_key now refuse Service::Backend ("Dieser Schlüssel wird über die Backend-Anmeldung verwaltet"): its key is only ever set by the backend login, which holds backend_key_lock and binds it to this build's backend. Co-Authored-By: Claude Sonnet 5 --- src-tauri/src/backend.rs | 8 ++++++++ src-tauri/src/commands.rs | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/backend.rs b/src-tauri/src/backend.rs index c87aaa1..9c850d2 100644 --- a/src-tauri/src/backend.rs +++ b/src-tauri/src/backend.rs @@ -440,6 +440,14 @@ fn lock(mutex: &Mutex) -> std::sync::MutexGuard<'_, T> { mod tests { use super::*; + #[test] + fn backend_urls_have_no_trailing_slash_and_use_https() { + for url in [RELEASE_BACKEND_URL, DEV_BACKEND_URL] { + assert!(!url.ends_with('/'), "{url}"); + assert!(url.starts_with("https://"), "{url}"); + } + } + #[test] fn everyone_waiting_gets_the_one_login_result() { let waiters = LoginWaiters::default(); diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 920e329..c7a5241 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -23,6 +23,10 @@ use crate::state::{ }; const LEVELS_INTERVAL: Duration = Duration::from_millis(100); +/// `set_api_key`/`has_api_key` refuse this service: its key is managed only +/// by the backend login (`backend::login`), which holds `backend_key_lock` +/// and binds the key to this build's backend. +const BACKEND_KEY_MANAGED: &str = "Dieser Schlüssel wird über die Backend-Anmeldung verwaltet"; type CmdResult = Result; @@ -55,9 +59,11 @@ pub fn save_settings( Ok(()) } -/// Stores `key` in the keyring; an empty key deletes the entry. +/// Stores `key` in the keyring; an empty key deletes the entry. Refuses +/// `Service::Backend` (see [`BACKEND_KEY_MANAGED`]). #[tauri::command] pub async fn set_api_key(app: AppHandle, service: Service, key: String) -> CmdResult<()> { + reject_backend_service(service)?; blocking(app, move |app| { let secrets = &app.state::().secrets; let key = key.trim(); @@ -71,9 +77,11 @@ pub async fn set_api_key(app: AppHandle, service: Service, key: String) -> CmdRe .await } -/// Whether a key is stored. Never returns the key itself. +/// Whether a key is stored. Never returns the key itself. Refuses +/// `Service::Backend` (see [`BACKEND_KEY_MANAGED`]). #[tauri::command] pub async fn has_api_key(app: AppHandle, service: Service) -> CmdResult { + reject_backend_service(service)?; blocking(app, move |app| { let key = app.state::().secrets.get(service); Ok(key.is_some_and(|key| !key.is_empty())) @@ -81,6 +89,15 @@ pub async fn has_api_key(app: AppHandle, service: Service) -> CmdResult { .await } +/// Refuses direct keyring access to `Service::Backend`: its key is only +/// ever set by the backend login flow. +fn reject_backend_service(service: Service) -> CmdResult<()> { + if service == Service::Backend { + return Err(BACKEND_KEY_MANAGED.to_string()); + } + Ok(()) +} + #[tauri::command] pub async fn list_devices() -> CmdResult { tauri::async_runtime::spawn_blocking(recorder_audio::list_devices) @@ -418,3 +435,17 @@ where .await .map_err(|e| e.to_string())? } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_backend_key_is_managed_only_by_the_login() { + assert_eq!( + reject_backend_service(Service::Backend), + Err(BACKEND_KEY_MANAGED.to_string()) + ); + assert_eq!(reject_backend_service(Service::DeepScript), Ok(())); + } +}