From 04b312312087ed79213ef8f40abcc44eee97af29 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 11:41:45 -0800 Subject: [PATCH 1/2] Clipboard: paste checks the size of another app's data before copying it Paste takes at most 64 MiB of a format another app copied, VectorCraft's existing limit for copied files, and reads the size before the copy: - On Windows, each registered format, the CF_DIBV5 and CF_DIB bitmaps (together), CF_UNICODETEXT and the CF_HDROP file list. A larger format is skipped and the next one is tried. - On macOS, each pasteboard type's data length is checked before it is copied. - A copied file list of more than 1,024 files pastes no file, and no file in it is opened, on every platform, as the Linux text/uri-list reader already does. On Windows the CF_HDROP names are counted before they are read. --- apps/vectorcraft/src/clipboard.rs | 281 ++++++++++++++++++++++++++---- docs/target-app-parity.md | 1 + 2 files changed, 244 insertions(+), 38 deletions(-) diff --git a/apps/vectorcraft/src/clipboard.rs b/apps/vectorcraft/src/clipboard.rs index ec6256a114..4ed77b2e05 100644 --- a/apps/vectorcraft/src/clipboard.rs +++ b/apps/vectorcraft/src/clipboard.rs @@ -157,6 +157,10 @@ fn copied_file(paths: Vec, mimes: &[&'static str]) -> Option { /// [`copied_file`] with a file of more than `limit` bytes skipped. fn copied_file_within(paths: Vec, mimes: &[&'static str], limit: usize) -> Option { + // A list of more than MAX_URIS files (untrusted data) names none, before any file is looked at. + if paths.len() > MAX_URIS { + return None; + } paths.into_iter().find_map(|path| { // text/uri-list lines end in CRLF (RFC 2483), but arboard splits them on LF only. let path = match path.to_str() { @@ -197,6 +201,65 @@ fn read_rest(file: impl Read, mut data: Vec, limit: usize) -> Option (data.len() <= limit).then_some(data) } +/// The data `copy` takes off the clipboard, called only when `size` (what the clipboard reports +/// before the copy) is known and from 1 to `limit` bytes. `None` without a call otherwise, and +/// when the copy comes to more than `limit` bytes. +#[cfg_attr(not(any(windows, target_os = "macos")), allow(dead_code))] +fn copy_within(size: Option, limit: usize, copy: impl FnOnce() -> Option>) -> Option> { + size.filter(|&n| n > 0 && n <= limit)?; + copy().filter(|data| data.len() <= limit) +} + +/// The copies of `ids`' data, in order, [`copy_within`] what is left of `limit` bytes once the +/// copies before them are taken: an id whose data doesn't fit is skipped and the next one is tried. +#[cfg_attr(not(windows), allow(dead_code))] +fn copies_within( + ids: impl IntoIterator, + limit: usize, + mut size: impl FnMut(T) -> Option, + mut copy: impl FnMut(T) -> Option>, +) -> Vec> { + let mut left = limit; + ids.into_iter() + .filter_map(|id| { + let data = copy_within(size(id), left, || copy(id))?; + left = left.saturating_sub(data.len()); + Some(data) + }) + .collect() +} + +/// Whether a Windows file list (`CF_HDROP`: a `DROPFILES` header, then file names each ended by a +/// NUL and the list by one more, in UTF-16 when the header's `fWide` is set) names at most `limit` +/// files. Counting stops at the first name past `limit`. A block too short for its header, or +/// whose names would start past its end, is not within. +#[cfg_attr(not(windows), allow(dead_code))] +fn drop_files_within(block: &[u8], limit: usize) -> bool { + let field = |at: usize| Some(u32::from_le_bytes(block.get(at..at.checked_add(4)?)?.try_into().ok()?)); + let (Some(start), Some(wide)) = (field(0), field(16)) else { + return false; + }; + let Some(names) = usize::try_from(start).ok().and_then(|start| block.get(start..)) else { + return false; + }; + let (mut count, mut in_name) = (0usize, false); + for unit in names.chunks_exact(if wide != 0 { 2 } else { 1 }) { + if unit.iter().all(|&b| b == 0) { + if !in_name { + break; + } + in_name = false; + } else if !in_name { + in_name = true; + count += 1; + if count > limit { + return false; + } + } + } + true +} + /// The most URIs a copied file list may hold, VectorCraft's own limit: a longer list (untrusted /// data) names no files. const MAX_URIS: usize = 1024; @@ -278,12 +341,13 @@ fn file_uri_path(uri: &str) -> Option { #[cfg(windows)] mod win { - use std::num::NonZeroU32; + use std::num::{NonZeroU32, NonZeroUsize}; use clipboard_win::{formats, options::NoClear, raw}; use super::{ - BITMAP, ClipboardRead, Cursor, Flavour, PNG, SVG, SystemClipboard, TEXT, copied_file, decode_png, dib_image, png_flavour, windows_formats, + BITMAP, ClipboardRead, Cursor, Flavour, MAX_BYTES, MAX_URIS, PNG, SVG, SystemClipboard, TEXT, copied_file, copies_within, copy_within, + decode_png, dib_image, drop_files_within, png_flavour, windows_formats, }; /// The device-independent bitmaps, the one that carries alpha first. Each is there whenever the @@ -370,8 +434,11 @@ mod win { let mut files = vec![]; { let _open = clipboard_win::Clipboard::new_attempts(10).ok()?; - // None copied when the clipboard holds no file list. - let _ = raw::get_file_list_path(&mut files); + // None copied when the clipboard holds no file list, or one of more than MAX_BYTES + // or MAX_URIS files: the list is counted before its names are copied. + if copy_registered(formats::CF_HDROP, MAX_BYTES).is_some_and(|block| drop_files_within(&block, MAX_URIS)) { + let _ = raw::get_file_list_path(&mut files); + } } // The files are read with the clipboard closed again, not keeping other apps waiting. copied_file(files, mimes).map(ClipboardRead::Now).or_else(|| { @@ -390,44 +457,64 @@ mod win { } } - /// Read the first registered format of `mime` the clipboard holds into `data`. - fn read_registered(mime: &str, data: &mut Vec) -> bool { - formats_of(mime).filter(|id| raw::is_format_avail(*id)).any(|id| { - data.clear(); - raw::get_vec(id, data).is_ok() && !data.is_empty() + /// The size the clipboard reports for format `id`, while it is open. + fn size_of(id: u32) -> Option { + raw::size(id).map(NonZeroUsize::get) + } + + /// The data of format `id`, while the clipboard is open, copied only when its size is at most + /// `limit` bytes ([`copy_within`]). + fn copy_registered(id: u32, limit: usize) -> Option> { + copy_within(size_of(id), limit, || { + let mut data = vec![]; + raw::get_vec(id, &mut data).ok()?; + Some(data) }) } + /// The first registered format of `mime` the clipboard holds that is not empty and at most + /// [`MAX_BYTES`]; a larger one is skipped and the next tried. + fn read_registered(mime: &str) -> Option> { + formats_of(mime).filter(|id| raw::is_format_avail(*id)).find_map(|id| copy_registered(id, MAX_BYTES).filter(|data| !data.is_empty())) + } + /// A device-independent bitmap as a PNG flavour. fn dib_png(dib: &[u8]) -> Option { png_flavour(&dib_image(dib)?) } /// Copy both bitmap formats while the clipboard is open, then decode them in preference order. - pub(super) fn read_dibs(mut get: impl FnMut(u32) -> Option>) -> Option { - let dibs: Vec<_> = DIBS.into_iter().filter_map(&mut get).collect(); + /// `size` gives each format's size before `get` copies it, and the two copies come to at most + /// [`MAX_BYTES`] together ([`copies_within`]). + pub(super) fn read_dibs(size: impl FnMut(u32) -> Option, get: impl FnMut(u32) -> Option>) -> Option { + let dibs = copies_within(DIBS, MAX_BYTES, size, get); (!dibs.is_empty()).then_some(ClipboardRead::Later(Box::new(move || dibs.iter().find_map(|dib| dib_png(dib))))) } /// One flavour, while the clipboard is open. A device-independent bitmap is copied here, and - /// the work that comes back decodes each captured format in preference order. + /// the work that comes back decodes each captured format in preference order. Text whose + /// UTF-16 block is larger than [`MAX_BYTES`], or comes to more as UTF-8, is not read. fn read_one(mime: &'static str) -> Option { - let mut data = vec![]; - let mime = match mime { - BITMAP if read_registered(PNG, &mut data) => PNG, - BITMAP => { - return read_dibs(|id| { - let mut dib = vec![]; - raw::get_vec(id, &mut dib).ok()?; - Some(dib) - }); - } + let (mime, mut data) = match mime { + BITMAP => match read_registered(PNG) { + Some(png) => (PNG, png), + None => { + return read_dibs(size_of, |id| { + let mut dib = vec![]; + raw::get_vec(id, &mut dib).ok()?; + Some(dib) + }); + } + }, TEXT => { - raw::get_string(&mut data).ok()?; - TEXT + let text = copy_within(size_of(formats::CF_UNICODETEXT), MAX_BYTES, || { + let mut text = vec![]; + raw::get_string(&mut text).ok()?; + Some(text) + })?; + (TEXT, text) } - m if read_registered(m, &mut data) => m, - _ => return None, + m => (m, read_registered(m)?), }; // Memory blocks can be longer than the data: SVG text ends at its first NUL. if mime == SVG @@ -3092,7 +3179,7 @@ mod mac { use objc2_app_kit::NSPasteboard; use objc2_foundation::{NSArray, NSString}; - use super::{Flavour, pasteboard_types}; + use super::{Flavour, MAX_BYTES, copy_within, pasteboard_types}; /// Does the pasteboard hold one of `mimes` ([`pasteboard_types`])? Asks for its types only. pub fn has(mimes: &[&'static str]) -> bool { @@ -3103,13 +3190,16 @@ mod mac { }) } - /// The pasteboard's data for `mime`, the first of its types held ([`pasteboard_types`]). + /// The pasteboard's data for `mime`, the first of its types held ([`pasteboard_types`]) that is + /// not empty and at most [`MAX_BYTES`]: its length is checked before it is copied, and a larger + /// type is skipped and the next tried. pub fn read(mime: &str) -> Option { autoreleasepool(|_| { let pasteboard = NSPasteboard::generalPasteboard(); pasteboard_types(mime).iter().find_map(|&(t, mime)| { - let data = pasteboard.dataForType(&NSString::from_str(t))?.to_vec(); - (!data.is_empty()).then_some(Flavour { mime, data }) + let held = pasteboard.dataForType(&NSString::from_str(t))?; + let data = copy_within(Some(held.len()), MAX_BYTES, || Some(held.to_vec()))?; + Some(Flavour { mime, data }) }) }) } @@ -3296,6 +3386,105 @@ mod tests { assert!(uri_list_paths(list(MAX_URIS + 1).as_bytes()).is_empty()); } + /// A copied file list of more than 1,024 files pastes none, before any file is looked at, + /// wherever the list came from; 1,024 files paste the first that is art. + #[test] + fn a_copied_file_list_longer_than_the_limit_pastes_no_file() { + let d = dir("many"); + let art = d.path().join("art.svg"); + std::fs::write(&art, "").unwrap(); + let missing = |n: usize| (0..n).map(|i| d.path().join(format!("gone{i}.png"))).collect::>(); + let last = [missing(MAX_URIS - 1), vec![art.clone()]].concat(); + assert_eq!(copied_file(last, &PASTE_ORDER).map(|f| f.mime), Some(SVG)); + // The art first: no file is opened past the count. + let first = [vec![art.clone()], missing(MAX_URIS)].concat(); + assert!(copied_file(first, &PASTE_ORDER).is_none()); + assert!(copied_file([missing(MAX_URIS), vec![art]].concat(), &PASTE_ORDER).is_none()); + } + + /// Clipboard data is copied only when the size the clipboard gives is from 1 byte to the limit, + /// and the copy is not asked for otherwise. + #[test] + fn clipboard_data_is_copied_only_when_its_size_fits() { + let copy = |size: Option, got: usize| { + let mut asked = false; + let data = copy_within(size, 8, || { + asked = true; + Some(vec![1; got]) + }); + (data.map(|d| d.len()), asked) + }; + assert_eq!(copy(Some(8), 8), (Some(8), true)); + assert_eq!(copy(Some(1), 1), (Some(1), true)); + for size in [None, Some(0), Some(9), Some(usize::MAX)] { + assert_eq!(copy(size, 1), (None, false), "{size:?}"); + } + // Data that comes to more than the limit after all is refused. + assert_eq!(copy(Some(8), 9), (None, true)); + assert_eq!(MAX_BYTES, 64 << 20); + } + + /// Formats copied together come to the limit at most: one that doesn't fit what is left is + /// skipped without a copy and the next one is tried. + #[test] + fn formats_copied_together_share_the_limit() { + let sizes = [5, 4, 2, 0]; + let mut asked = vec![]; + let copies = copies_within( + 0..sizes.len(), + 8, + |i| sizes.get(i).copied(), + |i| { + asked.push(i); + Some(vec![0; sizes[i]]) + }, + ); + assert_eq!(copies.iter().map(Vec::len).collect::>(), [5, 2]); + assert_eq!(asked, [0, 2]); + assert!(copies_within([0], 8, |_| None, |_| Some(vec![0])).is_empty()); + } + + /// A Windows file list (CF_HDROP) of `names`: a DROPFILES header, the names in UTF-16 or one + /// byte a character, each ended by a NUL, and one more NUL. + fn drop_files(names: &[&str], wide: bool) -> Vec { + let mut block = vec![]; + block.extend(20u32.to_le_bytes()); + block.extend([0u8; 12]); + block.extend(u32::from(wide).to_le_bytes()); + for name in names.iter().copied().chain([""]) { + for unit in name.encode_utf16().chain([0]) { + if wide { + block.extend(unit.to_le_bytes()); + } else { + block.push(unit as u8); + } + } + } + block + } + + #[test] + fn a_windows_file_list_is_counted_before_its_names_are_read() { + let names = |n: usize| (0..n).map(|i| format!("C:\\art\\{i}.svg")).collect::>(); + for wide in [true, false] { + let list = |n: usize| drop_files(&names(n).iter().map(String::as_str).collect::>(), wide); + assert!(drop_files_within(&list(MAX_URIS), MAX_URIS), "{wide}"); + assert!(!drop_files_within(&list(MAX_URIS + 1), MAX_URIS), "{wide}"); + assert!(drop_files_within(&list(0), MAX_URIS), "{wide}"); + // Names past the list's closing NUL are not counted. + let mut ended = list(2); + ended.extend(list(MAX_URIS + 1).get(20..).unwrap()); + assert!(drop_files_within(&ended, 2), "{wide}"); + // A list cut short counts the names it holds. + assert!(!drop_files_within(&list(3)[..list(3).len() - 4], 2), "{wide}"); + } + // A header cut short, or names that would start past the block's end. + assert!(!drop_files_within(&drop_files(&["a"], true)[..19], MAX_URIS)); + let mut past = drop_files(&["a"], true); + past[0..4].copy_from_slice(&u32::MAX.to_le_bytes()); + assert!(!drop_files_within(&past, MAX_URIS)); + } + #[test] fn gnome_files_list_names_its_files_after_copy_or_cut() { let paths = |list: &[u8]| gnome_copied_paths(list).into_iter().map(|p| p.to_string_lossy().into_owned()).collect::>(); @@ -3349,19 +3538,35 @@ mod tests { let valid = dib(40, 1, 1, 32, 0, &[], &bgra(&[RED])); let mut captured = Vec::new(); - let read = win::read_dibs(|id| { - captured.push(id); - match id { - CF_DIBV5 => Some(vec![40, 0, 0]), - CF_DIB => Some(valid.clone()), - _ => None, - } - }) + let get = |id: u32| match id { + CF_DIBV5 => Some(vec![40, 0, 0]), + CF_DIB => Some(valid.clone()), + _ => None, + }; + let read = win::read_dibs( + |id| get(id).map(|d| d.len()), + |id| { + captured.push(id); + get(id) + }, + ) .unwrap(); assert_eq!(captured, [CF_DIBV5, CF_DIB]); let flavour = read.flavour().unwrap(); assert_eq!(flavour.mime, PNG); assert_eq!(pixels(&decode_png(&flavour.data).unwrap()), [RED]); + // A bitmap larger than the limit is not copied, and the other one still pastes. + captured.clear(); + let read = win::read_dibs( + |id| if id == CF_DIBV5 { Some(MAX_BYTES + 1) } else { get(id).map(|d| d.len()) }, + |id| { + captured.push(id); + get(id) + }, + ) + .unwrap(); + assert_eq!(captured, [CF_DIB]); + assert_eq!(read.flavour().map(|f| f.mime), Some(PNG)); } #[test] diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 65cbf9e216..c83a9e8379 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1979,6 +1979,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Import and paste, 2026-10-13: a multi-artboard `.ai` with notes beside its artboards opens from its editing data, a page's art past its artboard no longer lying over the page before it (#1528); a `.ai` gradient whose stops are listed upwards keeps each midpoint on the stop that starts its part of the ramp (#1530); SVG text on a path follows basic shapes (circle, ellipse, rect, line, polyline, polygon) as their SVG 2 equivalent paths (#1531, fixes #1527); PDFs whose alpha soft masks are drawn as images import with the mask instead of empty art (#1551); EPS and PostScript type set in Type 42 (TrueType) fonts imports at its size instead of 1000 times too large (#1553); PDF and EPS import keep a clipping path whose bounding box covers the page but whose shape doesn't (#1544, fixes #1542). Opening or placing a file with an image too large to decode says so, and the Links panel shows it on the row (#1540, part of #1479). Paste takes a PDF or EPS another app copied as editable vector art: from the macOS pasteboard (with SVG), EPS on Windows beside PDF, and `.eps` files copied in a file manager, with the new command `clipboard.importEps` (#1538, fixes #1499). - A `.ai` or `.ait` file copied in a file manager pastes as its art: one that holds a PDF pastes the objects of its editing data, and one saved as PostScript pastes as a copied `.eps` file does. - Paste checks the size of a file copied in a file manager before reading it: a file of more than 64 MiB, VectorCraft's own limit, is skipped and the next copied file that is art pastes. A file that grows past the limit while it is read is skipped too, after at most one byte more than the limit is read. + - Paste checks the size of the data another app copied before copying it, and takes at most 64 MiB of a format, VectorCraft's own limit: on Windows each registered format, the two bitmap formats (CF_DIBV5 and CF_DIB, together), text (both its UTF-16 data and the UTF-8 text made from it) and the file list (CF_HDROP), and on macOS each pasteboard type. A larger format is skipped and the next one pastes. A list of more than 1,024 copied files pastes no file and no file in it is opened, as on the Linux clipboard; on Windows the names are counted before they are read (`clipboard::tests::clipboard_data_is_copied_only_when_its_size_fits`, `formats_copied_together_share_the_limit`, `a_copied_file_list_longer_than_the_limit_pastes_no_file`, `a_windows_file_list_is_counted_before_its_names_are_read`; not yet run on Windows or macOS). - A copied `.ai` or `.ait` file that holds a PDF pastes the objects of its editing data as File › Open reads them: every layer's objects go into the active layer in their stacking order, with their names, hidden objects and type objects, instead of the PDF page's art with its type as outlines. When the editing data can't be read, the file pastes its page as before (`clipboard.importPdf {editingData}`). The objects of a hidden layer come in hidden, from a pasted EPS too. - Import and export, later 2026-10-13 merges: SVGs that move objects with a CSS transform (`translate(100px, 0)`, `rotate(45deg)`) or style type with the `font` shorthand import as drawn (#1565); SVG symbols whose shapes use percentage lengths import at the size and place they are drawn, the percentages being of the symbol's `viewBox` (#1596); Inkscape's flowed text (`flowRoot`) opens as area type in its rectangle, a paragraph per `flowPara`, instead of being dropped, and a region that can't be read is left out with a warning (#1569, fixes #1568); the warning for an unsupported SVG filter names the filter, its primitives and the object, and says the object is drawn without it (#1599, fixes #1598); the pages of an Inkscape multi-page SVG become artboards, named by their labels (#1603, fixes #1601); SVG export with CSS classes or style entities stays fast in documents with many distinct styles (#1567); PDF and `.ai` import: evenly letter-spaced or tightened type on a path keeps its length along the path instead of closing up to the font's advances (#1586), and type on a path whose stroke is written glyph by glyph comes in as one text object with its stroke, not one object per glyph (#1585); the ICC profiles embedded in JPEG, TIFF and PSD exports have every tag on a 4-byte boundary (ICC.1 §7.1.2), so strict readers accept them (#1589, fixes #1584); GIF, PNG-8 and other indexed exports find each pixel's palette colour through an index instead of a scan, with identical output (#1576); a zero-length subpath draws its round cap as a dot and its projecting cap as a square, so dots in imported `.ai` and SVG files show (#1619, fixes #1613). - Files and untrusted input, 2026-10-14 merges: EPS import: a stitched shading keeps its hard colour changes (#1654, #1653). WMF/EMF import refuses an oversized embedded PNG or JPEG from its header, before any pixels are decoded (#1776). PDF open no longer takes exponential time on a crafted name tree with shared nodes when it looks for the editing data (#1781). The size limits on a native file's embedded data also apply to files read from memory, not only streamed ones (#1783). Saving over a private file on Unix no longer leaves a readable temporary copy beside it while the file is written (#1778). An Export for Screens suffix can no longer place files outside the export folder (#1777). Package refuses a folder where it would overwrite the open document or one of its linked files (#1786). ruzstd 0.9.1 fixes a decompression bomb (RUSTSEC-2026-0344) reachable from EPS/`.ai` and Affinity import (#1807). File Info dates are stamped only after a save succeeds, so a failed save no longer changes them (#1774). Linked files that change during a drag are updated after the drag instead of being missed (#1773). From f66aa6508350ca676414aba0defbbc9d634bd6a5 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 12:06:56 -0800 Subject: [PATCH 2/2] Clipboard: Windows text is bounded before it is converted, and each bitmap format has its own limit The size of a CF_UNICODETEXT block is checked against the most UTF-8 it could come to (3 bytes a code unit), so the converted text is never larger than the limit. CF_DIBV5 and CF_DIB are each copied when they fit the limit, so a large first format that doesn't decode still leaves the second to paste. --- apps/vectorcraft/src/clipboard.rs | 80 +++++++++++++++---------------- docs/target-app-parity.md | 2 +- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/apps/vectorcraft/src/clipboard.rs b/apps/vectorcraft/src/clipboard.rs index 4ed77b2e05..cf42079691 100644 --- a/apps/vectorcraft/src/clipboard.rs +++ b/apps/vectorcraft/src/clipboard.rs @@ -210,23 +210,11 @@ fn copy_within(size: Option, limit: usize, copy: impl FnOnce() -> Option< copy().filter(|data| data.len() <= limit) } -/// The copies of `ids`' data, in order, [`copy_within`] what is left of `limit` bytes once the -/// copies before them are taken: an id whose data doesn't fit is skipped and the next one is tried. +/// The most bytes of UTF-8 that `utf16` bytes of UTF-16 text come to: a code unit (2 bytes) is at +/// most 3 bytes of UTF-8, and a surrogate pair (4 bytes) is 4. #[cfg_attr(not(windows), allow(dead_code))] -fn copies_within( - ids: impl IntoIterator, - limit: usize, - mut size: impl FnMut(T) -> Option, - mut copy: impl FnMut(T) -> Option>, -) -> Vec> { - let mut left = limit; - ids.into_iter() - .filter_map(|id| { - let data = copy_within(size(id), left, || copy(id))?; - left = left.saturating_sub(data.len()); - Some(data) - }) - .collect() +fn utf8_most(utf16: usize) -> usize { + utf16.div_ceil(2).saturating_mul(3) } /// Whether a Windows file list (`CF_HDROP`: a `DROPFILES` header, then file names each ended by a @@ -346,8 +334,8 @@ mod win { use clipboard_win::{formats, options::NoClear, raw}; use super::{ - BITMAP, ClipboardRead, Cursor, Flavour, MAX_BYTES, MAX_URIS, PNG, SVG, SystemClipboard, TEXT, copied_file, copies_within, copy_within, - decode_png, dib_image, drop_files_within, png_flavour, windows_formats, + BITMAP, ClipboardRead, Cursor, Flavour, MAX_BYTES, MAX_URIS, PNG, SVG, SystemClipboard, TEXT, copied_file, copy_within, decode_png, + dib_image, drop_files_within, png_flavour, utf8_most, windows_formats, }; /// The device-independent bitmaps, the one that carries alpha first. Each is there whenever the @@ -484,16 +472,16 @@ mod win { } /// Copy both bitmap formats while the clipboard is open, then decode them in preference order. - /// `size` gives each format's size before `get` copies it, and the two copies come to at most - /// [`MAX_BYTES`] together ([`copies_within`]). - pub(super) fn read_dibs(size: impl FnMut(u32) -> Option, get: impl FnMut(u32) -> Option>) -> Option { - let dibs = copies_within(DIBS, MAX_BYTES, size, get); + /// `size` gives each format's size before `get` copies it, and a format of more than + /// [`MAX_BYTES`] is not copied ([`copy_within`]) and the next one is tried. + pub(super) fn read_dibs(mut size: impl FnMut(u32) -> Option, mut get: impl FnMut(u32) -> Option>) -> Option { + let dibs: Vec<_> = DIBS.into_iter().filter_map(|id| copy_within(size(id), MAX_BYTES, || get(id))).collect(); (!dibs.is_empty()).then_some(ClipboardRead::Later(Box::new(move || dibs.iter().find_map(|dib| dib_png(dib))))) } /// One flavour, while the clipboard is open. A device-independent bitmap is copied here, and /// the work that comes back decodes each captured format in preference order. Text whose - /// UTF-16 block is larger than [`MAX_BYTES`], or comes to more as UTF-8, is not read. + /// UTF-16 block could come to more than [`MAX_BYTES`] as UTF-8 ([`utf8_most`]) is not read. fn read_one(mime: &'static str) -> Option { let (mime, mut data) = match mime { BITMAP => match read_registered(PNG) { @@ -507,7 +495,7 @@ mod win { } }, TEXT => { - let text = copy_within(size_of(formats::CF_UNICODETEXT), MAX_BYTES, || { + let text = copy_within(size_of(formats::CF_UNICODETEXT).map(utf8_most), MAX_BYTES, || { let mut text = vec![]; raw::get_string(&mut text).ok()?; Some(text) @@ -3424,24 +3412,21 @@ mod tests { assert_eq!(MAX_BYTES, 64 << 20); } - /// Formats copied together come to the limit at most: one that doesn't fit what is left is - /// skipped without a copy and the next one is tried. + /// The UTF-8 that UTF-16 text comes to is bounded from the UTF-16 block's size alone, before + /// the text is converted: a block that could pass the limit as UTF-8 is not read. #[test] - fn formats_copied_together_share_the_limit() { - let sizes = [5, 4, 2, 0]; - let mut asked = vec![]; - let copies = copies_within( - 0..sizes.len(), - 8, - |i| sizes.get(i).copied(), - |i| { - asked.push(i); - Some(vec![0; sizes[i]]) - }, - ); - assert_eq!(copies.iter().map(Vec::len).collect::>(), [5, 2]); - assert_eq!(asked, [0, 2]); - assert!(copies_within([0], 8, |_| None, |_| Some(vec![0])).is_empty()); + fn text_is_bounded_by_what_its_utf8_could_come_to() { + for text in ["", "a", "\u{20ac}\u{20ac}\u{20ac}", "\u{1f600}", "a\u{20ac}\u{1f600}\u{ffff}"] { + let utf16 = text.encode_utf16().count() * 2; + assert!(utf8_most(utf16) >= text.len(), "{text:?}"); + } + // Every code unit at 3 bytes of UTF-8: the bound is exact. + assert_eq!(utf8_most("\u{20ac}\u{20ac}".encode_utf16().count() * 2), "\u{20ac}\u{20ac}".len()); + let copied = |utf16: usize| copy_within(Some(utf8_most(utf16)), MAX_BYTES, || Some(vec![])).is_some(); + assert!(copied(MAX_BYTES / 3 * 2)); + assert!(!copied(MAX_BYTES / 3 * 2 + 2)); + assert!(!copied(MAX_BYTES)); + assert_eq!(utf8_most(usize::MAX), usize::MAX); } /// A Windows file list (CF_HDROP) of `names`: a DROPFILES header, the names in UTF-16 or one @@ -3567,6 +3552,19 @@ mod tests { .unwrap(); assert_eq!(captured, [CF_DIB]); assert_eq!(read.flavour().map(|f| f.mime), Some(PNG)); + // Each format has the limit to itself: a first one at the limit that doesn't decode + // leaves the second to paste. + captured.clear(); + let read = win::read_dibs( + |id| if id == CF_DIBV5 { Some(MAX_BYTES) } else { get(id).map(|d| d.len()) }, + |id| { + captured.push(id); + get(id) + }, + ) + .unwrap(); + assert_eq!(captured, [CF_DIBV5, CF_DIB]); + assert_eq!(read.flavour().map(|f| f.mime), Some(PNG)); } #[test] diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index e306204afd..26fcc3fbbd 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1982,7 +1982,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Import and paste, 2026-10-13: a multi-artboard `.ai` with notes beside its artboards opens from its editing data, a page's art past its artboard no longer lying over the page before it (#1528); a `.ai` gradient whose stops are listed upwards keeps each midpoint on the stop that starts its part of the ramp (#1530); SVG text on a path follows basic shapes (circle, ellipse, rect, line, polyline, polygon) as their SVG 2 equivalent paths (#1531, fixes #1527); PDFs whose alpha soft masks are drawn as images import with the mask instead of empty art (#1551); EPS and PostScript type set in Type 42 (TrueType) fonts imports at its size instead of 1000 times too large (#1553); PDF and EPS import keep a clipping path whose bounding box covers the page but whose shape doesn't (#1544, fixes #1542). Opening or placing a file with an image too large to decode says so, and the Links panel shows it on the row (#1540, part of #1479). Paste takes a PDF or EPS another app copied as editable vector art: from the macOS pasteboard (with SVG), EPS on Windows beside PDF, and `.eps` files copied in a file manager, with the new command `clipboard.importEps` (#1538, fixes #1499). - A `.ai` or `.ait` file copied in a file manager pastes as its art: one that holds a PDF pastes the objects of its editing data, and one saved as PostScript pastes as a copied `.eps` file does. - Paste checks the size of a file copied in a file manager before reading it: a file of more than 64 MiB, VectorCraft's own limit, is skipped and the next copied file that is art pastes. A file that grows past the limit while it is read is skipped too, after at most one byte more than the limit is read. - - Paste checks the size of the data another app copied before copying it, and takes at most 64 MiB of a format, VectorCraft's own limit: on Windows each registered format, the two bitmap formats (CF_DIBV5 and CF_DIB, together), text (both its UTF-16 data and the UTF-8 text made from it) and the file list (CF_HDROP), and on macOS each pasteboard type. A larger format is skipped and the next one pastes. A list of more than 1,024 copied files pastes no file and no file in it is opened, as on the Linux clipboard; on Windows the names are counted before they are read (`clipboard::tests::clipboard_data_is_copied_only_when_its_size_fits`, `formats_copied_together_share_the_limit`, `a_copied_file_list_longer_than_the_limit_pastes_no_file`, `a_windows_file_list_is_counted_before_its_names_are_read`; not yet run on Windows or macOS). + - Paste checks the size of the data another app copied before copying it, and takes at most 64 MiB of a format, VectorCraft's own limit: on Windows each registered format, each of the two bitmap formats (CF_DIBV5 and CF_DIB), text (the most UTF-8 its UTF-16 data could come to, checked before it is converted) and the file list (CF_HDROP), and on macOS each pasteboard type. A larger format is skipped and the next one pastes. A list of more than 1,024 copied files pastes no file and no file in it is opened, as on the Linux clipboard; on Windows the names are counted before they are read (`clipboard::tests::clipboard_data_is_copied_only_when_its_size_fits`, `text_is_bounded_by_what_its_utf8_could_come_to`, `a_copied_file_list_longer_than_the_limit_pastes_no_file`, `a_windows_file_list_is_counted_before_its_names_are_read`; not yet run on Windows or macOS). - A copied `.ai` or `.ait` file that holds a PDF pastes the objects of its editing data as File › Open reads them: every layer's objects go into the active layer in their stacking order, with their names, hidden objects and type objects, instead of the PDF page's art with its type as outlines. When the editing data can't be read, the file pastes its page as before (`clipboard.importPdf {editingData}`). The objects of a hidden layer come in hidden, from a pasted EPS too. - Import and export, later 2026-10-13 merges: SVGs that move objects with a CSS transform (`translate(100px, 0)`, `rotate(45deg)`) or style type with the `font` shorthand import as drawn (#1565); SVG symbols whose shapes use percentage lengths import at the size and place they are drawn, the percentages being of the symbol's `viewBox` (#1596); Inkscape's flowed text (`flowRoot`) opens as area type in its rectangle, a paragraph per `flowPara`, instead of being dropped, and a region that can't be read is left out with a warning (#1569, fixes #1568); the warning for an unsupported SVG filter names the filter, its primitives and the object, and says the object is drawn without it (#1599, fixes #1598); the pages of an Inkscape multi-page SVG become artboards, named by their labels (#1603, fixes #1601); SVG export with CSS classes or style entities stays fast in documents with many distinct styles (#1567); PDF and `.ai` import: evenly letter-spaced or tightened type on a path keeps its length along the path instead of closing up to the font's advances (#1586), and type on a path whose stroke is written glyph by glyph comes in as one text object with its stroke, not one object per glyph (#1585); the ICC profiles embedded in JPEG, TIFF and PSD exports have every tag on a 4-byte boundary (ICC.1 §7.1.2), so strict readers accept them (#1589, fixes #1584); GIF, PNG-8 and other indexed exports find each pixel's palette colour through an index instead of a scan, with identical output (#1576); a zero-length subpath draws its round cap as a dot and its projecting cap as a square, so dots in imported `.ai` and SVG files show (#1619, fixes #1613). - Files and untrusted input, 2026-10-14 merges: EPS import: a stitched shading keeps its hard colour changes (#1654, #1653). WMF/EMF import refuses an oversized embedded PNG or JPEG from its header, before any pixels are decoded (#1776). PDF open no longer takes exponential time on a crafted name tree with shared nodes when it looks for the editing data (#1781). The size limits on a native file's embedded data also apply to files read from memory, not only streamed ones (#1783). Saving over a private file on Unix no longer leaves a readable temporary copy beside it while the file is written (#1778). An Export for Screens suffix can no longer place files outside the export folder (#1777). Package refuses a folder where it would overwrite the open document or one of its linked files (#1786). ruzstd 0.9.1 fixes a decompression bomb (RUSTSEC-2026-0344) reachable from EPS/`.ai` and Affinity import (#1807). File Info dates are stamped only after a save succeeds, so a failed save no longer changes them (#1774). Linked files that change during a drag are updated after the drag instead of being missed (#1773).