diff --git a/crates/data/src/xml.rs b/crates/data/src/xml.rs index e4a8600fac..8c51ef4203 100644 --- a/crates/data/src/xml.rs +++ b/crates/data/src/xml.rs @@ -232,7 +232,7 @@ fn close(prefixes: &mut HashMap<&[u8], usize>, prefix: &[u8]) { } /// Whether a DOCTYPE declares an entity whose value holds markup (a `<`). -fn entity_with_markup(decl: &[u8]) -> bool { +pub fn entity_with_markup(decl: &[u8]) -> bool { let mut at = 0; while let Some(p) = decl.get(at..).and_then(|r| r.windows(8).position(|w| w == b"( *seen.borrow_mut().entry(p).or_insert_with(|| check(p)) }; // The names with an image refused so, by each refusal in `WHY`, in the order their notices go. - const WHY: [MarkerRefusal; 10] = [ + const WHY: [MarkerRefusal; 11] = [ MarkerRefusal::NotAnImage, MarkerRefusal::TooManyBytes, MarkerRefusal::TooManyPixels, MarkerRefusal::TooMuchToDraw, + MarkerRefusal::SvgTooMuchToRead, MarkerRefusal::SvgTooNested, MarkerRefusal::SvgUnknownRadius, MarkerRefusal::SvgTooSmall, @@ -2294,7 +2295,8 @@ pub(crate) fn picture_notices_by<'a>( ], )); } - let [not_images, too_long, too_large, too_much, too_nested, unknown_radius, too_small, not_utf8, filtered, past_run] = &bad; + let [not_images, too_long, too_large, too_much, too_much_to_read, too_nested, unknown_radius, too_small, not_utf8, filtered, past_run] = + &bad; if !not_images.is_empty() { notices.push(NoticeText::format( "Image markers in {names} aren't PNG, JPEG or SVG images and are drawn as circles", @@ -2321,6 +2323,10 @@ pub(crate) fn picture_notices_by<'a>( } // Each SVG's own cause (#2713), its figures as the renderer's phrase gives them. for (names, what) in [ + ( + too_much_to_read, + "Image markers in {names} are SVGs whose style sheets, element attributes or entities holding tags take more to read than RustGIS allows and are drawn as circles", + ), ( too_nested, "Image markers in {names} are SVGs with more than 40,000 XML nodes or nested deeper than 64 levels and are drawn as circles", diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index af5180eb7a..fe44a0bc11 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2747,6 +2747,10 @@ fn svg_image_markers_that_dont_draw_are_said_with_why() { "Image markers in \"Cities\", \"Rivers\" are SVGs with filters, text, images, animation or links to other files, which RustGIS doesn't draw in marker images, and are drawn as circles", ), (MarkerRefusal::TooManyBytes, "Image markers in \"Cities\", \"Rivers\" are longer than 2 MiB as base64 and are drawn as circles"), + ( + MarkerRefusal::SvgTooMuchToRead, + "Image markers in \"Cities\", \"Rivers\" are SVGs whose style sheets, element attributes or entities holding tags take more to read than RustGIS allows and are drawn as circles", + ), ( MarkerRefusal::SvgTooNested, "Image markers in \"Cities\", \"Rivers\" are SVGs with more than 40,000 XML nodes or nested deeper than 64 levels and are drawn as circles", diff --git a/crates/render/src/picture.rs b/crates/render/src/picture.rs index 0e643e1bf2..6bf4648622 100644 --- a/crates/render/src/picture.rs +++ b/crates/render/src/picture.rs @@ -81,6 +81,10 @@ pub enum MarkerRefusal { /// It is an SVG with more to draw than [`MARKER_PIXELS`] (in the units of the renderer's SVG /// count, `svg::work`), or more elements than `svg::ELEMENTS` (10,000). TooMuchToDraw, + /// It is an SVG whose style sheets and `style` attributes, or whose elements' attributes, take + /// more to read than `svg::STYLE_UNITS` and `svg::ATTRIBUTE_PAIRS` allow, or whose entities + /// hold tags, which no count sees. + SvgTooMuchToRead, /// It is an SVG with more XML nodes than `svg::XML_NODES` (40,000) or nested deeper than /// `svg::DEPTH` (64). SvgTooNested, @@ -136,6 +140,7 @@ refusal_phrases! { TooManyBytes => "is longer than 2 MiB as base64", TooManyPixels => "has more pixels than 2048 × 2048", TooMuchToDraw => "is an SVG with more to draw than a 2048 × 2048 image", + SvgTooMuchToRead => "is an SVG whose style sheets, element attributes or entities holding tags take more to read than RustGIS allows", SvgTooNested => "is an SVG with more than 40,000 XML nodes or nested deeper than 64 levels", SvgUnknownRadius => "is an SVG with a radius whose size RustGIS can't know (in em or ex, or in % of a size in em or ex, or of a nested size in % over 100%)", SvgTooSmall => "is an SVG too small to draw", @@ -371,6 +376,7 @@ impl From for MarkerRefusal { svg::Refusal::NotSvg => MarkerRefusal::NotAnImage, svg::Refusal::TooMuch => MarkerRefusal::TooMuchToDraw, svg::Refusal::PastRun => MarkerRefusal::PastRunPixels, + svg::Refusal::TooMuchToRead => MarkerRefusal::SvgTooMuchToRead, svg::Refusal::TooNested => MarkerRefusal::SvgTooNested, svg::Refusal::UnknownRadius => MarkerRefusal::SvgUnknownRadius, svg::Refusal::Undrawn => MarkerRefusal::SvgUndrawn, @@ -776,6 +782,7 @@ mod tests { TooManyBytes, TooManyPixels, TooMuchToDraw, + SvgTooMuchToRead, SvgTooNested, SvgUnknownRadius, SvgTooSmall, @@ -784,7 +791,7 @@ mod tests { PastRunPixels ] .map(MarkerRefusal::lasts), - [true, true, true, true, true, true, true, true, true, false] + [true, true, true, true, true, true, true, true, true, true, false] ); // A kept image the run's spent budget refuses is kept as it is; one refused for itself // isn't; one that draws is. @@ -903,6 +910,8 @@ mod tests { (long_png, TooManyBytes), (svg(&deep), SvgTooNested), (svg(&nodes), SvgTooNested), + // Its own cause (#2800), not "more to draw": 20,000 declarations in a `style` take too long to read. + (svg(&format!(r#""#, "fill: red; ".repeat(20_000))), SvgTooMuchToRead), (svg(r#""#), SvgUnknownRadius), (svg(r#""#), SvgUnknownRadius), // A radius in % of a size in em or ex with no view box, or of a nested one past 100%. diff --git a/crates/render/src/picture/svg.rs b/crates/render/src/picture/svg.rs index 2dc44450bf..196bc5525b 100644 --- a/crates/render/src/picture/svg.rs +++ b/crates/render/src/picture/svg.rs @@ -32,6 +32,10 @@ pub(super) enum Refusal { NotSvg, /// It has more to draw than the cap given, or more elements than [`ELEMENTS`]. TooMuch, + /// Its style sheets and `style` attributes come to more than [`STYLE_UNITS`], the attributes + /// of its elements to more than [`ATTRIBUTE_PAIRS`], or an entity of it holds a tag, whose + /// expansion no count sees: more to read than the readers do in time. + TooMuchToRead, /// It has more XML nodes than [`XML_NODES`], or nests deeper than [`DEPTH`]. TooNested, /// A shape's radius may be any size: in em or ex, font sizes, which no font is loaded to give, @@ -67,6 +71,21 @@ const ARC_CUBICS: f64 = 200.0; /// attribute's value matched costs a step each 64 bytes too, and each node walked back past to a /// sibling one. Icons take thousands of steps. pub(super) const CSS_STEPS: u64 = 4_000_000; +/// The most that reading an SVG's style sheets and `style` attributes may come to, in units of a +/// declaration scanned a byte ([`style_units`]): simplecss reads each declaration's value to an +/// end that fails (`consume_term`) and works out where that was by scanning the text from its +/// start, so a sheet costs its declarations times its length, and is read twice (here and by usvg). +/// Measured: 10,000 rules of 199 KB 0.64 s, 40,000 of 829 KB 10.5 s, one rule of 20,000 +/// declarations 1.4 s, but those are billions of units, which run faster a unit; at the bound's own +/// size, 40 million units took 46 ms to read whole, so ~1.2 ns a unit: at most ~20 ms. Icons come +/// to a few million. +pub(super) const STYLE_UNITS: u64 = 16_000_000; +/// The most that reading an SVG's tags may come to, in pairs of attributes of one element +/// ([`attribute_pairs`]): roxmltree checks each attribute of an element against those before it +/// for a duplicate, so an element's cost is the square of its attributes, and nothing else +/// limits them. Measured: 2,000 attributes on one element 2.7 ms, some 0.7 ns a pair, so at most +/// ~11 ms (4,000 attributes). Elements have a dozen. +pub(super) const ATTRIBUTE_PAIRS: u64 = 16_000_000; /// The most XML nodes (elements, text, comments) read before giving up, a bound on reading the /// text itself: [`ELEMENTS`] and the text and spaces between them. pub(super) const XML_NODES: u32 = 4 * ELEMENTS as u32; @@ -80,12 +99,22 @@ const NODE: u64 = 100; /// 695 ns a pixel, 100 stops 93 ns, 2 stops 21 ns), so each stop costs 1/16 of a unit a pixel. const STOPS_PER_UNIT: u64 = 16; -/// SVG text `text` read into a tree: within [`XML_NODES`], [`ELEMENTS`], [`POINTS`] and -/// [`CSS_STEPS`], its root an ``, with no images resolved and no files read. Matching its -/// style sheets ([`css_steps`]) stops at `cap` too, what the run has left, refused there for the -/// run ([`Refusal::PastRun`]) when below [`CSS_STEPS`]; `matched` is set to the steps it took, -/// refused or not, which its length doesn't bound, for the run to be charged them. +/// SVG text `text` read into a tree: within [`XML_NODES`], [`ELEMENTS`], [`POINTS`], [`CSS_STEPS`], +/// [`STYLE_UNITS`] and [`ATTRIBUTE_PAIRS`], its root an ``, with no images resolved and no +/// files read. Matching its style sheets ([`css_steps`]) stops at `cap` too, what the run has +/// left, refused there for the run ([`Refusal::PastRun`]) when below [`CSS_STEPS`]; `matched` is +/// set to the steps it took, refused or not, which its length doesn't bound, for the run to be +/// charged them. pub(super) fn read(text: &str, cap: u64, matched: &mut u64) -> Result { + // An entity of a tag expands to tags `attribute_pairs` doesn't see, once for each use (a text + // entity, as in an Illustrator file's `xmlns` values, is no way round it). + if rustgis_data::xml::entity_with_markup(text.as_bytes()) { + return Err(Refusal::TooMuchToRead); + } + // Before roxmltree checks each attribute of a tag against those before it. + if attribute_pairs(text) > ATTRIBUTE_PAIRS { + return Err(Refusal::TooMuchToRead); + } let xml = roxmltree::ParsingOptions { allow_dtd: true, nodes_limit: XML_NODES, ..Default::default() }; let doc = roxmltree::Document::parse_with_options(text, xml).map_err(|e| match e { roxmltree::Error::NodesLimitReached => Refusal::TooNested, @@ -101,6 +130,10 @@ pub(super) fn read(text: &str, cap: u64, matched: &mut u64) -> Result STYLE_UNITS { + return Err(Refusal::TooMuchToRead); + } with_sheet(&doc, |sheet| { let (_, by_sheet) = elements(&doc, sheet, cap, matched)?; if nested_markers(&doc, &by_sheet) { Err(Refusal::TooMuch) } else { Ok(()) } @@ -322,6 +355,45 @@ impl simplecss::Element for Stepped<'_, '_> { } } +/// What reading `doc`'s style sheets and `style` attributes comes to, in [`STYLE_UNITS`]: each one's +/// declarations (the `;` and `}` that end them, more than they are, never fewer) times its length, +/// all added up. +fn style_units(doc: &roxmltree::Document) -> u64 { + doc.descendants().map(style_attribute_units).fold(sheet_units(doc), u64::saturating_add) +} + +/// What reading `doc`'s style sheets comes to, in [`STYLE_UNITS`]. +fn sheet_units(doc: &roxmltree::Document) -> u64 { + doc.descendants() + .filter(|n| n.has_tag_name("style")) + .map(|n| { + let sheet = sheet_of(n); + (declarations(&sheet) + 1).saturating_mul(sheet.len() as u64) + }) + .fold(0, u64::saturating_add) +} + +/// The declarations of CSS `text`: the `;` and `}` that end them, more than they are, never fewer. +fn declarations(text: &str) -> u64 { + text.bytes().filter(|b| matches!(b, b';' | b'}')).count() as u64 +} + +/// What reading element `n`'s `style` attribute comes to ([`style_units`]), which usvg reads again +/// for each copy a `` makes of it ([`Counted::style`]). +fn style_attribute_units(n: roxmltree::Node) -> u64 { + n.attribute("style").map_or(0, |style| (declarations(style) + 1).saturating_mul(style.len() as u64)) +} + +/// What reading `text`'s tags comes to, in [`ATTRIBUTE_PAIRS`]: the square of the `=` in each piece +/// of it between `<`s, added up. A `<` isn't in a tag's attributes in XML that reads (roxmltree +/// stops at it before it checks them), so each tag is one piece, and an attribute is one `=` of it, +/// perhaps more (in a value or in the text after the tag): more than it is, never fewer. +fn attribute_pairs(text: &str) -> u64 { + text.split('<') + .map(|piece| piece.bytes().filter(|b| *b == b'=').count() as u64) + .fold(0u64, |pairs, n| pairs.saturating_add(n.saturating_mul(n))) +} + /// `f` of `doc`'s style sheets, read once as usvg reads them (`resolve_css`), every `