From 3c05829edaa8ae45e551486ccdd014b5dc9fe54c Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Sat, 17 Aug 2024 14:28:43 +0800 Subject: [PATCH] chore: Enhance security in base_dir replacement to target specific configuration lines only Signed-off-by: HouXiaoxuan --- common/src/config.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/common/src/config.rs b/common/src/config.rs index 7663ed716..33e7b0ea2 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -44,10 +44,20 @@ impl Default for Config { std::env::var("MEGA_BASE_DIR").unwrap_or_else(|_| "/tmp/.mega".to_string()), ); std::fs::create_dir_all(&base_dir).unwrap(); - + // use mega/config.toml because mega use sqlite as default db let default_config = include_str!("../../mega/config.toml"); - let default_config = default_config.replace("/tmp/.mega", base_dir.to_str().unwrap()); + let default_config = default_config + .lines() + .map(|line| { + if line.starts_with("base_dir ") { + format!("base_dir = \"{}\"", base_dir.to_str().unwrap()) + } else { + line.to_string() + } + }) + .collect::>() + .join("\n"); let config_path = base_dir.join("config.toml"); std::fs::write(&config_path, default_config).unwrap();