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)
This commit is contained in:
Luiz Gustavo Benicio Neves authored and trustno1-coder committed 2026-11-27 17:29:36 -08:00
1 parent aa7dd70250
commit ee69dc0e1d
2 files changed
+48 -1

No files matched your search

+41 -1
View File
@@ -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<Config> {
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());
}
}
+7
View File
@@ -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<bool, PolicyError> {
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<PathBuf, PolicyError> {