Grant cleanup rights for Windows wrapper requests

This commit is contained in:
David Wiesen
2026-06-11 16:53:21 -07:00
parent 85e99b5090
commit f799d91a82
3 changed files with 41 additions and 0 deletions
+6
View File
@@ -504,6 +504,12 @@ impl WindowsSandboxWrapperRequestFile {
request_dir.display()
))
})?;
codex_windows_sandbox::ensure_current_user_cleanup_access(&request_dir).map_err(|err| {
internal_error(format!(
"failed to grant cleanup access to windows fs sandbox wrapper request dir {}: {err}",
request_dir.display()
))
})?;
let path = request_dir.join(format!(
"fs-helper-wrapper-request-{}.json",
uuid::Uuid::new_v4()
+33
View File
@@ -1,3 +1,4 @@
use crate::winutil::resolve_sid;
use crate::winutil::to_wide;
use anyhow::Result;
use anyhow::anyhow;
@@ -416,6 +417,14 @@ pub unsafe fn ensure_allow_write_aces(path: &Path, sids: &[*mut c_void]) -> Resu
ensure_allow_mask_aces(path, sids, WRITE_ALLOW_MASK)
}
/// Ensure the current real user can clean up files and child directories under
/// `path`, even when the parent tree grants write access without delete rights.
pub fn ensure_current_user_cleanup_access(path: &Path) -> Result<bool> {
let real_user = std::env::var("USERNAME").unwrap_or_else(|_| "Administrators".to_string());
let real_user_sid = resolve_sid(&real_user)?;
unsafe { ensure_allow_write_aces(path, &[real_user_sid.as_ptr() as *mut c_void]) }
}
/// Adds an allow ACE granting read/write/execute to the given SID on the target path.
///
/// # Safety
@@ -482,6 +491,30 @@ pub unsafe fn add_allow_ace(path: &Path, psid: *mut c_void) -> Result<bool> {
Ok(added)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn current_user_cleanup_access_grants_delete_rights() -> Result<()> {
let temp_dir = tempfile::TempDir::new()?;
let request_dir = temp_dir.path().join("wrapper-requests");
std::fs::create_dir(&request_dir)?;
ensure_current_user_cleanup_access(&request_dir)?;
let real_user = std::env::var("USERNAME").unwrap_or_else(|_| "Administrators".to_string());
let real_user_sid = resolve_sid(&real_user)?;
assert!(path_mask_allows(
&request_dir,
&[real_user_sid.as_ptr() as *mut c_void],
DELETE | FILE_DELETE_CHILD,
/*require_all_bits*/ true,
)?);
Ok(())
}
}
/// Adds a deny ACE to prevent write/append/delete for the given SID on the target path.
///
/// # Safety
+2
View File
@@ -136,6 +136,8 @@ pub use acl::ensure_allow_mask_aces_with_inheritance;
#[cfg(target_os = "windows")]
pub use acl::ensure_allow_write_aces;
#[cfg(target_os = "windows")]
pub use acl::ensure_current_user_cleanup_access;
#[cfg(target_os = "windows")]
pub use acl::fetch_dacl_handle;
#[cfg(target_os = "windows")]
pub use acl::path_mask_allows;