diff --git a/crates/argus-core/src/startup.rs b/crates/argus-core/src/startup.rs index 6c66dd62..eb7f0a35 100644 --- a/crates/argus-core/src/startup.rs +++ b/crates/argus-core/src/startup.rs @@ -3,12 +3,13 @@ mod packaged; -use std::path::Path; +use std::{io::ErrorKind, os::windows::fs::OpenOptionsExt, path::Path}; use serde::Serialize; use windows::Win32::Foundation::{ ERROR_FILE_NOT_FOUND, ERROR_SUCCESS, FILETIME, MAX_PATH, WIN32_ERROR, }; +use windows::Win32::Storage::FileSystem::{DELETE, FILE_FLAG_OPEN_REPARSE_POINT}; use windows::Win32::System::Com::{ CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED, CoCreateInstance, CoInitializeEx, IPersistFile, STGM_READ, @@ -508,6 +509,20 @@ fn remove_file(folder: &Path, name: &str, recycle: bool) -> Result<(), ActionErr if !path.is_file() { return Err(ActionError::NotFound); } + // Check the caller's delete access on the directory entry itself before + // handing it to the Shell. Release the handle before the recycle operation. + if let Err(error) = std::fs::File::options() + .access_mode(DELETE.0) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT.0) + .open(&path) + { + crate::diagnostics::file_failure("startup open for deletion", &path, &error); + return Err(match error.kind() { + ErrorKind::PermissionDenied => ActionError::AccessDenied, + ErrorKind::NotFound => ActionError::NotFound, + _ => ActionError::Os(error.to_string()), + }); + } // The paths, each ended by a NUL, and the list by another. let mut from = wide(&path.to_string_lossy()); from.push(0); @@ -727,10 +742,11 @@ mod tests { .expect("icacls"); assert!(status.success()); } - assert!(matches!( - remove_file(&folder.0, "Locked.lnk", true), - Err(ActionError::AccessDenied) - )); + let result = remove_file(&folder.0, "Locked.lnk", true); + assert!( + matches!(result, Err(ActionError::AccessDenied)), + "{result:?}" + ); assert!(folder.0.join("Locked.lnk").exists()); } diff --git a/src-gpui/src/commands.rs b/src-gpui/src/commands.rs index 04c9581a..6370aba1 100644 --- a/src-gpui/src/commands.rs +++ b/src-gpui/src/commands.rs @@ -1015,6 +1015,13 @@ pub fn history_move( Ok(settings_view(&app)) } +pub fn history_reset_dir( + app: crate::native_runtime::Host, + move_records: bool, +) -> Result { + history_move(app, String::new(), move_records) +} + pub fn history_open_dir(dir: State<'_, HistoryDir>) -> Result<(), String> { open_folder(&dir.read()).map_err(|_| tr!("history.error.open")) } diff --git a/src-gpui/src/dispatch.rs b/src-gpui/src/dispatch.rs index 7e701c48..af6bc606 100644 --- a/src-gpui/src/dispatch.rs +++ b/src-gpui/src/dispatch.rs @@ -14,10 +14,6 @@ fn argument(args: &Value, key: &str) -> Result) -> Result { - serde_json::to_value(result.map_err(Value::String)?) - .map_err(|_| Value::String(argus_core::tr!("app.invalidRequest"))) -} pub fn invoke(host: &Host, name: &str, args: Value) -> Result { match name { "desktop_shell" => Ok(serde_json::to_value(crate::commands::desktop_shell()) @@ -301,15 +297,13 @@ pub fn invoke(host: &Host, name: &str, args: Value) -> Result { argument::>(&args, "keys")?, )) .expect("serializable command response")), - "history_events" => serde_json::to_value( - crate::commands::history_events( - host.state(), - argument::(&args, "from")?, - argument::(&args, "to")?, - ) - .map_err(Value::String)?, + "history_events" => crate::commands::history_events( + host.state(), + argument::(&args, "from")?, + argument::(&args, "to")?, ) - .map_err(|_| Value::String(argus_core::tr!("app.invalidRequest"))), + .map(|value| serde_json::to_value(value).expect("serializable command response")) + .map_err(|error| serde_json::to_value(error).unwrap_or(serde_json::Value::Null)), "history_channels" => Ok(serde_json::to_value(crate::commands::history_channels( host.state(), argument::(&args, "from")?, @@ -347,29 +341,36 @@ pub fn invoke(host: &Host, name: &str, args: Value) -> Result { ) .map(|value| serde_json::to_value(value).expect("serializable command response")) .map_err(|error| serde_json::to_value(error).unwrap_or(serde_json::Value::Null)), - "history_move" => history_settings(crate::commands::history_move( + "set_history_enabled" => crate::commands::set_history_enabled( host.clone(), - argument(&args, "dir")?, - argument(&args, "moveRecords")?, - )), - "history_reset_dir" => history_settings(crate::commands::history_move( + host.state(), + host.state(), + argument::(&args, "enabled")?, + ) + .map(|value| serde_json::to_value(value).expect("serializable command response")) + .map_err(|error| serde_json::to_value(error).unwrap_or(serde_json::Value::Null)), + "set_history_options" => crate::commands::set_history_options( host.clone(), - String::new(), - argument(&args, "moveRecords")?, - )), - "history_open_dir" => crate::commands::history_open_dir(host.state()) - .map(|()| Value::Null) - .map_err(Value::String), - "set_history_options" => history_settings(crate::commands::set_history_options( + argument::(&args, "options")?, + ) + .map(|value| serde_json::to_value(value).expect("serializable command response")) + .map_err(|error| serde_json::to_value(error).unwrap_or(serde_json::Value::Null)), + "history_move" => crate::commands::history_move( host.clone(), - argument(&args, "options")?, - )), - "set_history_enabled" => history_settings(crate::commands::set_history_enabled( + argument::(&args, "dir")?, + argument::(&args, "moveRecords")?, + ) + .map(|value| serde_json::to_value(value).expect("serializable command response")) + .map_err(|error| serde_json::to_value(error).unwrap_or(serde_json::Value::Null)), + "history_reset_dir" => crate::commands::history_reset_dir( host.clone(), - host.state(), - host.state(), - argument(&args, "enabled")?, - )), + argument::(&args, "moveRecords")?, + ) + .map(|value| serde_json::to_value(value).expect("serializable command response")) + .map_err(|error| serde_json::to_value(error).unwrap_or(serde_json::Value::Null)), + "history_open_dir" => crate::commands::history_open_dir(host.state()) + .map(|value| serde_json::to_value(value).expect("serializable command response")) + .map_err(|error| serde_json::to_value(error).unwrap_or(serde_json::Value::Null)), "set_kernel_sensors" => crate::commands::set_kernel_sensors( host.clone(), host.state(),