Skip to content

feat(rules): add content/unrendered-markup, literal markdown or escaped HTML in rendered copy - #217

Merged
nc9 merged 5 commits into
mainfrom
feat/1352-content-unrendered-markup
Sep 6, 2026
Merged

nc9 merged 5 commits into
mainfrom
feat/1352-content-unrendered-markup

Conversation

@nc9

@nc9 nc9 commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Refs squirrelscan/repo#1352

Summary

Adds content/unrendered-markup, a per-page rule that detects markup which failed to render and leaked into the copy a visitor reads: literal markdown (emphasis, links, headings, fences), visible escaped HTML, and double-encoded entities.

The rule reads decoded prose from the DOM, never raw HTML, with script-like and code-like subtrees excluded. Excluding those is the rule rather than a refinement, because a documentation site displaying markdown source is doing its job. <code>, <pre>, <samp>, <kbd> and <textarea> come out, along with whole-token syntax-highlighter classes (highlight, hljs, shiki, chroma, codehilite, preformatted, any language-*) and the data-language attribute family. Whole tokens, so a highlight-box callout is still judged as prose.

A note on the acceptance criteria. The issue says the rule registers in loader.ts. loader.ts registers namespaces, not rules; a new content rule is registered in packages/rules/src/content/index.ts, which is what loadAllRules() reads. The criterion's intent is met and no loader.ts change is needed. The issue also names stripHtmlForText; that helper takes a string and cannot exclude <pre>/<code>, so the rule uses collectTextExcluding, the non-mutating DOM extraction that content/mojibake adopted for exactly this problem.

A shared helper changed, and #214 changed it too. collectTextExcluding gained an optional fourth parameter, isBoundary. Without it, sibling blocks concatenate with nothing between them, <p>a</p><p>b</p> reads back as ab, and every line-anchored pattern here can only match at offset 0, which on a real page means never. The parameter defaults to undefined, so existing call sites are unchanged, and the 518-page canonical golden gate confirms zero divergence for every other rule.

content/placeholder-text (#214) added the same parameter independently, emitting the separator on ENTERING a matching element only. That is enough between two blocks, which is all that rule needs, but not when a block is followed by inline content: <h3>Price *</h3><a>terms* apply</a> still reads back as *terms*, emphasis present in neither element. This branch keeps the symmetric version, which serves both rules, and merges the two test suites into one describe block. The consequence is that extracted text now carries a closing separator as well, which moved three exact-string expectations in packages/rules/tests/placeholder-text.test.ts. Only the strings moved: each test still asserts what its name says, and every rule-outcome assertion in that suite is untouched and green.

Rule counts after the rebase. Both branches bumped the published counts 279 to 280 with byte-identical edits, so git merged them as one change and raised no conflict, leaving every literal at 280 while the loader returned 281. Corrected across all five documentation surfaces; both per-category tables and all four score-group card sets sum to 281, with content at 20.

Precision

This rule accuses a page of a rendering bug, so it was measured against real sites rather than fixtures alone. 83 of 85 real pages clean, worst per-page rule time 10 ms. Each false positive found in the wild became a rule and a test:

Real page What it does Rule that fixed it
commonmark.org markdown source in td class="preformatted" preformatted joins the highlighter class tokens
vercel.com/docs a copyable agent prompt full of backticks backticks corroborate, never accuse alone
astro.build <Article>, <Video> in the hero escaped tags must be lowercase; those are component names
MDN /pre <pre> and <meta name> in a breadcrumb one tag must close cleanly or carry a recognised valued attribute
MDN /pre # the-pre-element, a URL fragment on its own line a lone # is the number sign unless a capital follows

Prose full of identifiers stays clean: CLOUDFLARE_API_TOKEN, some_file_name.ts, max_pages, __init__, 2 * 3, width * height, array[i](arg), Johnson&Johnson; and if a<b then c=1>0 are all covered by tests. Both emphasis forms require CommonMark flanking, and the underscore form additionally requires a space inside the run, which is what silences Python dunders.

The residual is two w3schools tutorials that display HTML source in a bespoke w3-code div. That is reported honestly rather than tuned away: silencing every *-code class token would also silence promo-code and discount-code, which are prose. The docs page names this case and tells authors to use <pre> or <code>.

Verification

packages/rules       bun test tests/content-unrendered-markup.test.ts   84 pass
packages/parser      bun test tests/dom-text.test.ts                    14 pass
packages/rules       bun test (5 neighbouring content + limit suites)  145 pass
packages/audit-engine  bun test tests/*golden*  (canonical + 4 more)    14 pass
tsgo --noEmit        parser, rules, audit-engine, apps/cli              all clean
bun run lint         root                                               clean
docs                 bun run check && bun run build                     clean, 389 pages

Real-site sweep: 85 pages fetched, 83 clean. End to end through the CLI: a served page with a mis-rendered markdown field reports the finding and ignores the <pre><code> block on the same page; the same page with its markdown correctly rendered is silent.

The golden merge gate moved as a new rule id should. findings 98721 to 99221, which is one check on each of the 500 pages that have a document, and perRuleTally 280 to 281. The pin asserts the rule's tally directly (pass: 500, warn: 0, fail: 0) rather than inferring "no false positives" from an unmoved health score, so the gate proves zero false positives across 500 synthetic pages.

Two independent review passes ran over the diff (the codex MCP was unavailable on this account, so both were fresh-eyes model reviews). Every finding is fixed and each has a regression test. The timing tests were rewritten after one pass showed the original fixtures carried a single trigger character each and could not detect backtracking at all: a deliberately bad pattern measured 0.1 ms on the old fixture and 5.2 seconds on a dense one. There are now 18 dense-trigger fixtures at a 400 ms budget.

Follow-up for the gitlink bump

A rule change makes the private monorepo's generated catalogue stale. That bump needs cd apps/api && bun run generate:rule-catalog plus the totalRules literal and its header comment in apps/web/src/components/rules.tsx, or Test - API goes red on the rule-count and catalogue drift guards.

Checklist

  • I added or updated tests for behavioral changes.
  • I did not include secrets, customer data, or incompatible third-party material.
  • Every commit includes a DCO Signed-off-by line (git commit -s).

Nik Cubrilovic added 5 commits September 7, 2026 09:31
…caped HTML

Detects literal markdown (emphasis, links, headings, fences) and visible
escaped HTML or double-encoded entities in the text a visitor actually reads.

Reads prose from the DOM with script-like AND code-like subtrees excluded, so
a docs page displaying markdown source stays clean. That exclusion is the rule,
not a refinement: <code>, <pre>, <samp>, <kbd>, <textarea>, plus whole-token
syntax-highlighter classes and the data-language family.

Precision work, measured against 88 real pages (1 residual, w3schools' HTML
tutorial, which displays source in a bespoke div):

- emphasis requires CommonMark flanking, so CLOUDFLARE_API_TOKEN, 2 * 3 and
  some_file_name.ts are not emphasis; the underscore form also requires a space
  inside, which silences __init__ and __main__
- a markdown link needs a URL-shaped target, so array[i](arg) is not a link
- escaped tags must be lowercase (<Article> is a component name) and at least
  one must carry a closing form or a valued attribute (<meta name> in a
  sentence is documentation)
- entities match a curated list, so Johnson&Johnson; is not an entity
- visible backticks corroborate but never accuse on their own

Regexes are kept linear with bounded, non-adjacent quantifiers and covered by
adversarial 64 KB timing tests.

Refs squirrelscan/repo#1352

Signed-off-by: Nik Cubrilovic <[email protected]>
Six findings from a fresh-eyes pass, in descending order of severity.

The heading and fence families were effectively dead on any real page.
collectTextExcluding emits its separator only for EXCLUDED subtrees, so sibling
blocks concatenate with nothing between them and a /m ^ anchor matched only at
offset 0 — which, on a page with a header, is never. Adds an optional isBoundary
predicate to collectTextExcluding (default undefined, so no existing caller
moves) and passes a block-tag predicate. Verified: <p>## Pricing</p> behind a
nav went from silent to reported, as did adjacent list items sharing emphasis.

`rest.includes("=")` called any prose equals sign an attribute, so
`if a<b then c=1>0 holds` was reported at fail. Replaced with a recognised
attribute-name allowlist, the same shape that already keeps Johnson&Johnson;
from reading as an entity. A path in angle brackets is no longer a closing tag.

The reported count, kind list and example included markdown-inline-code, which
the threshold excludes: a page with one bold phrase and five backtick spans
announced six occurrences at warn, and showed a backtick span as the example.
The headline now reports only the evidence that decided the status.

Also: the tag-name bound is derived from the tag sets rather than a literal
{0,9} that blockquote and figcaption already sat exactly on; the emphasis family
honours one match budget instead of one per pattern; a vouching tag past the cap
no longer drops the whole family; isCodeLikeElement makes one pass over
attributes rather than five (19.8ms -> 1.9ms per extraction on a 10k-element
DOM); bare "pre" is gone from the highlighter class tokens.

Tests: the eight "adversarial" fixtures carried a single trigger character each
and could not detect backtracking at all — a deliberately bad link pattern
measured 0.1ms on the old fixture and 5.2s on a dense one. Replaced with 18
dense-trigger fixtures at a 400ms budget, plus assertions that the scan cap
truncates rather than merely finishing fast. The golden pin now checks the
per-rule tally directly instead of inferring "all 500 are passes" from an
unmoved health score.

Re-smoked against 75 reachable real pages: 74 clean. The residual is w3schools'
HTML tutorial, which displays source in a bespoke div. One new false positive
the boundary work introduced (MDN renders a URL fragment on its own line) is
fixed by rejecting slug-shaped headings.

Two rule-count literals the first commit missed: README's per-category table and
the duplicate content card in docs/rules/seo. Both category tables now sum to
280.

Refs squirrelscan/repo#1352

Signed-off-by: Nik Cubrilovic <[email protected]>
Second review pass. Every finding reproduced before it was fixed.

The boundary was emitted on ENTER only, which moved the gluing bug to the other
edge rather than fixing it: `<h3>Price *</h3><a>terms* apply</a>` still read back
as `*terms*`, emphasis present in neither element, and a split escaped tag
reached `fail` on the same basis. collectTextExcluding now pushes a stack marker
before a boundary element's children so the separator closes after them, and the
escaped-tag pattern no longer accepts a newline inside an attribute list, which
was the second way two blocks could assemble one tag.

Making every block start a line gave the number sign a line start too, so
`<th># of seats</th>` warned as a leaked heading. A single `#` now needs a
capitalised word after it; `##` and deeper have no second reading and are taken
as written, which also restores `## my-package` and `## getting-started` that
the previous slug guard was silencing.

The attribute allowlist had no legacy presentational names, so escaped table and
email HTML - no closing tags, `align`/`bgcolor`/`valign` only - had nothing to
vouch for it and passed clean. Added those, deliberately without single-letter
SVG names, which would resurrect the `a<b then d=1>0` false positive.

Also: sibling `<option>`s glued like `<li>`s, so option/optgroup/caption/legend/
menu join the block tags; the escaped-tag sample now leads with the tag that
actually vouched rather than whichever bare mention came first, moved by index
because moving it by value would delete its duplicates and undercount; and the
`closing` helper moved above its callers, since a const arrow is not hoisted and
packages/* is not linted.

Tests: two of five boundary fixtures passed with the boundary deleted because
they sat at the body's first byte. Replaced with fixtures that all follow chrome,
plus five gluing fixtures that must stay clean and one proving a tag whole within
a single block is still reported.

Re-smoked against 85 real pages, all reachable: 83 clean, worst rule time 10ms.
Both residuals are w3schools tutorials displaying HTML source in a bespoke div,
the known and documented case. MDN's element pages are now clean.

Refs squirrelscan/repo#1352

Signed-off-by: Nik Cubrilovic <[email protected]>
Rule solutions render in reports and on the docs site, so they are public copy,
where the house style is a colon rather than an em-dash. Also corrects two
comments that described the previous implementation: the attribute read is one
pass now, and the escaped-tag sample is the vouching tag rather than the first.

Refs squirrelscan/repo#1352

Signed-off-by: Nik Cubrilovic <[email protected]>
Rebase onto content/placeholder-text (#214). Three things needed a decision
rather than a mechanical resolution.

Both branches bumped the published counts 279 -> 280 with byte-identical edits,
so git merged them as one change and no conflict was raised. The live loader now
returns 281 rules and 20 content rules while every literal still said 280 and 19.
Corrected across README, docs/index.mdx, docs/rules/index.mdx,
docs/rules/seo/index.mdx and docs/agent-setup/index.mdx; both per-category tables
and all four score-group card sets now sum to 281.

Both branches also added the SAME optional isBoundary parameter to
collectTextExcluding, independently. #214's version emits the separator on
entering a matching element only, on the reasoning that the next block's own
entry closes the previous one. That holds between two blocks, which is all
placeholder-text needs, but not when a block is followed by inline content:
`<h3>Price *</h3><a>terms* apply</a>` still reads back as `*terms*`, emphasis
present in neither element. Kept the symmetric version, which satisfies both
rules, and merged the two test suites into one describe block.

That makes the extracted text carry a closing separator as well, so three
exact-string expectations in placeholder-text.test.ts moved. Only the strings:
each test still asserts what its name says, and every rule-outcome assertion in
that suite is untouched and green.

The golden gate now pins 99221 findings and 281 tally keys, both rules
contributing 500 passing page checks to the 518-page fixture.

Verified: packages/rules content suites 177 pass, packages/parser 41 pass,
audit-engine golden 15 pass across six files, tsgo clean in parser, rules,
audit-engine and apps/cli, root lint clean, docs check clean.

Refs squirrelscan/repo#1352

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the feat/1352-content-unrendered-markup branch from 9bef33a to 4d45119 Compare September 6, 2026 23:31
@nc9
nc9 merged commit 99a0cbe into main Sep 6, 2026
13 checks passed
@nc9
nc9 deleted the feat/1352-content-unrendered-markup branch September 6, 2026 23:38
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.

1 participant