Skip to content

fix(rules): read element-separated text in schema/rating-scope #739 - #748

Merged
nc9 merged 1 commit into
mainfrom
fix/rating-scope-glued-text-739
Oct 13, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/rating-scope-glued-text-739

Conversation

@nc9

@nc9 nc9 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Closes #739.

What changed

schema/rating-scope searched ctx.parsed.content.textContent, the body's .textContent, which glues adjacent elements. <h3>Customer rating</h3><p>5/5</p> reads Customer rating5/5, so no word-bounded rating pattern matched there. With a whole-number rating (no decimal-value fallback), the rule then warned that a rating printed on the page was not visible.

  • New helper: getPageSeparatedText(doc) in packages/rules/src/content/text-content.ts. It is the body's existing getSeparatedText (a space at every element boundary; scripts, styles, noscript and template skipped), with whitespace collapsed, memoised per parsed document in a WeakMap. This is the element-boundary helper integrity/brand-impersonation loses a signal without inter-element whitespace #696 will reuse.
  • rating-scope: visibleText searches the separated text next to the glued text. Every visibility test asks whether a rating appears anywhere, so more text can only clear the check, never trip it. Nothing that passed before can warn.
  • integrity/kit-signature: checked and unaffected, documented in code. It passes the glued text as the signature text corpus. The only bundled signature (calendly-kit) reads text only through a case-insensitive substring with no word boundary, which gluing cannot change. Switching it to separated text would instead split a brand an author wrapped across inline elements (Cal<b>endly</b>). A new test in packages/threat-intel/tests/signatures.test.ts pins that the kit page and a legit Calendly page get identical matches with glued and spaced text. A comment at the call site says why the text stays glued.

Acceptance criteria

  • Adjacent elements with no whitespace are evaluated the same as the spaced version by schema/rating-scope: packages/rules/tests/glued-element-text.test.ts has four fixture pairs (block label then score, table row, label then count, inline spans), each passing both ways, plus a no-rating page that warns both ways. On main, the glued half of every pair warns; I checked that by swapping main's rating-scope.ts back in.
  • integrity/kit-signature is checked against a glued/spaced pair and documented as unaffected (above).
  • One shared helper (getPageSeparatedText), built on the existing getSeparatedText. No new extraction.
  • The canonical golden is unchanged (pass, no re-pin). The rating-scope doc's "text it searches" sentence now names the separated text.

Verification

  • bun test packages/rules/tests/glued-element-text.test.ts: 9 pass.
  • bun test packages/threat-intel/tests/signatures.test.ts packages/rules/tests/schema-rating-scope.test.ts packages/rules/tests/integrity-intel.test.ts: 44 pass.
  • bun test packages/rules/tests/rule-verdict-scope.test.ts: 340 pass.
  • bun test packages/audit-engine/tests/template-fanout-parity-golden.test.ts: 53 pass.
  • bun test packages/audit-engine/tests/streaming-rules-canonical-golden.test.ts: 1 pass, no re-pin.
  • tsgo --noEmit in packages/rules and packages/threat-intel: clean. oxlint is clean on the changed files. The oxfmt drift in rating-scope.ts and text-content.ts is pre-existing on main.
  • make validate in docs/: all checks passed.

Agent skill

No change needed.

rating-scope searched `content.textContent`, which glues adjacent
elements: `<h3>Customer rating</h3><p>5/5</p>` reads "Customer
rating5/5", no word-bounded rating pattern matches, and the rule warned
that a printed rating was not visible.

Add getPageSeparatedText (content/text-content.ts): the body's
getSeparatedText, whitespace collapsed, memoised per document. The
rating check searches it next to the glued text, so more text can only
clear the check, never trip it. #696 will reuse the same helper.

kit-signature keeps the glued text: every bundled string that reads
`text` is a substring with no word boundary, so gluing cannot change a
verdict; a threat-intel test pins the glued/spaced pair.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 marked this pull request as ready for review October 13, 2026 13:43
@claude

claude Bot commented Oct 13, 2026

Copy link
Copy Markdown

Reviewed the diff. The change is small, matches the stated scope, and I found nothing blocking.

  • (nit) packages/rules/src/content/text-content.ts: pageSeparatedTextCache is keyed on the document, so it is only correct while rules treat the DOM as read-only. The "is computed once per document" test pins that by showing a stale value after mutation. A one-line note in the doc comment saying mutation after the first call is not reflected would stop a future rule from tripping on it.
  • (nit) packages/rules/tests/glued-element-text.test.ts: the "document without a body" test only asserts typeof === "string". Asserting "" would check the empty-body branch more tightly, if linkedom reliably yields no body for that input.
  • (low) packages/rules/src/schema/rating-scope.ts: visibleText now holds two copies of the page text, so memory and regex scan time roughly double on very large pages. The WeakMap cache keeps the build to once per page, so this is acceptable. Only the cost, not correctness, would matter if more rules adopt the helper (integrity/brand-impersonation loses a signal without inter-element whitespace #696).

Correctness: the reasoning that adding text can only clear the visibility check, never newly trip it, holds. The glued text is still searched, so nothing that passed before can warn. Leaving kit-signature on glued text is justified, and the new signature test pins it. I did not run the tests myself. I'm relying on the PR's reported results and the CI checks.

@nc9
nc9 merged commit d6bbf6b into main Oct 13, 2026
16 checks passed
@nc9
nc9 deleted the fix/rating-scope-glued-text-739 branch October 13, 2026 13:58
nc9 added a commit that referenced this pull request Oct 14, 2026
…mpty-body branch #739

Follow-up to the #748 review nits: say the per-document cache assumes a
read-only DOM, and assert "" for a document with no body instead of
only the return type.

Signed-off-by: Nik Cubrilovic <[email protected]>
nc9 added a commit that referenced this pull request Oct 14, 2026
…mpty-body branch #739

Follow-up to the #748 review nits: say the per-document cache assumes a
read-only DOM, and assert "" for a document with no body instead of
only the return type.

Signed-off-by: Nik Cubrilovic <[email protected]>
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.

schema/rating-scope, integrity/kit-signature: element-glued text breaks word-boundary checks

1 participant