From 45ace810b3d394f177fb6b1c8a10db9c698e6e05 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Fri, 20 Nov 2026 06:50:25 +0000 Subject: [PATCH] Copy pasted image files into temporary TUI attachments (#62687) ## Why A pasted image path could become unreadable before submission if the original file was moved or deleted. ## What changed Copy the image into a private temporary file at paste time and use that copy as the attachment. Apply a 1 GiB limit when saving image files and clipboard PNGs. If copying a pasted image path fails, show an error and preserve the draft. ## Testing Cover attachment readability after deleting the original file and verify that an oversized image paste reports an error and preserves the draft. GitOrigin-RevId: 5f797979f36173fb3355345e0f124b81fcf86d01 --- codex-rs/tui/src/bottom_pane/chat_composer.rs | 55 +----------- .../chat_composer/edit_history_tests.rs | 13 ++- .../bottom_pane/chat_composer/paste_input.rs | 89 +++++++++++++++++-- .../bottom_pane/chat_composer/paste_tests.rs | 66 ++++++++++++++ ...e_tests__oversized_pasted_image_error.snap | 5 ++ .../chatwidget/tests/composer_submission.rs | 4 +- codex-rs/tui/src/clipboard_paste.rs | 33 ++++--- 7 files changed, 188 insertions(+), 77 deletions(-) create mode 100644 codex-rs/tui/src/bottom_pane/chat_composer/snapshots/codex_tui__bottom_pane__chat_composer__paste_tests__oversized_pasted_image_error.snap diff --git a/codex-rs/tui/src/bottom_pane/chat_composer.rs b/codex-rs/tui/src/bottom_pane/chat_composer.rs index 89bc83b55f..fa3d3d1aea 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer.rs @@ -174,6 +174,7 @@ //! - Treats a leading `!` revealed only by paste expansion as literal model input, not shell input. //! - Prunes local attached images so only placeholders that survive expansion are sent. //! - Preserves remote image URLs as separate attachments even when text is empty. +//! - Copies pasted image files into private temporary attachments before retaining their paths. //! //! When these paths clear the visible textarea after a successful submit or slash-command //! dispatch, they intentionally preserve the textarea kill buffer. That lets users `Ctrl+K` part @@ -467,8 +468,6 @@ use crate::app_event_sender::AppEventSender; use crate::bottom_pane::LocalImageAttachment; use crate::bottom_pane::MentionBinding; use crate::bottom_pane::textarea::TextArea; -use crate::clipboard_paste::normalize_pasted_path; -use crate::clipboard_paste::pasted_image_format; use crate::history_cell; use crate::skills_helpers::skill_display_name; use crate::tui::FrameRequester; @@ -1340,32 +1339,6 @@ impl ChatComposer { self.history.record_replayed_submission(entry); } - pub fn handle_paste_image_path(&mut self, pasted: &str) -> bool { - let Some(path_buf) = normalize_pasted_path(pasted) else { - return false; - }; - - // Opening a serial device or FIFO can block the input loop. - if !path_buf.is_file() { - return false; - } - - match image::image_dimensions(&path_buf) { - Ok((width, height)) => { - tracing::info!("OK: {pasted}"); - tracing::debug!("image dimensions={}x{}", width, height); - let format = pasted_image_format(&path_buf); - tracing::debug!("attached image format={}", format.label()); - self.attach_image(path_buf); - true - } - Err(err) => { - tracing::trace!("ERR: {err}"); - false - } - } - } - /// Replace the composer content with text from an external editor. /// Clears pending paste placeholders and keeps only attachments whose /// placeholder labels still appear in the new text. Image placeholders @@ -11888,32 +11861,6 @@ mod tests { assert_eq!(composer.draft.textarea.text(), "[Image #1]"); } - #[test] - fn pasting_filepath_attaches_image() { - let tmp = tempdir().expect("create TempDir"); - let tmp_path: PathBuf = tmp.path().join("codex_tui_test_paste_image.png"); - let img: ImageBuffer, Vec> = - ImageBuffer::from_fn(3, 2, |_x, _y| Rgba([1, 2, 3, 255])); - img.save(&tmp_path).expect("failed to write temp png"); - - let (tx, _rx) = unbounded_channel::(); - let sender = AppEventSender::new(tx); - let mut composer = ChatComposer::new( - /*has_input_focus*/ true, - sender, - /*enhanced_keys_supported*/ false, - "Ask Codex to do anything".to_string(), - /*disable_paste_burst*/ false, - ); - - let needs_redraw = composer.handle_paste(tmp_path.to_string_lossy().to_string()); - assert!(needs_redraw); - assert!(composer.draft.textarea.text().starts_with("[Image #1] ")); - - let imgs = composer.take_recent_submission_images(); - assert_eq!(imgs, vec![tmp_path]); - } - #[test] fn slash_path_input_submits_without_command_error() { use crossterm::event::KeyCode; diff --git a/codex-rs/tui/src/bottom_pane/chat_composer/edit_history_tests.rs b/codex-rs/tui/src/bottom_pane/chat_composer/edit_history_tests.rs index 4ff10aad79..68aaa4b021 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer/edit_history_tests.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer/edit_history_tests.rs @@ -738,7 +738,18 @@ fn vim_editing_modes_use_the_same_large_and_image_paste_handling() { let mut replace = vim_composer("next"); keys(&mut replace, "R"); replace.handle_paste(pasted); - assert_eq!(replace.draft_snapshot(), insert.draft_snapshot()); + let insert = insert.draft_snapshot(); + let mut replace = replace.draft_snapshot(); + for (replace_image, insert_image) in + replace.local_images.iter_mut().zip(&insert.local_images) + { + std::fs::remove_file(&replace_image.path).expect("remove replacement image copy"); + replace_image.path.clone_from(&insert_image.path); + } + assert_eq!(replace, insert); + for image in insert.local_images { + std::fs::remove_file(image.path).expect("remove inserted image copy"); + } } } diff --git a/codex-rs/tui/src/bottom_pane/chat_composer/paste_input.rs b/codex-rs/tui/src/bottom_pane/chat_composer/paste_input.rs index a42e583a39..568ca846d4 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer/paste_input.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer/paste_input.rs @@ -6,12 +6,69 @@ //! answer fields. //! Provisional startup input, search queries, and shell input remain literal. //! Paste classification and insertion share one textarea edit target, including selection replacement. +//! Pasted image files are copied into private temporary attachments while the client can read them. use super::*; use crate::bottom_pane::paste_burst::FlushResult; use crate::bottom_pane::textarea::EditTarget; +use crate::clipboard_paste::normalize_pasted_path; +use crate::clipboard_paste::pasted_image_format; impl ChatComposer { + fn copy_pasted_image_path(pasted: &str) -> std::io::Result> { + use std::io::Seek; + #[cfg(unix)] + use std::os::unix::fs::OpenOptionsExt; + + let Some(path_buf) = normalize_pasted_path(pasted) else { + return Ok(None); + }; + // Opening a serial device or FIFO can block the input loop. + if !path_buf.is_file() { + return Ok(None); + } + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(unix)] + options.custom_flags(libc::O_NONBLOCK); + let Ok(mut file) = options.open(&path_buf) else { + return Ok(None); + }; + let Ok(metadata) = file.metadata() else { + return Ok(None); + }; + if !metadata.is_file() { + return Ok(None); + } + let Ok(image_format) = image::ImageFormat::from_path(&path_buf) else { + return Ok(None); + }; + let dimensions = + image::ImageReader::with_format(std::io::BufReader::new(&mut file), image_format) + .into_dimensions(); + match dimensions { + Ok((width, height)) => { + tracing::info!("OK: {pasted}"); + tracing::debug!("image dimensions={}x{}", width, height); + let format = pasted_image_format(&path_buf); + tracing::debug!("attached image format={}", format.label()); + let suffix = path_buf + .extension() + .map(|extension| format!(".{}", extension.to_string_lossy())) + .unwrap_or_default(); + if metadata.len() > crate::clipboard_paste::MAX_PASTED_IMAGE_BYTES { + return Err(std::io::Error::other("image exceeds the 1 GiB input limit")); + } + file.rewind()?; + crate::clipboard_paste::persist_pasted_image(file, &suffix).map(Some) + } + Err(err) => { + tracing::trace!("ERR: {err}"); + Ok(None) + } + } + } + pub(crate) fn insert_str(&mut self, text: &str) { self.insert_str_at_target( self.draft.textarea.edit_target(), @@ -220,22 +277,20 @@ impl ChatComposer { pasted: String, shell_prefix: ShellPrefixHandling, ) -> bool { - self.draft.skill_completion_space = None; let pasted = pasted.replace("\r\n", "\n").replace('\r', "\n"); let pasted = sanitize_user_text(pasted.into()); if self.history_search.is_some() { + self.draft.skill_completion_space = None; if !pasted.is_empty() { self.update_history_search_query(|query| query.push_str(&pasted)); } return true; } if let Some(query) = self.draft.textarea.vim_query_mut() { + self.draft.skill_completion_space = None; query.editor.insert_str(&pasted); return true; } - self.draft.textarea_state.get_mut().follow_cursor(); - let started_edit = self.begin_direct_edit(); - let elements_before = self.draft.textarea.element_payloads(); let target = self.draft.textarea.edit_target(); let before_cursor = &self.draft.textarea.text()[..target.start()]; let current_line = before_cursor.rsplit('\n').next().unwrap_or_default(); @@ -253,6 +308,26 @@ impl ChatComposer { pasted }; let char_count = pasted.chars().count(); + let image = if (2..=LARGE_PASTE_CHAR_THRESHOLD).contains(&char_count) + && self.image_paste_enabled() + { + match Self::copy_pasted_image_path(&pasted) { + Ok(image) => image, + Err(error) => { + self.draft.paste_burst.clear_after_explicit_paste(); + self.app_event_tx.send(AppEvent::InsertHistoryCell(Box::new( + history_cell::new_error_event(format!("Failed to paste image: {error}")), + ))); + return true; + } + } + } else { + None + }; + self.draft.skill_completion_space = None; + self.draft.textarea_state.get_mut().follow_cursor(); + let started_edit = self.begin_direct_edit(); + let elements_before = self.draft.textarea.element_payloads(); if char_count > LARGE_PASTE_CHAR_THRESHOLD { let placeholder = self.next_large_paste_placeholder(char_count); self.draft @@ -261,10 +336,8 @@ impl ChatComposer { self.draft .pending_pastes .push((placeholder, pasted.into_owned())); - } else if char_count > 1 - && self.image_paste_enabled() - && self.handle_paste_image_path(&pasted) - { + } else if let Some(image) = image { + self.attach_image(image); let cursor = self.draft.textarea.cursor(); self.draft.textarea.insert_str_at(cursor, " "); } else { diff --git a/codex-rs/tui/src/bottom_pane/chat_composer/paste_tests.rs b/codex-rs/tui/src/bottom_pane/chat_composer/paste_tests.rs index 9ff03f5059..55eb5d0a82 100644 --- a/codex-rs/tui/src/bottom_pane/chat_composer/paste_tests.rs +++ b/codex-rs/tui/src/bottom_pane/chat_composer/paste_tests.rs @@ -2,9 +2,12 @@ use super::tests::new_test_composer; use super::*; +use image::ImageBuffer; +use image::Rgba; use pretty_assertions::assert_eq; use ratatui::Terminal; use ratatui::backend::TestBackend; +use tempfile::tempdir; #[cfg(unix)] #[test] @@ -166,3 +169,66 @@ fn paste_burst_modified_queue_binding_still_dispatches() { } ); } + +#[test] +fn pasting_filepath_attaches_image() { + let tmp = tempdir().expect("create TempDir"); + let tmp_path: PathBuf = tmp.path().join("codex_tui_test_paste_image.png"); + let img: ImageBuffer, Vec> = + ImageBuffer::from_fn(3, 2, |_x, _y| Rgba([1, 2, 3, 255])); + img.save(&tmp_path).expect("failed to write temp png"); + + let (mut composer, _rx) = new_test_composer(); + + let needs_redraw = composer.handle_paste(tmp_path.to_string_lossy().to_string()); + assert!(needs_redraw); + assert!(composer.draft.textarea.text().starts_with("[Image #1] ")); + + let imgs = composer.take_recent_submission_images(); + let [attachment] = imgs.as_slice() else { + panic!("expected one image attachment"); + }; + let expected = std::fs::read(&tmp_path).unwrap(); + std::fs::remove_file(&tmp_path).unwrap(); + assert_eq!(std::fs::read(attachment).unwrap(), expected); + std::fs::remove_file(attachment).unwrap(); +} + +#[cfg(unix)] +#[test] +fn oversized_pasted_image_reports_error_without_copying() { + let tmp = tempdir().unwrap(); + let path = tmp.path().join("oversized.png"); + ImageBuffer::from_pixel(1, 1, Rgba([0u8, 0, 0, 255])) + .save(&path) + .unwrap(); + std::fs::OpenOptions::new() + .write(true) + .open(&path) + .unwrap() + .set_len(crate::clipboard_paste::MAX_PASTED_IMAGE_BYTES + 1) + .unwrap(); + let (mut composer, mut rx) = new_test_composer(); + composer.set_text_content("draft".to_string(), Vec::new(), Vec::new()); + let before = composer.draft_snapshot(); + let now = Instant::now(); + composer.draft.paste_burst.on_plain_char_no_hold(now); + composer + .draft + .paste_burst + .begin_with_retro_grabbed(path.to_string_lossy().into_owned(), now); + let flushed_at = now + PasteBurst::recommended_active_flush_delay(); + assert!(composer.handle_paste_burst_flush(flushed_at)); + assert_eq!(composer.draft_snapshot(), before); + assert!(!composer.handle_paste_enter(flushed_at)); + let AppEvent::InsertHistoryCell(cell) = rx.try_recv().unwrap() else { + panic!("expected an image paste error"); + }; + let message = cell + .display_lines(/*width*/ 80) + .into_iter() + .map(|line| line.to_string()) + .collect::>() + .join("\n"); + insta::assert_snapshot!("oversized_pasted_image_error", message); +} diff --git a/codex-rs/tui/src/bottom_pane/chat_composer/snapshots/codex_tui__bottom_pane__chat_composer__paste_tests__oversized_pasted_image_error.snap b/codex-rs/tui/src/bottom_pane/chat_composer/snapshots/codex_tui__bottom_pane__chat_composer__paste_tests__oversized_pasted_image_error.snap new file mode 100644 index 0000000000..ccb886d63b --- /dev/null +++ b/codex-rs/tui/src/bottom_pane/chat_composer/snapshots/codex_tui__bottom_pane__chat_composer__paste_tests__oversized_pasted_image_error.snap @@ -0,0 +1,5 @@ +--- +source: tui/src/bottom_pane/chat_composer/paste_tests.rs +expression: message +--- +■ Failed to paste image: image exceeds the 1 GiB input limit diff --git a/codex-rs/tui/src/chatwidget/tests/composer_submission.rs b/codex-rs/tui/src/chatwidget/tests/composer_submission.rs index 7efdd8b90e..a4f6e8e976 100644 --- a/codex-rs/tui/src/chatwidget/tests/composer_submission.rs +++ b/codex-rs/tui/src/chatwidget/tests/composer_submission.rs @@ -3033,8 +3033,8 @@ async fn image_preparation_failure_restores_full_input_without_submitting() { let pixels = image::RgbImage::new(/*width*/ 2, /*height*/ 2); pixels.save(&path).unwrap(); chat.set_remote_image_urls(vec!["https://example.com/remote.png".into()]); - chat.handle_paste(path.display().to_string()); - chat.handle_paste("describe $file".into()); + chat.bottom_pane.attach_image(path.clone()); + chat.handle_paste(" describe $file".into()); chat.bottom_pane.set_composer_text_with_mention_bindings( chat.bottom_pane.composer_text(), chat.bottom_pane.composer_text_elements(), diff --git a/codex-rs/tui/src/clipboard_paste.rs b/codex-rs/tui/src/clipboard_paste.rs index a314146698..dec373d321 100644 --- a/codex-rs/tui/src/clipboard_paste.rs +++ b/codex-rs/tui/src/clipboard_paste.rs @@ -1,4 +1,4 @@ -//! Read clipboard images and text on the TUI host. Text reads run on the clipboard worker. +//! Read and persist pasted images on the TUI host. Text reads run on the clipboard worker. #[cfg(target_os = "macos")] mod macos; @@ -8,6 +8,25 @@ use std::path::Path; use std::path::PathBuf; use tempfile::Builder; +pub(crate) const MAX_PASTED_IMAGE_BYTES: u64 = 1024 * 1024 * 1024; + +pub(crate) fn persist_pasted_image( + source: impl std::io::Read, + suffix: &str, +) -> std::io::Result { + let mut tmp = Builder::new() + .prefix("codex-clipboard-") + .suffix(suffix) + .tempfile()?; + if std::io::copy(&mut source.take(MAX_PASTED_IMAGE_BYTES + 1), &mut tmp)? + > MAX_PASTED_IMAGE_BYTES + { + return Err(std::io::Error::other("image exceeds the 1 GiB input limit")); + } + let (_, path) = tmp.keep().map_err(|error| error.error)?; + Ok(path) +} + #[derive(Debug, Clone)] pub enum PasteImageError { ClipboardUnavailable(String), @@ -128,18 +147,8 @@ pub fn paste_image_to_temp_png() -> Result<(PathBuf, PastedImageInfo), PasteImag // First attempt: read image from system clipboard via arboard (native paths or image data). match paste_image_as_png() { Ok((png, info)) => { - // Create a unique temporary file with a .png suffix to avoid collisions. - let tmp = Builder::new() - .prefix("codex-clipboard-") - .suffix(".png") - .tempfile() + let path = persist_pasted_image(png.as_slice(), ".png") .map_err(|e| PasteImageError::IoError(e.to_string()))?; - std::fs::write(tmp.path(), &png) - .map_err(|e| PasteImageError::IoError(e.to_string()))?; - // Persist the file (so it remains after the handle is dropped) and return its PathBuf. - let (_file, path) = tmp - .keep() - .map_err(|e| PasteImageError::IoError(e.error.to_string()))?; Ok((path, info)) } Err(e) => {