Skip to content

fix: deep templates overflowed the stack, and other hostile inputs - #136

Merged
jamesconwaydev merged 1 commit into
mainfrom
sync-security-fixes
Nov 21, 2026
Merged

jamesconwaydev merged 1 commit into
mainfrom
sync-security-fixes

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Problem

Pages built to be hostile could crash Parsec or stall it:

  • Nested <template>s at the end of input recursed once per template. On a 512 KB thread (Python's worker threads, macOS's default), 2,000 of them, about 20 KB of HTML, overflowed the stack. Nothing can catch a stack overflow, so it ended the process in every binding.
  • The parse buffer could pass 2^30 bytes on input under MAX_INPUT, because a NUL becomes three bytes. It then panicked, or read the wrong string.
  • Duplicate attributes were checked against every earlier attribute: 100k on one tag took seconds, and the time budget didn't fire inside the check.
  • Quadratic paths: end tags under deep nesting, :has() with more than one compound, and nested :is().
  • XPath panicked on a scope or index that didn't fit the document, and could run for tens of seconds before its work limit stopped it.
  • The edit API took any name, so a name with a space, quote or > wrote other markup.

Change

Syncs Netweir b1c8809: netweir/netweir#358, #359, #360 and #361.

  • Core and XPath: as above, with a regression test for each. MatchOptions gains max_steps, a step limit that needs no clock. Wiring it into the bindings is The bindings can't set a selector step limit #135.
  • New edit calls:
    • The checked edit calls now return Result and refuse bad names with EditError::InvalidName.
    • create_element_as_parsed and add_attr_as_parsed copy a parsed node's names as they are, as importNode does. The parser itself makes names like a"b.
  • Bindings: parsec_create_element and parsec_attr_set return PARSEC_NO_NODE or PARSEC_ERR_INVALID for a refused name. Python raises ValueError and WASM throws. The Python binding copies parsed nodes with the new calls. The header is regenerated.
  • README: the note that a fix for the template overflow was coming is replaced by what's now true.

Tests

  • cargo test --workspace (release): 347 test groups, 0 failed. html5lib tokenizer and tree-construction counts are unchanged, and so are the chunked-split checks.
  • Clippy -D warnings is clean for the workspace and both bindings, including wasm32-unknown-unknown.
  • Python: 42 passed, 1 skipped.
    • 100,000 nested <template>s parse.
    • create_node('a b') raises ValueError.
    • <a"b x=1> round-trips through a copy.
  • WASM: wasm-pack test --node passes.

Syncs Netweir b1c8809: netweir/netweir#358, #359, #360 and #361.

- Nested <template>s at the end of input no longer recurse, so a page
  can't overflow the stack; nothing else in the parser recurses on
  what a page holds either.
- A parse buffer that would pass 2^30 bytes no longer panics, the
  duplicate-attribute check is linear and keeps to the time budget,
  and end tags under deep nesting, :has() and nested :is() no longer
  take quadratic time. MatchOptions gains max_steps.
- XPath refuses a scope or index that doesn't fit instead of
  panicking, and its work limit counts more of the work.
- The edit API refuses element and attribute names that would write
  other markup, with EditError::InvalidName; create_element_as_parsed
  and add_attr_as_parsed copy parsed names as they are. The C, Python
  and WASM bindings return the refusal as PARSEC_ERR_INVALID,
  ValueError and an Error.

@foothillsdigital foothillsdigital left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked 28f5bae:

  • The sync is exact. I diffed netweir d2796e6..b1c8809 against this PR for every synced file, with names normalised. All 15 have identical changes: dom.rs (158 lines), tree.rs (386), css.rs, edit.rs, serialize.rs, shadow.rs and tokenizer.rs; the four core test files; and XPath's eval.rs, index.rs, lib.rs and tests/xpath.rs. The range is netweir#358 to #361, plus the rusqlite bump, which touches none of them.
  • The template overflow is fixed. A small program parsing "<template>".repeat(100_000) on a 512 KiB thread:
    • upstream/main: thread '<unknown>' has overflowed its stack, fatal runtime error: stack overflow, aborting;
    • this branch: parses.
  • The bindings handle EditError::InvalidName consistently. In the C API, parsec_attr_set gives PARSEC_ERR_INVALID and parsec_create_element gives PARSEC_NO_NODE, through edit_status, as the regenerated header says. Python raises ValueError, naming the bad value. WASM's setAttribute throws.
  • Copies keep parsed names. Python's build and the two parsec-css test helpers copy parsed nodes with create_element_as_parsed and add_attr_as_parsed, so a name like a"b survives a copy. Only new names from the user go through the checked calls.
  • Checks: parsec-capi's tests pass here, the C program included, and 35 CI checks pass with 2 still pending.

Approving.

@jamesconwaydev
jamesconwaydev added this pull request to the merge queue Nov 21, 2026
Merged via the queue into main with commit 0f6dba1 Nov 21, 2026
39 checks passed
@jamesconwaydev
jamesconwaydev deleted the sync-security-fixes branch November 21, 2026 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants