ADR 0005: UTF-8/C1 isolation, grapheme caps, and Unicode 17 two-stage table¶
Status¶
Accepted. 2026-11-21.
Context¶
Adversarial testing and differential fuzzing uncovered four Unicode and byte framing hazards in terminal emulators:
UTF-8 continuation
0x9Cvs 8-bitC1STescape-injection breakout: In UTF-8, valid multi-byte characters including“(U+201C=\xe2\x80\x9c),本(U+672C=\xe6\x9c\xac),Ü(\xc3\x9c), and😜(\xf0\x9f\x98\x9c) contain byte0x9C. Treating raw0x9Cas 8-bit String Terminator (ST) insideOSC,DCS, orAPCstrings truncates non-ASCII titles/hyperlinks mid-codepoint and executes the remaining payload directly inGROUNDstate — enabling escape-sequence injection past callers that filter\x07and\x1b.Incomplete UTF-8 ordering before C0 controls and combining-mark teleportation: Flushing incomplete UTF-8 lead bytes only on
ESC(and not C0 controls like\nor\r) emitsU+FFFDon the wrong line, while retaining_last_printed_posacross cursor movements (CUP,CUB,LF), erases, scrolls, or?1049switches causes subsequent combining marks to mutate a stale cell elsewhere on screen.Single-cell combining-mark DoS (“grapheme bomb”): Appending combining marks without a per-cell codepoint cap or scanning the growing cluster string on every mark causes quadratic CPU freeze (
10+ son 200 KB ofU+0301) and multi-megabyte single-cell strings.Unicode version drift across Python 3.12-3.15 and missing UAX #29 rules: Python’s
unicodedatamodule bundles Unicode 15.0 in 3.12, 15.1 in 3.13, 16.0 in 3.14, and 17.0 in 3.15, and lacksExtended_Pictographic,Regional_Indicatorpair clustering (🇯🇵,🇺🇸), and UAX #29GB9cIndicInCB(Linker/Consonant/Extend) properties needed for DEC mode?2027.
Decision¶
STRING_ESC7-bit string termination and separated wire modes (libvt.parser):Parser(encoding="utf-8")ignores raw0x80..0x9F8-bitC1bytes and UTF-8-encodedU+0080..U+009F(0xC2 0x80..0x9F) as control introducers;Parser(encoding="latin-1")provides complete 8-bit DECC1anywheretransitions for legacy Latin-1 byte streams.OSC,DCS, andAPC/SOS/PMtransition on0x1Binto a dedicatedParserState.STRING_ESCsubstate:\(0x5C) dispatches withDCSheader intermediates intact,0x1Binside a latchedDCS tmux;envelope collapsesESC ESCinto one payloadESCbyte, and any other byte aborts and discards the unclosed payload per DEC STD 070 §5.1.2.3 before re-evaluating that byte inESCAPE.Incomplete UTF-8 sequences flush
U+FFFDbefore executing any C0 control (< 0x20),ESC,CAN(0x18), orSUB(0x1A).
Strict
_last_printed_posinvalidation andO(1)combining cap:Any cursor motion, C0 execution, erase, scroll, resize, reset, or screen switch clears
_last_printed_pos = None.Terminaltracks_cluster_codepoints: intand enforcesLimits.max_grapheme_codepoints(default32), dropping excess zero-width extenders inO(1)time without re-scanning the cluster string.When
VS16(\ufe0f) widens a 1-column base glyph atcol == right(right margin),TerminalreplacescolwithSPACER_HEAD(""), soft-wraps to the next line, and writes the widened 2-column cluster atleft(col 0).VS15(\ufe0e) narrows default-wide emoji to1column under?2027.
SHA-256-pinned Unicode 17.0.0 two-stage block table (
libvt._ucd):scripts/generate_ucd.pycompiles official SHA-256-verified Unicode 17.0.0 UCD and Emoji data (GraphemeBreakProperty.txt,DerivedCoreProperties.txt,EastAsianWidth.txt,DerivedGeneralCategory.txt,emoji-data.txt, andemoji-variation-sequences.txt) into a zlib-compressed base85 2-stage 256-codepointu16block table inlibvt._ucd(~70.5 KiBunpacked, zero runtime dependencies), validated inpytestagainst all officialGraphemeBreakTest.txtvectors.Python 3.12, 3.13, 3.14, and 3.15 therefore compute identical cell widths and UAX #29
?2027cluster boundaries (GB9,GB9a,GB9cIndic conjuncts,GB11ZWJ pictographics, andGB12/GB13Regional Indicator pairs).
Alternatives considered¶
Calling stdlib
unicodedatadirectly at runtime: Rejected because emoji and Indic conjunct properties (Extended_Pictographic,InCB) are absent fromunicodedatain Python 3.12-3.14, and Unicode table versions drift across interpreter versions.Runtime
wcwidth/graphemePyPI dependency: Violates ADR 0001’sdependencies = []invariant.
Consequences¶
Positive¶
UTF-8 titles, hyperlinks, and status messages containing
0x9Ccontinuation bytes never break out ofOSC/DCS/APCframing.Hostile combining-mark floods stay bounded at
<= 32codepoints per cell inO(1)time per byte.Flag emoji (
🇯🇵), ZWJ sequences (🧑🌾), variation selectors (❤️), and Devanagari conjuncts cluster identically on Python 3.12 through 3.15.
Tradeoffs¶
src/libvt/_ucd.pyembeds~14 KiBof compressed base85 lookup data, regenerated viascripts/generate_ucd.pywhen upgrading Unicode versions.
Risks¶
BiDi override characters (
U+202A..U+202E,U+2066..U+2069) spoofing visual test diagnostics. Mitigation:libvt._ucd.sanitize_terminal_text()escapes C0, C1, and BiDi override codepoints in diagnostic views and rejects them inOSC 7501status fields.