Skip to content

fix(rules): integrity controls: attributes read once and charged to a work budget; aria-labelledby target reads charged to the visits #1462 - #1538

Closed
nc9 wants to merge 4 commits into
mainfrom
fix/1462-integrity-attr-budget
Closed

nc9 wants to merge 4 commits into
mainfrom
fix/1462-integrity-attr-budget

Conversation

@nc9

@nc9 nc9 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1462.

On v0.0.159 the integrity rules were abandoned ("Not checked") on pages of tens of thousands of multi-attribute controls: controls.ts walked every attribute of every control with a string allocation per attribute and looked each control's attributes up a second time, with no work budget on either, and the aria-labelledby target reads were cached but not charged to the page's visits.

What changes (packages/rules/src/integrity/controls.ts)

  • readControlAttributes: one pass over a control's attributes (up to 4,096; names compared by length before any string work) returns href, aria-label, title, value, alt, aria-labelledby, the cue flag and how many attributes were looked at. The href and the cues are found wherever they sit in that range (a first version stopped at the 64th: a link or cue past it was counted unread instead of read, which lowered a Calendly kit page from fail to info; padding with 64 junk attributes was the evasion). The listing and prepare steps read it once; the second per-control lookup of 6 attributes is gone. A control with more than 4,096 attributes is counted in controlsUnread.
  • The listing pass spends a work budget of 2,000,000 units (one per control, one per attribute looked at). Controls it does not reach are counted unread, never clean. The prepare pass has a budget of 1,500,000 units (one per control, one per aria-labelledby id up to the 4 read) that ordinary pages cannot reach (at most 60,000 prepared controls at 5 units each is 300,000): it is a backstop. A control past it keeps its destination, loses its label attributes and is counted cut. Both are in attributeLimits (mutable, like integrityLimits) so tests can lower them.
  • A link whose attribute read is cut at the 4,096th is never a silent skip: a null href or a missing cue after a cut read is counted in controlsUnread (and the rule reports scanTruncated) before the control is dropped.
  • aria-labelledby target text is read for at most 300,000 visits in all (three quarters of TOTAL_VISITS), checked BEFORE each read: a page of thousands of distinct targets cannot spend more, and a control whose target is not read is counted cut. The visits come off TOTAL_VISITS before the controls' own reads share what is left. The reviewer's shape (100,000 nested <span id> targets, 25,000 links naming 4 distinct ids each, 4.3 MB) took 37.9 s with the budget off on the previous head; here 1.4 to 2.0 s at the default budget at load about 12 (controlsUnread 24,707, the real control found), and one of four runs was abandoned. The budgets bound the work a page can cost, not the time: a rule still runs against a wall-clock budget and a busy machine spends it. Not counted, not new: a cue in a target past its first 300 characters or 256 visits, and a button with no destination (an onclick-only button outside a form).
  • When the budgets cover the page nothing changes: the 14 existing integrity test files pass unchanged (293 tests).

How the budgets were chosen, and what real pages spend

Per unit the listing pass costs about 0.1 to 0.5 microseconds (one control plus its attributes), the prepare pass a few microseconds per control; 2,000,000 listing units is about 0.2 to 0.6 s of work on the densest shapes, and 1,500,000 prepare units about 0.3 s, so a page at the budgets stays well inside the 2 s rule budget. They are about twice what the heaviest shapes the rules read in full need (the 10.4 MB, 40-attribute shape is 1.3M listing units; 4.9 MB of 6-attribute links is 0.55M).

Measured on the 400 largest real HTML pages (0.4 to 6 MB) in a local content store of audited sites (script: ops/scratch/units-1462.ts, run from packages/rules with bun run; it prints the numbers below): listing units median 2,280, 95th percentile 17,747, maximum 33,041 (1.7% of the budget); prepare units at most 8,728 (0.6% of 1.5M); no page had a control past a budget (controlsUnread 0 on all 400). The three heaviest: 2.15 MB, 5,966 controls, 33,041 units; 2.07 MB, 5,966 controls, 33,030 units; 2.26 MB, 5,966 controls, 33,022 units.

machine under load 8 to 23, so wall-clock varies; bun 1.4.3, both integrity page rules through RuleRunner at the default 2 s budget, real control last; controls are off-site links unless stated)

Shape v0.0.159 this PR
4.9 MB, 5 extra attributes (about 76,000 controls) 865 ms 541 ms
7 MB, 40 extra attributes 672 ms 461 ms
10.4 MB, 40 extra attributes 2,139 ms 553 ms
10.4 MB, aria-labelledby with 5 ids abandoned at 3,706 ms 706 ms, Google found, controlsUnread 173,099
135,000 links naming 2 ids (5 MB) 2,087 ms 500 ms
330,000 <img> in one control 1,223 ms 401 ms

scanControls alone on the 4.9 MB shape went from about 840 ms to about 400 ms, and on the 10.4 MB 40-attribute shape from about 1,070 ms to about 220 ms, in the same runs. On the 10.4 MB shapes the real control is found, or controlsUnread is reported.

Tests

Deterministic, by counts (no wall-clock):

  • integrity-caps-1462.test.ts (12): the number of controls each cap cuts, exact: MAX_PREPARED (62,000 off-site links and a sign-in path link: 60,000 prepared, controlsUnread 2,001, the sign-in link kept), MAX_LABELLEDBY (5 ids: 1, 4 ids: 0), the page's visits (2,000 controls of 241 visits: 1,833 counted; 1,000 of them: 0; 300 controls of 601 visits: 233 counted; a control of about 4,200 visits: 1), the edges (1,600 controls of 250 visits read whole, 1,601 leave 90 counted; 300 controls of 256 visits read whole, of 257 leave 144 counted; the floor of 4 visits with 30,000 controls once the targets' reads are spent), MAX_ALTS_PER_CONTROL, and the distinct targets (10,000 nested targets named by 2,500 links: exactly 1,172 are read and 2,207 controls are counted; 1,000 targets that fit are all read; integrityCounters, a test hook that is off in production).
  • integrity-attribute-budget-1462.test.ts (13): readControlAttributes (the href and cues found at the 64th, 65th, 101st and 4,000th attribute; overlong past 4,096), the listing and prepare budgets by exact counts, a cut read is never a silent skip, the target visits charge, the default budgets pinned.
  • integrity.test.ts (3 added): the Calendly kit page stays fail (brand and obfuscated-script) with the credential link's href at the 64th, 65th, 101st and 4,000th attribute; an off-site link labelled by an aria-label at the 70th attribute is a finding; an input type=submit value and an input type=image alt past the 64th attribute in an off-site form are findings.
  • Mutants, each restored from a clean copy, all fail a deterministic test: MAX_PREPARED, MAX_LABELLEDBY, TOTAL_VISITS, SHORT_VISITS (255 and 257), MIN_ALLOWANCE (3), LONG_VISITS, LONG_BUDGET_VISITS and MAX_ALTS_PER_CONTROL changed or made unbounded; the target cache removed; the target budget check removed (the survivor of the previous round), the control not counted when a target is not read, the target-visits charge removed; the listing charge, exhaustion check, prepare charge, covered, the id charge, the overlong cut and both new default budgets; the per-control attribute cap at 64, 4,095 and 4,097; the existing window tests kill the five window caps and the floor. Not distinguishable by any fixture: TOTAL_VISITS by one visit (a cut read costs less than the share, so the share grows back), LONG_BUDGET_VISITS by one (the last read overshoots anyway), MIN_ALLOWANCE raised (more reading, no cut).

Through RuleRunner at the default 2 s budget (integrity-default-budget-1462.test.ts, 18; the old fixture's ruleTimeBudgetMs: 0 hid the abandonment): per shape (4.9 MB, 7 MB with 40 attributes, 7 MB with 5 ids, 135,000 labelled links, 330,000 <img>) the real control is found or controlsUnread is reported with the budget off; the same shapes at a quarter of the size are not abandoned at the default budget (always run); and the full-size default-budget run is skipped when the machine is busy (loadavg above half the CPUs), since the budget is wall-clock. With that guard forced off at load 12 to 17 the full-size run passed on this PR, and on v0.0.159 the 330,000-<img> shape was abandoned. The abandonment depends on machine load near the 2 s line, which is why the fix is a per-attribute and per-target charge. The 10 MB shapes are measured above, not tests: they take 0.9 GB to parse.

Docs: docs/rules/integrity/brand-impersonation.mdx states the attribute pass, both budgets and the target charge. CHANGELOG under Unreleased. Skill change: none.

nc9 added 4 commits November 2, 2026 16:34
…ge work budget; aria-labelledby target reads charged to the visits #1462

Signed-off-by: Nik Cubrilovic <[email protected]>
…ds counted; default-budget shapes also at a quarter size #1462

Signed-off-by: Nik Cubrilovic <[email protected]>
…ounted unread, not skipped as having no destination or cue #1462

Signed-off-by: Nik Cubrilovic <[email protected]>
…p 4,096), distinct aria-labelledby targets metered before reading, edges pinned, test hook off in production #1462

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

nc9 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Shipped in v0.0.160. The commits were cherry-picked onto the release branch and rebase-merged: cf70c35 629d494 73c9daa e6b0d6b.

Closing this PR since the change is on main.

nik

@nc9 nc9 closed this Nov 2, 2026
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.

integrity rules: budgets count nodes, not attributes, so 5-7 MB of tiny multi-attribute controls abandons the rules

1 participant