A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
feat(remote-update): send verified installers and metadata over SSH
RoversX927f9e078333
diff --git a/Cargo.lock b/Cargo.lockindex 08000960..0e38795d 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -9673,6 +9673,7 @@ dependencies = ["serde","serde_json","sha2 0.10.9",+ "ssh-key","tempfile",]diff --git a/docs/thinkterm/remote-updates.md b/docs/thinkterm/remote-updates.mdnew file mode 100644index 00000000..45a087ee--- /dev/null+++ b/docs/thinkterm/remote-updates.md@@ -0,0 +1,119 @@+# Remote server updates++The SSH update/install flow prefers an online `install.sh` signed with the+pinned release key in the separate `thinkterm-installer-v1` SSHSIG namespace.+The client fetches it from the `installer-v1` prerelease assets using the+configured sources, without sending tokens. All requests, redirects, sources,+and the detached signature share a four-second budget; each HTTP request is+limited to 1.5 seconds or the remaining budget, whichever is smaller. Missing+curl, network failure, invalid signatures, incompatible protocol headers, or+an exhausted budget select the bundled script. Nothing unverified is executed.+There is no persistent script cache.++The selected script and trusted release metadata travel over authenticated+SSH. The server downloads the archive itself. The client does not download or+upload the archive on its behalf. Already released clients need one upgrade+to gain this selection flow; later script fixes do not require a client build.++The `publish installer` workflow tests, signs, and publishes `install.sh` and+`install.sh.sig` when the script changes on main, or when dispatched on main.+It uses the existing `THINKTERM_RELEASE_SIGNING_KEY` secret and does not build+application packages. The channel remains a prerelease so it is not offered+as the latest application release. Updating only an unsigned website copy+cannot update this signed channel: the signing workflow must succeed.+During publication, a temporarily mismatched script/signature pair safely+falls back to the bundled copy. Mirror copies may lag the origin.++The v1 script must preserve `--release-info`, `--version`, `--server`,+`--desktop`, `--mirror`, and noninteractive SSH behavior. Its first two lines+are `#!/bin/sh` and `# thinkterm-installer-v1`. Breaking that contract requires+a new channel and namespace; old clients continue using v1 or their fallback.++The client first asks the GitHub releases API for its exact release version.+If that fails, it tries `thinkterm-release.txt` and its detached `.sig` file,+using the configured download sources. A pinned Ed25519 public key verifies+the SSHSIG signature in the `thinkterm-release` namespace, and the inventory+must name the exact requested version. The desktop application's own updater+continues to use its existing lookup and installation flow.++The standalone installer also supports signed metadata when an explicit+`--version` lookup fails. It needs `ssh-keygen` with `-Y verify` to verify it.+If no verifier or no signed inventory is available for an older release, it+uses GitHub directly with a warning; it never authorizes a mirror without a+trusted SHA-256. A downloaded inventory with an invalid signature or wrong+version is rejected. Resolving an unspecified latest version still requires+the GitHub API.++With automatic source selection, the server probes GitHub for up to three+seconds. If it is slow, it probes the known mirrors concurrently and ranks+them by observed throughput, including connection setup. Probes are limited+in time and size. If all sources fail, sources that delivered bytes before+the speed cutoff get a second pass in rank order, allowing slow downloads+while still detecting stalled transfers and verifying the archive.+Transfers retry interrupted downloads using HTTP ranges;+a source that refuses resume gets a fresh attempt. Partials are never reused+after a checksum failure or across sources. Temporary files are removed when+the installer exits, so resume applies within an invocation, not across+separate installer runs. Explicit direct/mirror choices retain their meaning.++Linux server installs prefer the `.tar.xz` archive when both the release and+the host support it, otherwise they use `.tar.gz`. SSH output includes a static+wordmark and periodic byte counts, percentage, and rate without terminal+redrawing. Installing and restarting remain separate decisions in the client.++## Release signing++The repository's Actions secret `THINKTERM_RELEASE_SIGNING_KEY` contains the+complete, unencrypted OpenSSH Ed25519 private key dedicated to release signing.+The public half is pinned in `assets/release-signing.pub` and `install.sh`.+Never put the private half in the repository. Tests generate disposable keys+inside temporary directories; they do not use the release key or ssh-agent.++The publish job collects both this run's packages and existing assets from a+partial rerun, then `ci/sign-release.sh` generates and signs a canonical+inventory. It verifies the result against the pinned public key before any+metadata is uploaded. A missing secret or mismatched key fails publishing.+Archives are uploaded before the inventory/signature pair. Symbol bundles+are excluded. The workflow does not change existing releases unless invoked+for that release with publishing enabled.++The inventory format is UTF-8 with LF endings:++```text+thinkterm-release-v1+version 9.9.9+<64 lowercase SHA-256 hex digits> <byte count> <asset filename>+```++Rows are sorted by filename. Filenames cannot contain paths or whitespace,+duplicates are rejected, and metadata is bounded to 1 MiB. The client-to-server+copy also permits `-` in place of an absent API checksum; this never authorizes+a mirror and is not accepted in signed inventories.++Replacing a package after signing requires regenerating the inventory and+signature. This includes manually uploaded macOS packages. Rerunning the+publish workflow includes existing assets for platforms not rebuilt in that+run. Older releases need separately prepared inventories for the fallback to+work; this change does not backfill them automatically. Already released+clients retain their original API/raw-script dependencies until upgraded.++Keep the signing secret and pinned public key in sync when rotating keys, and+publish clients that trust the replacement key. A new key by itself cannot+make old clients trust new signatures. Merge workflow changes before running+the project's release process.++## Verification++```sh+python3 ci/test-remote-update.py -v+cargo test -p thinkterm-update -p wezterm-client --no-fail-fast+cargo test --no-fail-fast --workspace --exclude wezterm-ssh+python3 ci/check-privacy.py+```++The offline installer suite replaces network commands and platform probes,+uses isolated HOME/prefix/temp directories, and covers signing interoperability,+metadata fallback, wrong signatures/versions, fake mirror packages, source+selection, missing xz/verifier, resumed transfers, and non-TTY progress.+Real Linux/OpenSSH backends, GUI presentation, actual mainland connectivity,+and the GitHub signing-secret configuration still need integration validation.diff --git a/thinkterm-update/Cargo.toml b/thinkterm-update/Cargo.tomlindex b1576939..555bc09a 100644--- a/thinkterm-update/Cargo.toml+++ b/thinkterm-update/Cargo.toml@@ -16,6 +16,7 @@ humantime.workspace = trueserde = { workspace = true, features = ["derive"] }serde_json.workspace = truetempfile.workspace = true+ssh-key = { version = "=0.7.0-rc.11", default-features = false, features = ["std", "ed25519"] }[target.'cfg(target_os = "macos")'.dependencies]plist.workspace = truediff --git a/thinkterm-update/src/lib.rs b/thinkterm-update/src/lib.rsindex 4613fd3e..90a14751 100644--- a/thinkterm-update/src/lib.rs+++ b/thinkterm-update/src/lib.rs@@ -16,6 +16,7 @@ use std::time::{Duration, Instant};pub mod manifest;pub mod method;+pub mod remote;pub use manifest::InstallManifest;pub use method::InstallMethod;@@ -26,9 +27,10 @@ pub use method::InstallMethod;pub const REPO_URL: &str = "https://github.com/RoversX/thinkterm";/// Raw address of the installer on the default branch. `thinkterm update`-/// and the remote update fetch it from here rather than from the release-/// they install: the script has to understand the *current* archive layout,-/// and a fix to it should reach every existing install at once.+/// fetches it from here rather than from the release it installs: the script+/// has to understand the *current* archive layout,+/// and a fix to it should reach every existing install at once. Remote+/// installs prefer a signed online script with a bundled fallback in `remote`.pub const INSTALL_SCRIPT_URL: &str ="https://raw.githubusercontent.com/RoversX/thinkterm/main/install.sh";@@ -38,9 +40,9 @@ pub const INSTALL_SCRIPT_URL: &str =pub const MIRRORS: [&str; 4] = ["ghfast.top", "gh-proxy.org", "ghproxy.net", "gh.llkk.cc"];/// Where release archives and the Windows installer are downloaded from.-/// Only those: the releases API and the installer script always come from-/// GitHub, and an archive from a mirror is used only when it matches the-/// digest GitHub recorded for it.+/// Signed SSH installer scripts and release inventories also use these+/// sources. The releases API stays on GitHub; mirrored archives require+/// a trusted checksum and mirrored scripts require a pinned-key signature.#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]pub enum DownloadSource {/// GitHub, then each of `MIRRORS` in turn, then GitHub again: a link@@ -732,6 +734,17 @@ fn curl_get(headers: &[(&str, String)],give_way: Option<GiveWay>,body: &mut dyn Write,+) -> anyhow::Result<Answer> {+ curl_get_inner(extra, url, headers, give_way, body, true)+}++fn curl_get_inner(+ extra: &[&str],+ url: &str,+ headers: &[(&str, String)],+ give_way: Option<GiveWay>,+ body: &mut dyn Write,+ allow_fallback: bool,) -> anyhow::Result<Answer> {use std::io::{BufRead, BufReader, Read};use std::process::{Command, Stdio};@@ -770,7 +783,7 @@ fn curl_get(.spawn(){Ok(child) => child,- Err(err) if err.kind() == std::io::ErrorKind::NotFound => {+ Err(err) if allow_fallback && err.kind() == std::io::ErrorKind::NotFound => {return http_req_transport(url, headers, give_way, body);}Err(err) => return Err(err).context("running curl"),diff --git a/thinkterm-update/src/remote.rs b/thinkterm-update/src/remote.rsnew file mode 100644index 00000000..91277129--- /dev/null+++ b/thinkterm-update/src/remote.rs@@ -0,0 +1,731 @@+//! Release metadata and signed installer selection for SSH installs only.++use crate::{Asset, DownloadSource, GiveWay, Release};+use anyhow::{bail, ensure, Context};+use std::collections::HashSet;+use std::io::Write;+use std::time::{Duration, Instant};++pub const INSTALL_SCRIPT: &str = include_str!("../../install.sh");+pub const SIGNING_KEY: &str = include_str!("../../assets/release-signing.pub");+pub const MANIFEST_NAME: &str = "thinkterm-release.txt";+const NAMESPACE: &str = "thinkterm-release";+const INSTALLER_NAMESPACE: &str = "thinkterm-installer-v1";+const INSTALLER_HEADER: &str = "#!/bin/sh\n# thinkterm-installer-v1\n";+const INSTALLER_BUDGET: Duration = Duration::from_secs(4);+const METADATA_LIMIT: usize = 1024 * 1024;+const ARCHIVE_LIMIT: u64 = 8 * 1024 * 1024 * 1024;+const LOOKUP_TIMEOUT: GiveWay = GiveWay {+ connect: Duration::from_secs(4),+ stall: Duration::from_secs(8),+ min_rate: 128,+ window: Duration::from_secs(8),+};++fn safe_name(value: &str) -> bool {+ !value.is_empty()+ && value.len() <= 200+ && value.as_bytes()[0].is_ascii_alphanumeric()+ && value+ .bytes()+ .all(|b| b.is_ascii_alphanumeric() || b"._+-".contains(&b))+}++fn valid_digest(value: &str) -> bool {+ value.len() == 64+ && value+ .bytes()+ .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))+}++/// The same format is used for signed release metadata and for metadata+/// obtained from GitHub and delivered over the authenticated SSH connection.+/// `-` means GitHub supplied no digest; only the latter path accepts it.+pub fn release_info(release: &Release) -> anyhow::Result<String> {+ ensure!(safe_name(&release.tag_name), "invalid release tag");+ let mut text = format!("thinkterm-release-v1\nversion {}\n", release.tag_name);+ let mut names = HashSet::new();+ for asset in &release.assets {+ ensure!(safe_name(&asset.name), "invalid release asset name");+ ensure!(+ asset.size > 0 && asset.size as u64 <= ARCHIVE_LIMIT,+ "invalid release asset size"+ );+ ensure!(names.insert(&asset.name), "duplicate release asset name");+ let digest = asset.sha256().filter(|s| valid_digest(s)).unwrap_or("-");+ text.push_str(&format!("{digest} {} {}\n", asset.size, asset.name));+ }+ ensure!(+ text.len() <= METADATA_LIMIT,+ "release metadata is too large"+ );+ Ok(text)+}++fn parse_manifest(bytes: &[u8], tag: &str) -> anyhow::Result<Release> {+ ensure!(safe_name(tag), "invalid release tag");+ ensure!(+ bytes.len() <= METADATA_LIMIT,+ "release metadata is too large"+ );+ let text = std::str::from_utf8(bytes).context("release metadata is not UTF-8")?;+ ensure!(+ !text.contains('\r'),+ "invalid release metadata line endings"+ );+ let mut lines = text.lines();+ ensure!(+ lines.next() == Some("thinkterm-release-v1"),+ "unknown release metadata format"+ );+ ensure!(+ lines.next() == Some(format!("version {tag}").as_str()),+ "release metadata version mismatch"+ );+ let mut assets = Vec::new();+ let mut names = HashSet::new();+ for line in lines {+ let fields: Vec<_> = line.split(' ').collect();+ ensure!(fields.len() == 3, "invalid release metadata row");+ ensure!(valid_digest(fields[0]), "invalid release checksum");+ ensure!(+ !fields[1].is_empty() && fields[1].bytes().all(|b| b.is_ascii_digit()),+ "invalid release asset size"+ );+ let size: usize = fields[1].parse().context("invalid release asset size")?;+ ensure!(+ size > 0 && size as u64 <= ARCHIVE_LIMIT,+ "invalid release asset size"+ );+ let name = fields[2];+ ensure!(+ safe_name(name) && names.insert(name),+ "invalid or duplicate release asset name"+ );+ let url = format!("{}/releases/download/{tag}/{name}", crate::REPO_URL);+ assets.push(Asset {+ name: name.into(),+ size,+ url: url.clone(),+ browser_download_url: url,+ digest: Some(format!("sha256:{}", fields[0])),+ });+ }+ ensure!(!assets.is_empty(), "empty release metadata");+ Ok(Release {+ url: crate::release_tag_url(tag),+ html_url: crate::release_tag_url(tag),+ tag_name: tag.into(),+ body: String::new(),+ assets,+ thinkterm_commit_time: None,+ })+}++fn verified_manifest(+ bytes: &[u8],+ signature: &[u8],+ tag: &str,+ key: &str,+) -> anyhow::Result<Release> {+ let key =+ ssh_key::PublicKey::from_openssh(key).context("invalid release signing public key")?;+ let signature = ssh_key::SshSig::from_pem(signature).context("invalid release signature")?;+ key.verify(NAMESPACE, bytes, &signature)+ .context("release signature verification failed")?;+ parse_manifest(bytes, tag)+}++struct LimitedBody {+ bytes: Vec<u8>,+ limit: usize,+}++impl Write for LimitedBody {+ fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {+ if bytes.len() > self.limit.saturating_sub(self.bytes.len()) {+ return Err(std::io::Error::other("release metadata exceeds size limit"));+ }+ self.bytes.extend_from_slice(bytes);+ Ok(bytes.len())+ }+ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+}++// Authentication is explicitly allowed only for the API lookup. In particular,+// a mirror is never the initial host to which an update token may be sent.+fn fetch(+ url: &str,+ limit: usize,+ auth: Option<&str>,+ transport: crate::Transport,+) -> anyhow::Result<Vec<u8>> {+ let mut current = url.to_string();+ for _ in 0..=crate::MAX_REDIRECTS {+ let mut body = LimitedBody {+ bytes: Vec::new(),+ limit,+ };+ let mut headers = vec![("User-Agent", "thinkterm-release".to_string())];+ if let Some(token) = auth.filter(|_| crate::same_host(url, ¤t)) {+ headers.push(("Authorization", format!("Bearer {token}")));+ }+ let answer = transport(¤t, &headers, Some(LOOKUP_TIMEOUT), &mut body)?;+ if (300..400).contains(&answer.status) {+ current = crate::redirect_target(+ ¤t,+ &answer.location.context("redirect without Location")?,+ )?;+ } else {+ ensure!(+ (200..300).contains(&answer.status),+ "HTTP {} {}",+ answer.status,+ answer.reason+ );+ return Ok(body.bytes);+ }+ }+ bail!("too many release metadata redirects")+}++pub fn get_release(tag: &str) -> anyhow::Result<Release> {+ lookup(+ tag,+ crate::download_source(),+ crate::update_token().as_deref(),+ crate::transport(),+ SIGNING_KEY,+ )+}++fn lookup(+ tag: &str,+ source: DownloadSource,+ token: Option<&str>,+ transport: crate::Transport,+ key: &str,+) -> anyhow::Result<Release> {+ ensure!(safe_name(tag), "invalid release tag");+ let api_result = (|| -> anyhow::Result<Release> {+ let bytes = fetch(+ &format!("{}/tags/{tag}", crate::API_RELEASES),+ METADATA_LIMIT,+ token,+ transport,+ )?;+ let release: Release =+ serde_json::from_slice(&bytes).context("invalid GitHub release response")?;+ ensure!(release.tag_name == tag, "GitHub release version mismatch");+ release_info(&release)?;+ Ok(release)+ })();+ let api_error = match api_result {+ Ok(release) => return Ok(release),+ Err(err) => err,+ };+ let url = format!(+ "{}/releases/download/{tag}/{MANIFEST_NAME}",+ crate::REPO_URL+ );+ let source = if token.is_some() {+ DownloadSource::Direct+ } else {+ source+ };+ let mut visited = HashSet::new();+ let mut failures = Vec::new();+ for url in source.urls(&url) {+ if !visited.insert(url.clone()) {+ continue;+ }+ let result = (|| {+ let bytes = fetch(&url, METADATA_LIMIT, None, transport)?;+ let signature = fetch(&format!("{url}.sig"), 16 * 1024, None, transport)?;+ verified_manifest(&bytes, &signature, tag, key)+ })();+ match result {+ Ok(release) => return Ok(release),+ Err(err) => failures.push(format!(+ "{}: {err:#}",+ crate::host_of(&url).unwrap_or_default()+ )),+ }+ }+ bail!("could not obtain trusted metadata for {tag}; GitHub API: {api_error:#}; signed release metadata: {}", failures.join("; "))+}++pub struct InstallerScript {+ pub text: String,+ pub online: bool,+}++pub fn installer_script() -> InstallerScript {+ let source = if crate::update_token().is_some() {+ DownloadSource::Direct+ } else {+ crate::download_source()+ };+ select_installer(source, SIGNING_KEY, INSTALLER_BUDGET, installer_fetch)+}++fn installer_fetch(url: &str, limit: usize, deadline: Instant) -> anyhow::Result<Vec<u8>> {+ let mut current = url.to_owned();+ for _ in 0..=crate::MAX_REDIRECTS {+ let remaining = deadline.saturating_duration_since(Instant::now());+ ensure!(+ remaining >= Duration::from_millis(10),+ "installer lookup timed out"+ );+ let timeout = format!(+ "{:.3}",+ remaining.min(Duration::from_millis(1500)).as_secs_f64()+ );+ let mut body = LimitedBody {+ bytes: Vec::new(),+ limit,+ };+ // No unbounded http_req fallback when curl is absent. Each request,+ // including redirects and signatures, spends the same total budget.+ let answer = crate::curl_get_inner(+ &["--max-time", &timeout, "--proto", "=https", "--retry", "0"],+ ¤t,+ &[("User-Agent", "thinkterm-installer".into())],+ Some(LOOKUP_TIMEOUT),+ &mut body,+ false,+ )?;+ if (300..400).contains(&answer.status) {+ current = crate::redirect_target(+ ¤t,+ &answer.location.context("redirect without Location")?,+ )?;+ } else {+ ensure!(+ (200..300).contains(&answer.status),+ "installer HTTP {}",+ answer.status+ );+ return Ok(body.bytes);+ }+ }+ bail!("too many installer redirects")+}++fn select_installer(+ source: DownloadSource,+ key: &str,+ budget: Duration,+ mut fetch: impl FnMut(&str, usize, Instant) -> anyhow::Result<Vec<u8>>,+) -> InstallerScript {+ let deadline = Instant::now() + budget;+ let url = format!(+ "{}/releases/download/installer-v1/install.sh",+ crate::REPO_URL+ );+ let mut visited = HashSet::new();+ for url in source.urls(&url) {+ if Instant::now() >= deadline {+ break;+ }+ if !visited.insert(url.clone()) {+ continue;+ }+ let verified = (|| -> anyhow::Result<String> {+ let bytes = fetch(&url, METADATA_LIMIT, deadline)?;+ ensure!(+ bytes.len() <= METADATA_LIMIT,+ "installer exceeds size limit"+ );+ ensure!(Instant::now() < deadline, "installer lookup timed out");+ let signature = fetch(&format!("{url}.sig"), 16 * 1024, deadline)?;+ ensure!(+ signature.len() <= 16 * 1024,+ "installer signature exceeds size limit"+ );+ ensure!(Instant::now() < deadline, "installer lookup timed out");+ let key = ssh_key::PublicKey::from_openssh(key)?;+ key.verify(+ INSTALLER_NAMESPACE,+ &bytes,+ &ssh_key::SshSig::from_pem(&signature)?,+ )?;+ let script = String::from_utf8(bytes)?;+ ensure!(+ script.starts_with(INSTALLER_HEADER),+ "unsupported installer protocol"+ );+ Ok(script)+ })();+ if let Ok(text) = verified {+ return InstallerScript { text, online: true };+ }+ }+ InstallerScript {+ text: INSTALL_SCRIPT.into(),+ online: false,+ }+}++/// Receive exact lengths rather than waiting for EOF: closing exec stdin in+/// wezterm-ssh also closes the channel. The caller keeps stdin until exit.+pub fn install_payload(+ variant: &str,+ release: &Release,+ script: &str,+) -> anyhow::Result<(String, Vec<u8>)> {+ payload_for_script(variant, release, script)+}++fn payload_for_script(+ variant: &str,+ release: &Release,+ script: &str,+) -> anyhow::Result<(String, Vec<u8>)> {+ ensure!(+ matches!(variant, "server" | "desktop"),+ "invalid install variant"+ );+ let metadata = release_info(release)?;+ let version = &release.tag_name;+ let command = format!(+ "(export PATH=\"$HOME/.local/bin:$PATH\"; \+ work=$(mktemp -d \"${{TMPDIR:-/tmp}}/thinkterm-update.XXXXXX\") || exit 1; \+ trap 'rm -rf \"$work\"' EXIT; trap 'exit 130' INT; trap 'exit 143' TERM; \+ head -c {total_len} > \"$work/payload\" && \+ test \"$(wc -c < \"$work/payload\")\" -eq {total_len} && \+ head -c {script_len} \"$work/payload\" > \"$work/install.sh\" && \+ tail -c {info_len} \"$work/payload\" > \"$work/release-info\" && \+ THINKTERM_INSTALL_PROGRESS=1 sh \"$work/install.sh\" --{variant} --version {version} \+ --release-info \"$work/release-info\"{mirror} </dev/null)",+ script_len = script.len(),+ info_len = metadata.len(),+ total_len = script.len() + metadata.len(),+ mirror = crate::mirror_arg(if crate::update_token().is_some() {+ DownloadSource::Direct+ } else {+ crate::download_source()+ }),+ );+ let mut payload = script.as_bytes().to_vec();+ payload.extend_from_slice(metadata.as_bytes());+ Ok((command, payload))+}++#[cfg(test)]+mod tests {+ use super::*;+ use std::cell::RefCell;+ use std::process::{Command, Stdio};++ fn metadata() -> Vec<u8> {+ format!("thinkterm-release-v1\nversion 9.9.9\n{} 42 thinkterm-server-9.9.9-linux-x86_64.tar.gz\n", "a".repeat(64)).into_bytes()+ }++ // All signing material used here is generated in an owned temporary+ // directory and removed with it. No developer key or ssh-agent is used.+ fn signed(bytes: &[u8], namespace: &str) -> (String, Vec<u8>) {+ let dir = tempfile::tempdir().unwrap();+ let key = dir.path().join("example-key");+ let data = dir.path().join("manifest");+ assert!(Command::new("ssh-keygen")+ .args(["-q", "-t", "ed25519", "-N", "", "-C", "example", "-f"])+ .arg(&key)+ .output()+ .unwrap()+ .status+ .success());+ std::fs::write(&data, bytes).unwrap();+ assert!(Command::new("ssh-keygen")+ .args(["-Y", "sign", "-n", namespace, "-f"])+ .arg(&key)+ .arg(&data)+ .output()+ .unwrap()+ .status+ .success());+ (+ std::fs::read_to_string(key.with_extension("pub")).unwrap(),+ std::fs::read(data.with_extension("sig")).unwrap(),+ )+ }++ #[test]+ fn rejects_wrong_versions_duplicates_paths_and_missing_checksums() {+ let bytes = metadata();+ assert!(parse_manifest(&bytes, "9.9.9").is_ok());+ assert!(parse_manifest(&bytes, "9.9.8").is_err());+ let text = String::from_utf8(bytes).unwrap();+ for bad in [+ text.replace("thinkterm-server-", "../thinkterm-server-"),+ text.replace(&"a".repeat(64), "-"),+ text.replace(" 42 ", " +42 "),+ text.replace(" 42 ", " 0 "),+ format!("{text}{}\n", text.lines().last().unwrap()),+ ] {+ assert!(parse_manifest(bad.as_bytes(), "9.9.9").is_err());+ }+ }++ #[test]+ fn openssh_signature_checks_content_key_namespace_and_version() {+ let bytes = metadata();+ let (key, signature) = signed(&bytes, NAMESPACE);+ assert!(verified_manifest(&bytes, &signature, "9.9.9", &key).is_ok());+ let mut tampered = bytes.clone();+ tampered[0] ^= 1;+ assert!(verified_manifest(&tampered, &signature, "9.9.9", &key).is_err());+ assert!(verified_manifest(&bytes, &signature, "9.9.8", &key).is_err());+ assert!(verified_manifest(&bytes, &signature, "9.9.9", SIGNING_KEY).is_err());+ let (key, signature) = signed(&bytes, "example-other-namespace");+ assert!(verified_manifest(&bytes, &signature, "9.9.9", &key).is_err());+ }++ thread_local! {+ static RESPONSES: RefCell<(Vec<u8>, Vec<u8>, Vec<String>)> = const { RefCell::new((Vec::new(), Vec::new(), Vec::new())) };+ }++ fn offline_github(+ url: &str,+ headers: &[(&str, String)],+ deadline: Option<GiveWay>,+ body: &mut dyn Write,+ ) -> anyhow::Result<crate::Answer> {+ assert!(deadline.is_some());+ RESPONSES.with(|state| {+ let mut state = state.borrow_mut();+ state.2.push(url.into());+ if url.starts_with("https://api.github.com/") || url.starts_with("https://github.com/")+ {+ bail!("connection timed out");+ }+ assert!(!headers.iter().any(|(name, _)| *name == "Authorization"));+ body.write_all(if url.ends_with(".sig") {+ &state.1+ } else {+ &state.0+ })?;+ Ok(crate::Answer {+ status: 200,+ reason: "OK".into(),+ location: None,+ })+ })+ }++ #[test]+ fn api_failure_uses_verified_mirror_metadata_but_direct_and_token_do_not() {+ let bytes = metadata();+ let (key, signature) = signed(&bytes, NAMESPACE);+ RESPONSES.with(|state| *state.borrow_mut() = (bytes, signature, Vec::new()));+ assert!(lookup("9.9.9", DownloadSource::Auto, None, offline_github, &key).is_ok());+ for token in [None, Some("example-token")] {+ RESPONSES.with(|state| state.borrow_mut().2.clear());+ assert!(lookup(+ "9.9.9",+ if token.is_some() {+ DownloadSource::Auto+ } else {+ DownloadSource::Direct+ },+ token,+ offline_github,+ &key+ )+ .is_err());+ RESPONSES.with(|state| {+ assert!(state+ .borrow()+ .2+ .iter()+ .all(|url| url.starts_with("https://api.github.com/")+ || url.starts_with("https://github.com/")))+ });+ }+ RESPONSES.with(|state| state.borrow_mut().0[0] ^= 1);+ assert!(lookup("9.9.9", DownloadSource::Auto, None, offline_github, &key).is_err());+ }++ #[test]+ fn metadata_download_has_a_memory_bound() {+ let mut body = LimitedBody {+ bytes: vec![],+ limit: 4,+ };+ assert_eq!(body.write(b"abc").unwrap(), 3);+ assert!(body.write(b"de").is_err());+ assert_eq!(body.bytes, b"abc");+ }++ #[test]+ fn online_installer_requires_the_key_namespace_content_and_protocol() {+ let script = format!("{INSTALLER_HEADER}echo example\n");+ let (key, signature) = signed(script.as_bytes(), INSTALLER_NAMESPACE);+ let select = |bytes: &[u8], signature: &[u8], key: &str| {+ select_installer(+ DownloadSource::Direct,+ key,+ INSTALLER_BUDGET,+ |url, _, _| {+ Ok(if url.ends_with(".sig") {+ signature+ } else {+ bytes+ }+ .to_vec())+ },+ )+ };+ let good = select(script.as_bytes(), &signature, &key);+ assert!(good.online);+ assert_eq!(good.text, script);+ for bad in [+ select(b"tampered", &signature, &key),+ select(script.as_bytes(), b"invalid", &key),+ select(script.as_bytes(), &signature, SIGNING_KEY),+ ] {+ assert!(!bad.online);+ assert_eq!(bad.text, INSTALL_SCRIPT);+ }+ let (other_key, other_sig) = signed(script.as_bytes(), NAMESPACE);+ assert!(!select(script.as_bytes(), &other_sig, &other_key).online);+ let incompatible = b"#!/bin/sh\n# thinkterm-installer-v2\n";+ let (key, signature) = signed(incompatible, INSTALLER_NAMESPACE);+ assert!(!select(incompatible, &signature, &key).online);+ }++ #[test]+ fn installer_mirrors_and_signature_share_one_deadline() {+ let script = format!("{INSTALLER_HEADER}echo example\n");+ let (key, signature) = signed(script.as_bytes(), INSTALLER_NAMESPACE);+ let mut first_deadline = None;+ let mut calls = 0;+ let selected = select_installer(+ DownloadSource::Auto,+ &key,+ INSTALLER_BUDGET,+ |url, _, deadline| {+ calls += 1;+ assert_eq!(*first_deadline.get_or_insert(deadline), deadline);+ if url.starts_with(crate::REPO_URL) {+ bail!("offline");+ }+ Ok(if url.ends_with(".sig") {+ signature.clone()+ } else {+ script.as_bytes().to_vec()+ })+ },+ );+ assert!(selected.online);+ assert_eq!(calls, 3);+ let mut calls = 0;+ let selected = select_installer(+ DownloadSource::Auto,+ &key,+ Duration::from_millis(20),+ |_, _, deadline| {+ calls += 1;+ std::thread::sleep(+ deadline.saturating_duration_since(Instant::now()) + Duration::from_millis(5),+ );+ Ok(script.as_bytes().to_vec())+ },+ );+ assert_eq!(calls, 1);+ assert!(!selected.online);+ assert_eq!(selected.text, INSTALL_SCRIPT);+ }++ #[test]+ fn unavailable_and_oversized_online_installers_use_bundled_copy() {+ for oversized in [false, true] {+ let selected = select_installer(+ DownloadSource::Direct,+ SIGNING_KEY,+ INSTALLER_BUDGET,+ |_, limit, _| {+ if oversized {+ Ok(vec![b'a'; limit + 1])+ } else {+ bail!("offline")+ }+ },+ );+ assert!(!selected.online);+ assert_eq!(selected.text, INSTALL_SCRIPT);+ }+ }++ #[test]+ fn curl_installer_deadline_interrupts_a_stalled_connection() {+ use std::net::TcpListener;+ let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();+ let url = format!(+ "https://localhost:{}/install.sh",+ listener.local_addr().unwrap().port()+ );+ // Keep TCP open without completing TLS; curl must enforce its own+ // total deadline rather than wait for a response or a read timeout.+ let started = Instant::now();+ let mut body = Vec::new();+ let result = crate::curl_get_inner(+ &["--max-time", "0.1", "--noproxy", "*"],+ &url,+ &[],+ Some(LOOKUP_TIMEOUT),+ &mut body,+ false,+ );+ assert!(result.is_err());+ assert!(started.elapsed() < Duration::from_secs(2));+ drop(listener);+ }++ #[test]+ fn shell_and_client_pin_the_same_public_key() {+ assert!(INSTALL_SCRIPT.contains(&format!("release_signing_key='{}'", SIGNING_KEY.trim())));+ }++ #[cfg(unix)]+ #[test]+ fn ssh_payload_finishes_with_stdin_held_open_and_removes_staging() {+ let release = parse_manifest(&metadata(), "9.9.9").unwrap();+ let dir = tempfile::tempdir().unwrap();+ let (command, payload) = payload_for_script("server", &release,+ "test \"$1\" = --server || exit 1\ncat \"$5\"\nif read -r ignored; then exit 2; fi\necho done\n").unwrap();+ let mut child = Command::new("sh")+ .args(["-c", &command])+ .env("HOME", dir.path())+ .env("TMPDIR", dir.path())+ .stdin(Stdio::piped())+ .stdout(Stdio::piped())+ .stderr(Stdio::piped())+ .spawn()+ .unwrap();+ let mut held_stdin = child.stdin.take().unwrap();+ held_stdin.write_all(&payload).unwrap();+ let started = std::time::Instant::now();+ while child.try_wait().unwrap().is_none() {+ if started.elapsed() > Duration::from_secs(5) {+ child.kill().unwrap();+ panic!("installer waited for SSH EOF");+ }+ std::thread::sleep(Duration::from_millis(10));+ }+ let out = child.wait_with_output().unwrap();+ assert!(+ out.status.success(),+ "{}",+ String::from_utf8_lossy(&out.stderr)+ );+ assert_eq!(+ String::from_utf8(out.stdout).unwrap(),+ format!("{}done\n", release_info(&release).unwrap())+ );+ assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0);+ drop(held_stdin);+ }+}diff --git a/wezterm-client/src/remote_update.rs b/wezterm-client/src/remote_update.rsindex 009743e6..6fff0018 100644--- a/wezterm-client/src/remote_update.rs+++ b/wezterm-client/src/remote_update.rs@@ -27,7 +27,7 @@ use config::SshDomain;use mux::connui::ConnectionUI;use portable_pty::Child as _;use std::convert::TryFrom;-use std::io::Read;+use std::io::{BufRead, BufReader, Read, Write};use std::sync::atomic::{AtomicBool, Ordering};use wezterm_ssh::Session;@@ -109,21 +109,14 @@ pub fn offer_remote_update());return Ok(RemoteUpdateOutcome::Declined);}- if let Err(lookup) = thinkterm_update::get_release_by_tag(&local) {- if !is_not_published(&lookup) {- ui.output_str(&format!("Could not look up {local} on GitHub: {lookup:#}\n"));+ ui.output_str(&format!("Looking up release information for {local}...\n"));+ let release = match thinkterm_update::remote::get_release(&local) {+ Ok(release) => release,+ Err(lookup) => {+ ui.output_str(&format!("Could not resolve release {local}: {lookup:#}\n"));return Ok(RemoteUpdateOutcome::Declined);}- let latest = thinkterm_update::get_latest_release_info()- .map(|latest| format!(" (the latest is {})", latest.tag_name))- .unwrap_or_default();- ui.output_str(&format!(- "GitHub has no release {local} yet{latest}. Update {host} once it is \- released, or build a matching thinkterm-mux-server there from the same \- source now.\n"- ));- return Ok(RemoteUpdateOutcome::Declined);- }+ };// The ssh session comes first: the host has to be asked what is// installed there before the question can say what will change. A@@ -163,8 +156,8 @@ pub fn offer_remote_update(let answer = ui.input(&format!("ThinkTerm can install {local} there now ({variant} variant, as the ssh user, \- under ~/.local, with no root):\n \- {command}\n\+ under ~/.local, with no root). A verified online installer (or bundled fallback) \+ and trusted release information will be sent over SSH; the host downloads the archive.\n\{caveat}Install it? [y/N] ",));let answer = match answer {@@ -182,10 +175,10 @@ pub fn offer_remote_update(}ui.output_str(&format!("\nInstalling ThinkTerm {local} on {host}...\n"));- let status = run_and_relay(&session, ui, &command)?;+ let status = run_remote_installer(&session, ui, variant, &release)?;if !status {ui.output_str(&format!(- "\nThe installer on {host} failed; its output is above. Nothing was changed there.\n\+ "\nThe installer on {host} failed; its output is above.\n\If it refused the host (glibc, musl, architecture), a release binary cannot run on it.\n{}\n",thinkterm_update::build_from_source_instructions(&local)));@@ -319,9 +312,9 @@ pub fn offer_remote_install(String::new()};let answer = ui.input(&format!(- "ThinkTerm can install {local} there now (downloaded from GitHub, as the ssh \- user, under ~/.local, with no root):\n \- {command}\n\+ "ThinkTerm can install {local} there now (as the ssh user, under ~/.local, \+ with no root). A verified online installer (or bundled fallback) and trusted release \+ information will be sent over SSH; the host downloads the archive.\n\{caveat}Install it? [y/N] "));let answer = match answer {@@ -340,17 +333,14 @@ pub fn offer_remote_install());return Ok(RemoteInstallOutcome::Declined);}- if let Err(lookup) = thinkterm_update::get_release_by_tag(&local) {- if is_not_published(&lookup) {- ui.output_str(&format!(- "GitHub has no release {local} yet. Install a matching \- thinkterm-mux-server on {host} by hand, or switch the host to SSH.\n"- ));- } else {- ui.output_str(&format!("Could not look up {local} on GitHub: {lookup:#}\n"));+ ui.output_str(&format!("Looking up release information for {local}...\n"));+ let release = match thinkterm_update::remote::get_release(&local) {+ Ok(release) => release,+ Err(lookup) => {+ ui.output_str(&format!("Could not resolve release {local}: {lookup:#}\n"));+ return Ok(RemoteInstallOutcome::Unavailable);}- return Ok(RemoteInstallOutcome::Unavailable);- }+ };let ssh_config = mux::ssh::ssh_domain_to_ssh_config(ssh_dom)?;let mut ui_for_auth = ui.clone();@@ -369,7 +359,7 @@ pub fn offer_remote_install(}ui.output_str(&format!("\nInstalling ThinkTerm {local} on {host}...\n"));- if !run_and_relay(&session, ui, &command)? {+ if !run_remote_installer(&session, ui, variant, &release)? {ui.output_str(&format!("\nThe installer on {host} failed; its output is above.\n{}\n",thinkterm_update::build_from_source_instructions(&local)@@ -415,7 +405,10 @@ fn release_refusal(probe: &str, variant: &str) -> Option<String> {let arch = lines.next()?;let libc = lines.next().unwrap_or("");if os == "Linux" && !matches!(arch, "x86_64" | "aarch64") {- return Some(format!("{host_arch} has no release build", host_arch = arch));+ return Some(format!(+ "{host_arch} has no release build",+ host_arch = arch+ ));}if os == "Linux" && libc.to_ascii_lowercase().contains("musl") {return Some("The host uses musl libc".to_string());@@ -474,11 +467,66 @@ fn is_yes(answer: &str) -> bool {matches!(answer.trim(), "y" | "Y" | "yes" | "YES")}-/// Whether a release lookup failed because GitHub has no such release, as-/// opposed to GitHub being unreachable. `thinkterm_update` reports HTTP-/// errors as "fetching <uri>: HTTP <status> <reason>".-fn is_not_published(err: &anyhow::Error) -> bool {- format!("{err:#}").contains(": HTTP 404 ")+fn run_remote_installer(+ session: &Session,+ ui: &ConnectionUI,+ variant: &str,+ release: &thinkterm_update::Release,+) -> anyhow::Result<bool> {+ ui.output_str("Checking for a signed installer (up to 4 seconds)...\n");+ let installer = thinkterm_update::remote::installer_script();+ ui.output_str(if installer.online {+ "Verified the online installer; sending it and release information over SSH...\n"+ } else {+ "No verified online installer available; sending the bundled installer over SSH...\n"+ });+ let (command, payload) =+ thinkterm_update::remote::install_payload(variant, release, &installer.text)?;+ let exec =+ smol::block_on(session.exec(&command, None)).context("starting the remote installer")?;+ let mut stdin = exec.stdin;+ let mut child = exec.child;+ let stdout = exec.stdout;+ let stderr = exec.stderr;+ // The receiver reads exact byte counts, not EOF. Keep stdin alive until+ // both output streams have drained and the remote command has exited.+ let writer = std::thread::spawn(move || {+ let result = stdin.write_all(&payload).and_then(|_| stdin.flush());+ (stdin, result)+ });+ let ui_err = ui.clone();+ let stderr_thread = std::thread::spawn(move || relay_lines(stderr, &ui_err));+ let stdout_result = relay_lines(stdout, ui);+ let stderr_result = stderr_thread.join();+ let status = child.wait();+ let (stdin, sent) = writer+ .join()+ .map_err(|_| anyhow::anyhow!("installer upload thread failed"))?;+ drop(stdin);+ sent.context("sending the installer over SSH")?;+ stdout_result?;+ stderr_result.map_err(|_| anyhow::anyhow!("installer output thread failed"))??;+ Ok(status+ .context("waiting for the remote installer")?+ .success())+}++fn relay_lines(reader: impl Read, ui: &ConnectionUI) -> std::io::Result<()> {+ // Buffer UTF-8 across SSH reads so the wordmark cannot split a character.+ // The installer emits bounded, newline-terminated progress in this mode.+ let mut reader = BufReader::new(reader);+ let mut line = Vec::new();+ loop {+ line.clear();+ let len = reader+ .by_ref()+ .take(16 * 1024)+ .read_until(b'\n', &mut line)?;+ if len == 0 {+ return Ok(());+ }+ ui.output_str(&String::from_utf8_lossy(&line));+ }}/// Run `command` on the session, copying its stdout and stderr into the UI@@ -519,14 +567,16 @@ fn run_and_relay_status(ui.output_str(&String::from_utf8_lossy(&buf[..len]));}let _ = stderr_thread.join();- let status = child.wait().with_context(|| format!("waiting for `{command}`"));+ let status = child+ .wait()+ .with_context(|| format!("waiting for `{command}`"));drop(stdin);status}#[cfg(test)]mod tests {- use super::{is_not_published, is_yes, release_refusal, without_input};+ use super::{is_yes, release_refusal, without_input};#[test]fn only_an_explicit_yes_counts() {@@ -599,21 +649,4 @@ mod tests {std::io::Read::read_to_string(child.stdout.as_mut().unwrap(), &mut out).unwrap();assert_eq!(out, "done\n");}-- #[test]- fn only_a_404_means_the_release_is_not_out() {- let uri = "https://api.github.com/repos/o/r/releases/tags/9.9.9";- assert!(is_not_published(&anyhow::anyhow!(- "fetching {}: HTTP 404 Not Found",- uri- )));- assert!(!is_not_published(&anyhow::anyhow!(- "fetching {}: HTTP 403 Forbidden",- uri- )));- assert!(!is_not_published(&anyhow::anyhow!(- "fetching {}: failed to lookup address information",- uri- )));- }}