rustploy-gui 0.1.25

Rustploy — desktop client (glacier-ui) for the Rustploy PaaS daemon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! A ponte nativa do "Atualizar app": o que o Luau não consegue fazer sozinho (rodar `curl` e
//! `pkexec`, reabrir o app) e as decisões de segurança que não devem ser reescritas em Luau
//! (comparar versões, escolher o pacote certo da release).
//!
//! Exposto ao Luau como o global `app_update` (ver `views/scripts/glacier.d.luau`). Nada aqui
//! instala algo: quem instala é `/usr/libexec/rustploy-gui-update`, que roda como root via
//! `pkexec` e só aceita um pacote assinado pela chave do projeto.

use glacier_ui::mlua::{Lua, Result as LuaResult, Table};
use std::{
    path::{Path, PathBuf},
    process::Command,
};
use update_core::{
    is_newer,
    release::{debian_architecture, package_version, parse_release, select_package_assets},
};

/// Repositório das releases do app. Fixo no binário: a UI não escolhe de onde vem uma atualização.
const REPOSITORY: &str = "antoniofernandodj/rustploy";
/// O helper root e o binário que ele troca.
const HELPER: &str = "/usr/libexec/rustploy-gui-update";
const INSTALLED_BINARY: &str = "/usr/bin/rustploy-gui";
/// Relativo ao cache do usuário. Tem de ser o mesmo que o helper lê (`USER_CACHE_SUBDIR`).
const CACHE_SUBDIR: &str = "rustploy-gui/updates";
const MAX_DEB_BYTES: u64 = 200 * 1024 * 1024;

/// Como este executável foi instalado: só a instalação por `.deb` tem helper root.
#[derive(Debug, PartialEq, Eq)]
enum InstallKind {
    /// `/usr/bin/rustploy-gui`, instalado por `.deb`: dá para atualizar pelo app.
    Deb,
    /// `cargo install`, zip portátil, compilado do código: o app só avisa.
    Other,
}

impl InstallKind {
    fn label(&self) -> &'static str {
        match self {
            Self::Deb => "deb",
            Self::Other => "other",
        }
    }
}

fn install_kind_of(executable: &Path) -> InstallKind {
    // Compara o caminho resolvido: um link para fora de /usr/bin não conta como instalado por .deb.
    match std::fs::canonicalize(executable) {
        Ok(resolved) if resolved == Path::new(INSTALLED_BINARY) => InstallKind::Deb,
        _ => InstallKind::Other,
    }
}

fn install_kind() -> InstallKind {
    std::env::current_exe()
        .map(|executable| install_kind_of(&executable))
        .unwrap_or(InstallKind::Other)
}

/// O app pode se atualizar sozinho? `.deb` + helper instalado + `pkexec` no PATH.
fn can_update() -> bool {
    install_kind() == InstallKind::Deb
        && Path::new(HELPER).exists()
        && find_in_path("pkexec").is_some()
}

fn find_in_path(program: &str) -> Option<PathBuf> {
    std::env::var_os("PATH").and_then(|paths| {
        std::env::split_paths(&paths)
            .map(|directory| directory.join(program))
            .find(|candidate| candidate.is_file())
    })
}

/// `$XDG_CACHE_HOME` ou `~/.cache`, resolvido como o helper espera.
fn updates_dir() -> Result<PathBuf, String> {
    let cache = match std::env::var_os("XDG_CACHE_HOME").filter(|value| !value.is_empty()) {
        Some(path) => PathBuf::from(path),
        None => PathBuf::from(
            std::env::var_os("HOME").ok_or_else(|| "sem HOME para achar o cache".to_string())?,
        )
        .join(".cache"),
    };
    // O helper só olha `<home>/.cache/rustploy-gui/updates`: com XDG_CACHE_HOME fora disso ele não
    // acharia o arquivo. Melhor recusar aqui, com a causa, do que falhar lá com "fora do diretório".
    let expected = PathBuf::from(std::env::var_os("HOME").unwrap_or_default()).join(".cache");
    if cache != expected {
        return Err(format!(
            "o cache do app precisa estar em {} (XDG_CACHE_HOME aponta para {})",
            expected.display(),
            cache.display()
        ));
    }
    Ok(cache.join(CACHE_SUBDIR))
}

/// Baixa `url` para `destination` com o `curl` do sistema (TLS e certificados do sistema), seguindo
/// redirecionamentos só por https. A integridade e a autenticidade NÃO dependem daqui: o helper
/// root confere a assinatura do que chegar.
fn download(url: &str, destination: &Path, max_bytes: u64) -> Result<(), String> {
    if !url.starts_with("https://github.com/") {
        return Err(format!("recusei baixar de fora do GitHub: {url}"));
    }
    let partial = destination.with_extension("partial");
    let status = Command::new("curl")
        .args([
            "--fail",
            "--silent",
            "--show-error",
            "--location",
            "--proto",
            "=https",
            "--proto-redir",
            "=https",
            "--max-redirs",
            "5",
            "--connect-timeout",
            "20",
            "--max-time",
            "600",
            "--max-filesize",
        ])
        .arg(max_bytes.to_string())
        .arg("--output")
        .arg(&partial)
        .arg(url)
        .output()
        .map_err(|error| format!("não consegui rodar o curl: {error}"))?;
    if !status.status.success() {
        let _ = std::fs::remove_file(&partial);
        return Err(format!(
            "o download falhou: {}",
            String::from_utf8_lossy(&status.stderr).trim()
        ));
    }
    std::fs::rename(&partial, destination).map_err(|error| format!("gravando o download: {error}"))
}

/// O nome vira parte de um caminho: só o que um `.deb` do rustploy-gui teria.
fn validate_deb_name(deb_name: &str) -> Result<(), String> {
    let plain = deb_name
        .chars()
        .all(|character| character.is_ascii_alphanumeric() || "._+-~".contains(character));
    if plain
        && deb_name.starts_with("rustploy-gui_")
        && deb_name.ends_with(".deb")
        && !deb_name.contains("..")
    {
        Ok(())
    } else {
        Err(format!("nome de pacote inesperado: {deb_name}"))
    }
}

/// Baixa o pacote e a assinatura para o cache do usuário e chama o helper por `pkexec`.
/// Devolve a mensagem do resultado; bloqueia até o fim (download + senha + instalação).
fn install(deb_url: &str, signature_url: &str, deb_name: &str) -> Result<String, String> {
    if !can_update() {
        return Err("este app não foi instalado por .deb (ou falta o pkexec): atualize pelo comando de instalação".into());
    }
    validate_deb_name(deb_name)?;
    let directory = updates_dir()?;
    std::fs::create_dir_all(&directory)
        .map_err(|error| format!("criando {}: {error}", directory.display()))?;
    // Resto de uma tentativa anterior não pode ser confundido com o pacote novo.
    if let Ok(entries) = std::fs::read_dir(&directory) {
        for entry in entries.flatten() {
            let _ = std::fs::remove_file(entry.path());
        }
    }
    let deb_path = directory.join(deb_name);
    let signature_path = directory.join(format!("{deb_name}.minisig"));
    download(deb_url, &deb_path, MAX_DEB_BYTES)?;
    download(signature_url, &signature_path, 8 * 1024)?;

    let output = Command::new("pkexec")
        .arg(HELPER)
        .arg("install")
        .arg(&deb_path)
        .arg(&signature_path)
        .output()
        .map_err(|error| format!("não consegui rodar o pkexec: {error}"))?;
    let _ = std::fs::remove_file(&deb_path);
    let _ = std::fs::remove_file(&signature_path);
    match output.status.code() {
        Some(0) => Ok("instalado".to_string()),
        // 126/127 do pkexec: o usuário cancelou o diálogo ou não está autorizado.
        Some(126) | Some(127) => Err("a autenticação foi cancelada ou negada".into()),
        _ => {
            let detail = String::from_utf8_lossy(&output.stderr);
            let detail = detail
                .lines()
                .rev()
                .find(|line| !line.trim().is_empty())
                .unwrap_or("falhou");
            Err(detail.trim().to_string())
        }
    }
}

/// Abre uma nova instância do app e encerra esta.
fn restart() -> Result<(), String> {
    Command::new(INSTALLED_BINARY)
        .spawn()
        .map_err(|error| format!("não consegui reabrir o app: {error}"))?;
    std::process::exit(0);
}

/// Instala o global `app_update` numa VM Luau nova.
pub(crate) fn install_lua(lua: &Lua) -> LuaResult<()> {
    let table: Table = lua.create_table()?;

    // { version, install_kind, can_update }
    table.set(
        "info",
        lua.create_function(|lua, ()| {
            let info = lua.create_table()?;
            info.set("version", env!("CARGO_PKG_VERSION"))?;
            info.set("install_kind", install_kind().label())?;
            info.set("can_update", can_update())?;
            Ok(info)
        })?,
    )?;

    // Lê o JSON de `releases/latest` e devolve { tag, latest_version, is_update_available, html_url,
    // deb_url, signature_url, deb_name } — ou `nil, mensagem`.
    table.set(
        "plan",
        lua.create_function(|lua, json: String| {
            let plan = (|| -> Result<Table, String> {
                let release = parse_release(&json)?;
                let architecture = debian_architecture()?;
                // A versão do PACOTE que a release traz, não a tag: a tag (`v0.1.18`) e a versão do
                // app (`0.1.23`) são numerações independentes.
                let package =
                    package_version(&release.assets, REPOSITORY, "rustploy-gui", architecture)
                        .ok_or_else(|| {
                            format!(
                                "a release {} não traz um pacote do app para {architecture}",
                                release.tag
                            )
                        })?;
                let is_update_available = is_newer(&package.upstream, env!("CARGO_PKG_VERSION"))
                    .map_err(|error| error.to_string())?;
                let result = lua.create_table().map_err(|error| error.to_string())?;
                let set = |key: &str, value: String| {
                    result.set(key, value).map_err(|error| error.to_string())
                };
                set("tag", release.tag.clone())?;
                set("latest_version", package.upstream.clone())?;
                set("html_url", release.html_url.clone())?;
                result
                    .set("is_update_available", is_update_available)
                    .map_err(|error| error.to_string())?;
                if is_update_available {
                    let (deb, signature) = select_package_assets(
                        &release.assets,
                        REPOSITORY,
                        "rustploy-gui",
                        debian_architecture()?,
                        &release.tag,
                    )?;
                    set("deb_url", deb.url)?;
                    set("deb_name", deb.name)?;
                    set("signature_url", signature.url)?;
                }
                Ok(result)
            })();
            Ok(match plan {
                Ok(table) => (Some(table), None),
                Err(message) => (None, Some(message)),
            })
        })?,
    )?;

    // Baixa, autentica (pkexec) e instala. Devolve `true, mensagem` ou `false, causa`.
    table.set(
        "install",
        lua.create_function(
            |_, (deb_url, signature_url, deb_name): (String, String, String)| {
                Ok(match install(&deb_url, &signature_url, &deb_name) {
                    Ok(message) => (true, message),
                    Err(message) => (false, message),
                })
            },
        )?,
    )?;

    table.set("restart", lua.create_function(|_, ()| Ok(restart().err()))?)?;

    lua.globals().set("app_update", table)
}

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

    #[test]
    fn only_the_installed_path_counts_as_deb() {
        // Um caminho que não existe, ou que não é /usr/bin/rustploy-gui, nunca é "deb".
        assert_eq!(
            install_kind_of(Path::new("/nao/existe/rustploy-gui")),
            InstallKind::Other
        );
        assert_eq!(
            install_kind_of(Path::new("/usr/bin/true")),
            InstallKind::Other
        );
        assert_eq!(InstallKind::Deb.label(), "deb");
        assert_eq!(InstallKind::Other.label(), "other");
    }

    #[test]
    fn deb_names_are_validated_before_becoming_paths() {
        assert!(validate_deb_name("rustploy-gui_0.1.21-1_amd64.deb").is_ok());
        for bad in [
            "",
            "rustployd_0.1.11-1_amd64.deb",
            "rustploy-gui_x/../../etc/passwd.deb",
            "rustploy-gui_a b.deb",
            "rustploy-gui_..deb",
            "rustploy-gui_0.1.21-1_amd64.deb;rm",
            "rustploy-gui_0.1.21-1_amd64.tar",
        ] {
            assert!(
                validate_deb_name(bad).is_err(),
                "{bad:?} deveria ser recusado"
            );
        }
    }

    #[test]
    fn downloads_only_from_github() {
        let destination = std::env::temp_dir().join("rustploy-gui-never");
        for url in [
            "http://github.com/x",
            "https://evil.example/x",
            "file:///etc/passwd",
            "ftp://github.com/x",
        ] {
            let error = download(url, &destination, 1024).unwrap_err();
            assert!(error.contains("fora do GitHub"), "{url}: {error}");
        }
        assert!(!destination.exists());
    }

    #[test]
    fn updates_dir_must_match_where_the_helper_looks() {
        // SAFETY: único teste deste binário que mexe nessas variáveis.
        unsafe {
            std::env::set_var("HOME", "/home/pessoa");
            std::env::set_var("XDG_CACHE_HOME", "/outro/lugar");
        }
        let error = updates_dir().unwrap_err();
        assert!(error.contains("/home/pessoa/.cache"), "{error}");

        unsafe { std::env::set_var("XDG_CACHE_HOME", "/home/pessoa/.cache") };
        assert_eq!(
            updates_dir().unwrap(),
            PathBuf::from("/home/pessoa/.cache/rustploy-gui/updates")
        );

        unsafe { std::env::remove_var("XDG_CACHE_HOME") };
        assert_eq!(
            updates_dir().unwrap(),
            PathBuf::from("/home/pessoa/.cache/rustploy-gui/updates")
        );
    }

    /// A VM é devolvida junto: uma `Table` não vive mais que a `Lua` que a criou.
    fn run_plan(json: &str) -> (Lua, Option<Table>, Option<String>) {
        let lua = Lua::new();
        install_lua(&lua).unwrap();
        let native: Table = lua.globals().get("app_update").unwrap();
        let function: glacier_ui::mlua::Function = native.get("plan").unwrap();
        let (plan, error) = function
            .call::<(Option<Table>, Option<String>)>(json.to_string())
            .unwrap();
        (lua, plan, error)
    }

    fn release_json(tag: &str) -> String {
        release_json_with(tag, tag.trim_start_matches('v'))
    }

    /// `tag` e a versão do app que a release traz são numerações independentes.
    fn release_json_with(tag: &str, app_version: &str) -> String {
        let base = format!("https://github.com/{REPOSITORY}/releases/download/{tag}");
        format!(
            r#"{{"tag_name":"{tag}","html_url":"https://github.com/{REPOSITORY}/releases/tag/{tag}","assets":[
              {{"name":"rustploy-gui_{app_version}-1_amd64.deb","browser_download_url":"{base}/rustploy-gui_{app_version}-1_amd64.deb"}},
              {{"name":"rustploy-gui_{app_version}-1_amd64.deb.minisig","browser_download_url":"{base}/rustploy-gui_{app_version}-1_amd64.deb.minisig"}},
              {{"name":"rustploy-gui_{app_version}-1_arm64.deb","browser_download_url":"{base}/rustploy-gui_{app_version}-1_arm64.deb"}},
              {{"name":"rustploy-gui_{app_version}-1_arm64.deb.minisig","browser_download_url":"{base}/rustploy-gui_{app_version}-1_arm64.deb.minisig"}}
            ]}}"#
        )
    }

    #[test]
    fn plan_picks_this_architectures_package_and_its_signature() {
        let (_lua, plan, error) = run_plan(&release_json("v9.9.9"));
        assert_eq!(error, None);
        let plan = plan.unwrap();
        assert!(plan.get::<bool>("is_update_available").unwrap());
        assert_eq!(plan.get::<String>("latest_version").unwrap(), "9.9.9");
        let architecture = debian_architecture().unwrap();
        let deb_name: String = plan.get("deb_name").unwrap();
        assert_eq!(deb_name, format!("rustploy-gui_9.9.9-1_{architecture}.deb"));
        assert!(
            plan.get::<String>("signature_url")
                .unwrap()
                .ends_with(".deb.minisig")
        );
        assert!(
            plan.get::<String>("deb_url")
                .unwrap()
                .starts_with("https://github.com/")
        );
    }

    #[test]
    fn plan_for_an_older_or_equal_release_has_no_files() {
        let (_lua, plan, error) =
            run_plan(&release_json(&format!("v{}", env!("CARGO_PKG_VERSION"))));
        assert_eq!(error, None);
        let plan = plan.unwrap();
        assert!(!plan.get::<bool>("is_update_available").unwrap());
        assert!(matches!(plan.get::<Value>("deb_url").unwrap(), Value::Nil));
    }

    #[test]
    fn the_release_tag_does_not_decide_only_the_app_package_does() {
        // Tag nova (v9.9.9) com o MESMO app que já roda: em dia. Tag velha com app mais novo: há versão.
        let running = env!("CARGO_PKG_VERSION");
        let (_lua, plan, error) = run_plan(&release_json_with("v9.9.9", running));
        assert_eq!(error, None);
        let plan = plan.unwrap();
        assert!(!plan.get::<bool>("is_update_available").unwrap());
        assert_eq!(plan.get::<String>("latest_version").unwrap(), running);

        let (_lua, plan, _) = run_plan(&release_json_with("v0.0.1", "9.9.9"));
        assert!(plan.unwrap().get::<bool>("is_update_available").unwrap());
    }

    #[test]
    fn a_release_without_an_app_package_is_an_error() {
        let json = release_json("v9.9.9").replace("rustploy-gui_", "outro-produto_");
        let (_lua, plan, error) = run_plan(&json);
        assert!(plan.is_none());
        assert!(error.unwrap().contains("não traz um pacote do app"));
    }

    #[test]
    fn plan_reports_garbage_as_an_error_message() {
        for json in ["<html>", "{}", r#"{"tag_name":"nightly","html_url":"x"}"#] {
            let (_lua, plan, error) = run_plan(json);
            assert!(plan.is_none(), "{json}");
            assert!(error.is_some(), "{json}");
        }
    }

    #[test]
    fn plan_without_a_signature_in_the_release_is_refused() {
        let json = release_json("v9.9.9").replace(".deb.minisig", ".deb.sig");
        let (_lua, plan, error) = run_plan(&json);
        assert!(plan.is_none());
        assert!(error.unwrap().contains("só instalo pacote assinado"));
    }
}