diff --git a/apps/figcraft/src/launch_profile.rs b/apps/figcraft/src/launch_profile.rs index 908748dd3..cb768fc0d 100644 --- a/apps/figcraft/src/launch_profile.rs +++ b/apps/figcraft/src/launch_profile.rs @@ -17,7 +17,16 @@ pub fn current() -> &'static Profile { pub fn prepare(flavor: crate::build_flavor::BuildFlavor, args: impl IntoIterator) -> Result { let spec = crate::profile_root::spec(); - craft_nightly::prepare_launch(flavor, &spec, args, &StartupOptions::new()) + let args: Vec = args.into_iter().collect(); + let options = StartupOptions::new(); + // Refused before `prepare_launch` makes the root: a portable stable copy's folders stay untouched. + let parsed = craft_nightly::parse_startup(flavor, &spec, args.clone(), &options)?; + if let (false, Profile::NightlyV1(root)) = (parsed.is_version_query(), &parsed.profile) + && let Some(copy) = crate::profile_root::portable_copy_around(root) + { + return Err(ProfileError::ForbiddenRootOverlap { profile_root: root.clone(), forbidden_root: copy }); + } + craft_nightly::prepare_launch(flavor, &spec, args, &options) } #[cfg(test)] diff --git a/apps/figcraft/src/profile_root.rs b/apps/figcraft/src/profile_root.rs index 25e54daa4..146abd569 100644 --- a/apps/figcraft/src/profile_root.rs +++ b/apps/figcraft/src/profile_root.rs @@ -15,6 +15,20 @@ pub const FORBIDDEN_ENV_VARS: &[&str] = &["FIGCRAFT_CONFIG_DIR"]; /// file beside the executable keeps settings in `config/` beside it. pub const PORTABLE_MARKERS: &[&str] = &["portable"]; pub const FOLDER_ONLY_ENTRIES: &[&str] = &["Recovery", "Plugins", "Fonts", "logs"]; +/// The FigCraft executables a portable copy holds beside its marker. +const PORTABLE_EXES: &[&str] = &["figcraft.exe", "figcraft", "FigCraft.app"]; + +/// The portable FigCraft copy that profile root `root` would lie in: the folder itself or an +/// ancestor, as given or as its existing part resolves, holding the `portable` marker beside a +/// FigCraft executable. `craft-nightly` only looks beside the running Nightly, so a root pointed +/// into a stable portable copy (its settings folder) is caught here. A marker with no FigCraft +/// beside it is someone else's file and doesn't count. +pub fn portable_copy_around(root: &std::path::Path) -> Option { + let copy = + |dir: &std::path::Path| PORTABLE_MARKERS.iter().any(|m| dir.join(m).is_file()) && PORTABLE_EXES.iter().any(|exe| dir.join(exe).exists()); + let resolved = root.ancestors().find_map(|a| std::fs::canonicalize(a).ok()); + root.ancestors().chain(resolved.iter().flat_map(|r| r.ancestors())).find(|d| copy(d)).map(std::path::Path::to_path_buf) +} /// The Linux wrapper's record of an XDG base it overrode (`FIGCRAFT_STABLE_XDG_CONFIG_HOME`, ...). #[cfg(all(unix, not(target_os = "macos")))] @@ -241,6 +255,43 @@ mod tests { assert!(craft_nightly::current().is_none(), "a failed launch must not install any profile"); } + fn nightly_args(root: &std::path::Path) -> [OsString; 5] { + [ + OsString::from("--launch-profile"), + OsString::from(craft_nightly::NIGHTLY_V1_CONTRACT), + OsString::from("--profile-root"), + root.as_os_str().to_os_string(), + OsString::from("--"), + ] + } + + // A portable stable copy, and its settings folder, can't be a Nightly's profile: refused before + // anything is made there, and no profile is installed. + #[test] + fn a_portable_copy_and_its_config_folder_are_refused_before_any_probe() { + let tmp = TempDir::new("portable-copy"); + let copy = tmp.0.join("FigCraft Portable"); + std::fs::create_dir_all(copy.join("config")).unwrap(); + std::fs::write(copy.join("portable"), b"stable portable marker").unwrap(); + std::fs::write(copy.join("figcraft.exe"), b"stable portable copy, never run").unwrap(); + for root in [copy.join("config"), copy.join("config").join("nightly"), copy.clone()] { + let refused = crate::launch_profile::prepare(crate::build_flavor::BuildFlavor::NightlyV1, nightly_args(&root)); + assert!(matches!(&refused, Err(ProfileError::ForbiddenRootOverlap { .. })), "{root:?}: {refused:?}"); + } + assert!(!copy.join("config").join("nightly").exists(), "nothing is made inside the copy"); + assert!(craft_nightly::current().is_none(), "a refused launch installs no profile"); + } + + // A `portable` file with no FigCraft beside it is someone else's: it doesn't make a folder a copy. + #[test] + fn a_marker_that_no_figcraft_copy_sits_beside_is_someone_elses_file() { + let tmp = TempDir::new("foreign-marker"); + std::fs::write(tmp.0.join("portable"), b"another program's file").unwrap(); + assert_eq!(super::portable_copy_around(&tmp.0.join("nightly")), None); + std::fs::write(tmp.0.join("figcraft"), b"now a FigCraft copy").unwrap(); + assert_eq!(super::portable_copy_around(&tmp.0.join("nightly")), Some(tmp.0.clone())); + } + #[test] fn a_leftover_write_probe_does_not_block_a_writable_profile() { let tmp = TempDir::new("leftover-probe");