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
mux-server: write the TLS key the way the token store is written
RoversX21cdc57f7359
diff --git a/PRIVACY.md b/PRIVACY.mdindex 23d1ec84..6190ab0a 100644--- a/PRIVACY.md+++ b/PRIVACY.md@@ -66,7 +66,13 @@ loads and keeps it in that tab's `sessionStorage`, which the browserdiscards when the tab closes.While a listener is on, ThinkTerm keeps two things on the local disk,-readable by your user only, and sends neither anywhere.+for your user, and sends neither anywhere. On Linux and macOS both are+written readable by their owner alone. Windows has no equivalent to set:+they take the permissions of the directory they land in, which for a+default user profile is you, SYSTEM and the Administrators group, and+nobody else. An administrator of the machine can therefore read them --+though an administrator can read the terminals themselves and does not+need either file to do it.The first is the tokens, and only if a `token_file` is configured: unsetmeans memory only, and restarting the server forgets every token. What is@@ -80,7 +86,8 @@ than who used it.The second is a self-signed certificate and its private key, under`web-tls/` in ThinkTerm's data directory (`~/.local/share/thinkterm` on-Linux, `~/Library/Application Support/thinkterm` on macOS). It is made+Linux, `~/Library/Application Support/thinkterm` on macOS,+`%APPDATA%\thinkterm` on Windows). It is madeonly when you bind a listener off loopback without supplying a certificateof your own, and it names the machine's hostname and every non-loopbackaddress it has, so that a browser reaching it by any of them lands in adiff --git a/wezterm-mux-server-impl/src/lib.rs b/wezterm-mux-server-impl/src/lib.rsindex 701c32ea..5b96c5c3 100644--- a/wezterm-mux-server-impl/src/lib.rs+++ b/wezterm-mux-server-impl/src/lib.rs@@ -11,6 +11,7 @@ pub mod dispatch;pub mod handoff;pub mod local;pub mod pki;+mod private_file;mod sent_images;pub mod sessionhandler;pub mod thinkterm_access;diff --git a/wezterm-mux-server-impl/src/private_file.rs b/wezterm-mux-server-impl/src/private_file.rsnew file mode 100644index 00000000..5114c868--- /dev/null+++ b/wezterm-mux-server-impl/src/private_file.rs@@ -0,0 +1,84 @@+//! Replacing a file that holds a secret, in one step.+//!+//! Both the web token store and the web listener's self-signed key land+//! this way, and only one of them used to do it correctly.++use anyhow::Context;+use std::path::Path;++/// Write `bytes` to `path`, replacing whatever was there in one step.+///+/// A uniquely named temporary file in the same directory -- created 0600 on+/// unix, and created rather than opened, so a symlink someone planted at a+/// name this could have chosen is not followed -- synced before the rename,+/// so a crash leaves either the old file or the new one and never a torn+/// one. `prefix` names those temporaries so a directory holding several of+/// these reads at a glance.+///+/// On Windows there is no mode to set and none is claimed: the file takes+/// the ACL it inherits. Under ThinkTerm's data directory that is this user,+/// SYSTEM and the Administrators group, with no entry for anyone else --+/// narrower than world-readable, wider than 0600, and not something this+/// code enforces, because a local administrator can read the panes+/// directly and does not need the key to do it. `PRIVACY.md` says so too.+pub fn replace(path: &Path, prefix: &str, bytes: &[u8]) -> anyhow::Result<()> {+ let dir = match path.parent() {+ Some(dir) if !dir.as_os_str().is_empty() => dir,+ _ => Path::new("."),+ };+ std::fs::create_dir_all(dir)+ .with_context(|| format!("creating {}", dir.display()))?;+ let mut tmp = tempfile::Builder::new()+ .prefix(prefix)+ .tempfile_in(dir)+ .with_context(|| format!("creating a temporary file in {}", dir.display()))?;+ std::io::Write::write_all(&mut tmp, bytes)?;+ tmp.as_file().sync_all()?;+ tmp.persist(path)+ .with_context(|| format!("replacing {}", path.display()))?;+ Ok(())+}++#[cfg(test)]+mod tests {+ /// The replacement is atomic and leaves nothing behind. The temporary+ /// is uniquely named, so two of these racing cannot share one, and a+ /// name planted in advance cannot be the one it picks.+ #[test]+ fn a_secret_is_replaced_in_one_step() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("key.pem");++ super::replace(&path, ".key.", b"first").unwrap();+ assert_eq!(std::fs::read(&path).unwrap(), b"first");++ // A file sitting at the obvious temporary name is not what gets+ // written, and is not disturbed.+ let decoy = dir.path().join("key.tmp");+ std::fs::write(&decoy, b"decoy").unwrap();+ super::replace(&path, ".key.", b"second").unwrap();+ assert_eq!(std::fs::read(&path).unwrap(), b"second");+ assert_eq!(std::fs::read(&decoy).unwrap(), b"decoy");++ // Nothing but the file and the decoy remains.+ let mut left: Vec<String> = std::fs::read_dir(dir.path())+ .unwrap()+ .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())+ .collect();+ left.sort();+ assert_eq!(left, vec!["key.pem".to_string(), "key.tmp".to_string()]);+ }++ /// On unix the file is readable by its owner alone; there is no+ /// equivalent to assert on Windows, where it inherits its directory's.+ #[cfg(unix)]+ #[test]+ fn a_secret_is_readable_by_its_owner_only() {+ use std::os::unix::fs::PermissionsExt as _;+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("key.pem");+ super::replace(&path, ".key.", b"secret").unwrap();+ let mode = std::fs::metadata(&path).unwrap().permissions().mode();+ assert_eq!(mode & 0o777, 0o600, "mode was {:o}", mode & 0o777);+ }+}diff --git a/wezterm-mux-server-impl/src/web_auth.rs b/wezterm-mux-server-impl/src/web_auth.rsindex 2789c2d7..f03cb4e3 100644--- a/wezterm-mux-server-impl/src/web_auth.rs+++ b/wezterm-mux-server-impl/src/web_auth.rs@@ -473,25 +473,8 @@ impl WebTokenStore {}}-/// Write `bytes` to `path` readable by the owner only, replacing whatever-/// was there in one step. A uniquely named temporary file (created 0600,-/// never following a planted symlink), synced before the rename so a-/// crash leaves either the old file or the new one, never a torn one.fn write_private(path: &Path, bytes: &[u8]) -> anyhow::Result<()> {- let dir = match path.parent() {- Some(dir) if !dir.as_os_str().is_empty() => dir,- _ => Path::new("."),- };- std::fs::create_dir_all(dir)?;- let mut tmp = tempfile::Builder::new()- .prefix(".web-tokens.")- .tempfile_in(dir)- .with_context(|| format!("creating a temporary file in {}", dir.display()))?;- std::io::Write::write_all(&mut tmp, bytes)?;- tmp.as_file().sync_all()?;- tmp.persist(path)- .with_context(|| format!("replacing {}", path.display()))?;- Ok(())+ crate::private_file::replace(path, ".web-tokens.", bytes)}#[cfg(test)]diff --git a/wezterm-mux-server-impl/src/web_tls.rs b/wezterm-mux-server-impl/src/web_tls.rsindex 1ab5c566..0697534a 100644--- a/wezterm-mux-server-impl/src/web_tls.rs+++ b/wezterm-mux-server-impl/src/web_tls.rs@@ -56,20 +56,7 @@ fn fingerprint(der: &[u8]) -> String {}fn write_private(path: &Path, bytes: &[u8]) -> anyhow::Result<()> {- let tmp = path.with_extension("tmp");- {- let mut options = std::fs::OpenOptions::new();- options.write(true).create(true).truncate(true);- #[cfg(unix)]- {- use std::os::unix::fs::OpenOptionsExt;- options.mode(0o600);- }- let mut file = options.open(&tmp).with_context(|| format!("writing {}", tmp.display()))?;- std::io::Write::write_all(&mut file, bytes)?;- }- std::fs::rename(&tmp, path).with_context(|| format!("renaming into {}", path.display()))?;- Ok(())+ crate::private_file::replace(path, ".web-tls.", bytes)}/// The listener's certificate: the stored one when it still names every