Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(filesystem): prevent path traversal in mv, mkdir, and write_file_sync

greatjourney58928381c2fbaa9
diff --git a/src-tauri/src/core/filesystem/commands.rs b/src-tauri/src/core/filesystem/commands.rs
index 59acccea0..2f67d4494 100644
--- a/src-tauri/src/core/filesystem/commands.rs
+++ b/src-tauri/src/core/filesystem/commands.rs
@@ -39,7 +39,8 @@ pub fn mkdir<R: Runtime>(app_handle: tauri::AppHandle<R>, args: Vec<String>) ->
return Err("mkdir error: Invalid argument".to_string());
}
- let path = resolve_path(app_handle, &args[0]);
+ let (_jan_data_folder, path) = resolve_app_path_within_jan_data_folder(app_handle, &args[0])
+ .map_err(|_| format!("mkdir error: path {} is not under jan data folder", args[0]))?;
fs::create_dir_all(&path).map_err(|e| e.to_string())
}
@@ -49,8 +50,15 @@ pub fn mv<R: Runtime>(app_handle: tauri::AppHandle<R>, args: Vec<String>) -> Res
return Err("mv error: Invalid argument - source and destination required".to_string());
}
- let source = resolve_path(app_handle.clone(), &args[0]);
- let destination = resolve_path(app_handle, &args[1]);
+ let (_jan_data_folder, source) =
+ resolve_app_path_within_jan_data_folder(app_handle.clone(), &args[0]).map_err(|_| {
+ format!("mv error: source {} is not under jan data folder", args[0])
+ })?;
+ let (_jan_data_folder, destination) = resolve_app_path_within_jan_data_folder(
+ app_handle,
+ &args[1],
+ )
+ .map_err(|_| format!("mv error: destination {} is not under jan data folder", args[1]))?;
if !source.exists() {
return Err("mv error: Source path does not exist".to_string());
@@ -125,7 +133,13 @@ pub fn write_file_sync<R: Runtime>(
return Err("write_file_sync error: Invalid argument".to_string());
}
- let path = resolve_path(app_handle, &args[0]);
+ let (_jan_data_folder, path) = resolve_app_path_within_jan_data_folder(app_handle, &args[0])
+ .map_err(|_| {
+ format!(
+ "write_file_sync error: path {} is not under jan data folder",
+ args[0]
+ )
+ })?;
let content = &args[1];
fs::write(&path, content).map_err(|e| e.to_string())
}
diff --git a/src-tauri/src/core/filesystem/tests.rs b/src-tauri/src/core/filesystem/tests.rs
index 0b48fa95f..f02e8cb3e 100644
--- a/src-tauri/src/core/filesystem/tests.rs
+++ b/src-tauri/src/core/filesystem/tests.rs
@@ -33,6 +33,79 @@ fn test_mkdir() {
let _ = fs::remove_dir_all(get_jan_data_folder_path(app.handle().clone()).join(path));
}
+#[test]
+fn test_mkdir_rejects_path_outside_jan_data_folder() {
+ let app = mock_app();
+ let outside = unique_test_dir("mkdir-outside");
+ let args = vec![outside.to_string_lossy().to_string()];
+ let result = mkdir(app.handle().clone(), args);
+ assert!(result.is_err());
+ assert!(!outside.exists());
+}
+
+#[test]
+fn test_mv_rejects_source_outside_jan_data_folder() {
+ let app = mock_app();
+ let outside_dir = unique_test_dir("mv-outside-src");
+ fs::create_dir_all(&outside_dir).unwrap();
+ let outside_file = outside_dir.join("source.txt");
+ File::create(&outside_file).unwrap();
+
+ let destination = get_jan_data_folder_path(app.handle().clone()).join("moved.txt");
+ let args = vec![
+ outside_file.to_string_lossy().to_string(),
+ destination.to_string_lossy().to_string(),
+ ];
+ let result = mv(app.handle().clone(), args);
+ assert!(result.is_err());
+ assert!(outside_file.exists());
+ assert!(!destination.exists());
+
+ let _ = fs::remove_dir_all(&outside_dir);
+}
+
+#[test]
+fn test_mv_rejects_destination_outside_jan_data_folder() {
+ let app = mock_app();
+ let source = get_jan_data_folder_path(app.handle().clone()).join("mv_src.txt");
+ fs::create_dir_all(source.parent().unwrap()).unwrap();
+ File::create(&source).unwrap();
+
+ let outside_dir = unique_test_dir("mv-outside-dest");
+ fs::create_dir_all(&outside_dir).unwrap();
+ let outside_dest = outside_dir.join("moved.txt");
+
+ let args = vec![
+ source.to_string_lossy().to_string(),
+ outside_dest.to_string_lossy().to_string(),
+ ];
+ let result = mv(app.handle().clone(), args);
+ assert!(result.is_err());
+ assert!(source.exists());
+ assert!(!outside_dest.exists());
+
+ let _ = fs::remove_file(&source);
+ let _ = fs::remove_dir_all(&outside_dir);
+}
+
+#[test]
+fn test_write_file_sync_rejects_path_outside_jan_data_folder() {
+ let app = mock_app();
+ let outside = unique_test_dir("write-outside");
+ fs::create_dir_all(&outside).unwrap();
+ let outside_file = outside.join("forbidden.txt");
+
+ let args = vec![
+ outside_file.to_string_lossy().to_string(),
+ "malicious content".to_string(),
+ ];
+ let result = write_file_sync(app.handle().clone(), args);
+ assert!(result.is_err());
+ assert!(!outside_file.exists());
+
+ let _ = fs::remove_dir_all(&outside);
+}
+
#[test]
fn test_join_path() {
let app = mock_app();