Skip to content

Commit f64b083

Browse files
committed
fix(scanners): narrow math-alphanumeric evasion to ASCII-mapping letters
Restrict the rule to U+1D400-U+1D6A3, the styled letters that NFKC to ASCII. The Greek and digit subranges cannot spell an ASCII instruction. Skip single-character runs when grouping across whitespace, so spaced variables ("let a b c d be reals") are no longer joined into a finding. Chunked payloads use runs longer than one character and still block. Pin the threshold with tests at 3 (allow) and a contiguous 4 (block).
1 parent eb7b4da commit f64b083

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

‎sdk/src/unplug/scanners/injection/scanner.py‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@
3232
# ASCII substitutes. Ordinary CJK punctuation (,。) and ideographs must not
3333
# be flagged.
3434
_CONFUSABLE_RE = re.compile(r"[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a\u24b6-\u24cf\u24d0-\u24e9]+")
35-
# Mathematical alphanumeric symbols (U+1D400-U+1D7FF: bold, italic, script,
36-
# fraktur, double-struck, sans-serif, monospace letters/digits) normalize
37-
# straight to ASCII under NFKC, same as the confusables above. Unlike those,
38-
# ordinary math notation strings several styled variables together ("let
39-
# f(x) = ax + b", "define xy as the product"), so this block can't be
40-
# unconditionally suspicious at any short run length. Benign notation tops
41-
# out at 2 adjacent styled characters; a payload rendered in math-bold runs
42-
# 9-12. The threshold sits between the two: below it is notation, at or
43-
# above it is a styled word.
35+
# Mathematical alphanumeric letters (U+1D400-U+1D6A3: bold, italic, script,
36+
# fraktur, double-struck, sans-serif, monospace) normalize straight to ASCII
37+
# under NFKC, same as the confusables above. The Greek and digit subranges
38+
# after U+1D6A3 do not normalize to ASCII letters, so they cannot spell an
39+
# instruction and are left out. Ordinary math notation strings a few styled
40+
# variables together ("let f(x) = ax + b"), so a run only counts as evasion
41+
# at _MATH_ALPHANUMERIC_MIN_RUN characters: benign notation tops out at 2-3
42+
# adjacent styled characters, while a payload in math-bold runs 9-12.
4443
_MATH_ALPHANUMERIC_MIN_RUN = 4
45-
_MATH_ALPHANUMERIC_RE = re.compile(r"[\U0001d400-\U0001d7ff]+")
44+
_MATH_ALPHANUMERIC_RE = re.compile(r"[\U0001d400-\U0001d6a3]+")
4645
_WORD_RE = re.compile(r"\w+")
4746

4847
# Only collapse evasion spans that sit close together (e.g. zero-width chars
@@ -85,10 +84,14 @@ def _group_whitespace_runs(
8584
the grouped runs' own lengths, not end - start: measuring span width
8685
would let a wide whitespace gap between two short runs *dilute* the
8786
count exactly the way this exists to prevent, in the other direction.
87+
88+
Single-character runs are not grouped: spaced single variables ("let a b
89+
c d be reals") are ordinary notation, and a chunked payload's fragments
90+
are longer than one character.
8891
"""
92+
runs = sorted(run for run in runs if run[1] - run[0] > 1)
8993
if not runs:
9094
return []
91-
runs = sorted(runs)
9295
groups: list[list[tuple[int, int]]] = [[runs[0]]]
9396
for span_start, span_end in runs[1:]:
9497
_, last_end = groups[-1][-1]

‎sdk/tests/unit/scanners/test_scanners.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ def to_math_bold(s: str) -> str:
145145
findings = self.scanner.scan(_make_text(chunked), self.ctx)
146146
assert any(f.subcategory == "invisible_text" for f in findings)
147147

148+
def _has_invisible_text(self, raw: str) -> bool:
149+
findings = self.scanner.scan(_make_text(raw), self.ctx)
150+
return any(f.subcategory == "invisible_text" for f in findings)
151+
152+
def test_math_run_threshold_is_pinned(self):
153+
# 3 contiguous styled letters is notation; 4 is a styled word.
154+
assert not self._has_invisible_text("the term \U0001d5ee\U0001d5ef\U0001d5f0 here")
155+
assert self._has_invisible_text("the term \U0001d5ee\U0001d5ef\U0001d5f0\U0001d5f1 here")
156+
157+
def test_spaced_single_math_variables_not_invisible_text(self):
158+
# Four single styled variables listed with spaces are notation, not a
159+
# chunked payload. See PR #151 review.
160+
assert not self._has_invisible_text(
161+
"let \U0001d44e \U0001d44f \U0001d450 \U0001d451 be positive reals"
162+
)
163+
assert not self._has_invisible_text(
164+
"the hierarchy \U0001d553 \U0001d564 \U0001d55a \U0001d55d"
165+
)
166+
167+
def test_math_greek_and_digits_not_invisible_text(self):
168+
# These subranges do not NFKC to ASCII letters, so they cannot carry
169+
# an ASCII instruction. See PR #151 review.
170+
assert not self._has_invisible_text("coupling " + "\U0001d6fc" * 4 + " here")
171+
assert not self._has_invisible_text("chapter \U0001d7d0\U0001d7d1\U0001d7d2\U0001d7d3")
172+
148173
def test_interleaved_zero_width_emits_single_outer_span(self):
149174
raw = "h\u200be\u200bl\u200bl\u200bo w\u200bo\u200br\u200bl\u200bd"
150175
findings = self.scanner.scan(_make_text(raw), self.ctx)

0 commit comments

Comments
 (0)