Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,19 @@ async fn refresh(client: &reqwest::Client, address: &str, path: PathBuf) -> anyh

fn install_path(executable: &Path, directory: &Path) -> anyhow::Result<PathBuf> {
let expected = fs::canonicalize(directory)?.join("proofline");
if executable != expected {
// Checked first: on Linux `current_exe` already names the link's target,
// so the comparison below would hide why the update is refused.
if fs::symlink_metadata(&expected).is_ok_and(|metadata| metadata.file_type().is_symlink()) {
bail!("the installed binary is a symbolic link; update it through its package manager");
}
// `current_exe` is not canonical on every platform (macOS reports the path
// the binary was started through), while `expected` is.
if fs::canonicalize(executable).ok().as_deref() != Some(expected.as_path()) {
bail!(
"this binary is outside the installer directory {}; update it through its package manager or rebuild it from source",
directory.display()
);
}
if fs::symlink_metadata(&expected)?.file_type().is_symlink() {
bail!("the installed binary is a symbolic link; update it through its package manager");
}
Ok(expected)
}

Expand Down
44 changes: 44 additions & 0 deletions src/update/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,47 @@ async fn detached_discovery_is_serialized_and_notices_arrive_on_the_next_invocat
server.abort();
assert!(server.await.unwrap_err().is_cancelled());
}

/// `current_exe` is not canonical on every platform (macOS reports the path
/// the binary was started through), so an installer directory reached through
/// a symbolic link is still the installer directory.
#[cfg(unix)]
#[test]
fn installer_directory_reached_through_a_symlink() {
let root = tempfile::tempdir().unwrap();
let real = root.path().join("real-bin");
fs::create_dir_all(&real).unwrap();
fs::write(real.join("proofline"), b"binary").unwrap();
let linked = root.path().join("linked-bin");
std::os::unix::fs::symlink(&real, &linked).unwrap();

let expected = fs::canonicalize(real.join("proofline")).unwrap();
assert_eq!(
install_path(&linked.join("proofline"), &linked).unwrap(),
expected
);
assert_eq!(install_path(&expected, &linked).unwrap(), expected);
}

/// A package manager's symbolic link in the installer directory is reported
/// as such, whichever path `current_exe` gives for it: the link on macOS, its
/// target on Linux.
#[cfg(unix)]
#[test]
fn installed_symlink_is_reported_as_a_symlink() {
let root = tempfile::tempdir().unwrap();
let store = root.path().join("store");
fs::create_dir_all(&store).unwrap();
fs::write(store.join("proofline"), b"binary").unwrap();
let bin = root.path().join("bin");
fs::create_dir_all(&bin).unwrap();
std::os::unix::fs::symlink(store.join("proofline"), bin.join("proofline")).unwrap();

for executable in [
bin.join("proofline"),
fs::canonicalize(store.join("proofline")).unwrap(),
] {
let error = install_path(&executable, &bin).unwrap_err().to_string();
assert!(error.contains("symbolic link"), "{executable:?}: {error}");
}
}