Skip to content

Commit

Permalink
chore: Enhance security in base_dir replacement to target specific co…
Browse files Browse the repository at this point in the history
…nfiguration lines only

Signed-off-by: HouXiaoxuan <[email protected]>
  • Loading branch information
Hou-Xiaoxuan committed Aug 17, 2024
1 parent ec35f7c commit 3c05829
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<String>>()
.join("\n");

let config_path = base_dir.join("config.toml");
std::fs::write(&config_path, default_config).unwrap();
Expand Down

0 comments on commit 3c05829

Please sign in to comment.