Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue "Folder content is not encrypted #36" and Fixed Critical Security Bug #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ dirs = "5.0.1"
field_count = "0.1.1"
zip = "0.6.6"
walkdir = "2.3.3"
rayon = "1.8.0"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pub mod input_manager;
pub mod structure_manager;
pub mod trash_item;
pub mod trash_manager;
pub mod secure_delete;
36 changes: 36 additions & 0 deletions src/secure_delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::fs::{self, File};
use std::io::{self, Write};
use rand::{Rng, thread_rng};
use rayon::prelude::*;

pub fn delete_file(file_path: &str) -> io::Result<()> {
let mut file = File::create(file_path)?;
let metadata = fs::metadata(file_path)?;
let file_size = metadata.len() as usize;
let mut rng = thread_rng();
let random_bytes: Vec<u8> = (0..file_size).map(|_| rng.gen()).collect();
file.write_all(&random_bytes)?;
file.sync_all()?;
fs::remove_file(file_path)?;
Ok(())
}

pub fn delete_folder(folder_path: &str) -> io::Result<()> {
let result: io::Result<()> = fs::read_dir(folder_path)?
.par_bridge()
.into_par_iter()
.try_for_each(|entry| {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
delete_file(path.to_str().unwrap())?;
} else {
delete_file(path.to_str().unwrap())?;
}
Ok(())
}
);
result?;
fs::remove_dir(folder_path)?;
Ok(())
}
6 changes: 3 additions & 3 deletions src/structure_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rusqlite::Connection;

use crate::{argument_errors::RmtArgumentErrors, config::Config, config_manager, data_manager};
use crate::{argument_errors::RmtArgumentErrors, config::Config, config_manager, data_manager, secure_delete::delete_folder};
use std::{
ffi::OsStr,
fs,
Expand Down Expand Up @@ -45,13 +45,13 @@ pub fn clear_structure(is_test: bool) {
let trash_path = get_trash_directory_path(is_test);
println!("{}", trash_path);
if Path::new(&trash_path).is_dir() {
fs::remove_dir_all(trash_path.clone())
delete_folder(&trash_path)
.unwrap_or_else(|_| panic!("Unable to delete {}", trash_path));
}

let data_base_path = get_data_base_path(is_test);
if Path::new(&data_base_path).is_dir() {
fs::remove_dir_all(&data_base_path)
delete_folder(&data_base_path)
.unwrap_or_else(|_| panic!("Unable to delete {}", data_base_path));
}
}
Expand Down
65 changes: 49 additions & 16 deletions src/trash_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::arguments_manager::ArgumentsManager;
use crate::display_manager;
use crate::secure_delete::{delete_file, delete_folder};
use crate::structure_manager::{self, get_element_path, get_home_directory_path};
use crate::{
config::Config, data_manager, structure_manager::get_trash_directory_path,
Expand Down Expand Up @@ -61,7 +62,7 @@ pub fn add_element_to_trash(
),
)
.expect("Failed to encrypt");
fs::remove_file(&compressed_path).unwrap();
delete_file(&compressed_path).unwrap();
is_encrypted = true;
} else {
let new_name = format!(
Expand All @@ -79,9 +80,9 @@ pub fn add_element_to_trash(
.unwrap();
}
if !element_is_directory {
fs::remove_file(element_path).unwrap();
delete_file(element_path).unwrap();
} else {
fs::remove_dir_all(element_path).unwrap();
delete_folder(element_path).unwrap();
}
} else if config.encryption && !element_is_directory {
encrypt_element(
Expand All @@ -95,8 +96,27 @@ pub fn add_element_to_trash(
)
.expect("Failed to encrypt");
is_encrypted = true;
fs::remove_file(element_path).unwrap();
} else {
delete_file(element_path).unwrap();
} else if config.encryption && element_is_directory {
let temp_path = element_path.to_string() + &String::from(".temp");
compress_element(element_path, &temp_path).unwrap();
delete_folder(element_path).unwrap();
fs::rename(&temp_path, element_path).unwrap();
encrypt_element(
element_path,
&format!(
"{}{}{}",
get_trash_directory_path(arguments_manager.is_test),
MAIN_SEPARATOR,
hash
),
)
.expect("Failed to encrypt");
is_encrypted = true;
is_compressed = true;
delete_file(element_path).unwrap();
}
else {
let new_name = format!(
"{}{}{}",
get_element_path(element_path),
Expand Down Expand Up @@ -259,7 +279,7 @@ fn encrypt_element(source_path: &str, dist_path: &str) -> Result<(), Box<dyn std
match ciphertext {
Ok(ciphertext) => dist_file.write(&ciphertext)?,
Err(_) => {
let _ = fs::remove_file(dist_path);
let _ = delete_file(dist_path);
panic!("Error encrypting file");
}
};
Expand All @@ -268,7 +288,7 @@ fn encrypt_element(source_path: &str, dist_path: &str) -> Result<(), Box<dyn std
match ciphertext {
Ok(ciphertext) => dist_file.write(&ciphertext)?,
Err(_) => {
let _ = fs::remove_file(dist_path);
let _ = delete_file(dist_path);
panic!("Error encrypting file");
}
};
Expand Down Expand Up @@ -324,7 +344,7 @@ fn decrypt_element(
match plaintext {
Ok(plaintext) => dist_file.write(&plaintext)?,
Err(_) => {
let _ = fs::remove_file(dist_path);
let _ = delete_file(dist_path);
panic!("Error decrypting file");
}
};
Expand All @@ -333,7 +353,7 @@ fn decrypt_element(
match plaintext {
Ok(plaintext) => dist_file.write(&plaintext)?,
Err(_) => {
let _ = fs::remove_file(dist_path);
let _ = delete_file(dist_path);
panic!("Error decrypting file");
}
};
Expand Down Expand Up @@ -396,9 +416,9 @@ fn remove_element(trash_item: &TrashItem, is_test: bool) {
trash_item.hash
);
if Path::new(&element_path).is_dir() {
fs::remove_dir_all(&element_path).unwrap();
delete_folder(&element_path).unwrap();
} else {
fs::remove_file(&element_path).unwrap();
delete_file(&element_path).unwrap();
}

println!(
Expand Down Expand Up @@ -441,17 +461,30 @@ fn restore_element(trash_item: &TrashItem, is_test: bool) {
decrypt_element(&path_in_trash, &decrypted_path_in_trash).expect("Failed to decrypt");
decompress_element(&decrypted_path_in_trash, &trash_item.path)
.expect("Failed to decompress");
fs::remove_file(decrypted_path_in_trash).unwrap();
delete_file(&decrypted_path_in_trash).unwrap();
} else if trash_item.is_encrypted {
decrypt_element(
&path_in_trash,
&format!("{}{}{}", &trash_item.path, MAIN_SEPARATOR, trash_item.name),
)
.expect("Failed to decrypt");
fs::remove_file(&path_in_trash).unwrap();
} else if trash_item.is_compressed {
delete_file(&path_in_trash).unwrap();
}else if trash_item.is_compressed && trash_item.is_encrypted && trash_item.is_folder {
let temp_name = path_in_trash.clone() + ".temp";
decrypt_element(
&path_in_trash,
&format!("{}{}{}", &trash_item.path, MAIN_SEPARATOR, temp_name),
)
.expect("Failed to decrypt");
decompress_element(
&format!("{}{}{}", &trash_item.path, MAIN_SEPARATOR, temp_name),
&format!("{}{}{}", &trash_item.path, MAIN_SEPARATOR, trash_item.name)
).expect("Failed to decompress");
delete_file(&format!("{}{}{}", &trash_item.path, MAIN_SEPARATOR, temp_name)).unwrap();
}
else if trash_item.is_compressed {
decompress_element(&path_in_trash, &trash_item.path).expect("Failed to decompress");
fs::remove_file(&path_in_trash).unwrap();
delete_file(&path_in_trash).unwrap();
} else {
let renamed_path_in_trash = format!(
"{}{}{}",
Expand Down Expand Up @@ -524,7 +557,7 @@ fn restore_element(trash_item: &TrashItem, is_test: bool) {
if trash_item.is_encrypted {
let dist_path = format!("{}{}{}", &new_path, MAIN_SEPARATOR, trash_item.name);
decrypt_element(&path_in_trash, &dist_path).expect("Error decrypting file");
fs::remove_file(&path_in_trash).unwrap();
delete_file(&path_in_trash).unwrap();
} else {
let new_name = format!(
"{}{}{}",
Expand Down