Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat: safe define remove dir list during the factory reset
Vanalitefde34c43bb2e
diff --git a/src-tauri/src/core/app/constants.rs b/src-tauri/src/core/app/constants.rsindex a1b86fa33..920fe7597 100644--- a/src-tauri/src/core/app/constants.rs+++ b/src-tauri/src/core/app/constants.rs@@ -1,2 +1,15 @@// App Configuration Constantspub const CONFIGURATION_FILE_NAME: &str = "settings.json";++pub const JAN_DATA_SUBDIRS: &[&str] = &[+ "threads",+ "extensions",+ "logs",+ "llamacpp",+ "mlx",+ "openclaw",+ ".npx",+ ".uvx",+];++pub const JAN_DATA_FILES: &[&str] = &["mcp_config.json"];diff --git a/src-tauri/src/core/app/mod.rs b/src-tauri/src/core/app/mod.rsindex e0b10b55a..20ec7c001 100644--- a/src-tauri/src/core/app/mod.rs+++ b/src-tauri/src/core/app/mod.rs@@ -1,4 +1,4 @@pub mod commands;-mod constants;+pub mod constants;pub mod helpers;pub mod models;diff --git a/src-tauri/src/core/filesystem/commands.rs b/src-tauri/src/core/filesystem/commands.rsindex bc441f083..bfa03275f 100644--- a/src-tauri/src/core/filesystem/commands.rs+++ b/src-tauri/src/core/filesystem/commands.rs@@ -12,7 +12,19 @@ pub fn rm<R: Runtime>(app_handle: tauri::AppHandle<R>, args: Vec<String>) -> Resreturn Err("rm error: Invalid argument".to_string());}+ let jan_data_folder =+ crate::core::app::commands::get_jan_data_folder_path(app_handle.clone());let path = resolve_path(app_handle, &args[0]);+ let canonical_data = jan_data_folder.canonicalize().unwrap_or(jan_data_folder);+ let canonical_path = path.canonicalize().unwrap_or_else(|_| path.clone());+ if !canonical_path.starts_with(&canonical_data) {+ return Err(format!(+ "rm error: path {} is not under jan data folder {}",+ canonical_path.display(),+ canonical_data.display()+ ));+ }+if path.is_file() {fs::remove_file(&path).map_err(|e| e.to_string())?;} else if path.is_dir() {diff --git a/src-tauri/src/core/system/commands.rs b/src-tauri/src/core/system/commands.rsindex 48587c817..5085a5910 100644--- a/src-tauri/src/core/system/commands.rs+++ b/src-tauri/src/core/system/commands.rs@@ -6,10 +6,35 @@ use tauri_plugin_llamacpp::cleanup_llama_processes;use crate::core::app::commands::{default_data_folder_path, get_jan_data_folder_path, update_app_configuration,};+use crate::core::app::constants::{JAN_DATA_FILES, JAN_DATA_SUBDIRS};use crate::core::app::models::AppConfiguration;use crate::core::mcp::helpers::{stop_mcp_servers_with_context, ShutdownContext};use crate::core::state::AppState;+fn is_safe_to_delete(path: &std::path::Path) -> bool {+ let count = path.components().count();+ count >= 3+}++fn remove_jan_data_contents(data_folder: &std::path::Path) {+ for subdir in JAN_DATA_SUBDIRS {+ let path = data_folder.join(subdir);+ if path.is_dir() {+ if let Err(e) = fs::remove_dir_all(&path) {+ log::warn!("Failed to remove {}: {e}", path.display());+ }+ }+ }+ for file in JAN_DATA_FILES {+ let path = data_folder.join(file);+ if path.is_file() {+ if let Err(e) = fs::remove_file(&path) {+ log::warn!("Failed to remove {}: {e}", path.display());+ }+ }+ }+}+/// Detect the user's default shell and return the appropriate env file path./// Returns (shell_name, env_file_path).fn detect_shell_env_file(home_dir: &str, is_macos: bool) -> (&'static str, String) {@@ -86,15 +111,16 @@ pub fn factory_reset<R: Runtime>(app_handle: tauri::AppHandle<R>, state: State<'let _ = cleanup_llama_processes(app_handle.clone()).await;if data_folder.exists() {- if let Err(e) = fs::remove_dir_all(&data_folder) {- log::error!("Failed to remove data folder: {e}");+ if !is_safe_to_delete(&data_folder) {+ log::error!(+ "Refusing factory reset: path is too close to filesystem root: {}",+ data_folder.display()+ );return;}+ remove_jan_data_contents(&data_folder);}- // Recreate the data folder- let _ = fs::create_dir_all(&data_folder).map_err(|e| e.to_string());-// Reset the configurationlet mut default_config = AppConfiguration::default();default_config.data_folder = default_data_folder_path(app_handle.clone());