diff --git a/crates/io/src/svg.rs b/crates/io/src/svg.rs index aba7a9974a..692bcf8eda 100644 --- a/crates/io/src/svg.rs +++ b/crates/io/src/svg.rs @@ -597,15 +597,16 @@ struct SvgRefusals { depth: AtomicUsize, /// An image was refused for the chain depth or the byte budget. over_budget: AtomicBool, - /// craft-svg charged an image that couldn't be unpacked everything the byte budget had left: - /// later images refused for bytes are left out with it, not counted as the drawing's excess. - spent_by_unreadable: AtomicBool, + /// Bytes craft-svg charged images that couldn't be unpacked: it charges such an image + /// everything the byte budget had left, and so does a readable image that fits the budget + /// only without those charges. Neither is the drawing's content. + unread_charges: AtomicUsize, } /// `resolve`, also noting in `refusals` why craft-svg is about to refuse each embedded SVG. It /// repeats craft-svg's first two checks in their order: the chain depth, then unpacking within -/// what the byte budget has left (`text/plain` data is an SVG unless it is a raster craft-svg -/// draws). +/// the byte budget, less only what the drawing's own images used (`text/plain` data is an SVG +/// unless it is a raster craft-svg draws). fn noting_refusals( resolve: usvg::ImageHrefResolver<'static>, resolver: Arc, @@ -624,15 +625,16 @@ fn noting_refusals( if refusals.depth.load(Ordering::Relaxed) >= MAX_EMBEDDED_SVG_DEPTH { refusals.over_budget.store(true, Ordering::Relaxed); } else { - let left = usize::try_from(MAX_BOUNDED_SVG_BYTES).unwrap_or(usize::MAX).saturating_sub(resolver.usage().embedded_svg_bytes); - match craft_svg::svgz::text(&data, left) { - Ok(_) => {} - Err(craft_svg::svgz::Error::TooLarge(_)) => { - if !refusals.spent_by_unreadable.load(Ordering::Relaxed) { - refusals.over_budget.store(true, Ordering::Relaxed); - } + let budget = usize::try_from(MAX_BOUNDED_SVG_BYTES).unwrap_or(usize::MAX); + let used = resolver.usage().embedded_svg_bytes; + let left = budget.saturating_sub(used); + let content_left = budget.saturating_sub(used.saturating_sub(refusals.unread_charges.load(Ordering::Relaxed))); + match craft_svg::svgz::text(&data, content_left) { + Ok(text) if text.len() <= left => {} + Err(craft_svg::svgz::Error::TooLarge(_)) => refusals.over_budget.store(true, Ordering::Relaxed), + _ => { + refusals.unread_charges.fetch_add(left, Ordering::Relaxed); } - Err(_) => refusals.spent_by_unreadable.store(true, Ordering::Relaxed), } } refusals.depth.fetch_add(1, Ordering::Relaxed); diff --git a/crates/io/src/svg/tests.rs b/crates/io/src/svg/tests.rs index e7122f28f8..b1066556b6 100644 --- a/crates/io/src/svg/tests.rs +++ b/crates/io/src/svg/tests.rs @@ -1113,7 +1113,8 @@ fn a_chain_at_the_nesting_cap_beside_an_image_too_deep_in_xml_still_parses() { } /// An embedded SVGZ whose gzip stream is cut short is left out like any unreadable image, though -/// craft-svg charges it the rest of the byte budget; an image refused for bytes after it is too. +/// craft-svg charges it the rest of the byte budget. A small image refused for bytes after it is +/// left out too; one past the whole budget on its own still fails the drawing. #[test] fn a_damaged_embedded_svgz_is_left_out() { let svg = ""; @@ -1122,6 +1123,12 @@ fn a_damaged_embedded_svgz_is_left_out() { let after = embedded_image("image/svg+xml", svg.as_bytes()); let parsed = parse_bounded(image_svg(&format!("{damaged}{after}")).as_bytes(), MAX_RASTER_PIXELS).unwrap(); assert!(!parsed.tree.root().has_children(), "both images are left out"); + let mut huge = b"".to_vec(); + huge.resize(usize::try_from(MAX_BOUNDED_SVG_BYTES).unwrap() + 1024, b' '); + huge.extend_from_slice(b""); + let huge = embedded_image("image/svg+xml", &gzip(&huge)); + let error = parse_bounded(image_svg(&format!("{damaged}{huge}")).as_bytes(), MAX_RASTER_PIXELS).unwrap_err(); + assert!(error.to_string().contains("byte or nesting budget"), "{error}"); // A stored preview must draw completely, so it is still refused there. assert!(parse_bounded_work_with(image_svg(&damaged).as_bytes(), MAX_RASTER_PIXELS, true, fonts).is_err()); }