mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
fix(nightly): require platform storage boundary resolution (#4297)
This commit is contained in:
1 parent
f8f6ec9a2f
commit
f628499900
3 files changed
+74
-20
No files matched your search
@@ -17,6 +17,9 @@ Shared nightly-v1 startup and profile-root validation, for use by desktop apps a
|
||||
overwrite a populated destination or silently abandon a populated source. Direct launches and
|
||||
launcher launches must agree on the selected root after migration; the Flatpak sandbox uses
|
||||
its own XDG data scope, not a host filesystem path passed blindly into the sandbox.
|
||||
- Nightly startup must establish stable storage boundaries before creating its root. The validator
|
||||
requires `HOME` on Unix and `APPDATA`/`LOCALAPPDATA` on Windows, and refuses invalid configured
|
||||
platform directories. Resolving a default nightly path is not a substitute for this validation.
|
||||
- Validate each app's actual stable paths and environment overrides, not just its package ID.
|
||||
Test existing stable and nightly settings, direct launch and launcher launch, overrides,
|
||||
literal arguments, and platform-specific storage behavior before rollout.
|
||||
|
||||
@@ -9,6 +9,10 @@ pub enum ProfileError {
|
||||
InvalidAppId(String),
|
||||
/// Could not determine the user's home or local application data directory.
|
||||
MissingHomeDirectory,
|
||||
/// A required OS directory was unavailable when establishing stable storage boundaries.
|
||||
MissingStableBoundary(&'static str),
|
||||
/// A configured OS directory was not absolute or contained parent traversal.
|
||||
InvalidStableBoundary(&'static str),
|
||||
/// Nightly launch flags (`--launch-profile` or `--profile-root`) were passed to a stable build.
|
||||
NightlyFlagsInStableBuild,
|
||||
/// The app's profile-root override environment variable was set in a stable build.
|
||||
@@ -70,6 +74,8 @@ impl fmt::Display for ProfileError {
|
||||
match self {
|
||||
Self::InvalidAppId(id) => write!(f, "invalid Craft app id for nightly profile: {id:?}"),
|
||||
Self::MissingHomeDirectory => write!(f, "unable to resolve user home or local data directory for nightly profile"),
|
||||
Self::MissingStableBoundary(var) => write!(f, "unable to establish the stable `{var}` directory boundary"),
|
||||
Self::InvalidStableBoundary(var) => write!(f, "stable `{var}` must be an absolute path with no parent traversal"),
|
||||
Self::NightlyFlagsInStableBuild => write!(f, "nightly launch flags are not permitted in a stable build"),
|
||||
Self::ProfileRootEnvInStableBuild(var) => write!(f, "profile root environment variable `{var}` is not permitted in a stable build"),
|
||||
Self::ConflictingProfileOverrides(var) => {
|
||||
|
||||
@@ -6,7 +6,6 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::env::{Env, SystemEnv};
|
||||
use crate::error::ProfileError;
|
||||
use crate::protocol::non_empty_abs_env;
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
use crate::protocol::nightly_flatpak_id;
|
||||
@@ -59,7 +58,7 @@ pub(crate) fn validate_and_prepare_root_with(
|
||||
check_portable_markers(spec, exe_path)?;
|
||||
check_no_symlink_components(root)?;
|
||||
|
||||
let forbidden_roots = collect_forbidden_roots(spec, env, exe_path);
|
||||
let forbidden_roots = collect_forbidden_roots(spec, env, exe_path)?;
|
||||
check_forbidden_overlaps(root, &forbidden_roots)?;
|
||||
|
||||
let canonical = create_and_harden_root(root)?;
|
||||
@@ -152,9 +151,9 @@ fn check_no_symlink_components(root: &Path) -> Result<(), ProfileError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn collect_forbidden_roots(spec: &AppProfileSpec, env: &dyn Env, exe_path: Option<&Path>) -> Vec<PathBuf> {
|
||||
fn collect_forbidden_roots(spec: &AppProfileSpec, env: &dyn Env, exe_path: Option<&Path>) -> Result<Vec<PathBuf>, ProfileError> {
|
||||
let mut forbidden = Vec::new();
|
||||
let bases = stable_os_base_dirs(env);
|
||||
let bases = stable_os_base_dirs(env)?;
|
||||
for base in &bases {
|
||||
for &name in spec.stable_dir_names {
|
||||
if !name.is_empty() {
|
||||
@@ -178,30 +177,46 @@ fn collect_forbidden_roots(spec: &AppProfileSpec, env: &dyn Env, exe_path: Optio
|
||||
}
|
||||
|
||||
forbidden.extend(spec.extra_forbidden_roots.iter().cloned());
|
||||
forbidden
|
||||
Ok(forbidden)
|
||||
}
|
||||
|
||||
fn stable_os_base_dirs(env: &dyn Env) -> Vec<PathBuf> {
|
||||
/// Missing optional settings use the platform defaults, but an explicit nonempty setting
|
||||
/// must resolve to an absolute directory boundary rather than being silently dropped.
|
||||
#[cfg(any(unix, windows))]
|
||||
fn configured_boundary(env: &dyn Env, key: &'static str) -> Result<Option<PathBuf>, ProfileError> {
|
||||
let Some(value) = env.var_os(key).filter(|v| !v.is_empty()) else { return Ok(None) };
|
||||
let path = PathBuf::from(value);
|
||||
if !path.is_absolute() || path.components().any(|part| matches!(part, Component::ParentDir)) {
|
||||
return Err(ProfileError::InvalidStableBoundary(key));
|
||||
}
|
||||
Ok(Some(path))
|
||||
}
|
||||
|
||||
#[cfg(any(unix, windows))]
|
||||
fn required_boundary(env: &dyn Env, key: &'static str) -> Result<PathBuf, ProfileError> {
|
||||
configured_boundary(env, key)?.ok_or(ProfileError::MissingStableBoundary(key))
|
||||
}
|
||||
|
||||
fn stable_os_base_dirs(env: &dyn Env) -> Result<Vec<PathBuf>, ProfileError> {
|
||||
let mut bases = Vec::new();
|
||||
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
{
|
||||
for key in ["XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_STATE_HOME"] {
|
||||
if let Some(dir) = non_empty_abs_env(env, key) {
|
||||
if let Some(dir) = configured_boundary(env, key)? {
|
||||
bases.push(dir);
|
||||
}
|
||||
}
|
||||
if let Some(home) = non_empty_abs_env(env, "HOME") {
|
||||
bases.push(home.join(".config"));
|
||||
bases.push(home.join(".local").join("share"));
|
||||
bases.push(home.join(".cache"));
|
||||
bases.push(home.join(".local").join("state"));
|
||||
}
|
||||
let home = required_boundary(env, "HOME")?;
|
||||
bases.push(home.join(".config"));
|
||||
bases.push(home.join(".local").join("share"));
|
||||
bases.push(home.join(".cache"));
|
||||
bases.push(home.join(".local").join("state"));
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
if let Some(home) = non_empty_abs_env(env, "HOME") {
|
||||
let library = home.join("Library");
|
||||
{
|
||||
let library = required_boundary(env, "HOME")?.join("Library");
|
||||
bases.push(library.join("Application Support"));
|
||||
bases.push(library.join("Caches"));
|
||||
bases.push(library.join("Preferences"));
|
||||
@@ -210,11 +225,13 @@ fn stable_os_base_dirs(env: &dyn Env) -> Vec<PathBuf> {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
for key in ["APPDATA", "LOCALAPPDATA"] {
|
||||
if let Some(dir) = non_empty_abs_env(env, key) {
|
||||
bases.push(dir);
|
||||
}
|
||||
bases.push(required_boundary(env, key)?);
|
||||
}
|
||||
if let Some(profile) = non_empty_abs_env(env, "USERPROFILE") {
|
||||
// App specifications also use these configured roots for installed applications.
|
||||
for key in ["ProgramW6432", "ProgramFiles", "ProgramFiles(x86)"] {
|
||||
let _ = configured_boundary(env, key)?;
|
||||
}
|
||||
if let Some(profile) = configured_boundary(env, "USERPROFILE")? {
|
||||
let app_data = profile.join("AppData");
|
||||
bases.push(app_data.join("Roaming"));
|
||||
bases.push(app_data.join("Local"));
|
||||
@@ -224,7 +241,7 @@ fn stable_os_base_dirs(env: &dyn Env) -> Vec<PathBuf> {
|
||||
#[cfg(not(any(unix, target_os = "windows")))]
|
||||
let _ = env;
|
||||
|
||||
bases
|
||||
Ok(bases)
|
||||
}
|
||||
|
||||
fn check_forbidden_overlaps(candidate: &Path, forbidden_roots: &[PathBuf]) -> Result<(), ProfileError> {
|
||||
@@ -556,6 +573,8 @@ mod tests {
|
||||
use crate::env::TestEnv;
|
||||
use crate::error::ProfileError;
|
||||
use crate::validate::{AppProfileSpec, validate_and_prepare_root_with};
|
||||
#[cfg(any(unix, windows))]
|
||||
use crate::validate::{configured_boundary, required_boundary, stable_os_base_dirs};
|
||||
|
||||
static TEST_DIR_COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
@@ -622,6 +641,32 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(unix, windows))]
|
||||
#[test]
|
||||
fn stable_boundary_environment_lookup_distinguishes_required_and_optional() {
|
||||
let tmp = TempDir::new("stable-boundaries");
|
||||
let configured = tmp.path().join("base");
|
||||
let env = TestEnv::new().with_var("BASE", &configured);
|
||||
assert_eq!(required_boundary(&env, "BASE").expect("configured base"), configured);
|
||||
assert_eq!(configured_boundary(&env, "OPTIONAL").expect("unset optional base"), None);
|
||||
assert!(matches!(required_boundary(&env, "REQUIRED"), Err(ProfileError::MissingStableBoundary("REQUIRED"))));
|
||||
}
|
||||
|
||||
#[cfg(any(unix, windows))]
|
||||
#[test]
|
||||
fn platform_storage_boundary_defaults_are_available() {
|
||||
let tmp = TempDir::new("platform-defaults");
|
||||
let home = tmp.path().join("home");
|
||||
let env = test_env_with_home(&home);
|
||||
let dirs = stable_os_base_dirs(&env).expect("platform bases");
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
assert!(dirs.contains(&home.join(".local/share")));
|
||||
#[cfg(target_os = "macos")]
|
||||
assert!(dirs.contains(&home.join("Library/Application Support")));
|
||||
#[cfg(windows)]
|
||||
assert!(dirs.contains(&home.join("AppData/Local")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_profile_root_is_created_and_hardened() {
|
||||
let tmp = TempDir::new("valid");
|
||||
|
||||
Reference in new issue
Block a user