Repair missing or stale Windows sandbox Core registration (#62141)

## Why

When protected Core is requested, valid cached sandbox credentials can let
command startup skip setup even if Core registration is missing or stale.

## What changed

Run automatic setup when the requested registration is unready, even if
cached credentials are valid. Require registration readiness after setup
before returning sandbox credentials.

## Testing

Extend credential setup coverage to verify that valid cached credentials
trigger one setup attempt when registration is unready. Verify that setup
reloads credentials when registration becomes ready and returns an error
when it remains unready.

GitOrigin-RevId: 2822e8f294d98ea3df4f3bd7cf3f1e83ef5a2e7b
This commit is contained in:
zm-oai
2026-11-18 00:32:46 +00:00
committed by copyberry
parent f39fcc1bfb
commit 2ae806bdf7
2 changed files with 36 additions and 10 deletions
@@ -320,6 +320,10 @@ pub(crate) fn require_sandbox_account(
proxy_settings_mode,
run_automatic_setup,
local_user_flags,
|home| {
crate::registered_core_requested()
&& !crate::app_package::registered_setup_is_ready(home).unwrap_or(false)
},
)
}
@@ -328,6 +332,7 @@ fn require_sandbox_account_with_setup(
proxy_settings_mode: crate::WindowsSandboxProxySettingsMode,
run_full_setup: impl FnOnce(SandboxSetupRequest<'_>, &OfflineProxySettings) -> Result<()>,
read_local_user_flags: impl Fn(&str) -> Result<Option<u32>>,
registration_required: impl Fn(&Path) -> bool,
) -> Result<(SandboxCreds, OfflineProxySettings)> {
let &SandboxSetupRequest {
permissions,
@@ -391,6 +396,11 @@ fn require_sandbox_account_with_setup(
}
}
if identity.is_some() && registration_required(codex_home) {
setup_reason = Some("protected Core registration is missing or out of date".to_string());
identity = None;
}
if identity.is_none() {
if let Some(reason) = &setup_reason {
crate::logging::log_note(
@@ -410,6 +420,10 @@ fn require_sandbox_account_with_setup(
},
&desired_offline_proxy_settings,
)?;
ensure!(
!registration_required(codex_home),
"protected Core setup did not publish ready runtime ownership"
);
for username in [OFFLINE_USERNAME, ONLINE_USERNAME] {
if let Ok(Some(flags)) = read_local_user_flags(username) {
anyhow::ensure!(
@@ -69,12 +69,14 @@ fn credential_setup_repairs_expired_accounts_once_and_reloads_credentials() -> R
let permissions = ResolvedWindowsSandboxPermissions::try_from_permission_profile(
&PermissionProfile::read_only(),
)?;
for (expired_username, repair_succeeds) in [
(None, true),
(Some(OFFLINE_USERNAME), true),
(Some(ONLINE_USERNAME), true),
(Some(OFFLINE_USERNAME), false),
(Some(ONLINE_USERNAME), false),
for (expired_username, repair_succeeds, needs_registration) in [
(None, true, false),
(None, true, true),
(None, false, true),
(Some(OFFLINE_USERNAME), true, false),
(Some(ONLINE_USERNAME), true, false),
(Some(OFFLINE_USERNAME), false, false),
(Some(ONLINE_USERNAME), false, false),
] {
let home = tempfile::tempdir()?;
let marker = SetupMarker {
@@ -116,7 +118,10 @@ fn credential_setup_repairs_expired_accounts_once_and_reloads_credentials() -> R
WindowsSandboxProxySettingsMode::Preserve,
|_, _| {
// Older services must not accept stale credentials instead of repairing expiry.
assert!(!sandbox_users_path(home.path()).exists());
assert_eq!(
sandbox_users_path(home.path()).exists(),
expired_username.is_none()
);
setups.set(setups.get() + 1);
users.offline.password =
BASE64.encode(crate::dpapi::protect(b"new-test-password")?);
@@ -134,18 +139,24 @@ fn credential_setup_repairs_expired_accounts_once_and_reloads_credentials() -> R
},
))
},
|_| needs_registration && (setups.get() == 0 || !repair_succeeds),
);
assert_eq!(
setups.get(),
usize::from(expired_username.is_some() || needs_registration)
);
assert_eq!(setups.get(), usize::from(expired_username.is_some()));
assert_eq!(
result
.map(|(creds, _)| (creds.username, creds.password))
.map_err(|err| err.to_string()),
if !repair_succeeds {
if needs_registration && !repair_succeeds {
Err("protected Core setup did not publish ready runtime ownership".into())
} else if !repair_succeeds {
Err("Windows sandbox account password is still expired after setup".into())
} else {
Ok((
OFFLINE_USERNAME.into(),
if expired_username.is_some() {
if expired_username.is_some() || needs_registration {
"new-test-password"
} else {
"old-test-password"
@@ -246,6 +257,7 @@ fn credential_setup_reconciles_policy_and_repairs_accounts() -> Result<()> {
Ok(())
},
|_| Ok(account_flags.get()),
|_| false,
)
};
for _ in 0..2 {