Probe Windows terminal colors without risking startup input (#40598)

## Why

Windows Terminal and xterm.js can render a theme that differs from the
underlying console color table. Querying that visible theme through OSC shares
the console input queue with keystrokes, including input for security-sensitive
startup prompts.

## What changed

- Defer the Windows OSC color probe until protected startup decisions and the
  world-writable workspace scan have completed.
- Preserve and replay unrelated console input records around the probe, while
  removing only valid OSC 10 and 11 replies and retaining the console palette
  as a fallback.
- Require an explicit confirmation key after shortcuts select trust, sandbox,
  or world-writable warning choices, so a fragmented terminal reply cannot
  accept them.
- Accept one- through four-digit OSC RGB components so the visible terminal
  palette is applied across supported response formats.

## Testing

Added coverage for delayed startup scans, protected prompt confirmation,
console-record replay, bracketed paste and malformed OSC preservation, RGB
response parsing, and light-theme composer rendering.

GitOrigin-RevId: b8ae456fe32c701ba3af31e9cc452fc4e8bffa22
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-08-25 12:20:41 +00:00
1 parent 1f5c9ba183
commit 70b5cfc73b
19 files changed
+1162 -149

No files matched your search

+4
View File
@@ -1611,6 +1611,9 @@ impl App {
failed_scan,
);
}
AppEvent::StartupWorldWritableScanCompleted => {
self.windows_sandbox.startup_world_writable_scan_pending = false;
}
AppEvent::OpenFeedbackNote {
category,
include_logs,
@@ -2298,6 +2301,7 @@ impl App {
logs_base_dir,
permission_profile,
tx,
/*startup_scan*/ false,
);
}
}
+9
View File
@@ -10,6 +10,8 @@ pub(super) struct WindowsSandboxState {
pub(super) setup_started_at: Option<Instant>,
// One-shot suppression of the next world-writable scan after user confirmation.
pub(super) skip_world_writable_scan_once: bool,
/// A startup filesystem scan can still enqueue a protected warning after the app queue drains.
pub(super) startup_world_writable_scan_pending: bool,
}
impl App {
@@ -21,6 +23,7 @@ impl App {
logs_base_dir: AbsolutePathBuf,
permission_profile: PermissionProfile,
tx: AppEventSender,
startup_scan: bool,
) {
let Ok(permissions) =
codex_windows_sandbox::ResolvedWindowsSandboxPermissions::try_from_permission_profile_for_workspace_roots(
@@ -28,6 +31,9 @@ impl App {
workspace_roots.as_slice(),
)
else {
if startup_scan {
tx.send(AppEvent::StartupWorldWritableScanCompleted);
}
return;
};
@@ -45,6 +51,9 @@ impl App {
// Scan failed: warn without examples.
send_world_writable_scan_failed(&tx);
}
if startup_scan {
tx.send(AppEvent::StartupWorldWritableScanCompleted);
}
});
}
}
+28
View File
@@ -67,6 +67,17 @@ impl App {
.any(|event| matches!(event, ThreadBufferedEvent::Request(_))))
}
/// Wait until visible and queued startup decisions cannot consume a delayed OSC response.
#[cfg(any(windows, test))]
pub(super) fn ready_for_terminal_color_probe(&self, has_pending_app_events: bool) -> bool {
!has_pending_app_events
&& !self.chat_widget.has_active_view()
&& !self.windows_sandbox.startup_world_writable_scan_pending
&& !self.startup_pending_protected_request
&& !self.has_queued_startup_protected_request()
&& !self.chat_widget.has_pending_protected_request()
}
#[allow(clippy::too_many_arguments)]
pub async fn run(
tui: &mut tui::Tui,
@@ -584,6 +595,7 @@ See the Codex keymap documentation for supported actions and examples."
.hide_world_writable_warning
.unwrap_or(false);
if should_check {
app.windows_sandbox.startup_world_writable_scan_pending = true;
let cwd = app.config.cwd.clone();
let workspace_roots = app.config.effective_workspace_roots();
let env_map: std::collections::HashMap<String, String> = std::env::vars().collect();
@@ -596,6 +608,7 @@ See the Codex keymap documentation for supported actions and examples."
logs_base_dir,
startup_permission_profile,
tx,
/*startup_scan*/ true,
);
}
}
@@ -625,6 +638,14 @@ See the Codex keymap documentation for supported actions and examples."
.restore_startup_draft_when_ready(&mut pending_startup_draft);
}
#[cfg(windows)]
let mut terminal_color_probe_pending = true;
#[cfg(windows)]
if app.ready_for_terminal_color_probe(!app_event_rx.is_empty()) {
tui.probe_default_colors_after_protected_startup();
terminal_color_probe_pending = false;
}
let event_stream_started_at = Instant::now();
tui.schedule_screen_size_recheck(Duration::ZERO);
if let Err(err) = render_startup_frame(&mut app, tui) {
@@ -802,6 +823,13 @@ See the Codex keymap documentation for supported actions and examples."
app.chat_widget
.restore_startup_draft_when_ready(&mut pending_startup_draft);
}
#[cfg(windows)]
if terminal_color_probe_pending
&& app.ready_for_terminal_color_probe(!app_event_rx.is_empty())
{
tui.probe_default_colors_after_protected_startup();
terminal_color_probe_pending = false;
}
}
AppRunControl::Exit(reason) => break Ok(reason),
}
+100
View File
@@ -6,6 +6,7 @@ use crate::tui::FrameRequester;
use app_test_support::create_fake_parented_rollout_with_source;
use codex_app_server_protocol::ToolRequestUserInputOption;
use codex_app_server_protocol::ToolRequestUserInputQuestion;
use codex_utils_approval_presets::builtin_approval_presets;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
@@ -30,6 +31,105 @@ fn startup_bottom_pane() -> (BottomPane, UnboundedReceiver<AppEvent>) {
)
}
#[tokio::test]
async fn terminal_color_probe_waits_for_startup_sandbox_choice() {
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
app.startup_protected_input_boundary = true;
while app_event_rx.try_recv().is_ok() {}
assert!(app.ready_for_terminal_color_probe(/*has_pending_app_events*/ false));
assert!(!app.ready_for_terminal_color_probe(/*has_pending_app_events*/ true));
let preset = builtin_approval_presets()
.into_iter()
.find(|preset| preset.id == "auto")
.expect("auto preset");
app.chat_widget
.open_windows_sandbox_enable_prompt(preset, /*profile_selection*/ None);
assert!(!app.ready_for_terminal_color_probe(/*has_pending_app_events*/ false));
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Char('2'), KeyModifiers::NONE));
assert!(!app.ready_for_terminal_color_probe(/*has_pending_app_events*/ false));
assert!(app_event_rx.try_recv().is_err());
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
assert!(matches!(
app_event_rx.try_recv(),
Ok(AppEvent::BeginWindowsSandboxLegacySetup { .. })
));
assert!(app.ready_for_terminal_color_probe(/*has_pending_app_events*/ false));
}
#[tokio::test]
async fn terminal_color_probe_waits_for_delayed_world_writable_scan_failure() {
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
app.startup_protected_input_boundary = true;
app.windows_sandbox.startup_world_writable_scan_pending = true;
while app_event_rx.try_recv().is_ok() {}
assert!(!app.ready_for_terminal_color_probe(/*has_pending_app_events*/ false));
app.app_event_tx
.send(AppEvent::OpenWorldWritableWarningConfirmation {
preset: None,
profile_selection: None,
sample_paths: Vec::new(),
extra_count: 0,
failed_scan: true,
});
app.app_event_tx
.send(AppEvent::StartupWorldWritableScanCompleted);
assert!(!app.ready_for_terminal_color_probe(/*has_pending_app_events*/ true));
let warning = app_event_rx
.try_recv()
.expect("the delayed scan should queue its warning before completion");
let AppEvent::OpenWorldWritableWarningConfirmation {
preset,
profile_selection,
sample_paths,
extra_count,
failed_scan,
} = warning
else {
panic!("the delayed scan should open a protected warning before completion");
};
app.chat_widget.open_world_writable_warning_confirmation(
preset,
profile_selection,
sample_paths,
extra_count,
failed_scan,
);
assert!(matches!(
app_event_rx.try_recv(),
Ok(AppEvent::StartupWorldWritableScanCompleted)
));
app.windows_sandbox.startup_world_writable_scan_pending = false;
assert!(!app.windows_sandbox.startup_world_writable_scan_pending);
assert!(!app.ready_for_terminal_color_probe(/*has_pending_app_events*/ false));
for character in "20;rgb:2222/ffff/ffff".chars() {
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Char(character), KeyModifiers::NONE));
assert!(app_event_rx.try_recv().is_err());
}
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
assert!(matches!(
app_event_rx.try_recv(),
Ok(AppEvent::UpdateWorldWritableWarningAcknowledged(true))
));
assert!(matches!(
app_event_rx.try_recv(),
Ok(AppEvent::PersistWorldWritableWarningAcknowledged)
));
assert!(app.ready_for_terminal_color_probe(/*has_pending_app_events*/ false));
}
#[test]
fn startup_waiting_gate_is_only_for_fresh_or_exit_session_selection() {
assert_eq!(