mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Ensure Windows sandbox passwords include every character class (#62094)
## Why Randomly sampling a combined alphabet can omit character classes required by the machine's password policy, causing sandbox account password updates to fail. ## What changed Generate 24-character passwords with `OsRng`, guarantee uppercase and lowercase letters, digits, and symbols, and shuffle the result. ## Testing - Add a regression test checking length, allowed characters, and all four character classes across 256 generated passwords. - Let the profile cleanup test wait up to 10 seconds for directory deletion to complete, tolerating transient access, sharing, and delete-pending errors after `DeleteProfileW` succeeds. GitOrigin-RevId: f3adc1249383d7ad769a4e230f978df4a02cd4d3
This commit is contained in:
@@ -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)]
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user