Skip to content

feat(audit-engine): populate the template cluster key on page_features - #267

Merged
nc9 merged 1 commit into
mainfrom
lane/1949-template-fp
Sep 8, 2026
Merged

nc9 merged 1 commit into
mainfrom
lane/1949-template-fp

Conversation

@nc9

@nc9 nc9 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

page_features.template_fp shipped as an indexed column that extractPageFeatures set to null on every row, with no reader anywhere. This populates it with an equality cluster key, the prerequisite for template fan-out.

Tracks squirrelscan/repo#1949, part of squirrelscan/repo#1026.

What the key is

fingerprintPage (packages/rules/src/integrity/fingerprint.ts) already builds a per-page chrome signal: external asset hosts, <body> class tokens, CSS custom-property names, stylesheet hrefs, nav/footer presence. templateFingerprintKey canonicalises it (every Set sorted, because Set iterates in insertion order, i.e. document order; JSON-of-arrays so a marker containing a delimiter cannot forge a field boundary) and reduces it through the same FNV-1a lane findingFingerprint uses.

Chrome, not an exact DOM skeleton. On real crawls the two definitions disagree completely:

corpus pages chrome clusters exact-skeleton clusters
gymshark.com 247 13 (94.7% redundant) 231 (6.5%)
openelectricity.org.au 100 12 (88.0%) 42 (58.0%)

Real pages of one template share their chrome and differ in their body: different counts of variants, reviews, related items. The synthetic bench corpora are generated from 1-6 templates, so every clustering definition collapses to ~99% redundancy on them and agrees for the wrong reason.

One DOM walk, not two

fingerprintPage is five querySelectorAll passes. The streamed loop builds it once per page and hands the same object to both consumers: extractPageFeatures reduces it to the stored key, and the registered page-signal collector keeps it whole for integrity/template-discontinuity's fuzzy similarity, which is untouched.

Nothing about the output can prove that, because a second walk of the same DOM returns an equal fingerprint and hashes to the same key. fingerprintWalkCount() is the test seam that can, mirroring detachCounts in audit-engine. Removing the shared argument from the production call takes the streamed run from 6 walks to 12 and fails the gate.

Verification

apps/cli/scripts/template-cluster-census.ts replays the shipped writer over a finished project.db into a scratch :memory: store and reads the clusters back through getPageFeatureTemplateClusters (SiteQuery.templateClusters()), so what agrees is the stored GROUP BY, not an in-memory grouping this script computed. It never writes to the crawl it reads, and it mirrors production's scored universe (rate-limited pages excluded, not just WAF challenges).

gymshark.com            247 scored, 13 clusters (8 multi-page), 94.7% redundant, GROUP BY agrees
openelectricity.org.au  100 scored, 12 clusters (3 multi-page), 88.0% redundant, GROUP BY agrees

Key cost measured there: 0.04 to 0.08 ms/page, against a 1.0 to 5.6 ms/page fingerprint walk that was already being paid.

Report output

No rule reads SiteQuery.templateClusters(). This stores a key and changes no finding.

Tests

packages/audit-engine/tests/template-cluster-key-golden.test.ts (named *golden* so the Golden Gates job actually runs it; fingerprint-parity.test.ts is not matched by that glob and has never run in CI, public #229). It covers the key's determinism, order-insensitivity, injectivity across fields, the null case, the write reaching the column, GROUP BY clustering over the stored value, and the walk budget.

The cluster counts above cannot be asserted from a fixture in this repo, and specifically not from the synthetic sites; proving fan-out parity on real multi-page clusters is squirrelscan/repo#1950.

…#1949

`page_features.template_fp` shipped as an indexed column that
`extractPageFeatures` set to null on every row, with no reader anywhere.
Populate it with an equality cluster key: the chrome fingerprint
`fingerprintPage` already builds per page, canonicalised (every Set sorted,
JSON-of-arrays so a marker cannot forge a field boundary) and reduced through
the same FNV-1a lane `findingFingerprint` uses.

Chrome, not DOM skeleton. On gymshark.com's 247 real pages chrome gives 13
clusters and 94.7% redundancy where an exact skeleton gives 231 clusters and
6.5%; real pages of one template share their chrome and differ in their body.
The synthetic bench corpora are generated from 1-6 templates, so both
definitions collapse to ~99% there and agree for the wrong reason.

One DOM walk, not two. The streamed loop builds the fingerprint once and hands
it to both consumers: `extractPageFeatures` reduces it to the key, and the
registered page-signal collector keeps it whole for `template-discontinuity`'s
fuzzy comparison, which is untouched. `fingerprintWalkCount()` is the test seam
that proves it, since a second walk of the same DOM returns an equal
fingerprint and the same key and would leave every value assertion green.

Verified against the measured corpora with the census script, which replays the
real writer into a scratch store and reads the clusters back through
`getPageFeatureTemplateClusters`, so the stored `GROUP BY` is what agrees:
gymshark 247 pages -> 13 clusters / 8 multi-page / 94.7%; openelectricity 100
pages -> 12 / 3 / 88.0%.

No rule reads `templateClusters()`, so report output is unchanged.

Part of squirrelscan/repo#1026.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 merged commit 93932a9 into main Sep 8, 2026
13 checks passed
@nc9
nc9 deleted the lane/1949-template-fp branch September 8, 2026 02:22
nc9 pushed a commit that referenced this pull request Sep 8, 2026
…an its verdict out

Implements squirrelscan/repo#1951, the payoff for #1026. Builds on #267 (the
cluster key) and #269 (the classification and its gate).

In the streamed page-rule pass a rule declaring `verdictScope: "template"` now
runs once per template cluster and its verdict is copied onto the cluster's other
members. On gymshark.com that removes 5,850 of 48,906 rule invocations.

Copying a verdict asserts something about pages the rule never visited, so it is
allowed only where the copy cannot carry the page it was computed on:

- Each member gets its own copy, checked by IDENTITY. `detachFromPage` returns its
  argument when `structuredClone` throws, which for its usual callers costs
  retention and never a wrong finding; here it would let a member stamp `pageUrl`
  onto the cached objects and collapse the whole cluster onto one page. Anything
  that comes back uncopied is dropped and those pages run the ordinary path.
- The cached copy carries no `pageUrl`, so a member's stamp is a first write.
  Nothing rewrites urls inside a copied check: the normalisation that would have to
  be inverted is not injective, so the requirement is that there is nothing to
  rewrite, and a new test fails a declaration that breaks it by running each
  declaring rule under two urls sharing only scheme and host.
- The grouping key is (template cluster, page ORIGIN). `security/sri` decides
  cross-origin by comparing a resolved script's origin to the page's, so the same
  markup at https:// and http:// genuinely differs. Origin stays out of
  `page_features.template_fp`, which should still call those one template.
- A rule that threw is never cached, and the soft-404 gate is consulted before a
  fanned verdict because it is per-page where applicability is run-constant.

`core/charset` is DEMOTED to page scope. Two pages with identical markup and no
`<meta charset>`, served `text/html; charset=utf-8` and bare `text/html`, give
pass and fail. The cluster key constrains no response header. It measures constant
on gymshark.com, openelectricity.org.au and the authored corpus only because every
page of all three declares charset in a meta tag, so the header branch is never
reached; #269's own VerdictScope doc already disqualifies header-readers and this
one was missed. It is the only one of the 26 that reads `ctx.page.headers`.

Three falsifiers, because a gate only proves something about the pages it runs:
the equivalence gate compares the fanned pass against a resident loop over an
authored corpus with three multi-page clusters and two singletons; the bench
script's --verify does the same against real crawls (byte-identical on gymshark,
openelectricity and a 150-page synthetic); the page-independence test covers url
dependence, the charset counterexample and the soft-404 exclusion. Equal output is
also what a feature doing nothing produces, so the gates count calls: a declaring
rule must run once per CLUSTER and an undeclared one once per PAGE.

ON for the streamed pass, which both the CLI and the cloud run.
`SQUIRREL_TEMPLATE_FANOUT=0` turns it off. v1 `runRulesOnStorage` is untouched.

Follow-ups: #275 (the cluster key is chrome only, so a constructed pair can share
it and differ in a declared rule's inputs), #274 (per-invocation profiler payload
built whether or not profiling is on).

Signed-off-by: Nik Cubrilovic <[email protected]>
nc9 added a commit that referenced this pull request Sep 8, 2026
…an its verdict out (#1951) (#279)

* perf(audit-engine): run a template-scoped rule once per cluster and fan its verdict out

Implements squirrelscan/repo#1951, the payoff for #1026. Builds on #267 (the
cluster key) and #269 (the classification and its gate).

In the streamed page-rule pass a rule declaring `verdictScope: "template"` now
runs once per template cluster and its verdict is copied onto the cluster's other
members. On gymshark.com that removes 5,850 of 48,906 rule invocations.

Copying a verdict asserts something about pages the rule never visited, so it is
allowed only where the copy cannot carry the page it was computed on:

- Each member gets its own copy, checked by IDENTITY. `detachFromPage` returns its
  argument when `structuredClone` throws, which for its usual callers costs
  retention and never a wrong finding; here it would let a member stamp `pageUrl`
  onto the cached objects and collapse the whole cluster onto one page. Anything
  that comes back uncopied is dropped and those pages run the ordinary path.
- The cached copy carries no `pageUrl`, so a member's stamp is a first write.
  Nothing rewrites urls inside a copied check: the normalisation that would have to
  be inverted is not injective, so the requirement is that there is nothing to
  rewrite, and a new test fails a declaration that breaks it by running each
  declaring rule under two urls sharing only scheme and host.
- The grouping key is (template cluster, page ORIGIN). `security/sri` decides
  cross-origin by comparing a resolved script's origin to the page's, so the same
  markup at https:// and http:// genuinely differs. Origin stays out of
  `page_features.template_fp`, which should still call those one template.
- A rule that threw is never cached, and the soft-404 gate is consulted before a
  fanned verdict because it is per-page where applicability is run-constant.

`core/charset` is DEMOTED to page scope. Two pages with identical markup and no
`<meta charset>`, served `text/html; charset=utf-8` and bare `text/html`, give
pass and fail. The cluster key constrains no response header. It measures constant
on gymshark.com, openelectricity.org.au and the authored corpus only because every
page of all three declares charset in a meta tag, so the header branch is never
reached; #269's own VerdictScope doc already disqualifies header-readers and this
one was missed. It is the only one of the 26 that reads `ctx.page.headers`.

Three falsifiers, because a gate only proves something about the pages it runs:
the equivalence gate compares the fanned pass against a resident loop over an
authored corpus with three multi-page clusters and two singletons; the bench
script's --verify does the same against real crawls (byte-identical on gymshark,
openelectricity and a 150-page synthetic); the page-independence test covers url
dependence, the charset counterexample and the soft-404 exclusion. Equal output is
also what a feature doing nothing produces, so the gates count calls: a declaring
rule must run once per CLUSTER and an undeclared one once per PAGE.

ON for the streamed pass, which both the CLI and the cloud run.
`SQUIRREL_TEMPLATE_FANOUT=0` turns it off. v1 `runRulesOnStorage` is untouched.

Follow-ups: #275 (the cluster key is chrome only, so a constructed pair can share
it and differ in a declared rule's inputs), #274 (per-invocation profiler payload
built whether or not profiling is on).

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

* fix(audit-engine): never fan out a rule that is also soft-404 gated, and compare stored findings by key

Two findings from a second adversarial review of the template fan-out, plus the
secret-scan baseline.

A rule declaring BOTH `verdictScope: "template"` and `skipOnSoft404` is a
contradiction: the soft-404 gate is decided per page and the cluster key says
nothing about it. The runner's gate ordering already protected the RECIPIENT — a
soft-404 member takes its own skip rather than a sibling's real verdict — but not
the reverse, where a soft-404 REPRESENTATIVE cached its skip and handed it to
healthy members, reporting "skipped: soft 404" for pages that are fine. No
built-in rule declares both and a named test in packages/rules keeps it that way,
but a plugin registering one through `additionalNamespaces` only meets this at
runtime, so the fan-out now excludes such a rule outright. A test with exactly
that rule asserts it runs on every page; removing the exclusion drops it to 5.

The stored-finding assertion compared sorted keys and sorted fingerprints as two
independent lists, which passes on precisely the misattribution it exists for.
It now indexes both arms by (normalizedUrl, ruleId, checkName, locator) and
compares whole rows. Verified by mutation: moving a fingerprint onto a finding
that really has a different one now fails. The comment records what this can and
cannot show — a fingerprint swapped between members of ONE cluster is
undetectable on any corpus, because those fingerprints are legitimately equal by
the classification's own claim. The key set is the load-bearing half: a finding
stored against the representative's url shows up as a missing key.

The test file had also acquired three raw NUL bytes from an earlier edit, which
made it `file`-binary and silenced grep over it — which is how the first fix was
lost without any tool reporting a failure. Restored to the source escape; the
runtime value is unchanged.

`.secrets.baseline` is the line-number drift detect-secrets rewrites when a diff
moves lines in a file with baselined entries. No new candidate: the diff is two
lines of `line_number` and `generated_at`.

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

* docs(benchmarks): record what running a rule once per template is worth (#1951)

12.9% of the page-rule pass on gymshark.com and 17.6% on a single-template
synthetic, CPU time, minimum of 7 and 5 interleaved repeats, load average 2.2 to
3.8. Rule invocations fall from 48,906 to 43,056: each of the 25 declared rules
goes from 247 calls to 13. Three rules that scan the page's whole HTML are most of
it, and the 6,058 ms of rule bodies removed accounts for 93% of the CPU the same
profiled run moved.

Records why the number is 13% and not the 96.5% of page-rule time that sits on
non-first cluster members: that is the ceiling over ALL rules, and only 25 of 198
are declared. Also records the core/charset demotion and the residual risk in

Signed-off-by: Nik Cubrilovic <[email protected]>
#275, and updates #269's paragraph, which said 26 rules were declared.

---------

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

1 participant