fix(instance): reject unsafe instance rename targets

rename() now runs validate_name on the new name so paths like ../x or
.x cannot move instances out of (or hide them inside) the instances
root; failures surface as an error toast in the TUI.
This commit is contained in:
objz committed 2026-08-24 19:14:20 +02:00
1 parent 40db30ce5a
commit 51bb896f91
2 files changed
+21

No files matched your search

+7
View File
@@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: 2026 Constantin Bauer
// SPDX-License-Identifier: GPL-3.0-only
// CRUD for instances: create, delete, rename, load, save. // CRUD for instances: create, delete, rename, load, save.
// creation is the heavy one since it downloads the game, assets, and libraries. // creation is the heavy one since it downloads the game, assets, and libraries.
@@ -415,6 +418,10 @@ impl InstanceManager {
tracing::debug!("Ignoring no-op instance rename '{}'", old_name); tracing::debug!("Ignoring no-op instance rename '{}'", old_name);
return Ok(()); return Ok(());
} }
// same path-traversal guards as create: without this, "../x" or ".x"
// would move the instance directory out of (or hide it inside) the
// instances root.
validate_name(new_name)?;
let old_dir = self.instances_dir.join(old_name); let old_dir = self.instances_dir.join(old_name);
let new_dir = self.instances_dir.join(new_name); let new_dir = self.instances_dir.join(new_name);
if !old_dir.exists() { if !old_dir.exists() {
+14
View File
@@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: 2026 Constantin Bauer
// SPDX-License-Identifier: GPL-3.0-only
use super::*; use super::*;
use crate::instance::models::ModLoader; use crate::instance::models::ModLoader;
use tempfile::TempDir; use tempfile::TempDir;
@@ -129,6 +132,17 @@ fn rename_empty_target_rejects() {
assert!(matches!(err, InstanceError::InvalidName(_))); assert!(matches!(err, InstanceError::InvalidName(_)));
} }
#[test]
fn rename_traversal_target_rejects() {
let (manager, tmp) = test_manager();
std::fs::create_dir_all(tmp.path().join("orig")).unwrap();
manager.save(&dummy_config("orig")).expect("save");
let err = manager.rename("orig", "../escape").unwrap_err();
assert!(matches!(err, InstanceError::InvalidName(_)));
assert!(!tmp.path().parent().unwrap().join("escape").exists());
assert!(tmp.path().join("orig").exists(), "source must be untouched");
}
#[test] #[test]
fn rename_missing_source_errors() { fn rename_missing_source_errors() {
let (manager, _tmp) = test_manager(); let (manager, _tmp) = test_manager();