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

+12 -1
View File
@@ -498,7 +498,9 @@ pub(crate) fn init() -> Result<InitializedTerminal> {
!keyboard_modes::keyboard_enhancement_disabled() && detect_keyboard_enhancement_supported();
#[cfg(windows)]
probe_windows_default_colors();
// OSC replies can arrive after their deadline. Do not issue terminal queries before directory
// trust and other protected startup screens have finished accepting their security decisions.
crate::terminal_palette::set_default_colors_from_startup_probe(/*colors*/ None);
let tui = CustomTerminal::with_options_and_cursor_position(backend, cursor_pos)?;
let stderr_guard = terminal_stderr::TerminalStderrGuard::install()?;
@@ -693,6 +695,15 @@ impl Tui {
self.event_broker.resume_events();
}
/// Discover the visible Windows theme only after protected startup decisions have completed.
#[cfg(windows)]
pub(crate) fn probe_default_colors_after_protected_startup(&mut self) {
self.pause_events();
probe_windows_default_colors();
self.resume_events();
self.frame_requester.schedule_frame();
}
/// Reclaim terminal modes and stderr after a panic hook ran inside a recovery boundary.
pub(crate) fn recover_after_caught_panic(&mut self) -> Result<()> {
set_modes()?;