Skip to content

parser: truncateClassAttributes reads markup differently from the tokenizer (take class offsets from htmlparser2 Tokenizer callbacks) #1569

Closed

Description

@nc9

Found in the independent review of #1558 (the fix for the class-attribute parse crash, #1557). truncateClassAttributes in packages/parser/src/dom.ts cuts an over-long class attribute with a hand-written reader, and that reader disagrees with the tokenizer linkedom uses (htmlparser2). The retry only runs after a first-parse RangeError, which an attacker can force with a class of about 640,000 tokens anywhere on the page, so the disagreement is reachable.

Measured (htmlparser2 10.1.0 as the oracle, limit 20; Chrome 156 as a second oracle)

Per 100,000 generated documents (planted victim shapes included): identical 77,288; CLASS or Class cut as well 1,898; over-limit class not cut 13,893; bogus-comment or processing-instruction text altered 4,007; real text changed 192; real elements lost 2,707. Without planted victims, 20,000 documents gave 5 elements lost and 36 text changed. Chrome as the oracle: 3,961 elements lost and 1,241 text changed per 100,000. On the real PR path at the true limit, a page with a planted victim shape lost an element in 19 of 800 documents.

Shapes (minimal):

  • A bogus-comment, processing-instruction or CDATA opener holding a fake tag whose quoted class value spans real markup, for example <?x <b class="[20001 tokens] > <img id=victim> "> (also <!x, <![CDATA[, <!<d class=', </<p class='): the tokenizer sees the real <img>, the cut removes it.
  • Abrupt comments <!--> and <!---> are treated as unclosed: a class after them is left uncut, and text inside <title>, <textarea>, <script>, <style> or <xmp> after them is cut.
  • Not cut at all: whitespace written as entities (&#32;, &Tab;), NBSP, VT, U+2003 and FEFF separators (linkedom splits on JavaScript \s, the truncator on five ASCII characters), class values inside <iframe>, <noembed>, <noframes> and <plaintext>.
  • CLASS and Class are cut though linkedom never feeds them to the class setter, and an ignored duplicate class attribute is cut and counted.

Acceptance Criteria

  • The offsets of each class value are taken from htmlparser2's Tokenizer callbacks (onattribname, onattribdata, onattribend), not from a hand-written reader, and the PR text no longer says the pass reads "as the HTML tokenizer does" unless that is true.
  • A differential test (at least 100,000 generated documents from the fragments above, with a small limit) shows the cut changes nothing except the value of an over-limit class attribute: no element lost or moved, no text changed, no other class cut.
  • Every shape above has its own named test.
  • The retry on a 10 MiB page stays linear (the cut pass alone is currently 11 to 18 ms with one big class and at most 117 ms across 13 hostile shapes).

nik

Activity

  1. added
    bug
    size: S
    rules
    on Nov 2, 2026
  2. added theissue type on Nov 2, 2026
  3. nc9 commented on Nov 4, 2026

    @nc9
    ContributorAuthor

    Acceptance check against v0.0.162 (7c31d5b). The rewrite shipped in v0.0.161: b576fa9, with 50b3a74 for entity-led tokens.

    # Criterion Result Evidence
    1 Class offsets come from htmlparser2's tokenizer callbacks, not a hand-written reader; the text no longer claims otherwise PASS ClassProbe in packages/parser/src/dom.ts extends htmlparser2's Parser (same options as linkedom) and takes offsets from onattribname, onattribdata, onattribentity and onattribend; the old reader is gone. The changelog and header comment describe the tokenizer-based cut.
    2 A differential test of at least 100,000 generated documents shows the cut changes only an over-limit class value FAIL (gap) class-cut-differential.test.ts is a table of 61 hand-written cases (63 tests in the file) compared with linkedom as the oracle; there is no generator and no 100,000-document run.
    3 Every shape above has its own named test PASS The table has a named case for each: bogus comment, processing instruction, CDATA, <!<d and </<p openers; <!--> and <!---> followed by a class and by title, textarea, script, style and xmp; whitespace as &#32; and &Tab;; NBSP, VT, U+2003 and U+FEFF separators; iframe, noembed, noframes and plaintext; CLASS and Class; an ignored duplicate class.
    4 The retry on a 10 MiB page stays linear PASS Measured here: truncateClassAttributes with one 700,000-token class, 10.2 MiB 81 to 97 ms, 15.2 MiB 120 to 123 ms, 25.2 MiB 194 to 200 ms; 10 MiB of the hostile <?x <b class=... shape 43 ms. The committed test only bounds it at 10 s ("the cost is linear: 10 MiB with no big class, and 10 MiB of hostile shapes").

    Ran: class-cut-differential.test.ts (63 pass), huge-class-1557.test.ts (12).

    Ticked 1, 3 and 4. Left open for 2: add the generated differential (a seeded generator over these fragments, 100,000 documents, a small limit), or amend the criterion to the table.

    nik

  4. nc9 commented on Nov 4, 2026

    @nc9
    ContributorAuthor

    Acceptance check against main 0080fc5 (tag v0.0.165). Boxes 1, 3 and 4 stay ticked from the earlier check.

    # Criterion Result Evidence
    2 A differential test of at least 100,000 generated documents with a small limit shows the cut changes only the value of an over-limit class PASS packages/parser/tests/class-cut-fuzz-1569.test.ts (commit 3716f45, in v0.0.165). DOCS = 100_000, LIMIT = 4, a seeded generator (mulberry32) joining 2 to 6 of 55 fragments that include every shape in the issue (bogus comment, processing instruction, CDATA, <!<d, </<p, <!-->, <!--->, raw-text elements, entity and non-ASCII separators, CLASS/Class, a duplicate class, unfinished tags). For each document it checks the cut counts against htmlparser2's own parse, that the text is unchanged when nothing is cut, and that after a cut the linkedom tree has the same elements, ids and text with only an over-limit class list shortened to its first tokens. It also asserts the generator reaches the cut path (more than a quarter of documents cut) and compares exact class lists on more than a fifth. Ran on Bun 1.4.3: 1 pass, 0 fail (3.7 s). Mutants, each reverted after its run: the cut dropping one extra character after the value, and the limit lowered by one, each make the test fail at document 0 ("tree differs").

    nik

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

    bugrulessize: S

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions