Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

15.5 KB
Raw Blame
use base64::Engine;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use tauri::{AppHandle, Emitter, Manager};
use tauri_plugin_updater::{Update, UpdaterExt};

pub(crate) fn verify_bundle_signature(bytes: &[u8], signature: &str) -> Result<(), String> {
    let config: serde_json::Value = serde_json::from_str(include_str!("../tauri.conf.json"))
        .map_err(|error| error.to_string())?;
    let key = config["plugins"]["updater"]["pubkey"]
        .as_str()
        .ok_or("The desktop updater public key is missing.")?;
    verify_signature_with_key(bytes, signature, key)
}

fn verify_signature_with_key(bytes: &[u8], signature: &str, key: &str) -> Result<(), String> {
    let decode = |value: &str| -> Result<String, String> {
        let bytes = base64::engine::general_purpose::STANDARD
            .decode(value)
            .map_err(|error| format!("Invalid updater signature encoding: {error}"))?;
        String::from_utf8(bytes).map_err(|error| error.to_string())
    };
    let key = minisign_verify::PublicKey::decode(&decode(key)?)
        .map_err(|error| format!("Invalid updater public key: {error}"))?;
    let signature = minisign_verify::Signature::decode(&decode(signature)?)
        .map_err(|error| format!("Invalid updater signature: {error}"))?;
    key.verify(bytes, &signature, true)
        .map_err(|error| format!("Desktop update signature verification failed: {error}"))
}

const DOWNLOAD_EVENT: &str = "desktop-update-download";
const DOWNLOAD_EVENT_STEP: u64 = 512 * 1024;
const BUNDLE_FILE: &str = ".desktop-update-bundle";
const BUNDLE_METADATA_FILE: &str = ".desktop-update-bundle.json";

/// The prepared bundle lives on disk, not here, so it does not hold memory until restart.
#[derive(Default)]
pub(crate) struct DesktopUpdate {
    update: Option<Update>,
    bundle: Option<PathBuf>,
    downloading: bool,
}

fn bundle_path() -> PathBuf {
    crate::diagnostics::studio_dir().join(BUNDLE_FILE)
}

fn bundle_metadata_path() -> PathBuf {
    crate::diagnostics::studio_dir().join(BUNDLE_METADATA_FILE)
}

#[derive(Deserialize, Serialize)]
struct PreparedBundle {
    version: String,
    size: u64,
    sha256: String,
}

fn sha256(bytes: &[u8]) -> String {
    crate::native_backend_lease::hex_bytes(&Sha256::digest(bytes))
}

fn sync_parent(path: &Path) {
    #[cfg(unix)]
    if let Some(parent) = path.parent() {
        let _ = File::open(parent).and_then(|directory| directory.sync_all());
    }
}

fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
    let parent = path
        .parent()
        .ok_or_else(|| format!("{} has no parent directory", path.display()))?;
    fs::create_dir_all(parent).map_err(|error| format!("{}: {error}", parent.display()))?;
    let mut temporary = tempfile::Builder::new()
        .prefix(".desktop-update-")
        .tempfile_in(parent)
        .map_err(|error| format!("{}: {error}", parent.display()))?;
    temporary
        .write_all(bytes)
        .and_then(|()| temporary.as_file().sync_all())
        .map_err(|error| format!("{}: {error}", path.display()))?;
    temporary
        .persist(path)
        .map_err(|error| format!("{}: {}", path.display(), error.error))?;
    sync_parent(path);
    Ok(())
}

fn discard_prepared_bundle(bundle: &Path, metadata: &Path) {
    let _ = fs::remove_file(bundle);
    let _ = fs::remove_file(metadata);
}

fn persist_prepared_bundle(
    bundle: &Path,
    metadata: &Path,
    version: &str,
    bytes: &[u8],
) -> Result<(), String> {
    discard_prepared_bundle(bundle, metadata);
    write_atomic(bundle, bytes)?;
    let record = PreparedBundle {
        version: version.to_string(),
        size: bytes.len() as u64,
        sha256: sha256(bytes),
    };
    let body = serde_json::to_vec_pretty(&record).map_err(|error| error.to_string())?;
    if let Err(error) = write_atomic(metadata, &body) {
        discard_prepared_bundle(bundle, metadata);
        return Err(error);
    }
    Ok(())
}

fn read_prepared_bundle(
    bundle: &Path,
    metadata: &Path,
    expected_version: &str,
) -> Result<Vec<u8>, String> {
    let record: PreparedBundle = serde_json::from_slice(
        &fs::read(metadata).map_err(|error| format!("{}: {error}", metadata.display()))?,
    )
    .map_err(|error| format!("{}: {error}", metadata.display()))?;
    if record.version != expected_version {
        return Err(format!(
            "prepared update {} does not match {}",
            record.version, expected_version
        ));
    }
    let bytes = fs::read(bundle).map_err(|error| format!("{}: {error}", bundle.display()))?;
    if bytes.len() as u64 != record.size || sha256(&bytes) != record.sha256 {
        return Err("prepared update failed its integrity check".to_string());
    }
    Ok(bytes)
}

fn rehydrate_prepared_bundle(expected_version: &str) -> Option<PathBuf> {
    let bundle = bundle_path();
    let metadata = bundle_metadata_path();
    match read_prepared_bundle(&bundle, &metadata, expected_version) {
        Ok(_) => Some(bundle),
        Err(_) => {
            discard_prepared_bundle(&bundle, &metadata);
            None
        }
    }
}

pub(crate) type DesktopUpdateState = Arc<Mutex<DesktopUpdate>>;

pub(crate) fn new_desktop_update_state() -> DesktopUpdateState {
    Arc::new(Mutex::new(DesktopUpdate::default()))
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DesktopUpdateMetadata {
    current_version: String,
    version: String,
    date: Option<String>,
    body: Option<String>,
    raw_json: serde_json::Value,
}

#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DesktopUpdateDownload {
    version: String,
    downloaded: u64,
    total: Option<u64>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DesktopUpdateBundleStatus {
    version: Option<String>,
    downloaded: bool,
    downloading: bool,
}

/// Re-arm crash cleanup when the installer never took over; `on_before_exit` cleared kill-on-close.
#[tauri::command]
pub(crate) async fn resume_desktop_update_cleanup() -> Result<(), String> {
    // Restore the spent exit guard and kill-on-close, so a retry reaps the backend again.
    crate::reset_termination_cleanup();
    #[cfg(windows)]
    {
        crate::windows_job::resume_after_update_installer().map_err(|error| error.to_string())?;
    }
    Ok(())
}

#[tauri::command]
pub(crate) async fn desktop_update_cleanup_armed() -> Result<bool, String> {
    #[cfg(windows)]
    {
        crate::windows_job::kill_on_close_armed().map_err(|error| error.to_string())
    }
    #[cfg(not(windows))]
    {
        Ok(true)
    }
}

#[tauri::command]
pub(crate) async fn check_desktop_update(
    webview: tauri::Webview,
    state: tauri::State<'_, DesktopUpdateState>,
) -> Result<Option<DesktopUpdateMetadata>, String> {
    let app = webview.app_handle().clone();
    let builder = webview.updater_builder().on_before_exit(move || {
        #[cfg(windows)]
        {
            crate::cleanup_child_processes(&app);
            if let Err(error) = crate::windows_job::suspend_for_update_installer() {
                log::error!(
                    "Could not suspend Windows job cleanup for the updater; refusing to launch the installer: {error}"
                );
                std::process::exit(1);
            }
        }
        app.cleanup_before_exit();
    });

    #[cfg(target_os = "linux")]
    let builder = if crate::debian_update::is_debian_bundle() {
        builder.target(format!("linux-{}-deb", std::env::consts::ARCH))
    } else {
        builder
    };
    let updater = builder.build().map_err(|error| error.to_string())?;
    let update = updater.check().await.map_err(|error| error.to_string())?;
    let mut guard = state.lock().map_err(|error| error.to_string())?;
    let Some(update) = update else {
        guard.update = None;
        guard.bundle = None;
        discard_prepared_bundle(&bundle_path(), &bundle_metadata_path());
        return Ok(None);
    };

    let date = update
        .date
        .map(|date| date.format(&time::format_description::well_known::Rfc3339))
        .transpose()
        .map_err(|error| error.to_string())?;
    let metadata = DesktopUpdateMetadata {
        current_version: update.current_version.clone(),
        version: update.version.clone(),
        date,
        body: update.body.clone(),
        raw_json: update.raw_json.clone(),
    };
    if guard.update.as_ref().map(|u| u.version.as_str()) != Some(update.version.as_str()) {
        guard.bundle = None;
    }
    guard.bundle = rehydrate_prepared_bundle(&update.version);
    guard.update = Some(update);
    Ok(Some(metadata))
}

#[tauri::command]
pub(crate) async fn download_desktop_update(
    app: AppHandle,
    state: tauri::State<'_, DesktopUpdateState>,
) -> Result<(), String> {
    let update = {
        let mut guard = state.lock().map_err(|error| error.to_string())?;
        let Some(update) = guard.update.clone() else {
            return Err("No desktop update has been checked.".to_string());
        };
        if guard.bundle.is_some() {
            return Ok(());
        }
        if guard.downloading {
            return Err("Desktop update download is already running.".to_string());
        }
        guard.downloading = true;
        update
    };

    let version = update.version.clone();
    let mut downloaded: u64 = 0;
    let mut last_emitted: u64 = 0;
    let progress_app = app.clone();
    let result = update
        .download(
            |chunk, total| {
                downloaded += chunk as u64;
                if downloaded - last_emitted >= DOWNLOAD_EVENT_STEP || Some(downloaded) == total {
                    last_emitted = downloaded;
                    let _ = progress_app.emit(
                        DOWNLOAD_EVENT,
                        DesktopUpdateDownload {
                            version: version.clone(),
                            downloaded,
                            total,
                        },
                    );
                }
            },
            || {},
        )
        .await;

    let mut guard = state.lock().map_err(|error| error.to_string())?;
    guard.downloading = false;
    match result {
        Ok(bytes) => {
            if guard.update.as_ref().map(|u| u.version.as_str()) != Some(update.version.as_str()) {
                // A newer check landed mid-download; these bytes are stale.
                return Ok(());
            }
            let path = bundle_path();
            persist_prepared_bundle(&path, &bundle_metadata_path(), &update.version, &bytes)?;
            guard.bundle = Some(path);
            Ok(())
        }
        Err(error) => Err(error.to_string()),
    }
}

#[tauri::command]
pub(crate) async fn install_desktop_update(
    state: tauri::State<'_, DesktopUpdateState>,
) -> Result<(), String> {
    let (update, path) = {
        let mut guard = state.lock().map_err(|error| error.to_string())?;
        let Some(update) = guard.update.clone() else {
            return Err("No desktop update has been checked.".to_string());
        };
        let Some(path) = guard.bundle.take() else {
            return Err("Desktop update has not been downloaded.".to_string());
        };
        (update, path)
    };
    let metadata = bundle_metadata_path();
    let bundle = match read_prepared_bundle(&path, &metadata, &update.version) {
        Ok(bundle) => bundle,
        Err(error) => {
            discard_prepared_bundle(&path, &metadata);
            return Err(format!("Prepared update is invalid: {error}"));
        }
    };
    if let Err(error) = verify_bundle_signature(&bundle, &update.signature) {
        discard_prepared_bundle(&path, &metadata);
        return Err(error);
    }
    let result = tokio::task::spawn_blocking(move || {
        #[cfg(target_os = "linux")]
        if crate::debian_update::is_debian_bundle() {
            return crate::debian_update::install(&bundle, &update.signature, &update.version);
        }
        update.install(&bundle).map_err(|error| error.to_string())
    })
    .await
    .map_err(|error| format!("Desktop installer task failed: {error}"))?;
    if let Err(error) = result {
        discard_prepared_bundle(&path, &metadata);
        return Err(error);
    }
    discard_prepared_bundle(&path, &metadata);
    Ok(())
}

#[tauri::command]
pub(crate) fn desktop_update_bundle_status(
    state: tauri::State<'_, DesktopUpdateState>,
) -> Result<DesktopUpdateBundleStatus, String> {
    let guard = state.lock().map_err(|error| error.to_string())?;
    Ok(DesktopUpdateBundleStatus {
        version: guard.update.as_ref().map(|u| u.version.clone()),
        downloaded: guard.bundle.as_ref().is_some_and(|path| path.is_file()),
        downloading: guard.downloading,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    const TEST_PUBLIC_KEY: &str = "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXkgOEVDM0EzRDFDODFDNjZDNgpSV1RHWmh6STBhUERqdC9BQXhnMEJIcGFLYlVFc2pQbDBid1llM0tRc3FDdERKNVVTbEpZNHQ1bwo=";
    const TEST_SIGNATURE: &str = "dW50cnVzdGVkIGNvbW1lbnQ6IHNpZ25hdHVyZSBmcm9tIG1pbmlzaWduIHNlY3JldCBrZXkKUlVUR1poekkwYVBEamxUdGdMVDl5VG1NbWJ0ZEYzcE40UGNRU0VBV3MyNiswOGVpclovSGRkbXBTVDI4Y3MzY3JaaHAyYXlrZUtaREVqYytGUXl1Rm1MWHBVRVpCbE5PNUE4PQp0cnVzdGVkIGNvbW1lbnQ6IHRlc3QgZml4dHVyZQpCeWlPRW53ZzF4azBBdHh4WVhOWENDOGJzMjRFM2Zvc1lTM1BKU1JhV2Z1TzJac0ZmVXhMS1J1TnY4aWl1S2xrWTRMMkd6Wis2ZWloT0pmM2NlOXBBdz09Cg==";

    #[test]
    fn a_rewritten_cache_and_hash_do_not_bypass_the_release_signature() {
        let directory = tempfile::tempdir().unwrap();
        let bundle = directory.path().join(BUNDLE_FILE);
        let metadata = directory.path().join(BUNDLE_METADATA_FILE);
        let original = b"verified update fixture";
        assert!(verify_signature_with_key(original, TEST_SIGNATURE, TEST_PUBLIC_KEY).is_ok());
        persist_prepared_bundle(&bundle, &metadata, "0.1.811-beta", b"substituted package")
            .unwrap();
        let replaced = read_prepared_bundle(&bundle, &metadata, "0.1.811-beta").unwrap();
        assert!(verify_signature_with_key(&replaced, TEST_SIGNATURE, TEST_PUBLIC_KEY).is_err());
        assert!(verify_bundle_signature(original, TEST_SIGNATURE).is_err());
    }

    #[test]
    fn a_prepared_bundle_survives_state_recreation() {
        let directory = tempfile::tempdir().unwrap();
        let bundle = directory.path().join(BUNDLE_FILE);
        let metadata = directory.path().join(BUNDLE_METADATA_FILE);
        let bytes = b"signed updater bundle";

        persist_prepared_bundle(&bundle, &metadata, "0.1.901", bytes).unwrap();

        assert_eq!(
            read_prepared_bundle(&bundle, &metadata, "0.1.901").unwrap(),
            bytes
        );
    }

    #[test]
    fn a_corrupt_prepared_bundle_is_rejected() {
        let directory = tempfile::tempdir().unwrap();
        let bundle = directory.path().join(BUNDLE_FILE);
        let metadata = directory.path().join(BUNDLE_METADATA_FILE);
        persist_prepared_bundle(&bundle, &metadata, "0.1.901", b"verified").unwrap();
        fs::write(&bundle, b"corrupt!").unwrap();

        let error = read_prepared_bundle(&bundle, &metadata, "0.1.901").unwrap_err();

        assert!(error.contains("integrity"), "{error}");
    }

    #[test]
    fn a_bundle_for_another_release_is_rejected() {
        let directory = tempfile::tempdir().unwrap();
        let bundle = directory.path().join(BUNDLE_FILE);
        let metadata = directory.path().join(BUNDLE_METADATA_FILE);
        persist_prepared_bundle(&bundle, &metadata, "0.1.901", b"verified").unwrap();

        let error = read_prepared_bundle(&bundle, &metadata, "0.1.902").unwrap_err();

        assert!(error.contains("does not match"), "{error}");
    }
}