fix(host): APPIMAGE is believed only when this program runs from that image

The AppImage launcher sets APPIMAGE (the image's file) and APPDIR (where
it is mounted) for the program it starts, and every program that one
starts inherits both: a terminal, an editor, an agent's app. The host's
start, the agent's pointer file, the host's check for a replaced program
and `update` each read APPIMAGE and believed it whenever it named a file.

So a wanderbooks run from inside another app's AppImage took that app's
image for its own:

- it started that image as the host, and the books never opened ("the
  books server keeps failing to start");
- `connect` would have written that image into the agent's pointer file;
- `wanderbooks update` replaced that app's file with wanderbooks.

Found by the batch's gate: the_host_outlives_the_app_scope_that_started_it
passes its own environment on (the other tests clear it), and this gate
ran in a session started from an AppImage app. The first and the third
are reproduced by two new tests, which fail without this change (the
update test read an ELF header where the other app's file had been).

The rule now (start.rs INVARIANT 6, from the AppImage documentation's
"Environment variables"): the image is this program's own only when
APPIMAGE is an absolute file, APPDIR an absolute folder, and the running
executable's file is inside APPDIR, compared as resolved paths. One
function decides (`own_appimage`), and all four readers ask it. The host
is handed APPDIR beside APPIMAGE, and neither unless both are its own.

No books and no figure are touched. The tests that pretend to be an
AppImage now set APPDIR as a launcher does; translocation.rs used a bare
APPIMAGE to stand in for a macOS path, the very thing this closes, and
names the program's folder as the mount.

The failing test's message now lists the lines that did arrive, which is
what showed the host was refused rather than slow.

Not checked against a built AppImage (none has been built with this
change): the first trial of one shows `update` and a restart find the
image.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-10 02:28:05 -07:00
1 parent 0178809a3d
commit 3bc198594b
12 files changed
+331 -52

No files matched your search

+43 -2
View File
@@ -254,12 +254,16 @@ impl Running {
/// Wait for a line containing `text`.
fn expect(&self, text: &str, secs: u64) -> String {
let deadline = Instant::now() + Duration::from_secs(secs);
let mut seen = Vec::new();
loop {
let left = deadline.saturating_duration_since(Instant::now());
match self.lines.recv_timeout(left) {
Ok(line) if line.contains(text) => return line,
Ok(_) => {}
Err(_) => panic!("no line with {text:?} in {secs} s"),
Ok(line) => seen.push(line),
// What did arrive says why: a refusal, or nothing at all.
Err(e) => {
panic!("no line with {text:?} in {secs} s ({e}); the lines seen: {seen:?}")
}
}
}
}
@@ -1203,6 +1207,43 @@ fn books_commands_hold_the_books_while_they_run() {
}
}
/// start.rs INVARIANT 6: started from inside another app's AppImage (a
/// terminal, an editor, an agent's app: each hands `APPIMAGE` and `APPDIR`
/// to everything it starts), this program is still the one that runs as
/// the host. That image is never run.
#[test]
fn another_apps_appimage_is_never_run_as_the_host() {
use std::os::unix::fs::PermissionsExt;
let home = Home::new("foreign");
// The other app: a program that leaves a mark if it is ever run.
let mark = home.dir.path().join("other-app-ran");
let other = home.dir.path().join("Other.AppImage");
std::fs::write(&other, format!("#!/bin/sh\n: > '{}'\n", mark.display())).unwrap();
std::fs::set_permissions(&other, std::fs::Permissions::from_mode(0o755)).unwrap();
let mount = home.dir.path().join("other-mount");
std::fs::create_dir(&mount).unwrap();
let mut c = home.cmd("books");
c.args(["create", "acme"])
.env("APPIMAGE", &other)
.env("APPDIR", &mount)
.stdin(Stdio::null());
let out = c.output().unwrap();
assert!(out.status.success(), "{out:?}");
let mut c = home.cmd("serve");
c.env("APPIMAGE", &other).env("APPDIR", &mount);
let held = Running::start(c);
held.expect("The books are open", 30);
let host = home.host().unwrap();
assert_eq!(
std::fs::read_link(format!("/proc/{host}/exe")).unwrap(),
std::fs::canonicalize(&home.exe).unwrap(),
"the host is another program"
);
assert!(!mark.exists(), "the other app's image was run");
held.signal(libc::SIGINT);
assert!(home.wait_closed(10));
}
/// Under a systemd app scope (as uwsm sets up), closing the app that
/// started the host leaves the host for the other holders. Run only where
/// a systemd user manager is there to make scopes.
+10 -2
View File
@@ -6,8 +6,10 @@
//!
//! The check is on the path the program would give the agent
//! (`ledger_host::start::this_executable`), so a test can stand in for
//! macOS on any Unix: `APPIMAGE` names a file there, and a path with an
//! `AppTranslocation` part is refused just as the real one is.
//! macOS on any Unix: `APPIMAGE` names a file there (with `APPDIR` naming
//! the folder the program runs from, as an image's launcher sets the two:
//! `APPIMAGE` alone is not believed, start.rs INVARIANT 6), and a path
//! with an `AppTranslocation` part is refused just as the real one is.
#![cfg(unix)]
@@ -95,9 +97,14 @@ fn translocated_app_refuses_to_connect() {
);
std::fs::create_dir_all(translocated.parent().unwrap()).unwrap();
std::fs::write(&translocated, b"").unwrap();
// The folder the program under test runs from stands in for the mount.
let mount = Path::new(env!("CARGO_BIN_EXE_wanderbooks"))
.parent()
.unwrap();
let out = home
.cmd("connect", &bin)
.env("APPIMAGE", &translocated)
.env("APPDIR", mount)
.args(["claude", "--book", "beta"])
.output()
.unwrap();
@@ -122,6 +129,7 @@ fn translocated_app_refuses_to_connect() {
let out = home
.cmd("connect", &bin)
.env("APPIMAGE", &ordinary)
.env("APPDIR", mount)
.args(["claude", "--book", "beta"])
.output()
.unwrap();
+50 -2
View File
@@ -150,7 +150,10 @@ impl Home {
/// The environment of the pretend AppImage, if this is one.
fn image_env(&self, c: &mut Command) {
if let Some(image) = &self.image {
c.env("APPIMAGE", image);
// As an AppImage's launcher sets them: the image, and the
// mount the program runs from.
c.env("APPIMAGE", image)
.env("APPDIR", self.exe.parent().unwrap());
}
}
@@ -728,6 +731,7 @@ fn update_under_writes_in(home: Home) -> (Home, Agent, Vec<String>) {
);
let key = release.dir.path().join("release.pub");
let image = home.image.clone();
let mount = home.exe.parent().unwrap().to_path_buf();
std::thread::spawn(move || {
let _keep = &release;
let mut c = Command::new(exe);
@@ -739,7 +743,7 @@ fn update_under_writes_in(home: Home) -> (Home, Agent, Vec<String>) {
.env("PATH", "/usr/bin:/bin")
.env("HOME", dir);
if let Some(image) = image {
c.env("APPIMAGE", image);
c.env("APPIMAGE", image).env("APPDIR", mount);
}
c.output().unwrap()
})
@@ -832,6 +836,50 @@ fn a_replaced_appimage_restarts_the_books() {
assert!(agent.add_account("4001").is_ok(), "the session carries on");
}
/// start.rs INVARIANT 6: `APPIMAGE` and `APPDIR` are inherited by
/// everything an AppImage app starts (a terminal, an editor, an agent's
/// app). When this program is not inside that image, the image is someone
/// else's file: an update replaces this program and never that file.
#[test]
fn an_update_never_replaces_another_apps_appimage() {
let home = Home::new("foreign");
let old = home.program();
let other = home.dir.path().join("Other.AppImage");
std::fs::write(&other, b"another app's image").unwrap();
let mount = home.dir.path().join("other-mount");
std::fs::create_dir(&mount).unwrap();
let release = Release::new();
let next = newer(&home, "9.0.0");
release.publish("9.0.0", &next, |_| {});
let mut c = Command::new(&home.exe);
c.args(["update", "--root"])
.arg(home.root())
.arg("--feed")
.arg(format!(
"file://{}",
release.dir.path().join("manifest.json").display()
))
.arg("--trusted-key")
.arg(release.dir.path().join("release.pub"))
.env_clear()
.env("PATH", "/usr/bin:/bin")
.env("HOME", home.dir.path())
.env("APPIMAGE", &other)
.env("APPDIR", &mount)
.stdin(Stdio::null());
let out = c.output().unwrap();
assert!(out.status.success(), "{}", text(&out));
assert!(
std::fs::read(&other).unwrap() == b"another app's image",
"the other app's image was written over"
);
assert_eq!(sha256(&home.program()), sha256(&next), "this program");
assert_eq!(
sha256(&std::fs::read(home.updates().join("previous/wanderbooks")).unwrap()),
sha256(&old)
);
}
#[test]
fn update_waits_for_a_running_call() {
// No call is cut off or made twice: each account written across the