mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-24 11:21:27 +01:00
PrintCraft asks GitHub for the latest release and, when it is newer, offers its release page to download from. Nothing is downloaded or installed automatically (installing in place needs signed release artifacts). - ui-egui: updates module (version comparison, background check, dialog); no network code: the desktop app supplies the source, and without one (web build, tests) the command opens the releases page. - desktop: one HTTPS request (ureq over rustls with the OS's trusted roots, 10 s timeout, 1 MiB cap). Only a page under the releases list is offered. No bundled root list: webpki-roots/webpki-root-certs are CDLA-licensed. - The check at start is opt-in and silent unless there is a newer version. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
3.3 KiB
Rust
73 lines
3.3 KiB
Rust
//! Asks GitHub for the latest PrintCraft release (Help ▸ Check for updates, issue #28).
|
|
|
|
use std::time::Duration;
|
|
|
|
use printcraft_ui_egui::updates::{RELEASES_PAGE, Release};
|
|
|
|
const LATEST: &str = "https://api.github.com/repos/storytold/printcraft/releases/latest";
|
|
|
|
/// The latest release. The answer is untrusted: its size is capped, and only a page under
|
|
/// [`RELEASES_PAGE`] is ever offered for download (anything else falls back to that list).
|
|
pub fn latest_release() -> Result<Release, String> {
|
|
let agent = ureq::Agent::config_builder()
|
|
.timeout_global(Some(Duration::from_secs(10)))
|
|
.tls_config(ureq::tls::TlsConfig::builder().root_certs(os_roots()?).build())
|
|
.build()
|
|
.new_agent();
|
|
let mut response = agent
|
|
.get(LATEST)
|
|
.header("Accept", "application/vnd.github+json")
|
|
.header("User-Agent", concat!("PrintCraft/", env!("CARGO_PKG_VERSION")))
|
|
.call()
|
|
.map_err(|e| format!("couldn't reach GitHub ({e})"))?;
|
|
let body = response.body_mut().with_config().limit(1 << 20).read_to_string().map_err(|e| format!("unreadable answer ({e})"))?;
|
|
parse(&body)
|
|
}
|
|
|
|
/// The certificate authorities the operating system trusts.
|
|
fn os_roots() -> Result<ureq::tls::RootCerts, String> {
|
|
let found = rustls_native_certs::load_native_certs();
|
|
let certs: Vec<ureq::tls::Certificate<'static>> = found.certs.iter().map(|c| ureq::tls::Certificate::from_der(c.as_ref()).to_owned()).collect();
|
|
if certs.is_empty() {
|
|
return Err("no trusted certificates found on this system".into());
|
|
}
|
|
Ok(ureq::tls::RootCerts::new_with_certs(&certs))
|
|
}
|
|
|
|
fn parse(body: &str) -> Result<Release, String> {
|
|
let v: serde_json::Value = serde_json::from_str(body).map_err(|e| format!("unreadable answer ({e})"))?;
|
|
let version = v["tag_name"].as_str().filter(|t| !t.is_empty() && t.len() <= 64).ok_or("no release found")?.to_string();
|
|
let url = v["html_url"]
|
|
.as_str()
|
|
.filter(|u| u.strip_prefix(RELEASES_PAGE).is_some_and(|rest| rest.starts_with('/') && !rest.contains(['?', '#', '\\'])))
|
|
.unwrap_or(RELEASES_PAGE)
|
|
.to_string();
|
|
Ok(Release { version, url })
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn answers_are_read_and_only_our_release_pages_are_offered() {
|
|
let r = parse(r#"{"tag_name":"v0.2.0","html_url":"https://github.com/storytold/printcraft/releases/tag/v0.2.0"}"#).unwrap();
|
|
assert_eq!(r, Release { version: "v0.2.0".into(), url: "https://github.com/storytold/printcraft/releases/tag/v0.2.0".into() });
|
|
for elsewhere in ["https://example.com/printcraft.exe", "https://github.com/storytold/printcraft/releases.evil/x", "javascript:alert(1)"] {
|
|
let r = parse(&format!(r#"{{"tag_name":"v9.9.9","html_url":"{elsewhere}"}}"#)).unwrap();
|
|
assert_eq!(r.url, RELEASES_PAGE, "{elsewhere}");
|
|
}
|
|
assert!(parse(r#"{"message":"Not Found"}"#).is_err());
|
|
assert!(parse("<html>").is_err());
|
|
}
|
|
|
|
/// Live: asks GitHub over TLS with the OS's roots (`cargo test -p printcraft -- --ignored`).
|
|
#[test]
|
|
#[ignore = "needs network access"]
|
|
fn github_answers_with_the_latest_release() {
|
|
let r = latest_release().unwrap();
|
|
assert!(printcraft_ui_egui::updates::is_newer(&r.version, "0.0.0"), "{r:?}");
|
|
assert!(r.url.starts_with(RELEASES_PAGE), "{r:?}");
|
|
}
|
|
}
|