Created
September 4, 2026 11:26
-
-
Save raeq/62676d7258651578f89324de7e382d3b to your computer and use it in GitHub Desktop.
raeq/disarm#977: weaponizing-unicode against the normative confusables.txt — corpus composition and detector coverage, reproduction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """weaponizing-unicode against the normative confusables.txt: what the corpus is, and what each detector covers. | |
| Reads the paper's `new_predicted_homoglyphs.txt` from $DISARM_META_WEAPONIZING or the meta-benchmark | |
| cache, and UTS #39 `confusables.txt` from $DISARM_META_CONFUSABLES or `data/confusables.txt` in the | |
| checkout. Needs disarm and confusable-homoglyphs importable. Expected output on disarm 0.15.0 at | |
| fba73bc5 with confusables 17.0.0 and confusable-homoglyphs 3.3.1: | |
| scored 7402; UTS #39 sources 586; single-code-point prototype 39; of those in another script 35 | |
| multi-code-point-prototype sources by script: [('ARABIC', 193), ('CANADIAN', 129), ('HANGUL', 108), ('LATIN', 20)] | |
| scored n=7402 disarm 236 confusable-homoglyphs 362 | |
| UTS #39 sources n= 586 disarm 110 confusable-homoglyphs 353 | |
| single cross-script prototype n= 35 disarm 17 confusable-homoglyphs 2 | |
| disarm misses 18 of the 35 single cross-script prototypes: | |
| U+031A COMBINING LEFT ANGLE ABOVE -> U+1AE9 ? | |
| U+0348 COMBINING DOUBLE VERTICAL LINE BELOW -> U+10EFA ? | |
| U+0A20 GURMUKHI LETTER TTHA -> U+0920 DEVANAGARI LETTER TTHA | |
| U+0A3F GURMUKHI VOWEL SIGN I -> U+09BF BENGALI VOWEL SIGN I | |
| U+0CBF KANNADA VOWEL SIGN I -> U+0C3F TELUGU VOWEL SIGN I | |
| U+0CC3 KANNADA VOWEL SIGN VOCALIC R -> U+0C43 TELUGU VOWEL SIGN VOCALIC R | |
| U+0D16 MALAYALAM LETTER KHA -> U+0BB5 TAMIL LETTER VA | |
| U+0D46 MALAYALAM VOWEL SIGN E -> U+0BC6 TAMIL VOWEL SIGN E | |
| U+1002 MYANMAR LETTER GA -> U+0D30 MALAYALAM LETTER RA | |
| U+1031 MYANMAR VOWEL SIGN E -> U+0B47 ORIYA VOWEL SIGN E | |
| U+10D8 GEORGIAN LETTER IN -> U+0D30 MALAYALAM LETTER RA | |
| U+2143 REVERSED SANS-SERIF CAPITAL L -> U+16F00 MIAO LETTER PA | |
| U+21BA ANTICLOCKWISE OPEN CIRCLE ARROW -> U+1F10E CIRCLED ANTICLOCKWISE ARROW | |
| U+28FF BRAILLE PATTERN DOTS-12345678 -> U+1CEE0 ? | |
| U+2C8B COPTIC SMALL LETTER SOU -> U+03C2 GREEK SMALL LETTER FINAL SIGMA | |
| U+2CA1 COPTIC SMALL LETTER PI -> U+03C0 GREEK SMALL LETTER PI | |
| U+2CB7 COPTIC SMALL LETTER CRYPTOGRAMMIC EIE -> U+2261 IDENTICAL TO | |
| U+2DEE COMBINING CYRILLIC LETTER TE -> U+1ADB ? | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import unicodedata as ud | |
| from collections import Counter | |
| from pathlib import Path | |
| import disarm | |
| from confusable_homoglyphs import confusables as chg | |
| CACHE = Path(os.environ.get("DISARM_META_CACHE", "/tmp/disarm_meta_cache")) | |
| def corpus() -> list[int]: | |
| path = Path(os.environ.get("DISARM_META_WEAPONIZING") or CACHE / "weaponizing-unicode-homoglyphs.txt") | |
| cps = set() | |
| for line in path.read_text(encoding="utf-8", errors="replace").splitlines(): | |
| head = line.split(",", 1)[0].strip() | |
| if head.upper().startswith("U+"): | |
| try: | |
| cps.add(int(head[2:], 16)) | |
| except ValueError: | |
| pass | |
| return sorted(cp for cp in cps if ud.category(chr(cp)) not in ("Co", "Cn", "Cs")) | |
| def uts39() -> dict[int, tuple[int, ...]]: | |
| path = Path(os.environ.get("DISARM_META_CONFUSABLES") or "data/confusables.txt") | |
| rows: dict[int, tuple[int, ...]] = {} | |
| for line in path.read_text(encoding="utf-8").splitlines(): | |
| if line.startswith("#") or ";" not in line: | |
| continue | |
| src, tgt = line.split(";")[:2] | |
| rows[int(src.strip(), 16)] = tuple(int(x, 16) for x in tgt.split()) | |
| return rows | |
| def script(cp: int) -> str: | |
| return ud.name(chr(cp), "?").split(" ")[0] | |
| def main() -> None: | |
| scored, rows = corpus(), uts39() | |
| dets = [getattr(disarm, n) for n in ("has_anomalies", "is_confusable", "has_bidi_control", "has_bidi_conflict", "is_mixed_script", "is_zalgo")] | |
| d_flag = lambda cp: any(bool(f(chr(cp))) for f in dets) # noqa: E731 | |
| c_flag = lambda cp: bool(chg.is_confusable(chr(cp))) # noqa: E731 | |
| sources = [cp for cp in scored if cp in rows] | |
| single = [cp for cp in sources if len(rows[cp]) == 1] | |
| cross = [cp for cp in single if script(cp) != script(rows[cp][0])] | |
| print(f"scored {len(scored)}; UTS #39 sources {len(sources)}; single-code-point prototype {len(single)}; of those in another script {len(cross)}") | |
| print("multi-code-point-prototype sources by script:", Counter(script(cp) for cp in sources if len(rows[cp]) > 1).most_common(4)) | |
| for label, subset in (("scored", scored), ("UTS #39 sources", sources), ("single cross-script prototype", cross)): | |
| print(f" {label:30s} n={len(subset):4d} disarm {sum(map(d_flag, subset)):4d} confusable-homoglyphs {sum(map(c_flag, subset)):4d}") | |
| miss = [cp for cp in cross if not d_flag(cp)] | |
| print(f"disarm misses {len(miss)} of the {len(cross)} single cross-script prototypes:") | |
| for cp in miss: | |
| print(f" U+{cp:04X} {ud.name(chr(cp), '?')} -> U+{rows[cp][0]:04X} {ud.name(chr(rows[cp][0]), '?')}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment