Skip to content

feat(rules): perf/content-in-js-bundle finds page content compiled into a JS chunk (#1200) - #1307

Closed
nc9 wants to merge 8 commits into
mainfrom
feat/content-in-js-bundle-1200
Closed

nc9 wants to merge 8 commits into
mainfrom
feat/content-in-js-bundle-1200

Conversation

@nc9

@nc9 nc9 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1200.

Depends on #1304 (PR1, #1143). This branch contains the PR1 commit so it builds and tests on its own; the diff shows PR1's files until #1304 merges, then I rebase onto main and the diff is only the second commit (14ba8a08). Target is main, not the PR1 branch, so the squash-merge of PR1 cannot close this PR.

What

New site-scope rule perf/content-in-js-bundle: a same-origin JavaScript chunk (50 KB or more) that contains the text of the site's pages (compiled MDX or Markdown bodies, inlined CMS JSON), so the content downloads twice.

  • shared/content-in-script.ts (extends PR1's shared analyzer set): extractContentSentences(doc) keeps up to 5 distinctive sentences per page (single text nodes of 10+ words, longest first, from main/article/[role=main] or the body outside nav/header/footer/aside/form; code blocks skipped); normalizeScriptText decodes a chunk once (JS escapes \" \' \n \t \xNN \uNNNN \u{N}, entities named/decimal/hex, whitespace collapse) in one linear pass; countSentencesIn; estimateProseShare; gzipSize (CompressionStream, only for flagged chunks).
  • Page-time collection: CollectedPageSignal.contentSentences (bounded: 5 x 200 chars), built while the DOM is live; v1 reads site.pages documents. A crawl whose records predate the field reports skipped, not clean.
  • Rule: a page's content is inside a chunk when 3+ of its sentences are found (min_sentences). Sentences that open 3+ pages' main content are template text and dropped. Chunk with a loading page's own content: warning content-in-js-bundle. Also holds at least 2 other pages' content (escalate_other_pages): failure content-in-js-bundle-sitewide. Items give chunk URL, size, gzip size, pages with content, other pages, estimated prose share.
  • Chunks come from the existing script fetches (ctx.site.scripts, decoded once per audit, same 5 MB / timeout limits). Further bounds: chunk text over 8 MB is reported as not searched, 10 largest chunks, 300 pages per chunk (loading pages first, then an even sample), 2.5 s wall-clock for the whole search (reports partial). Measured: normalising 5 MB takes about 33 ms and 1500 absent needles against it about 300 ms.
  • No finding for: content only in HTML (inline hydration state and JSON-LD are inline scripts, never searched), chunks under 50 KB, nav/footer text, template text, unfetched chunks.

Four-file recipe and counts

Rule + registry (performance/index.ts), docs page docs/rules/perf/content-in-js-bundle.mdx + docs.json nav + generated hub table, perf count 31 to 32 (docs/rules/index.mdx, docs/rules/performance/index.mdx), constants.ts comment 311 to 312 (PUBLIC_RULE_COUNT_FLOOR 300 unchanged), canonical golden: perRuleTally 315 to 316 and findings +1 (103744 to 103745: the fixture has no chunk large enough, so one pass check), CHANGELOG bullet under Unreleased. A site rule needs no template-invariance-measured.json entry (rule-verdict-scope passes).

PRIVATE follow-up at release (not done here): regenerate the rule catalog, bump totalRules in apps/web/src/components/rules.tsx, bump the gitlink. Total rule count after PR1 and PR2: previous + 2.

Tests

packages/rules/tests/content-in-js-bundle-1200.test.ts (40): sentence extraction (chrome excluded, short text, inline-split text, code blocks, 60k-node page bounded); decoder per escape form (JSX/JS quotes, \n/\t/line continuation, \u \u{} \x, named/numeric entities, unknown sequences, over-cap); prose share; and for BOTH engines (resident and streaming collected signals): own content flagged with chunk, sizes and prose share; sitewide escalation counting other pages; match through quotes, \n/\t, \u, entities (one test each); hydration-only negative; chunk under 50 KB; nav/footer-only text; template text; unfetched chunk; huge chunk bounded and reported; 400 pages x 1.5 MB chunk within the cap; no scripts; old records skipped.

Verified / not

Verified: tsgo (rules), make validate, the new test, rule-verdict-scope, template-verdict-page-independence, template-verdict-inputs, docs-category-table, meta-string-limits, docs-rule-pages, the canonical golden, streaming-collector/pre-rules/template-cluster-key goldens, rule-time-budget, ws4-rule-batches. A real squirrel audit (dev entry, --offline --http) of a local 8-page site whose 110 KB entry chunk holds every page's compiled text reports the sitewide failure through the streaming engine with the chunk URL, size and all 9 pages (the fixture serves / and /p0.html as two pages). Not verified: the full scripts/test-suites.ts --run rules suite (left to CI), a smoke run over real MDX-on-Vite sites (none to hand; fixtures are synthetic), and gzip size in the cloud runtime (CompressionStream is used where present, omitted otherwise).

AC check: matching through JSX-escaped quotes, \n/\u escapes and entities has a test each; fetching reuses the existing script fetch; the bounded-chunk test is above.

Skill: no change needed.

Caps on untrusted input (scan follow-up: "denial-of-service in content-in-script.ts")

An automated scan flagged this file. Review found one real quadratic: the entity decode did indexOf(";") and the \u{ decode indexOf("}") from every & or \u{, each scanning to the end of the chunk when no terminator exists, so 5 MB of & was about 25 trillion steps. Fixed in c9be9b67:

  • Decoder (normalizeScriptText): one pass, fixed lookahead (an entity is looked for within 10 characters, \u{ hex within 8 digits), output into one preallocated Uint16Array (2 bytes per input character, no per-escape string allocation), chunks over 8 MB of text refused unread. No regex runs over a chunk.
  • Matcher (findNeedles): replaces one includes per needle per page (worst case O(chunk x needle length) each, and one call per page) with ONE pass using a rolling hash of 24-character windows against the needles' prefix hashes. Time is O(chunk) regardless of the number of pages or sentences; a hash hit is verified with startsWith at most 100,000 times per chunk, so a haystack or a needle set built to collide cannot force more; exits early when every needle is found; checks a deadline every 65,536 positions.
  • Extraction: one text node is read to its first 4,096 characters, 5,000 candidates and 100,000 nodes per page, 5 sentences of at most 200 characters kept, iterative walk (no recursion).
  • Rule: 10 largest chunks, 5,000 pages (max_pages_checked, hard max 20,000), 2.5 s wall-clock for the whole search with a visible partial note, prose-share estimate linear.
  • Tests: tests/content-in-script-adversarial-1200.test.ts (22): decoder on 5 MB of &, &#, \u{, \u{abcdef, backslashes, escape runs, quote/escape alternation, brackets, whitespace and astral entities (ceiling 4 s, observed tens of ms), a doubling test (4x input must stay under 10x time, best of three) that a quadratic decoder fails, counters for the matcher (positions never exceed the haystack, 0 verifications for 25,000 non-matching needles, verifications capped at 100,000 for a prefix-repeating haystack and for 20,000 needles sharing a prefix, early exit, past deadline stops within the first check), extraction of a 5 MB text node, 200,000 sibling nodes, 3,000-deep nesting and 20,000 paragraphs, and the rule over 200 chunks of 100 KB. Run three times locally with no flake; the structural limits use counters, only the ceilings use the clock.

Review round (g6)

  • Nested landmarks: only outermost main/article/[role=main] are walked and identical sentences are deduped before the top five, so <main><article> keeps its five (test).
  • Browser-rendered pages are not judged (their copy is in the script that rendered them): skipped in both engines (test).
  • Severity: a chunk with one page's copy is now info, a chunk that also holds at least two other pages' content is a warning (no failure) until measured. Smoke run, public sites only: 40 documentation and framework sites at 12 pages each (react.dev, nextjs.org, vuejs.org, vitejs.dev, astro, svelte, remix, tailwind, a few personal blogs, docusaurus, nuxt, tanstack, trpc, shadcn, mdxjs, mantine, chakra, rsbuild, deno, bun, hono, zod, tauri, typescriptlang, MDN, eleventy, gatsby, express, redux, vitest, playwright, pnpm, prettier, eslint, react-router, prisma, drizzle). 36 audited (4 stopped at a start URL that redirects to another site, the Start URL redirects (bit.ly etc.): resolve the chain and audit the destination, with consent for cross-domain (--follow-redirect / prompt); today it scores the shortener #1281 behaviour working as designed; 1 failed to start). 4 of 36 reported the single-page finding (react.dev, docusaurus.io, tanstack.com query, pnpm.io): one or two pages' copy in a 300 KB to 1.2 MB route chunk, prose share 0 to 11%, i.e. hard-coded route components, hence info. 0 of 36 reported the sitewide finding. I had no site with an eager MDX glob to confirm a true sitewide positive on a real site; that case is covered by the synthetic fixtures and a local site only. Reports are not in the repo.
  • Reporting: a verification-cap stop is reported with its own reason (not "stopped after 2.5 s"); a chunk the audit does not have (not fetched, failed with no size, skipped, over the 5 MB script fetch cap) gives an info content-in-js-bundle-unchecked check and never a clean pass; the docs no longer present the 8 MB decode limit as reachable (the 5 MB fetch cap comes first; 8 MB is a backstop).
  • Event loop: the deadline is checked before each chunk's decode and hash, the rule yields between chunks, and the default max_pages_checked is now 2,000 (max 20,000).
  • fix(parser,rules): empty-body pages and hostile ids no longer throw rule errors #1270 #1276 #1290: documentBody is used here and covers the empty-document case; fix(parser,rules): empty-body pages and hostile ids no longer throw rule errors #1270 #1276 #1290 fixes the same class at the parser root for the other rules.

nc9 added 2 commits October 30, 2026 10:56
…on static pages (#1143)

New info rule and a shared payload analyzer: __NEXT_DATA__, the Next.js app router flight chunks, __NUXT__, Remix, Qwik, SvelteKit and Astro island state, sized per page with bounded reads. Reports only when the payload is at least 100 KB and 20 times the visible text on a page with at most 5 form controls and embeds and 50 buttons.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/content-in-js-bundle-1200 branch from 14ba8a0 to 3ee3e51 Compare October 30, 2026 00:26
@nc9
nc9 force-pushed the feat/content-in-js-bundle-1200 branch from c9be9b6 to 3ebe2c6 Compare October 30, 2026 01:36
nc9 added 5 commits October 30, 2026 13:37
…to a JS chunk (#1200)

Page-time sentences from each page's main content, searched for in the site's same-origin chunks of 50 KB or more after decoding JS escapes, entities and whitespace. A chunk with a loading page's own content warns; one with at least two other pages' content fails. Bounded: 8 MB per chunk, 10 chunks, 300 pages, 2.5 s.

Signed-off-by: Nik Cubrilovic <[email protected]>
#1200)

The entity and \u{ lookaheads scanned to the end of the chunk when no terminator existed (quadratic on a chunk of '&'). Decode into a preallocated buffer with fixed lookahead, find every needle in one rolling-hash pass with a verification cap and a deadline, cap text-node and candidate reads, and add adversarial tests.

Signed-off-by: Nik Cubrilovic <[email protected]>
…ecked reporting, info for one page (#1200)

Walk only outermost landmarks and dedupe sentences before picking five. Skip browser-rendered pages. Report a verification-cap stop for what it is, never report a clean pass when chunks were not fetched, cap severity at warn (info for a single page's copy, from a 36-site run), yield between chunks and lower the default page cap to 2000.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/content-in-js-bundle-1200 branch from 3ebe2c6 to 63f3594 Compare October 30, 2026 02:58
nc9 added a commit that referenced this pull request Oct 30, 2026
…ntBody test) and oxfmt of the content store

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

nc9 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Landed in v0.0.143 as a7ddb52c 78430d33 aefda9bd 0874e629 24a2b03c fb39c591 02e3ac81 662565a4 via #1341. Release: https://github.com/squirrelscan/squirrelscan/releases/tag/v0.0.143. Closing this original.

nik

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.

New rule: perf/content-in-js-bundle (page content shipped in client JS)

1 participant