fix #14740: guard the backend url and the backend key

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 <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-06 15:40:15 +01:00
co-authored by Claude Sonnet 5
parent 96eec22971
commit dad98b4558
2 changed files with 41 additions and 2 deletions
+33 -2
View File
@@ -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<T> = Result<T, String>;
@@ -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::<AppState>().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<bool> {
reject_backend_service(service)?;
blocking(app, move |app| {
let key = app.state::<AppState>().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<bool> {
.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<Devices> {
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(()));
}
}