Skip to content

fix(parser,rules): a class attribute that overflows the parse is cut and the page noticed, not the audit failed #1557 - #1558

Closed
nc9 wants to merge 1 commit into
mainfrom
fix/huge-class-parse-1557
Closed

nc9 wants to merge 1 commit into
mainfrom
fix/huge-class-parse-1557

Conversation

@nc9

@nc9 nc9 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Closes #1557.

Reproduced on e86767f (v0.0.159) and fixed here

  • Parser alone: parseHTML of a page with one class attribute of 900,000 repeated tokens throws RangeError: Maximum call stack size exceeded. Only class does: the same number of tokens in id, rel and sandbox parse, and an upper-case CLASS never reaches the setter. In linkedom 0.18.13 the only spreads on an attribute path during a parse are the two className setters (esm/interface/element.js:115 and esm/svg/element.js:35, both classList.add(...String(value).split(/\s+/))); an svg class overflows too. querySelectorAll over 700,000 matches (matches.push(...findAll(...))) does not overflow.
  • Real CLI audit (squirrel audit http://127.0.0.1:<port>/ --offline --passive --no-publish -m 10 -f json, a local three-page fixture: home, B, and one page with a 1,000,000-token class attribute followed by an image and a paragraph): before, exit 1 with Audit failed: Maximum call stack size exceeded. and no report. After, exit 0, 3 pages, and the report carries html-parse on the big page.

Change: a workaround in our parser wrapper, not an upstream linkedom change

packages/parser/src/dom.ts (parseHTML, the one place pages are parsed, so the crawl-time parsePage, the rules-time parseHtmlForRules, the streaming engine and the hosted worker all go through it):

  • A page that parses the first time is untouched: no pre-pass, no copy.
  • On a RangeError the page is parsed again with each class attribute of more than 20,000 tokens cut to its first 20,000. truncateClassAttributes is one linear pass over the tags, reading each tag as the HTML tokenizer does (a quote opens a value only at its start; an unquoted value ends at whitespace or >), and skipping comments and the text of script, style, textarea, title, xmp, plaintext, noembed, noframes and iframe, so no page text is edited. Everything after the big element reaches the rules (a marker after it is tested at parser and engine level).
  • A throw that is not that, or a second throw, leaves an empty document (what a browser builds for markup with nothing in it) rather than failing the audit.
  • Either way the document carries a notice (parseNotices(document)), copied into ParsedPage.parseNotices by parsePage and parseHtmlForRules, and crawl/html-size reports it as an html-parse warn check on that page ("The page's markup could not be read in full: a class attribute of 1,000,000 tokens was cut to the first 20,000 to read the page"). The existing rule carries it because a new rule would change the rule catalogue for a case that is rare; the page is listed in the report with its notice, not dropped.

Cost, measured (one process, Bun 1.4.3): a 4.8 MB page with a 2.4 million token class: 264 ms; a 10 MiB page: 426 ms; a 10 MiB page with no big class: 64 ms (first-time parse, same code path as before); a 10 MiB page of tags each with a 300-token class: 321 ms, no retry.

Hosted: the hosted worker uses the same engine parse, so it takes the same path through dom.ts; nothing hosted was run.

Differential: only a page that previously failed the audit is read differently (its class attribute keeps the first 20,000 tokens, which is enough for any real stylesheet selector use; a page with such an attribute is not a real page). Nothing else changes.

Tests (failing first)

  • packages/parser/tests/huge-class-1557.test.ts (12, fixtures built at runtime): 1,000,000 repeated tokens and 700,000 distinct ones parse with the elements after them and a notice, the first 20,000 distinct tokens kept in order; svg class, single quotes, upper-case CLASS (never overflowed, no notice) and an unquoted value; two oversized attributes; parsePage carries the notice and a normal page has none; a 30,000-token class is untouched. truncateClassAttributes: exactly the limit kept and one more cut, quotes and whitespace variants, valueless attributes and a slash before the class, not-a-tag cases (comment, script, style and textarea text, data-class, an end tag, a quoted lookalike), a script that mentions a tag followed by a real one, and 10 MiB with no big class in linear time. A throw that is not an overflow leaves an empty document with a notice.
  • packages/audit-engine/tests/huge-class-parse-1557.test.ts: through runStreamingRules, the engine path squirrel audit and the hosted worker run: a site with a 1,000,000-token page and a normal page completes; the notice is on the big page only; both pages are audited; an image without alt after the big element is found on it. On e86767f it fails with the RangeError.
  • packages/rules/tests/html-size-parse-notice-1557.test.ts (2): the warn check and its message, and nothing without notices.
  • Mutants (each restored from a clean copy): the cut threshold at >= and +1, the 2x-limit fast path made larger, the class-only test removed, comment skipping removed, text-element skipping removed, the single-quote branch, the valueless-attribute branch, the token limit raised, and both notices removed each fail a test. Equivalent survivors: the 2x-limit fast path set to 0 (an optimisation), and the RangeError check and the cut.length > 0 guard (without them the retry throws again into the same empty document, one more parse).

Existing parser, audit-engine and rules suites pass (attribute-case 44, empty-document 20, parse-depth 7, dom-text 16, the streaming and report goldens, golden-finding-messages, the 1332 parse-cap test); golden-baseline exits 70 with no output here on main too.

Upstream

Nothing was filed or sent to linkedom. An upstream-shaped minimal repro, for Nik to use if wanted:

import { parseHTML } from "linkedom";
const tokens = Array.from({ length: 700_000 }, () => "a").join(" ");
parseHTML(`<html><body><div class="${tokens}"></div></body></html>`);
// RangeError: Maximum call stack size exceeded
//   at set className (esm/interface/element.js:115): classList.add(...String(value).split(/\s+/))

Docs (docs/rules/crawl/html-size.mdx) and CHANGELOG (Unreleased, Fixed) updated. Skill change: none.

…and the page noticed, not the audit failed #1557

Signed-off-by: Nik Cubrilovic <[email protected]>
@claude

claude Bot commented Nov 2, 2026

Copy link
Copy Markdown

Reviewed the packages/parser/src/dom.ts change. I did not run the tests.

  • (medium) dom.ts parseContained: the empty-document fallback runs for any thrown error, not only the RangeError this fix targets. A genuine linkedom bug on some other input used to fail loudly and now becomes an audited-empty page. The html-size notice does say so, but consider narrowing the fallback to RangeError and rethrowing the rest, or at least logging it.
  • (low) truncateClassAttributes counts tokens with the HTML five-whitespace set, but linkedom splits on /\s+/, which also matches NBSP and other Unicode spaces. A class value of more than 640k tokens separated by such characters would be counted as one token and not cut. The retry would then throw again and the page would be audited empty. That is safe and bounded, but worth a test or a comment.
  • (low) The retry reparses the whole page, which is fine for the rare overflow case. Nothing in the diff bounds the work on a page with many large class attributes, though the single linear pass plus one reparse looks acceptable.
  • (nit) countTokens and the in-loop counting duplicate each other. The loop could count to the end once and record the cut offset.

The tests cover the parser, the engine and the html-size rule. The change is scoped well, the CHANGELOG and docs are updated, and I found no security concerns.

@nc9

nc9 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Shipped in v0.0.161. The commits were cherry-picked onto the release branch and rebase-merged: b7ea286. It shipped through #1574, which replaced the cutter with one driven by the tokenizer's own reading after the first version was found to differ from it on crafted markup.

Closing this PR since the change is on main.

nik

@nc9 nc9 closed this Nov 4, 2026
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.

A page with ~640K+ class tokens (1.2 MB attribute) overflows the linkedom parse and fails the whole audit

1 participant