From 89a64b5526defb880196579ccbc69da3b7d5f546 Mon Sep 17 00:00:00 2001 From: HighestFidelity <157998857+HighestFidelity@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:04:39 +0000 Subject: [PATCH] fix(native-hid): release builds never load the HID helper from the build machine's checkout path When the bundled apply.mjs is missing, release binaries fell back to the build machine's checkout path (a CI runner's D:\a\... on Windows), which another account could create and fill. That dev fallback is now used only by debug builds and by binaries run from inside that checkout. The bundled helper paths are unchanged. Co-Authored-By: Claude Opus 5.5 --- src/drivers/native_hid.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/drivers/native_hid.rs b/src/drivers/native_hid.rs index d94417a..2715b34 100644 --- a/src/drivers/native_hid.rs +++ b/src/drivers/native_hid.rs @@ -289,13 +289,34 @@ fn locate_apply_script() -> Result { )); packaged .filter(|path| path.is_file()) - .or_else(|| development.is_file().then_some(development)) + .or_else(|| { + (development_checkout_allowed() && development.is_file()).then_some(development) + }) .context( "could not find native-hid/src/apply.mjs next to the Bridge executable \ or in the source checkout", ) } +/// Whether the checkout Bridge was built from may supply the helper scripts. +/// A release binary carries the build machine's checkout path (a CI runner's +/// `D:\a\...` on Windows), which another account could create and fill on the +/// user's machine; so a release build only uses it when it runs from inside +/// that checkout (`cargo build --release` then `target/release/...`). +fn development_checkout_allowed() -> bool { + cfg!(debug_assertions) || runs_inside(Path::new(env!("CARGO_MANIFEST_DIR"))) +} + +fn runs_inside(checkout: &Path) -> bool { + let (Ok(exe), Ok(checkout)) = ( + std::env::current_exe().and_then(|exe| exe.canonicalize()), + checkout.canonicalize(), + ) else { + return false; + }; + exe.starts_with(checkout) +} + /// Finds the Node.js runtime a packaged Bridge release bundles at /// `native-hid/node/` next to the executable (see `.github/workflows/release.yml`). /// `None` means no bundled runtime was found, so the caller should fall back @@ -537,4 +558,14 @@ mod tests { assert_eq!(status.dpi, Some(400)); assert_eq!(status.switch_mode, None); } + + #[test] + fn a_release_build_uses_the_checkout_only_from_inside_it() { + let exe = std::env::current_exe().unwrap(); + assert!(runs_inside(exe.parent().unwrap())); + assert!(!runs_inside(Path::new( + "/no/such/checkout/openmouse-bridge" + ))); + assert!(cfg!(not(debug_assertions)) || development_checkout_allowed()); + } }