Skip to content

SVG picture markers: tighter reading bounds and precise refusal phrases (follow-up to #2704) #2713

Done

Description

@jgregoriorincon

What

#2704 (part E1a of #2266) bounds SVG marker images before they are read and drawn. Its review raised bounds that can be tighter against crafted SVGs, and refusal phrases that name the wrong cause. They were left out of that PR to keep it to its behaviour, and are collected here.

Bounds on reading (crates/render/src/picture/svg.rs)

  • Style sheets on many elements. The reader tries each rule of a <style> on each element (usvg's parse_svg_element), so a large sheet on many elements takes as long as both. Nothing bounds that product today.
  • Marker points on every vertex. Each marker's elements count on every vertex it can sit on, but its points don't. A long path inside a marker on a shape of many vertices passes the point cap.
  • Markers counted where they are used. Today every <marker> counts on every vertex of the document, referenced or not. An icon with an unused marker and about 5,000 points is refused. Counting only markers a shape names (marker-start, marker-mid, marker-end), on that shape's points, is both tighter and fairer.
  • Gradient stops. tiny-skia goes through all of a gradient's stops at every pixel, but the cost counts a gradient the same whatever its stops.
  • One arc's curves. An arc may come to 1,000 cubic curves. svgtypes moves each across all those still to go, which is quadratic, so a lower cap (about 200) bounds reading more tightly.
  • Polyline numbers. points_of splits a polyline on non-digits, so 12.345,67.891 counts four runs and a polyline about twice its points. The doc's example (1e5 one number) is wrong too. Count numbers as svgtypes reads them.
  • Plain rectangles. A <rect> without rounded corners counts 0 points; usvg reads it as 5.
  • Radii in %. A radius in %, em or ex is refused, as it may resolve to any size. A % radius could be resolved against the root view box and counted at that size.
  • CSS escapes. @\69mport or \75rl( in a style sheet isn't matched by the @import and url( checks. The reader never fetches an import, and simplecss 0.2.2 doesn't decode escapes, so catching them means decoding the sheet first.

Refusal phrases (crates/render/src/picture.rs)

  • The 1.5 MiB byte cap, the XML node and depth limits, and a radius in %, em or ex all say "is an SVG with more to draw than a 2048 × 2048 image".
  • An SVG too small to scale, one that isn't drawn, and one that isn't UTF-8 say it "isn't a PNG, JPEG or SVG RustGIS reads".

Each should say its own cause.

Related

Activity

  1. jgregoriorincon commented on Nov 22, 2026

    @jgregoriorincon
    ContributorAuthor

    More from the review of #2704 (rounds 3 to 5):

    • a marker's elements are multiplied by every point in the document (vertices in elements), an unused marker's too; rename the binding and its comment's wording along with counting a marker only where a shape names it;
    • a test that draws hostile geometry (coordinates near ±3e38, huge stroke widths and miter limits, tiny dashes, extreme transforms) through svg::draw; checked by hand on d6cacc7, no panic;
    • quadratic curves count 2 points where usvg builds 3;
    • a zero radius in %, em or ex is refused instead of taken as zero;
    • a <use>'s href is trimmed in undrawn but not in count;
    • comments and docs: store()'s "80 ms" under the lock leaves out SVG reading and drawing; cartography.md's "(more where it says it is larger)" reads as past 700 px; wraps in svg.rs's module and undrawn docs and in cartography.md.
  2. jgregoriorincon commented on Nov 22, 2026

    @jgregoriorincon
    ContributorAuthor

    From round 6 of #2704: cartography.md:100 says an SVG given by command is "of at most 2048 × 2048 pixels", which holds for PNG and JPEG only; svg.rs:21 "animation (the still image is drawn)" reads as if animation were drawn; elsewhere lowercases every attribute value to search it, a copy of up to 1.5 MiB; the wall-clock bounds (under 1 s) in the SVG tests; the picker's unreadable-image notice names PNG and JPEG only (part E1b).

  3. jgregoriorincon commented on Nov 22, 2026

    @jgregoriorincon
    ContributorAuthor

    From round 7 of #2704: the points_of comment's sentence about a polyline's runs of digits is garbled; long lines in cartography.md and workspace.md; the test a_marker_is_a_png_or_a_jpeg and its comment predate SVG; MarkerRefusal's public docs link private svg items (private_intra_doc_links); with two markers, a style sheet styling only a class like .marker-dot is refused, as any sheet naming markers is.

  4. jgregoriorincon commented on Nov 23, 2026

    @jgregoriorincon
    ContributorAuthor

    From the last review of #2704: count reuses a subtree it memoized at a shallow depth when a <use> reaches it deeper, without checking its depth again, so usvg's own max_depth refuses such an SVG, said with "isn't a PNG, JPEG or SVG" rather than "more to draw".

  5. jgregoriorincon commented on Nov 23, 2026

    @jgregoriorincon
    ContributorAuthor

    I'll take this, in two PRs: (1) the reading bounds in crates/render/src/picture/svg.rs (markers counted only where a shape names them, style sheets × elements, gradient stops, a lower arc cap, polyline numbers, quadratic points, % radii against the view box and zero radii, the memoized depth); (2) the refusal phrases, one per cause, with the doc wording points from the comments. CSS escapes need no change: nothing is fetched and simplecss doesn't decode them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions