mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
tui: sanitize terminal controls in user messages (#31494)
## Why A pasted user message can contain a raw CSI sequence that corrupts terminal scrollback, making the latest response or input appear missing. The same sequence may already be present in persisted history when an affected conversation is resumed. ## What changed - Remove CSI sequences and non-whitespace control characters from explicit paste input before it enters the composer. - Apply the same small transform before rich and raw `UserHistoryCell` rendering, so existing conversations render safely without mutating stored message content.
This commit is contained in:
1 parent
9c6715924b
commit
9077300d7f
4 files changed
+39
-16
No files matched your search
@@ -11,6 +11,20 @@ pub(crate) struct UserHistoryCell {
|
||||
pub remote_image_urls: Vec<String>,
|
||||
}
|
||||
|
||||
/// Remove CSI sequences and control characters, preserving tabs and newlines.
|
||||
pub(crate) fn sanitize_user_text(text: &str) -> String {
|
||||
let mut sanitized = String::with_capacity(text.len());
|
||||
let mut chars = text.chars().peekable();
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '\x1b' && chars.next_if_eq(&'[').is_some() {
|
||||
let _ = chars.find(|ch| ('@'..='~').contains(ch));
|
||||
} else if matches!(ch, '\n' | '\t') || !ch.is_control() {
|
||||
sanitized.push(ch);
|
||||
}
|
||||
}
|
||||
sanitized
|
||||
}
|
||||
|
||||
/// Build logical lines for a user message with styled text elements.
|
||||
///
|
||||
/// This preserves explicit newlines while interleaving element spans and skips
|
||||
@@ -93,6 +107,12 @@ fn trim_trailing_blank_lines(mut lines: Vec<Line<'static>>) -> Vec<Line<'static>
|
||||
|
||||
impl HistoryCell for UserHistoryCell {
|
||||
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
|
||||
let message = sanitize_user_text(&self.message);
|
||||
let text_elements = if message == self.message {
|
||||
self.text_elements.as_slice()
|
||||
} else {
|
||||
&[]
|
||||
};
|
||||
let wrap_width = width
|
||||
.saturating_sub(
|
||||
LIVE_PREFIX_COLS + 1, /* keep a one-column right margin for wrapping */
|
||||
@@ -117,10 +137,10 @@ impl HistoryCell for UserHistoryCell {
|
||||
))
|
||||
};
|
||||
|
||||
let wrapped_message = if self.message.is_empty() && self.text_elements.is_empty() {
|
||||
let wrapped_message = if message.is_empty() && text_elements.is_empty() {
|
||||
None
|
||||
} else if self.text_elements.is_empty() {
|
||||
let message_without_trailing_newlines = self.message.trim_end_matches(['\r', '\n']);
|
||||
} else if text_elements.is_empty() {
|
||||
let message_without_trailing_newlines = message.trim_end_matches(['\r', '\n']);
|
||||
let wrapped = adaptive_wrap_lines(
|
||||
message_without_trailing_newlines
|
||||
.split('\n')
|
||||
@@ -133,8 +153,8 @@ impl HistoryCell for UserHistoryCell {
|
||||
(!wrapped.is_empty()).then_some(wrapped)
|
||||
} else {
|
||||
let raw_lines = build_user_message_lines_with_elements(
|
||||
&self.message,
|
||||
&self.text_elements,
|
||||
&message,
|
||||
text_elements,
|
||||
style,
|
||||
element_style,
|
||||
);
|
||||
@@ -177,7 +197,8 @@ impl HistoryCell for UserHistoryCell {
|
||||
}
|
||||
|
||||
fn raw_lines(&self) -> Vec<Line<'static>> {
|
||||
let mut lines = raw_lines_from_source(self.message.trim_end_matches(['\r', '\n']));
|
||||
let message = sanitize_user_text(&self.message);
|
||||
let mut lines = raw_lines_from_source(message.trim_end_matches(['\r', '\n']));
|
||||
if !self.remote_image_urls.is_empty() {
|
||||
if !lines.is_empty() {
|
||||
lines.push(Line::from(""));
|
||||
|
||||
+3
-6
@@ -1,9 +1,6 @@
|
||||
---
|
||||
source: tui/src/history_cell.rs
|
||||
source: tui/src/history_cell/tests.rs
|
||||
expression: rendered
|
||||
---
|
||||
|
||||
› one two
|
||||
three
|
||||
four five
|
||||
six seven
|
||||
› _count_ro
|
||||
ws
|
||||
@@ -1978,7 +1978,7 @@ fn ran_cell_multiline_with_stderr_snapshot() {
|
||||
}
|
||||
#[test]
|
||||
fn user_history_cell_wraps_and_prefixes_each_line_snapshot() {
|
||||
let msg = "one two three four five six seven";
|
||||
let msg = "_count_r\x1b[13;2:3uows";
|
||||
let cell = UserHistoryCell {
|
||||
message: msg.to_string(),
|
||||
text_elements: Vec::new(),
|
||||
@@ -1991,6 +1991,7 @@ fn user_history_cell_wraps_and_prefixes_each_line_snapshot() {
|
||||
let lines = cell.display_lines(width);
|
||||
let rendered = render_lines(&lines).join("\n");
|
||||
|
||||
assert_eq!(render_lines(&cell.raw_lines()), ["_count_rows"]);
|
||||
insta::assert_snapshot!(rendered);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user