Repository navigation
fix: deep templates overflowed the stack, and other hostile inputs - #136
Merged
Merged
Conversation
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.
jamesconwaydev
enabled auto-merge
November 21, 2026 01:42
foothillsdigital
approved these changes
Nov 21, 2026
foothillsdigital
left a comment
Collaborator
There was a problem hiding this comment.
Checked 28f5bae:
- The sync is exact. I diffed netweir
d2796e6..b1c8809against 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.rsandtokenizer.rs; the four core test files; and XPath'seval.rs,index.rs,lib.rsandtests/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.
- upstream/main:
- The bindings handle
EditError::InvalidNameconsistently. In the C API,parsec_attr_setgivesPARSEC_ERR_INVALIDandparsec_create_elementgivesPARSEC_NO_NODE, throughedit_status, as the regenerated header says. Python raisesValueError, naming the bad value. WASM'ssetAttributethrows. - Copies keep parsed names. Python's
buildand the twoparsec-csstest helpers copy parsed nodes withcreate_element_as_parsedandadd_attr_as_parsed, so a name likea"bsurvives 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.
This was referenced Nov 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Pages built to be hostile could crash Parsec or stall it:
<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.MAX_INPUT, because a NUL becomes three bytes. It then panicked, or read the wrong string.:has()with more than one compound, and nested:is().>wrote other markup.Change
Syncs Netweir b1c8809: netweir/netweir#358, #359, #360 and #361.
MatchOptionsgainsmax_steps, a step limit that needs no clock. Wiring it into the bindings is The bindings can't set a selector step limit #135.Resultand refuse bad names withEditError::InvalidName.create_element_as_parsedandadd_attr_as_parsedcopy a parsed node's names as they are, asimportNodedoes. The parser itself makes names likea"b.parsec_create_elementandparsec_attr_setreturnPARSEC_NO_NODEorPARSEC_ERR_INVALIDfor a refused name. Python raisesValueErrorand WASM throws. The Python binding copies parsed nodes with the new calls. The header is regenerated.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.-D warningsis clean for the workspace and both bindings, includingwasm32-unknown-unknown.<template>s parse.create_node('a b')raisesValueError.<a"b x=1>round-trips through a copy.wasm-pack test --nodepasses.