Skip to content

fix(audit): a rule scoped to another document is not judged by this window - #25

Merged
adamXbot merged 2 commits into
mainfrom
fix/moz-document-scope
Aug 31, 2026
Merged

adamXbot merged 2 commits into
mainfrom
fix/moz-document-scope

Conversation

@adamXbot

@adamXbot adamXbot commented Aug 31, 2026 •

Copy link
Copy Markdown
Collaborator

The live audit only ever opens a browser window, but a theme also styles the Library, DevTools and about: pages through @-moz-document. Extraction was blind to that scoping, so every such rule was compared against the browser window's DOM — which is not evidence about it either way, and which invents renames between unrelated documents.

The case that found it

WhiteSur's #placesToolbar is the Library window's toolbar. The theme is correct:

source
Firefox 153 omni.ja places.xhtml:1154 — <toolbar class="chromeclass-toolbar" id="placesToolbar" …>
Searchfox mozilla-esr140 places.xhtml:164 — same lowercase id
browser.xhtml:6472 id="PlacesToolbar" — a different element, the browser-window bookmarks toolbar

The live DOM lacks the Library's toolbar, so _is_near_miss matched the capital-P name one letter of case away and --patch called the rewrite confident. Applying it would have unstyled the Library window.

The change

extract_tokens records the @-moz-document condition each token sits under. suggest() skips every DOM-derived branch when a use is scoped to a document this audit does not open.

The shipped chrome is still consulted — omni.ja carries those documents — so a name Firefox ships resolves to offscreen exactly as before. Only when the pack cannot answer either does the new other-document confidence apply. It is deliberately distinct from unresolved: "not entitled to look" is a different claim from "looked and found nothing". Both are non-actionable, so --strict and --patch ignore them.

Unmodelled conditions (domain(), regexp()) count as covering the audited window. Being wrong that way reports a finding a human resolves; being wrong the other way hides a real one.

Two things this had to get right

Findings are grouped per scope, not per token. A token can be written both globally and inside an @-moz-document block, and --patch rewrites the exact file:line sites a finding carries — so one shared verdict let the confident half drag the unjudgeable half into the patch, which is the very failure the scope check exists to prevent. Caught in review by the session working #24. The findings sort gained a (file, line) tiebreak, since two findings can now share a token.

A condition wrapped across lines is read whole. Evaluated from its first line alone it finds no url() at all, scores as excluding the audited window, and silently hides every finding inside — worse than the false positive this fixes, because it fails in the direction of silence. All three shapes are pinned: brace on the prelude's last line, brace on its own line, and a wrapped multi-condition list.

Measured

Against WhiteSur on Firefox 153, pristine at its master:

  • findings 2 → 1; offscreen 78 → 79
  • the emitted patch no longer contains the bad rename
  • the remaining finding is real, and that theme's #221 fixes it; with both applied the full CI line exits 0 with an empty patch

247 tests pass (230 existing + 17 new), covering url/url-prefix/regexp conditions, scope ending with its block, a } inside a string, a nested @media not closing the block early, three multi-line prelude shapes, one token under two scopes, and the #placesToolbar regression directly.

Relationship to #24

Overlaps #24, does not replace it. #24 makes the shipped-chrome pack outrank the fuzzy matchers for any token Firefox ships under its own name; this fixes scope-blindness, including names absent from the pack. Both fix #placesToolbar, by different routes, which is a useful cross-check on each.

#24 should land first. This branch is off main and both touch suggest(), so this one takes the rebase — a one-function conflict.

The --audit upgrade-gate fix is deliberately not here. That gate used an inverted filter (!= "unresolved"), so offscreen — a name Firefox still ships — counted as a reason to block an upgrade, and other-document would have inherited it. #24 now fixes it properly by naming the predicate once in audit.actionable() and calling it from both report() and the CLI, so no future confidence can regress it. That is better than the one-line version I had here, so this PR drops it and depends on #24 for it.

…indow

The live audit only ever opens a browser window, but a theme also styles
the Library, DevTools and about: pages through @-moz-document. Extraction
was blind to that scoping, so every such rule was compared against the
browser window's DOM -- which is not evidence about it either way, and
which invents renames between unrelated documents.

WhiteSur's `#placesToolbar` is the case that found it. That is the
Library window's toolbar, `places.xhtml:1154` in the shipped omni.ja and
`places.xhtml:164` on ESR 140, so the theme is correct. The live DOM
lacks it, and `_is_near_miss` then matched browser.xhtml's
`#PlacesToolbar` -- the main window's bookmarks toolbar, a different
element one letter of case away -- at which point --patch called the
rewrite confident. Applying it would have unstyled the Library.

extract_tokens now records the @-moz-document condition each token sits
under, and suggest() skips every DOM-derived branch when a use is scoped
to a document this audit does not open. The shipped chrome is still
consulted: omni.ja carries those documents, so a name Firefox ships
resolves to `offscreen` as before. Only when the pack cannot answer
either does the new `other-document` confidence apply -- distinct from
`unresolved`, which means "looked and found nothing" rather than "not
entitled to look". Both are non-actionable, so --strict and --patch
ignore them.

Two things this had to get right:

Findings are grouped per scope, not per token. A token can be written
both globally and inside an @-moz-document block, and --patch rewrites
the exact file:line sites a finding carries -- so one shared verdict let
the confident half drag the unjudgeable half into the patch, the very
failure the scope check exists to prevent. Caught in review by the
session working #24. The findings sort gained a (file, line) tiebreak,
since two findings can now share a token.

A condition wrapped across lines is read whole. Evaluated from its first
line alone it finds no url() at all, scores as excluding the audited
window, and silently hides every finding inside -- worse than the false
positive this fixes, since it fails in the direction of silence. All
three shapes are pinned: the brace on the prelude's last line, the brace
on its own line, and a wrapped multi-condition list.

Measured against WhiteSur on Firefox 153: findings 2 -> 1, offscreen
78 -> 79, and the emitted patch no longer contains the bad rename. The
remaining finding is real and fixed by that theme's #221.

Co-Authored-By: Claude Opus 5 <[email protected]>
@adamXbot
adamXbot force-pushed the fix/moz-document-scope branch from 0165eda to 58473cf Compare August 31, 2026 05:15
An unbalanced brace inside an unquoted url() closes the block early if
brace counting runs on anything less neutralised than `cleaned` -- and
every rule after it silently goes back to being judged against the
browser window, reinstating the false positive this fixes. Balanced
braces cancel out, so it takes a deliberately unmatched one to catch.
Found by the session working #24, against their own implementation;
this one is already correct, and now says so.

Also pins mixed condition lists in both directions, since `_scope_covers`
handles them by construction rather than by an explicit branch.

Co-Authored-By: Claude Opus 5 <[email protected]>
@adamXbot
adamXbot merged commit c16c85b into main Aug 31, 2026
5 checks passed
@adamXbot
adamXbot deleted the fix/moz-document-scope branch August 31, 2026 06:13
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