diff --git a/codex-rs/windows-sandbox-rs/src/setup_provisioning/sandbox_users.rs b/codex-rs/windows-sandbox-rs/src/setup_provisioning/sandbox_users.rs index 480bbf7c39..9af048a73f 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_provisioning/sandbox_users.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_provisioning/sandbox_users.rs @@ -1,9 +1,9 @@ use anyhow::Result; use base64::Engine; use base64::engine::general_purpose::STANDARD as BASE64; -use rand::RngCore; -use rand::SeedableRng; -use rand::rngs::SmallRng; +use rand::Rng; +use rand::rngs::OsRng; +use rand::seq::SliceRandom; use serde::Serialize; use std::ffi::OsStr; use std::ffi::c_void; @@ -273,17 +273,25 @@ pub fn sid_bytes_to_psid(sid: &[u8]) -> Result<*mut c_void> { } fn random_password() -> String { - const CHARS: &[u8] = - b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+"; - let mut rng = SmallRng::from_entropy(); - let mut buf = [0u8; 24]; - rng.fill_bytes(&mut buf); - buf.iter() - .map(|b| { - let idx = (*b as usize) % CHARS.len(); - CHARS[idx] as char - }) - .collect() + const CLASSES: [&[u8]; 4] = [ + b"ABCDEFGHIJKLMNOPQRSTUVWXYZ", + b"abcdefghijklmnopqrstuvwxyz", + b"0123456789", + b"!@#$%^&*()-_=+", + ]; + // NetUserSetInfo enforces the machine's password policy. A random string drawn + // from the combined alphabet alone can still omit required character classes. + let mut rng = OsRng; + let mut password = [0u8; 24]; + for (slot, class) in password.iter_mut().zip(CLASSES) { + *slot = class[rng.gen_range(0..class.len())]; + } + let alphabet = CLASSES.concat(); + for slot in &mut password[CLASSES.len()..] { + *slot = alphabet[rng.gen_range(0..alphabet.len())]; + } + password.shuffle(&mut rng); + password.into_iter().map(char::from).collect() } #[derive(Serialize)] diff --git a/codex-rs/windows-sandbox-rs/src/setup_provisioning/sandbox_users_tests.rs b/codex-rs/windows-sandbox-rs/src/setup_provisioning/sandbox_users_tests.rs index 597a75e9ec..01e241b990 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_provisioning/sandbox_users_tests.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_provisioning/sandbox_users_tests.rs @@ -1,6 +1,7 @@ -//! Native account repair regression; uses a disposable account and requires elevation. +//! Tests sandbox account password policy and, with elevation, repairs a disposable account. use super::ensure_local_user; +use super::random_password; use crate::local_user_flags; use crate::set_local_user_flags; use crate::to_wide; @@ -15,6 +16,27 @@ use windows_sys::Win32::NetworkManagement::NetManagement::UF_DONT_EXPIRE_PASSWD; use windows_sys::Win32::NetworkManagement::NetManagement::UF_PASSWORD_EXPIRED; use windows_sys::Win32::NetworkManagement::NetManagement::USER_INFO_4; +#[test] +fn generated_passwords_meet_sandbox_account_policy() { + const SYMBOLS: &[u8] = b"!@#$%^&*()-_=+"; + + // Every draw must satisfy the policy; never assert on the random value itself. + for _ in 0..256 { + let password = random_password(); + let bytes = password.as_bytes(); + assert_eq!(bytes.len(), 24); + assert!( + bytes + .iter() + .all(|b| b.is_ascii_alphanumeric() || SYMBOLS.contains(b)) + ); + assert!(bytes.iter().any(u8::is_ascii_uppercase)); + assert!(bytes.iter().any(u8::is_ascii_lowercase)); + assert!(bytes.iter().any(u8::is_ascii_digit)); + assert!(bytes.iter().any(|b| SYMBOLS.contains(b))); + } +} + struct TestAccount(String); impl Drop for TestAccount { diff --git a/codex-rs/windows-sandbox-rs/src/uninstall_windows/principals_tests.rs b/codex-rs/windows-sandbox-rs/src/uninstall_windows/principals_tests.rs index cc95e4d0e2..919ac7c73b 100644 --- a/codex-rs/windows-sandbox-rs/src/uninstall_windows/principals_tests.rs +++ b/codex-rs/windows-sandbox-rs/src/uninstall_windows/principals_tests.rs @@ -17,7 +17,9 @@ use anyhow::ensure; use pretty_assertions::assert_eq; use rand::RngCore; use windows_sys::Win32::Foundation::ERROR_ACCESS_DENIED; +use windows_sys::Win32::Foundation::ERROR_DELETE_PENDING; use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND; +use windows_sys::Win32::Foundation::ERROR_SHARING_VIOLATION; use windows_sys::Win32::Foundation::ERROR_SUCCESS; use windows_sys::Win32::Foundation::WAIT_OBJECT_0; use windows_sys::Win32::NetworkManagement::NetManagement as network; @@ -198,7 +200,27 @@ fn profile_cleanup_preserves_account_until_profile_can_be_deleted() -> Result<() fixture.stop()?; fixture.user.remove()?; assert_eq!(local_user_flags(name)?, None); - assert!(!path.exists()); + // DeleteProfileW can succeed before the filesystem releases the last + // delete-pending directory handle. Observe completion without deleting it ourselves. + let deadline = Instant::now() + Duration::from_secs(10); + loop { + match path.try_exists() { + Ok(false) => break, + Ok(true) => ensure!( + Instant::now() < deadline, + "deleted profile directory still exists: {}", + path.display() + ), + Err(error) + if Instant::now() < deadline + && matches!( + error.raw_os_error().map(|code| code as u32), + Some(ERROR_ACCESS_DENIED | ERROR_SHARING_VIOLATION | ERROR_DELETE_PENDING) + ) => {} + Err(error) => return Err(error).context("check whether the deleted profile is gone"), + } + std::thread::sleep(Duration::from_millis(50)); + } let mut key = std::ptr::null_mut(); let status = unsafe { registry::RegOpenKeyExW(