fix(macos): keep self-update off signed bundles and other targets
The release feed needs asset constants on every target, and swapping a Mach-O inside a signed .app would break Gatekeeper.
@@ -33,6 +33,11 @@ const EXECUTABLE_MAGIC: &[u8] = b"\x7fELF";
33
33
const ASSET_SUFFIX: &str = "-macos-universal";
34
34
#[cfg(target_os = "macos")]
35
35
const EXECUTABLE_MAGIC: &[u8] = b"\xca\xfe\xba\xbe";
36
/// Other targets still compile. The suffix matches no release asset, so the feed stays idle.
37
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
38
const ASSET_SUFFIX: &str = "-unsupported";
39
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
40
const EXECUTABLE_MAGIC: &[u8] = b"\0unsupported";
36
41
const PARTIAL_SUFFIX: &str = ".partial";
37
42
const DOWNLOAD_CHUNK: usize = 64 * 1024;
38
43
const SHA256_PREFIX: &str = "sha256:";
@@ -33,6 +33,11 @@ const EXECUTABLE_MAGIC: &[u8] = b"\x7fELF";33
const ASSET_SUFFIX: &str = "-macos-universal";33
const ASSET_SUFFIX: &str = "-macos-universal";34
#[cfg(target_os = "macos")]34
#[cfg(target_os = "macos")]35
const EXECUTABLE_MAGIC: &[u8] = b"\xca\xfe\xba\xbe";35
const EXECUTABLE_MAGIC: &[u8] = b"\xca\xfe\xba\xbe";36
/// Other targets still compile. The suffix matches no release asset, so the feed stays idle.37
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]38
const ASSET_SUFFIX: &str = "-unsupported";39
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]40
const EXECUTABLE_MAGIC: &[u8] = b"\0unsupported";36
const PARTIAL_SUFFIX: &str = ".partial";41
const PARTIAL_SUFFIX: &str = ".partial";37
const DOWNLOAD_CHUNK: usize = 64 * 1024;42
const DOWNLOAD_CHUNK: usize = 64 * 1024;38
const SHA256_PREFIX: &str = "sha256:";43
const SHA256_PREFIX: &str = "sha256:";@@ -266,7 +266,8 @@ mod platform {
266
266
267
267
/// Whether this build may replace itself from GitHub releases. On Linux only the copy
268
268
/// installed in `~/.local/share/rusty-teams` updates; a binary run from a
269
/// build folder or a package location is left alone.
269
/// build folder or a package location is left alone. On macOS only an unsigned `.app`
270
/// may swap its Mach-O; a signed or notarized bundle would fail Gatekeeper after that.
270
271
pub fn self_update_allowed() -> bool {
271
272
#[cfg(target_os = "linux")]
272
273
{
@@ -278,9 +279,10 @@ pub fn self_update_allowed() -> bool {
278
279
#[cfg(target_os = "macos")]
279
280
{
280
281
return std::env::current_exe().is_ok_and(|executable| {
281
executable
282
.components()
283
.any(|part| part.as_os_str().to_string_lossy().ends_with(".app"))
282
let Some(bundle) = app_bundle(&executable) else {
283
return false;
284
};
285
macos_self_update_ok(&executable, bundle_is_signed(bundle))
284
286
});
285
287
}
286
288
#[cfg(windows)]
@@ -293,6 +295,33 @@ pub fn self_update_allowed() -> bool {
293
295
}
294
296
}
295
297
298
#[cfg(any(test, target_os = "macos"))]
299
fn app_bundle(executable: &Path) -> Option<&Path> {
300
executable.ancestors().find(|path| {
301
path.file_name()
302
.and_then(|name| name.to_str())
303
.is_some_and(|name| name.ends_with(".app"))
304
})
305
}
306
307
/// `signed` is the `codesign` result for the bundle. A signed Mach-O must stay put.
308
#[cfg(any(test, target_os = "macos"))]
309
fn macos_self_update_ok(executable: &Path, signed: bool) -> bool {
310
app_bundle(executable).is_some() && !signed
311
}
312
313
#[cfg(target_os = "macos")]
314
fn bundle_is_signed(bundle: &Path) -> bool {
315
match Command::new("/usr/bin/codesign")
316
.args(["--verify", "--strict"])
317
.arg(bundle)
318
.output()
319
{
320
Ok(output) => output.status.success(),
321
Err(_) => true,
322
}
323
}
324
296
325
/// Reading the new exe once lets the virus scanner finish before the restart, which otherwise blocks process start for seconds.
297
326
pub fn prescan(update_exe: &Path) {
298
327
if let Ok(mut file) = std::fs::File::open(update_exe) {
@@ -361,6 +390,15 @@ mod tests {
361
390
assert!(!update_is_ready("a-1", Some(" \n"), true));
362
391
}
363
392
393
#[test]
394
fn macos_self_update_stays_off_for_a_signed_bundle() {
395
let executable = Path::new("/Applications/RustyTeams.app/Contents/MacOS/teams");
396
assert!(macos_self_update_ok(executable, false));
397
assert!(!macos_self_update_ok(executable, true));
398
assert!(!macos_self_update_ok(Path::new("/tmp/teams"), false));
399
assert!(app_bundle(executable).is_some());
400
}
401
364
402
#[test]
365
403
fn plan_lists_the_operations_in_order() {
366
404
let paths = UpdatePaths::beside(&Path::new("/app").join(EXE_NAME));
@@ -266,7 +266,8 @@ mod platform {266
266
267
/// Whether this build may replace itself from GitHub releases. On Linux only the copy267
/// Whether this build may replace itself from GitHub releases. On Linux only the copy268
/// installed in `~/.local/share/rusty-teams` updates; a binary run from a268
/// installed in `~/.local/share/rusty-teams` updates; a binary run from a269
/// build folder or a package location is left alone.269
/// build folder or a package location is left alone. On macOS only an unsigned `.app`270
/// may swap its Mach-O; a signed or notarized bundle would fail Gatekeeper after that.270
pub fn self_update_allowed() -> bool {271
pub fn self_update_allowed() -> bool {271
#[cfg(target_os = "linux")]272
#[cfg(target_os = "linux")]272
{273
{@@ -278,9 +279,10 @@ pub fn self_update_allowed() -> bool {278
#[cfg(target_os = "macos")]279
#[cfg(target_os = "macos")]279
{280
{280
return std::env::current_exe().is_ok_and(|executable| {281
return std::env::current_exe().is_ok_and(|executable| {281
executable282
let Some(bundle) = app_bundle(&executable) else {282
.components()283
return false;283
.any(|part| part.as_os_str().to_string_lossy().ends_with(".app"))284
};285
macos_self_update_ok(&executable, bundle_is_signed(bundle))284
});286
});285
}287
}286
#[cfg(windows)]288
#[cfg(windows)]@@ -293,6 +295,33 @@ pub fn self_update_allowed() -> bool {293
}295
}294
}296
}295
297
298
#[cfg(any(test, target_os = "macos"))]299
fn app_bundle(executable: &Path) -> Option<&Path> {300
executable.ancestors().find(|path| {301
path.file_name()302
.and_then(|name| name.to_str())303
.is_some_and(|name| name.ends_with(".app"))304
})305
}306
307
/// `signed` is the `codesign` result for the bundle. A signed Mach-O must stay put.308
#[cfg(any(test, target_os = "macos"))]309
fn macos_self_update_ok(executable: &Path, signed: bool) -> bool {310
app_bundle(executable).is_some() && !signed311
}312
313
#[cfg(target_os = "macos")]314
fn bundle_is_signed(bundle: &Path) -> bool {315
match Command::new("/usr/bin/codesign")316
.args(["--verify", "--strict"])317
.arg(bundle)318
.output()319
{320
Ok(output) => output.status.success(),321
Err(_) => true,322
}323
}324
296
/// Reading the new exe once lets the virus scanner finish before the restart, which otherwise blocks process start for seconds.325
/// Reading the new exe once lets the virus scanner finish before the restart, which otherwise blocks process start for seconds.297
pub fn prescan(update_exe: &Path) {326
pub fn prescan(update_exe: &Path) {298
if let Ok(mut file) = std::fs::File::open(update_exe) {327
if let Ok(mut file) = std::fs::File::open(update_exe) {@@ -361,6 +390,15 @@ mod tests {361
assert!(!update_is_ready("a-1", Some(" \n"), true));390
assert!(!update_is_ready("a-1", Some(" \n"), true));362
}391
}363
392
393
#[test]394
fn macos_self_update_stays_off_for_a_signed_bundle() {395
let executable = Path::new("/Applications/RustyTeams.app/Contents/MacOS/teams");396
assert!(macos_self_update_ok(executable, false));397
assert!(!macos_self_update_ok(executable, true));398
assert!(!macos_self_update_ok(Path::new("/tmp/teams"), false));399
assert!(app_bundle(executable).is_some());400
}401
364
#[test]402
#[test]365
fn plan_lists_the_operations_in_order() {403
fn plan_lists_the_operations_in_order() {366
let paths = UpdatePaths::beside(&Path::new("/app").join(EXE_NAME));404
let paths = UpdatePaths::beside(&Path::new("/app").join(EXE_NAME));