Repository navigation
Conversation
…and the page noticed, not the audit failed #1557 Signed-off-by: Nik Cubrilovic <[email protected]>
|
Reviewed the
The tests cover the parser, the engine and the |
This was referenced Nov 2, 2026
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 |
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.
Closes #1557.
Reproduced on e86767f (v0.0.159) and fixed here
parseHTMLof a page with oneclassattribute of 900,000 repeated tokens throwsRangeError: Maximum call stack size exceeded. Onlyclassdoes: the same number of tokens inid,relandsandboxparse, and an upper-caseCLASSnever reaches the setter. In linkedom 0.18.13 the only spreads on an attribute path during a parse are the twoclassNamesetters (esm/interface/element.js:115andesm/svg/element.js:35, bothclassList.add(...String(value).split(/\s+/))); an svgclassoverflows too.querySelectorAllover 700,000 matches (matches.push(...findAll(...))) does not overflow.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 withAudit failed: Maximum call stack size exceeded.and no report. After, exit 0, 3 pages, and the report carrieshtml-parseon 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-timeparsePage, the rules-timeparseHtmlForRules, the streaming engine and the hosted worker all go through it):RangeErrorthe page is parsed again with eachclassattribute of more than 20,000 tokens cut to its first 20,000.truncateClassAttributesis 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).parseNotices(document)), copied intoParsedPage.parseNoticesbyparsePageandparseHtmlForRules, andcrawl/html-sizereports it as anhtml-parsewarn 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;parsePagecarries 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: throughrunStreamingRules, the engine pathsquirrel auditand 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.>=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 theRangeErrorcheck and thecut.length > 0guard (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-baselineexits 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:
Docs (
docs/rules/crawl/html-size.mdx) and CHANGELOG (Unreleased, Fixed) updated. Skill change: none.