From 66756e2e5790b46862bc84b7b9042c31af192c02 Mon Sep 17 00:00:00 2001 From: CUDE Date: Mon, 20 Jul 2026 00:40:58 +0200 Subject: [PATCH 1/2] test: cover the last reachable branches in analyze Three branch gaps, now closed: - The BMP non-character block U+FDD0-U+FDEF was never exercised, though its sibling U+xxFFFE/FFFF check was. - The whole-token ASCII guard on the styled-confusable rule never took its false path: a token can have >=2 letters that individually fold to ASCII while the token itself does not (styled "pr" + an accented e). - Runs of enclosed digits were documented but untested. analyze.ts branch coverage 98.52% -> 99.5%. The one remaining branch is unreachable by construction: characters only enter a styled run if they already pass the same two tests the flush guard re-applies. Left in place as defense-in-depth rather than deleted. Co-Authored-By: Claude Opus 4.8 (1M context) --- test/analyze.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/analyze.test.ts b/test/analyze.test.ts index 032b8be..795cbea 100644 --- a/test/analyze.test.ts +++ b/test/analyze.test.ts @@ -77,6 +77,16 @@ describe('analyze — spoofed content', () => { expect(hot!.signals).toContain('confusable_word'); }); + it('flags Unicode non-characters as illegal code points', () => { + // Two disjoint ranges reach the same verdict: the BMP block U+FDD0–U+FDEF + // and the U+xxFFFE/U+xxFFFF pair that closes every plane. + for (const text of ['hi ﷐ there', 'hi ￾ there', 'hi \u{1fffe} there']) { + const r = analyze(text); + expect(r.signals.illegal).toBe(true); + expect(r.normalized).toBe('hi there'); + } + }); + it('reports codepoint-accurate word offsets', () => { const r = analyze(CUSTOMER_SAMPLE); for (const w of r.words) { @@ -109,6 +119,20 @@ describe('analyze — legitimate content', () => { expect(r.normalized).toBe(text); }); + it('does not call a styled word confusable unless the whole word folds to ASCII', () => { + // "𝗉𝗋é" has two styled letters that individually fold to ASCII, but the + // token folds to "pré" — still non-ASCII, so it is not a disguised word. + const r = analyze('𝗉𝗋é'); + expect(r.signals.confusable_word).toBe(false); + }); + + it('ignores runs of enclosed digits, which fold to non-letters', () => { + // ①②③ → "123": a styled run, but not a disguised WORD. Legitimate + // enclosed numbering must survive untouched. + const r = analyze('see item ①②③ below'); + expect(r.signals.confusable_word).toBe(false); + }); + it('treats expected scripts as legitimate for whole words', () => { // Without expectedScripts this could look suspicious in Latin-dominant // text; with them, whole Cyrillic words are the sender's normal traffic. From 1554e562709ec19e96f701b7eb2b473e1c8ba6fc Mon Sep 17 00:00:00 2001 From: CUDE Date: Mon, 20 Jul 2026 00:43:01 +0200 Subject: [PATCH 2/2] docs: put the enclosed-digit note where the filtering happens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The styled-run block comment credited the flush guard with excluding digit runs, but digits are excluded earlier — they never join a run, because a styled digit fails the letter test at the accumulation step. Move the note there and mark the flush guard as the redundant check it is, so the next reader does not go hunting for a test that can cover it. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/analyze.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/analyze.ts b/src/analyze.ts index d07d378..fb91957 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -430,14 +430,15 @@ export function analyze(text: string, options: AnalyzeOptions = {}): AnalysisRes // are symbols, not letters (category So): circled "Ⓐⓓⓜⓘⓝ", parenthesized, // squared. A run of >=2 such glyphs whose NFKC form is an ASCII word is the // same disguised-word attack as fullwidth/math styling (which tokenize and are - // handled per-token). Runs of styled DIGITS (①②③ -> 123) fold to non-letters - // and are ignored, keeping legitimate enclosed numbering out. + // handled per-token). { let runStart = -1; let runCount = 0; const flush = (end: number) => { if (runStart >= 0 && runCount >= 2) { const fold = text.slice(runStart, end).normalize('NFKC').normalize('NFC'); + // Belt-and-braces: every character in the run already passed these two + // tests individually, so the concatenation cannot fail them. if (ASCII_PRINTABLE_RE.test(fold) && ASCII_LETTER_RE.test(fold)) { signals.confusable_word = true; words.push({ @@ -458,6 +459,8 @@ export function analyze(text: string, options: AnalyzeOptions = {}): AnalysisRes const width = cp > 0xffff ? 2 : 1; const ch = String.fromCodePoint(cp); const nf = ch.normalize('NFKC'); + // Styled DIGITS (①②③ -> 123) fail ASCII_LETTER_RE and so never join a + // run, keeping legitimate enclosed numbering out. const styledLetter = !TOKEN_CHAR_RE.test(ch) && nf !== ch &&