Skip to content

fix(image,markdown): untrusted SVG and Markdown stay within Rune's image limits and stack - #1608

Merged
Hona merged 1 commit into
devfrom
fix/587-hostile-svg-markdown
Nov 23, 2026
Merged

Hona merged 1 commit into
devfrom
fix/587-hostile-svg-markdown

Conversation

@Hona

@Hona Hona commented Nov 23, 2026

Copy link
Copy Markdown
Member

Fixes #587

Untrusted Markdown and SVG now stay inside the image limits Rune already has (MAX_SIDE = 8192 px, MAX_ALLOC = 256 MiB in crates/rune-image/src/lib.rs), and inside the stack.

What was wrong, and what changed

Defect on dev Root cause Fix
Deep Markdown overflows the stack The parser already limits nesting to 511 elements (#1122), but the commit flatten in crates/rune/src/runtime/commit/flatten.rs recursed once for each element with no stack check, so 2,000 nested > overflowed a 2 MiB thread flatten_owned/flatten_ref run inside rune_taffy::maybe_grow, the same stack continuation layout uses already (it does nothing on wasm)
Wide GFM tables No column limit A table keeps its first 128 columns in the parser and in the pending header (MAX_TABLE_COLUMNS). Cells past column 128 are dropped, as GFM drops cells past the header
SVGZ inflates with no cap usvg::decompress_svgz reads to the end and checks the size only afterwards New crates/rune-image/src/gzip.rs: a gzip/DEFLATE decoder that stops at 32 MiB, never reserves past that limit, and checks the CRC. It adds no dependency
Huge raster svg_rgba clamped px only when it was 0, so a 20000w url_set entry asked for a 20000 px pixmap Any px is clamped to 4096. Before resvg::render, a walk of the resolved tree enforces MAX_SIDE on each pattern tile and isolated layer, and MAX_ALLOC on the total of canvas, tiles, layers, masks, clips, nested SVGs and embedded images. It also enforces a limit of 100,000 nodes
<use> fan-out usvg expands <use> before anything can count A memoised count over the roxmltree document refuses more than 100,000 expanded nodes before Tree::from_xmltree
Nesting through entities The depth scan skipped DTD entities The scan expands only the entities that the body reaches, with roxmltree's own loop limits
Local files, FIFO, device The resolve_string resolver already returned None (#1188). The data resolver did not check raster headers, and ImageCache::file/url/font_file called std::fs::read on any path data: rasters go through header::check (the #1559 limits). File loads go through read_regular_file, which refuses anything that is not a regular file (directories, FIFOs, sockets, devices) before and after open, and stops reading at MAX_ALLOC

The SVG code moves out of lib.rs into crates/rune-image/src/svg.rs. The public svg_rgba and svg_rgba_cover keep their signatures.

Refused by default

  • An SVG <image> or <feImage> href that is not a data: URL, which includes files beside the SVG, absolute paths and UNC paths. No opt-in is added: if an app needs SVGs that reference files beside them, that is the owner's decision.
  • data: URLs of type image/svg+xml (nested SVG documents), as before.

Not fixed here

  • Reference cycles of three or more clip paths, masks or filters, and some fan-outs inside usvg 0.45 (mask fan-out, marker chains). usvg resolves these before any Rune check runs. They need a usvg upgrade, which this PR does not do because it adds or bumps no dependency.

Tests

All hostile inputs are built inside the tests. The SVGZ bomb is about 420 KiB of fixed-Huffman deflate that would inflate to 65 MiB. The tests create no FIFO or device file. The path check is tested with a directory and, on Windows, \\.\NUL.

  • crates/rune-image/src/svg_tests.rs:
    • SVGZ fixed, dynamic and header-field streams, the bomb, and a bad CRC
    • a 20000 px request and a 20000w url_set entry
    • huge and scaled pattern tiles
    • data: PNG headers of 100,000 px and u32::MAX
    • 1,000-deep, comment-hidden and entity-hidden nesting
    • linear and exponential <use> fan-out
    • a local file href that draws nothing
  • crates/rune-kit/src/markdown/tests.rs: a 500-column table and a pending table header are capped at 128 columns, and 2,000 nested quotes, deep lists and nine-digit lists render a frame. Before this fix the table kept 500 columns and the quotes overflowed the stack.

@Hona
Hona merged commit e647aa9 into dev Nov 23, 2026
1 check passed
@Hona
Hona deleted the fix/587-hostile-svg-markdown branch November 23, 2026 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant