mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
svg: an embedded SVG past the whole byte budget fails after an unreadable one too
Review on #14385: once an unreadable embedded SVG had been charged the rest of the byte budget, every later image refused for bytes was left out, including one too large for the whole budget on its own. The bounded resolver now counts what craft-svg charged images it could not unpack (and readable ones those charges starved), and checks each image against the budget less only the drawing's own content. An image past that still fails a non-strict parse; one refused only because of those charges is left out.
This commit is contained in:
1 parent
6b28346f2b
commit
5abe121ef5
2 files changed
+23
-14
No files matched your search
+15
-13
@@ -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<craft_svg::tree::ImageResolver>,
|
||||
@@ -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);
|
||||
|
||||
@@ -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 = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"4\" height=\"4\"><rect width=\"4\" height=\"4\"/></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"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"4\" height=\"4\">".to_vec();
|
||||
huge.resize(usize::try_from(MAX_BOUNDED_SVG_BYTES).unwrap() + 1024, b' ');
|
||||
huge.extend_from_slice(b"</svg>");
|
||||
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());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user