Skip to content

fix(audit-engine): item findings carry their own message, not the page's count - #228

Merged
nc9 merged 1 commit into
mainfrom
fix/1881-item-level-message
Sep 7, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/1881-item-level-message

Conversation

@nc9

@nc9 nc9 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

The defect

findingFingerprint hashes [status, message, value, expected] and is deliberately URL-free, so finding_defs (site_key, fingerprint) can hold one row per distinct defect. flattenChecks defeated that: it copied the parent check's page-level message/value/expected onto every item row, and a page-scope check's message is a count of that page's items.

One Shopify CDN script missing SRI on all 401 pages of a real audit stored 25 different messages, "26 cross-origin resources without Subresource Integrity" on one page and "24 ..." on another, and therefore 25 fingerprints for one defect.

The damage ran in both directions, which the issue did not anticipate. On the measured site, item rows carry 4,036 distinct (rule, check, locator) defects but only 561 distinct fingerprints, because the hash keyed on the page's item count rather than on the defect. One finding_defs row was standing in for many unrelated locators. So the dictionary was not merely bloated, it was wrong.

The fix

An item row's message is now the page-invariant part of its check's message, plus the item id:

cross-origin resources without Subresource Integrity: https://cdn.shopify.com/extensions/.../globo.filter.min.js
potential source map(s) detected: https://cdn.example/app.js.map
cookie(s) missing the Secure flag: cart_currency

pageInvariantCheckText drops a leading count and replaces any remaining digit run with N. That placeholder is not invented here: the report's own grouping already renders exactly that for a merged group whose members differ only in digits (packages/report/src/grouping.ts), so the two surfaces read alike.

value and expected were page-level aggregates for the same reason and are dropped from item rows.

item.label deliberately does not feed the message. Several rules stamp per-page numbers into the label, content/keyword-stuffing emits "everyday" (2.3%), so a label-bearing message re-creates the same split. The label is not lost; it rides in the payload's items[0], which every reader that rebuilds a CheckResult replays.

Nothing downstream changes text or score. The parent check's page-level trio is stashed in the item payload as m/v/e, and both readers that rebuild a CheckResult from storage restore it verbatim:

  • carriedFindingToCheck (scoring.ts), so a replayed carried check renders the page-level message. Report grouping keys on the message, so reading the item's own text there would split one carried aggregate into one group per item.
  • reconstructRuleChecks (reconstruct.ts), so the complete-store round-trip stays lossless.

m is the sole restore marker. A pre-#1881 row has none and falls back to its own columns; a whole-check row never writes one, so payload presence alone cannot misfire; an empty-id item is caught even though it lands in a locator-"" group. The marker is scanned across the whole group, not just group[0], because the stash is budgeted per item.

Payload budget. The stash is added only when the serialized payload still fits maxFindingPayload. The chunk ingest drops an over-cap payload whole and details.additional feeds the scorer's density penalty, so letting the stash tip a near-cap payload over the line would move the health score, the #1179 class. The stash is display detail, so it is the part that yields.

The budget never trims items/details/pages to make room. This same payload feeds the CLI's local SQLite store, which persists it with no cap at all, so dropping detail here to work around a transport limit would destroy data the local path would have kept. Shrinking an already-over-cap payload for transport belongs at the transport boundary, and the paired private PR adds exactly that retry there. An over-cap payload is therefore byte-identical to what it was before this change.

Whole-check findings (locator "") are untouched.

Which surfaces show this message to a person

Traced every reader of page_findings.message / finding_defs.message that does not first pass through carriedFindingToCheck or reconstructRuleChecks.

surface reads the raw stored column? what it shows after this PR
Dashboard report detail (report-detail.tsx) no unchanged; topIssues is built server-side from the R2 report payload (routes/v1/websites.ts), then @squirrelscan/report grouping
Issues tracker list + detail, and the issues sync no unchanged; issue-sync.ts writes title/description/recommendation from rule metadata, never from a finding message
MCP get_issue no unchanged; occurrences come from extractRuleOccurrences over the report payload (run-report.ts)
MCP list_issues no carries no message field at all
Audit summary email prompt (packages/issues/src/prompts.ts) no contains no message read; its input schema has no message field
Audit digest email no unchanged; audit-digest.ts takes its title from flattenReport, which reads the report payload
compare_audits no audit-diff/resolve.ts selects only normalizedUrl, ruleId, checkName, locator, state from page_findings, no message
Report renderers (packages/report) no never reads stored findings, only CheckResults
GET /v1/{reports,websites}/…/findings (findings-read.ts) yes the item-scoped message above

The one raw reader is serveFindings, and it is dark: REPORTS_FINDINGS_API_ENABLED is "false" in wrangler.toml and wrangler.dg.toml, requireFindingsApi 404s, and nothing in the monorepo calls it, not the dashboard, the API client, the cloud client or the CLI. It returns JSON, never a rendered string. So no human or agent sees this column today, and when that flag is flipped it will read as a sentence rather than a bare URL, which is why the message is not just the id.

Measurements

Page-invariance, whole production corpus: 296 sites, 545,822 item rows.

key count
distinct (site, rule, check, locator, status) 91,944
plus the new message 91,966
residual split 22, or 0.024%

All 22 are named and filed as #231: security/sri and content/meta-in-body alternate singular and plural wording, and schema/rating-scope interpolates the product name into its message. That is empirical rather than a proof, and #229 is the gate that would catch a future rule reintroducing it.

Collapse on the measured site (drscholls, 52,526 rows):

rows distinct (rule, check, locator) identity tuple before after distinct fingerprints before after
item rows 45,663 4,036 6,455 4,069 561 4,069
whole-check rows 6,863 43 519 519 519 519

Item-row inflation over the locator key falls from 59.9% to 0.8%. Whole-check rows are unchanged, as the third acceptance criterion requires. Site-wide the tuple lands 12.5% above the locator floor; the entire residual is the 43 whole-check groups this PR must leave alone, so that is the floor for #1881 and #1882/#1885 own the rest.

The fingerprint count for item rows rises from 561 to 4,069. That is the correct direction: 4,036 distinct defects previously shared 561 hashes.

Canonical 500-page golden fixture: item-row inflation is 0.0% before and after. The fixture's item locators are page-unique, so it never exhibited the count split and cannot demonstrate the fix, which is why the numbers above come from production.

Gates

No golden files exist to regenerate; every "golden" in packages/audit-engine/tests is a self-referential parity gate over an in-process synthetic fixture. All 18 files matched by test:golden pass, as do complete-store-parity, merge-scoring, fingerprint-parity and the two CLI suites that exercise flattenChecks. Health score is unchanged at 49 on the canonical fixture.

Worth naming for a future issue, filed as #229: the engine golden snapshot compares only (ruleId, checkName, status, pageUrl), so a change to a finding's message text passes every golden gate while re-fingerprinting every stored finding. That is why the numbers above come from measurement rather than from the gates.

One-time effect: smaller than the issue assumed

The issue predicted that every item-row fingerprint changing would surface those findings as new or worsened in compare_audits. Traced, that does not happen, and no mitigation is needed.

  • compare_audits never reads a stored fingerprint. flattenReport (audit-diff/identity.ts) computes its own fingerprints from the R2 report payload's ruleResults[].checks[].message, and audit-diff/resolve.ts selects only normalizedUrl, ruleId, checkName, locator, state from page_findings. Report checks are unchanged by this PR: fresh checks come from rule execution, and carried checks come from carriedFindingToCheck, which restores the page-level message from the stash. So both sides of a diff read exactly what they read before.
  • The merge does not key on the fingerprint either. computeMerge matches a prior finding by findingKey = (url, rule, check, locator) and simply stores the new fingerprint, preserving firstSeenAt. Nothing is re-created, so no finding reads as newly discovered and no first-seen date resets.
  • The issues tracker is untouched. issue-sync.ts writes title, description and recommendation from rule metadata, never from a finding message or fingerprint.

What does change is finding_defs, and in the intended direction. Projected over the current corpus it goes from 22,981 rows to about 102,499. That is not bloat: on the measured site, 4,036 distinct defects were sharing 561 fingerprints, so the dictionary was under-counting defects by 7x. Superseded rows become orphans as each site is re-audited and are removed by the existing sweepOrphanFindingDefs cron, which anti-joins finding_defs against page_findings on (site_key, fingerprint). Self-healing, no backfill and no migration.

Follow-up in the private repo

clampFindingPayload in the API's chunk ingest rebuilds the payload from a key allowlist (items, pages, details, i) and would silently drop m/v/e on the chunked-publish path. squirrelscan/repo#1890 adds the clamped pass-throughs and lands before the gitlink bump.

Blocks squirrelscan/repo#1880.

…e's count

`findingFingerprint` hashes [status, message, value, expected] and is
deliberately URL-free so `finding_defs` (site_key, fingerprint) can hold one
row per distinct defect. `flattenChecks` defeated that: it copied the parent
check's PAGE-LEVEL message/value/expected onto every item row, and a page-scope
check's message is a count of that page's items. One Shopify CDN script missing
SRI on 401 pages stored 25 messages ("26 cross-origin resources without
Subresource Integrity" on one page, "24 ..." on another) and 25 fingerprints
for one defect.

The damage ran both ways. On the measured site, item rows hold 4,036 distinct
(rule, check, locator) defects but only 561 distinct fingerprints, because the
hash keyed on the page's count rather than on the defect - so one dictionary row
stood in for many unrelated locators.

An item row's message is now the page-invariant part of its check's message
plus the item id:

  cross-origin resources without Subresource Integrity: https://cdn.../globo.js

`pageInvariantCheckText` drops a leading count and replaces any remaining digit
run with N, matching what the report's own grouping already shows for a merged
group. The id gets first claim on the store's message clamp, so a long URL is
never truncated away by a long prefix. `value`/`expected` were page-level
aggregates too and are dropped from the row. `item.label` deliberately does NOT
feed the message: several rules stamp per-page numbers into it
(content/keyword-stuffing emits `"everyday" (2.3%)`), which would re-create the
same split.

Nothing downstream changes text or score. The parent check's page-level trio is
stashed in the item payload as m/v/e, and both readers that rebuild a
CheckResult from storage - `carriedFindingToCheck` and `reconstructRuleChecks` -
restore it verbatim, so the replayed union check still groups as one. `m` is the
sole restore marker: a pre-#1881 row has none and falls back to its own columns,
a whole-check row never writes one, and an empty-id item is caught even though
it lands in a locator-"" group. The marker is scanned across the whole group,
because the stash is budgeted per item.

That budget only ever removes the STASH, never `items`/`details`/`pages`: the
chunk ingest drops an over-cap payload whole and `details.additional` feeds the
scorer's density penalty, so letting the stash tip a near-cap payload over the
line would inflate the health score (#1179 class). It does not trim detail to
fit either, because this same payload feeds the CLI's local SQLite store, which
has no cap - shrinking for transport belongs at the transport boundary. An
over-cap payload is therefore byte-identical to what it was pre-#1881.

Whole-check findings (locator "") are untouched.

Measured across the whole production corpus, 296 sites and 545,822 item rows:
distinct (site, rule, check, locator, status) is 91,944 and adding the new
message takes it to 91,966, so the text costs 22 tuples, 0.024%. All 22 are
named in public #231 (two rules alternating singular/plural, one interpolating
a product name). On the measured site, item-row inflation over the locator key
falls from 59.9% to 0.8% and whole-check rows are unchanged at 519.

No compare_audits churn, contrary to the issue's prediction: `flattenReport`
fingerprints the R2 report payload's checks, never the stored column, and report
checks are unchanged because carried checks restore the page-level message.
`computeMerge` matches priors by (url, rule, check, locator), not by
fingerprint, so first-seen dates are preserved. `finding_defs` grows from ~23k
to ~102k rows, which is the dictionary becoming correct rather than bloat, and
superseded rows are removed by the existing `sweepOrphanFindingDefs` cron.

Blocks #1880.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/1881-item-level-message branch from 7f17008 to eb21d97 Compare September 7, 2026 09:30
@nc9
nc9 merged commit 5d758ba into main Sep 7, 2026
13 checks passed
@nc9
nc9 deleted the fix/1881-item-level-message branch September 7, 2026 09:37
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