From ee69dc0e1ddde264330d61731483a50ffe27684b Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Benicio Neves Date: Fri, 27 Nov 2026 22:42:44 +0300 Subject: [PATCH] Config: refuse a data_dir equal to or inside the cache_dir (#38, #42) The cache may be deleted at any time; saves and settings kept inside it would go with it. `is_within` compares resolved whole components, so a sibling such as `cache-old` is still accepted, and a cache inside the data folder remains allowed. Ported from uiliannn/VItoriaBr 30d9ba1 (pull requests #38 and #42). (cherry picked from commit 0f52dbd892f2e9af7a84e0edc9bec635ec42dc32) --- crates/victory-core/src/config.rs | 42 ++++++++++++++++++++++++++++++- crates/victory-core/src/policy.rs | 7 ++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/crates/victory-core/src/config.rs b/crates/victory-core/src/config.rs index 6092554..9ba316c 100644 --- a/crates/victory-core/src/config.rs +++ b/crates/victory-core/src/config.rs @@ -9,7 +9,7 @@ use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; use serde::Deserialize; -use crate::policy::ensure_outside_repo; +use crate::policy::{ensure_outside_repo, is_within}; #[derive(Debug, Default, Clone)] pub struct Config { @@ -91,6 +91,14 @@ impl Config { let data_dir = env_path("VICTORY_DATA_DIR") .or(file.data_dir) .map_or_else(|| default_dir("data"), Ok)?; + if is_within(&data_dir, &cache_dir)? { + anyhow::bail!( + "data_dir ({}) must not be the cache_dir ({}) or inside it: the cache may be \ + deleted at any time", + data_dir.display(), + cache_dir.display() + ); + } Ok(Config { game_dir: env_path("VICTORY_GAME_DIR").or(file.game_dir), cache_dir, @@ -244,4 +252,36 @@ mod tests { }; assert!(cfg.data_subdir("x").is_err()); } + + fn resolve_dirs(cache: &Path, data: &Path) -> Result { + let file = FilePaths { + cache_dir: Some(cache.to_path_buf()), + data_dir: Some(data.to_path_buf()), + ..Default::default() + }; + Config::resolve(file, None, |_| None) + } + + #[test] + fn data_dir_equal_to_cache_dir_is_refused() { + let tmp = tempfile::tempdir().unwrap(); + assert!(resolve_dirs(tmp.path(), tmp.path()).is_err()); + } + + #[test] + fn data_dir_inside_cache_dir_is_refused() { + let tmp = tempfile::tempdir().unwrap(); + let cache = tmp.path().join("cache"); + assert!(resolve_dirs(&cache, &cache.join("data")).is_err()); + assert!(resolve_dirs(&cache, &cache.join("a/../data")).is_err()); + } + + #[test] + fn sibling_with_similar_prefix_and_cache_inside_data_are_allowed() { + let tmp = tempfile::tempdir().unwrap(); + let cache = tmp.path().join("cache"); + assert!(resolve_dirs(&cache, &tmp.path().join("cache-old")).is_ok()); + let data = tmp.path().join("data"); + assert!(resolve_dirs(&data.join("cache"), &data).is_ok()); + } } diff --git a/crates/victory-core/src/policy.rs b/crates/victory-core/src/policy.rs index d49acc4..5eb388d 100644 --- a/crates/victory-core/src/policy.rs +++ b/crates/victory-core/src/policy.rs @@ -62,6 +62,13 @@ pub fn ensure_outside_repo(path: &Path) -> Result<(), PolicyError> { Ok(()) } +/// True if `path` is `root` or lies below it, after resolving symlinks and +/// `..` (neither path has to exist yet). Compares whole components, so +/// `cache-old` is not inside `cache`. +pub fn is_within(path: &Path, root: &Path) -> Result { + Ok(resolve(path)?.starts_with(resolve(root)?)) +} + /// Canonicalizes the longest existing prefix of `path` and re-appends the /// rest, so that paths which do not exist yet can still be compared. fn resolve(path: &Path) -> Result {