Skip to content

Hostile content: deep Markdown overflows the stack, and an SVG can read local files, block on a FIFO, inflate without a cap or ask for a huge raster; fixes are on a fork #587

Closed

Description

@Entrpi

Markdown from a model or a chat message, and an SVG from a web response, can abort the app or read the user's files. catch_unwind can't stop any of these.

1. Markdown: unbounded nesting overflows the stack

The parser keeps every open quote, list and item with no limit. The renderer, the plain-text and block-map walks, and the block tree's own drop all recurse a level or more for each nesting. So a reply of 100,000 nested > or - overflows the main thread's stack and aborts the app. That's a few hundred KB, which a broken or hostile model can send.

The parser has two other gaps:

  • Tables have no column limit: a 10,000-column table lays out every column, and takes 10.9 s.
  • A list item's copied indent (Flow::cols, a u8) overflows at 24 levels of nine-digit numbered lists. That is a panic in debug builds.

2. SVG: local files, a FIFO, and unbounded expansion

svg_rgba parses with usvg::Options::default():

  • Local files. usvg's href resolver reads any existing path in an <image href>. So an SVG can draw the user's local files into the app, and an href naming a FIFO blocks the decode thread in open() for good.
  • SVGZ. usvg inflates SVGZ to the end, with no cap.
  • Reference cycles. usvg 0.45's cycle check (fix_recursive_links) catches only cycles of one or two steps. Three clip paths in a ring recurse until the decode thread's 2 MiB stack overflows, and the app aborts.
  • Nesting. A thousand nested <svg> overflow roxmltree the same way.
  • Expansion. <use> fan-out, mask fan-out and per-vertex markers build millions of nodes inside usvg before anything can count them.
  • Raster size. resvg draws a pattern's tile at its own size, so a 20,000 px tile in an 8 px SVG asks for a 1.6 GB pixmap. The 4096 px clamp applies only to px == 0, so a url_set source listed at 20000w asks for a 20000 px pixmap.

Fix

Three commits on dev 14e3c5d, on branch safety-hostile-content of Entrpi/rune.

a1d5a017: move only. SpanMap and BlockMap move unchanged from markdown/parser.rs into markdown/map.rs, so the next commit keeps the parser under 800 lines.

c67d901e: Markdown caps, in markdown/limits.rs. The builder applies them, so whole documents and streams get the same parse.

  • Depth. A quote or list opened past 32 levels is not pushed. Its blocks go to the innermost open container, so the text stays, in order.
  • Columns. A table keeps its first 128 columns, the way GFM drops a row's cells past its header's.
  • Blocks. After 100,000 blocks, list items and table rows, the rest of the source is one paragraph of its text as written. An image that would lift into its own block counts too. The kit's largest test document (1 MB) has 28,832.
  • Indent. The copied indent saturates at 255 columns.
  • Timing. Release-build parse and first frame of each adversarial input take 2.4 to 9.2 ms. The tests assert a 2 s bound in debug builds.

463f9472: SVG bounds that Rune can own, in image/svg.rs.

  • Images. An <image> draws only data: hrefs. The opt-in is ImageCache::new(waker).svg_file_images(true), and even then only a regular file is read, never a FIFO or a device.
  • SVGZ inflates through a 64 MiB cap.
  • Nesting. A linear pass over the raw markup refuses more than 64 levels before roxmltree parses it. Entities count only when the body can reach them.
  • After usvg builds the tree, a 100,000-node budget and a pixel budget apply. The pixel budget counts each image's raster, each nested SVG's canvas and each pattern tile at its drawn size, in saturating f64.
  • Raster size. Every raster is clamped to 4096 px a side, url_set widths included.

Not fixed here: cycles and fan-outs inside usvg

A reference cycle among clip paths, masks, filters, gradients, patterns or markers is resolved inside usvg 0.45, before any Rune bound runs. Rune can't stop that without re-implementing usvg's reference resolution, and every spelling usvg accepts is another way around such a check:

  • whitespace in a quoted url(), and @import;
  • duplicate or SVG-namespaced IDs, and a namespaced href;
  • clip-path: inherit;
  • a <use> that moves an element out of a clip path;
  • a comment that splits a <style>.

A minimal case, which aborts the app on dev:

<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8">
  <clipPath id="a" clip-path="url(#b)"><rect width="8" height="8"/></clipPath>
  <clipPath id="b" clip-path="url(#c)"><rect width="8" height="8"/></clipPath>
  <clipPath id="c" clip-path="url(#a)"><rect width="8" height="8"/></clipPath>
  <rect width="8" height="8" clip-path="url(#a)"/>
</svg>

I ran 30 hostile and benign cases three ways: resvg 0.45.1, resvg git main at 500d8605a2, and this branch. Each case ran in its own process, on a 2 MiB decode thread, with a 20 s limit.

Cases resvg 0.45.1 resvg main This branch
9 clip-path rings, however spelled abort draw abort
a two-node gradient href loop, reached through a prefix hang draw hang
mask fan-out, marker chain, combinatorial selector, declaration fan-out, tref copies hang hang hang
<use> fan-out to 222,222 nodes, 100,000 px and u32::MAX rasters, 20,000 px pattern tile, 1000-deep nesting draw or allocate draw or allocate refused
SVGZ bomb inflates refused refused
4 ordinary drawings draw draw draw

So:

Tests

  • Markdown. markdown/limits_tests.rs parses each adversarial source, then draws a frame of it whole and one streaming. Against the previous parser:

    • the quote and list tests abort with a stack overflow;
    • the nine-digit list test panics;
    • the table test takes 10.9 s.
  • SVG. image/svg_tests.rs covers the bounds Rune owns, on a 2 MiB decode thread, so a regression aborts the test rather than passing. The tests cover:

    • data: images, and trusted local files;
    • a FIFO href, which returns at once;
    • a 65 MiB SVGZ bomb;
    • 1000-deep nesting, comment-hidden close tags and nesting from an entity;
    • a <use> fan-out of 222,222 nodes;
    • PNG headers at 100,000 px and at u32::MAX;
    • a 20,000 px pattern tile;
    • a 20000 px request through url_set.

    On dev's svg_rgba, each of them draws, hangs or returns Ok.

  • No false refusals. On 15,618 unique local SVGs, the branch draws every file plain usvg draws, and none takes notably longer. They are icon sets, editor and app assets, and Inkscape and Illustrator exports.

Each item has a changelog fragment.

On macOS (Apple silicon), the documented test command, cargo test -p rune -p rune-kit --features rune-kit/markdown --no-fail-fast, passes with this branch and the other three safety branches merged on dev 14e3c5d: 2,786 passed, 0 failed. Dev alone gives 2,749 passed, 0 failed. The four branches are safety-secure-fields, safety-hostile-content, safety-gpu-failures and safety-release-dev-surface; they merge cleanly except for one import line in image.rs. cargo check --workspace --all-targets and scripts/file-size.sh pass.

Filed together with #586, #588 and #589, the other safety fixes from the same review.

Activity

  1. Hona commented on Nov 23, 2026

    @Hona
    Member

    Each of these is fixed on dev in #1608 (e647aa9): SVGZ inflates through a 32 MiB cap; SVG rasters and pattern tiles stay within the 8192 px and 256 MiB image limits, and any requested size is clamped to 4096 px; <use>\ fan-out and nesting hidden in entities are refused; SVG image hrefs other than \data:\ URLs stay refused; file loads refuse anything that is not a regular file; GFM tables keep 128 columns; deep Markdown no longer overflows the stack in the commit flatten. Two things remain, because they happen inside usvg 0.45: reference cycles of three or more clip paths, masks or filters, and mask or marker fan-out. They need a usvg upgrade.

  2. Hona commented on Nov 23, 2026

    @Hona
    Member

    Follow-up on SVG <image> / <feImage> references: Rune keeps external file references refused with no opt-in, matching Google Chrome and the SVG specifications for SVGs used as images.

    • Chromium blocks all non-data: subresource requests inside an SVG image unconditionally (BaseFetchContext::CanRequestInternal in third_party/blink/renderer/core/loader/base_fetch_context.cc and IsolatedSVGDocumentHost::LocalFrameClient::GetURLLoaderFactory in third_party/blink/renderer/core/svg/graphics/isolated_svg_document_host.cc) and loads SVGImage with a null base URL (third_party/blink/renderer/core/svg/graphics/svg_image.cc), so even a sibling image file next to the SVG is never loaded.
    • W3C SVG 2 (§2.2–2.3) and SVG Integration (§2–3) require SVGs used as images to run in secure static or secure animated mode, where external references are disabled and only same-document #id and data: URLs resolve; WPT css/css-images/svg-images-are-ignored.html verifies this for a sibling PNG file.

    In Rune, svg_options in crates/rune-image/src/svg.rs already follows this rule (resolve_string returns None, and resolve_data accepts validated raster data: URLs only). Apps that need raster images inside an SVG should inline them as data: URLs.

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