Skip to content

fix(rules): read integrity auth cues across element boundaries #696 - #749

Merged
nc9 merged 5 commits into
mainfrom
fix/integrity-text-boundary-696
Oct 14, 2026
Merged

nc9 merged 5 commits into
mainfrom
fix/integrity-text-boundary-696

Conversation

@nc9

@nc9 nc9 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #696.

Security note (hold-list): #696 is labelled security. This PR closes a detection-evasion gap. A phishing page could stop integrity/brand-impersonation from firing, and downgrade a correlated fake-auth-overlay from fail to info, just by removing the whitespace between its elements. Nothing else about the kit had to change. The fix only widens what the detectors can see (the extra text can find a cue, never hide one). The false-positive guards are unchanged: a brand-labelled control with an off-brand destination, and the two-signal escalation gate.

What changed

detectBrandImpersonation (packages/rules/src/integrity/signals.ts) gated everything on a word-bounded sign in test over the title and content.textContent. That text glues adjacent elements: with no whitespace between <h1>Applicability fixture</h1> and the off-brand "Sign in with Google" link it read fixturesign in with google. The gate failed before the link's own evidence was considered, brand-impersonation never fired, and the correlated fake-auth-overlay dropped from fail to info.

  • Boundary-aware haystack: a new authHaystack(ctx) reads the auth and brand cues from the glued text and the element-separated text (getPageSeparatedText, from schema/rating-scope, integrity/kit-signature: element-glued text breaks word-boundary checks #739 / fix(rules): read element-separated text in schema/rating-scope #739 #748). It is used by detectBrandImpersonation and by detectFakeAuthOverlay's page-level auth corroboration. Every test on that haystack asks whether a cue appears, so the extra text can only find a cue, never hide one.
  • Control evidence: a sign-in control's own label counts as a credential affordance. credentialDestinations only returns password forms and links whose own text matches sign in, log in, login, continue with or verify, so a missing page-wide word boundary no longer overrides that evidence.
  • Unchanged: the decisive condition (a control labelled with the brand whose destination is neither the brand's host nor the site's own), the two-signal escalation gate, and detectSeoDoorway. The doorway detector computes word counts and keyword density from its text, which a second copy would change.
  • Parser untouched: extractContent still feeds wordCount, contentHash and isThinContent exactly as before.

Acceptance criteria

  • The reporter's packages/rules/tests/integrity-text-boundary.test.ts is added as filed and passes. The compact markup now yields both brand-impersonation and fake-auth-overlay.

  • fake-auth-overlay reports fail with the correlated brand signal, with or without whitespace.

  • The text read by detectPageSignals has a boundary at every element, via the shared getPageSeparatedText.

  • No new false positives: the existing integrity suites pass unchanged (integrity.test.ts, integrity-cloaking.test.ts, integrity-intel.test.ts; 94 tests with the new file). New guard cases cover:

    • a compact legit accounts.google.com link (no signal);
    • a self-hosted sibling-subdomain link, glued and spaced (no signal);
    • a bare brand mention (no signal);
    • a compact off-brand link without an overlay (lone signal, info).

    streaming-collector-rules-golden and streaming-rules-canonical-golden pass with no re-pin.

  • Other readers of the same text:

On main, three of the nine tests in the new file fail: the reporter's regression, the lone off-brand link, and the glued off-self overlay corroboration. I checked that by swapping main's signals.ts back in.

Verification

  • bun test packages/rules/tests/integrity.test.ts packages/rules/tests/integrity-cloaking.test.ts packages/rules/tests/integrity-intel.test.ts packages/rules/tests/integrity-text-boundary.test.ts: 94 pass (103 with glued-element-text.test.ts).
  • bun test packages/audit-engine/tests/streaming-collector-rules-golden.test.ts: pass.
  • bun test packages/audit-engine/tests/streaming-rules-canonical-golden.test.ts: pass, no re-pin.
  • tsgo --noEmit in packages/rules: clean. oxlint's one error in signals.ts (no-object-as-default-parameter at opts = {}) and its oxfmt drift are both pre-existing on main and outside this diff.
  • docs/rules/integrity/brand-impersonation.mdx and fake-auth-overlay.mdx describe the boundary-aware reading. make validate passes.

Security review follow-up (063b5c9)

An Opus security review of the rebased head found two issues, both fixed here.

  • Gate widened. The credential-affordance gate counted every auth-labelled link. A lone "Verify your Google account" or "Continue with Microsoft account" link to a third party, with no sign-in copy on the page, now fired brand-impersonation (info, or fail with a second signal). That contradicted "the false-positive guards are unchanged". Now only a link whose own label says sign in, log in or login counts as an affordance on its own, which still covers the reporter's case.
  • Hidden whitespace (older, same bug class). Whitespace or an invisible character inside the link's own label still hid the cue: a newline, a double space, &nbsp;, <br>, a spacer span, U+200B or a soft hyphen in Sign in with Google. Each label is now read as written and element-separated, with invisible characters removed and whitespace collapsed, and the page haystack is read the same way.
  • Cost. The haystack is memoised per document; several detectors read it.

Two new tests cover these: seven label variants fire, and the verify / continue-with links stay silent. Both fail on 3edb9e9 and pass now.

The reviewer's remaining items predate this PR and are filed separately: label only in an attribute, <button>/<input> controls, "Continue with Google" missing from the lexicon, homoglyphs, identity brokers (Auth0, Okta) read as impersonation, and a few Microsoft hosts.

Also in this PR

Two #748 review nits on the shared helper it now has a second consumer for:

  • the getPageSeparatedText doc comment says the per-document cache assumes a read-only DOM;
  • the empty-body test asserts "" instead of only the return type.

Agent skill

No change needed.

@nc9
nc9 marked this pull request as ready for review October 13, 2026 14:01
@nc9

nc9 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Held for maintainer review: this closes a detection-evasion gap (security label), so it is not auto-merged. CI and review are the lane's to confirm; merge is Nik's call.

@claude

claude Bot commented Oct 13, 2026

Copy link
Copy Markdown

Review of #749: reads brand/auth cues from element-separated text as well as glued text. The change is small and well scoped. I read the diff and the getPageSeparatedText implementation. I did not run the test suite.

  • (low) packages/rules/src/integrity/signals.ts:282 credDests.length > 0 makes any credential control count as an affordance, so the page-wide sign in text test no longer gates brand-impersonation. The later brand-label and off-brand-host checks still apply, so I expect no new false positives. A test for a page with a brand-labelled verify link and no other auth copy would pin that behaviour down.
  • (low) signals.ts authHaystack: the comment says the extra text "can only find a cue, never hide one". That holds for the sign in tests. The split-word behaviour (Goo<b>gle</b> reads Goo gle) means the brand lexicon is read from text that contains both forms, so it can also match brand terms across element boundaries, for example Micro followed by soft in adjacent elements would not match but Docu Sign could produce odd bigrams. This is unlikely to matter in practice, but the comment slightly overstates the guarantee.
  • (nit) packages/rules/src/content/text-content.ts The new read-only-DOM caveat is accurate. Note that the cache is keyed on doc, so it is only safe if no rule mutates the document earlier in the pipeline.
  • (nit) packages/rules/tests/glued-element-text.test.ts The reworked "no body" test is better than the old one, which only asserted typeof. Removing body after parsing does exercise the "" branch.
  • (nit) integrity-text-boundary.test.ts The tests cover the glued and spaced variants of both rules plus the non-regression cases. A test for the new credDests.length > 0 affordance path with no password field and no auth copy would complete the coverage.

I found no critical or high issues. The CHANGELOG and docs updates match the behaviour.

@nc9
nc9 force-pushed the fix/integrity-text-boundary-696 branch from 7309c31 to 3edb9e9 Compare October 14, 2026 20:37
@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review of #749: the fix looks correct and well scoped. I read the diff only and did not run the tests.

  • (low) signals.ts (hasCredentialAffordance): adding credDests.length > 0 removes the page-level affordance check for any page with a verify or continue with link. This is safe today because the off-brand, brand-labelled control condition still gates the signal. If credentialDestinations is ever loosened, this line stops being a backstop, so a short comment tying the two together would help.
  • (nit) authHaystack concatenates three copies of the page text for every call. That is fine for these rules, but you could skip separated when it equals text. This is not worth doing unless profiling shows a cost.
  • (nit) The separated text can also split a word an author wrapped across inline elements (Sign<b>in</b>), so it could create a cue as well as reveal one. Your doc comment notes the split-word case. The effect on false positives looks negligible, and the new guard tests cover the main cases.
  • (nit) Test coverage is good. Consider adding one case where a password field is the only affordance in compact markup, plus a case with a split brand word.

No blocking issues.

@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review of #749: the change looks correct and I found nothing blocking.

  • (low) signals.ts: authHaystack now holds the title, the glued text and the separated text. That is roughly 2x the page text, kept in a WeakMap per document, and it is also run through readable() (two regex passes). It's bounded by the document's lifetime and cached, so this is fine. If large pages ever show up in profiles, this is the first place to look.
  • (low) signals.ts: detectFakeAuthOverlay still uses its own regex, which has no 'login', while detectBrandImpersonation includes it. This was already the case before the PR. Consider making the two agree, or add a comment saying why they don't.
  • (nit) haystackCache is declared below authHaystack. It works, because the const is initialised before any call. Moving it above the function would read better.
  • (nit) INVISIBLE does not cover U+200E/U+200F (directional marks) or U+2061 to U+2064. Add them if you want the 'invisible characters' claim in the docs to be complete.
  • (nit) One of the new tests contains a literal zero-width character in its label string. It is easy to lose in an editor or in a diff. Using a u200b escape would make the intent explicit.

Test coverage is good. The new tests cover the glued and spaced cases, newline, nbsp, br and invisible characters in the label, plus the negative cases for verify-only and continue-with-only links. The CHANGELOG and docs are updated.

nc9 added 4 commits October 15, 2026 07:57
detectBrandImpersonation gated on a word-bounded "sign in" test over
`content.textContent`, which glues adjacent elements: with no
whitespace between `<h1>Applicability fixture</h1>` and an off-brand
"Sign in with Google" link the text read "fixturesign in", the gate
failed, brand-impersonation never fired and a correlated
fake-auth-overlay dropped from fail to info.

Read the title and copy for the auth and brand cues from the glued text
and the element-separated text (getPageSeparatedText from #739), in
brand-impersonation and in fake-auth-overlay's page-level corroboration,
and count a sign-in control's own label (credentialDestinations) as a
credential affordance. Every test on that haystack asks whether a cue
appears, so the extra text can only find one. seo-doorway is untouched:
its word counts would change.

Adds the reporter's integrity-text-boundary.test.ts plus the cases the
fix must not change (legit Google link, self-hosted link, bare brand
mention, lone signal).

Signed-off-by: Nik Cubrilovic <[email protected]>
…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]>
…n affordance #696

Security review follow-up. A newline, double space, &nbsp;, <br> or a
zero-width character inside a link's label still hid "Sign in with Google":
read each label as written and element-separated, invisible characters
removed and whitespace collapsed, and the page haystack the same way. The
credential-affordance gate counted every auth-labelled link, so a lone
"Verify your Google account" link to a third party could fire; only a label
that says sign in or log in counts now. Memoise the haystack per document.

Signed-off-by: Nik Cubrilovic <[email protected]>
…character #696

Delta review: reading every anchor's label in full twice grew with the
square of nested unclosed anchors (2,000 took 1.5 to 3.6 s, past the rule
budget), so junk markup could hide a real signal. Skip a link whose text is
over 300 characters, and skip the element-separated read for a text-only
link. Strip Unicode's whole default-ignorable set (U+2063, U+034F were
missed). Tests time nested anchors at n, 2n and 4n.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/integrity-text-boundary-696 branch from 063b5c9 to 6f48142 Compare October 14, 2026 20:58
@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review of #749. The fix is sound and the false-positive guards are preserved. Two things to look at, plus nits. I read the diff only and did not run the tests.

  • (medium) packages/rules/src/integrity/signals.ts (credentialDestinations): if (raw.length > MAX_LABEL_CHARS) continue; creates a new evasion path in a PR that closes one. A phishing link whose textContent is over 300 characters is dropped from credDests. Padding the anchor with a visually hidden span, or wrapping a card or tile in it, is enough. The decisive brand-with-off-brand-host condition then never fires. A skip also loses the signIn evidence. Instead of skipping, truncate (raw.slice(0, MAX_LABEL_CHARS)) and still test the head. Or cap the total work across anchors, so the quadratic guard stays without making a long label a bypass. Please add a test with a long label.
  • (low) packages/rules/tests/integrity-text-boundary.test.ts (last test): hard wall-clock thresholds (400/700/1200 ms) will flake on loaded CI runners. Assert on how the work scales, for example by counting calls or comparing the 2n and n ratio, or loosen the bounds a lot.
  • (nit) MAX_LABEL_CHARS and the regexes are declared inside the function, so they are rebuilt on every call. Hoist them to module scope next to INVISIBLE.
  • (nit) authHaystack is defined before haystackCache and INVISIBLE. That works because they are only used at call time, but declaring the constants first reads better.
  • (nit) The new tests put literal invisible characters in the label strings, for example the zero-width space in the sign-in label and the soft hyphen in the Google label. Use \u escapes, as the later test does, so the characters are visible in review.

… skip it #696

Delta review: skipping a label over 300 characters let a kit pad one (a
<style>, an SVG title, sr-only text) and evade both rules, where main read
it. Read a long label as main did, lowercased, and skip only the
normalisation and the element-separated read. The timing test now measures
the detector against a bare textContent loop over the same anchors, so both
see the same machine load.

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

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review of #749. I read the diff only and did not run the tests.

The fix looks correct and well scoped. Reading the cues from both the glued and the element-separated text can only add matches, so it closes the evasion gap in #696 without touching the false-positive guards. The tests cover the reporter's repro, the unchanged controls, and invisible-character and whitespace splits inside the label.

  • (low) packages/rules/tests/integrity-text-boundary.test.ts (last test): the performance assertion detector < 3 * baseline + 100 compares wall-clock timings. It could flake on a noisy CI runner. The +100 ms slack and the best-of-2 runs help. If it does flake, consider a coarser absolute bound or an operation count.
  • (low) signals.ts, credentialDestinations: labels longer than MAX_LABEL_CHARS (300) skip normalization and the separated-text pass. A kit can pad a label so that Sign in is split by whitespace or an invisible character, and it will still evade. The test comment acknowledges this as a tracked decoy case. I'd link the tracking issue from the code comment.
  • (nit) authHaystack also folds the invisible-character stripping and whitespace collapsing into detectFakeAuthOverlay's haystack. That is a slight behaviour change beyond what the PR title says. It only widens detection, and the changelog already mentions it.
  • (nit) MAX_LABEL_CHARS is declared inside the function, so it is re-created on every call. Hoisting it to module scope would match the other constants.

No blocking issues.

@nc9
nc9 merged commit 10d82ab into main Oct 14, 2026
15 checks passed
@nc9
nc9 deleted the fix/integrity-text-boundary-696 branch October 14, 2026 21:19
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/brand-impersonation loses a signal without inter-element whitespace

1 participant