diff --git a/lib/connector/meshcore_protocol.dart b/lib/connector/meshcore_protocol.dart index 14d181a1..79ea546d 100644 --- a/lib/connector/meshcore_protocol.dart +++ b/lib/connector/meshcore_protocol.dart @@ -2,8 +2,37 @@ import 'dart:convert'; import 'dart:math'; import 'dart:typed_data'; +// `flutter/widgets.dart` re-exports package:characters, which supplies the +// grapheme-cluster iteration used by utf8TruncateToBytes below. import 'package:flutter/widgets.dart'; +/// Encodes [s] as UTF-8, truncated to at most [maxBytes], never splitting a +/// character. (#636) +/// +/// Truncation stops on a **grapheme cluster** boundary, not a byte boundary and +/// not merely a codepoint boundary. Cutting on bytes puts invalid UTF-8 on the +/// wire, which every reader then renders as replacement characters. Cutting +/// between codepoints is valid UTF-8 but still wrong: a ZWJ emoji sequence is +/// several codepoints joined by U+200D plus a variation selector, so a +/// codepoint-safe cut can still leave a bare glyph, a dangling joiner, or an +/// orphaned selector. +/// +/// The on-wire name field is 32 bytes including a null terminator, so callers +/// pass 31. That holds roughly 10 CJK characters, or two ZWJ emoji. +Uint8List utf8TruncateToBytes(String s, int maxBytes) { + if (maxBytes <= 0) return Uint8List(0); + final whole = utf8.encode(s); + if (whole.length <= maxBytes) return Uint8List.fromList(whole); + + final out = []; + for (final cluster in s.characters) { + final encoded = utf8.encode(cluster); + if (out.length + encoded.length > maxBytes) break; + out.addAll(encoded); + } + return Uint8List.fromList(out); +} + // Buffer Reader - sequential binary data reader with pointer tracking class BufferReader { int _pointer = 0; @@ -132,8 +161,10 @@ class BufferWriter { void writeCString(String string, int maxLength) { final bytes = Uint8List(maxLength); - final encoded = utf8.encode(string); - for (var i = 0; i < maxLength - 1 && i < encoded.length; i++) { + // Grapheme-safe: a raw byte copy would cut a multi-byte character in half + // and put invalid UTF-8 on the wire. (#636) + final encoded = utf8TruncateToBytes(string, maxLength - 1); + for (var i = 0; i < encoded.length; i++) { bytes[i] = encoded[i]; } writeBytes(bytes); @@ -1108,13 +1139,14 @@ Uint8List buildSendSelfAdvertFrame({bool flood = false}) { // Build CMD_SET_ADVERT_NAME frame // Format: [cmd][name...] Uint8List buildSetAdvertNameFrame(String name) { - final nameBytes = utf8.encode(name); - final nameLen = nameBytes.length < maxNameSize - ? nameBytes.length - : maxNameSize - 1; + // Grapheme-safe. This one matters most of the three: it sets THIS device's + // own advert name, which is what every other node on the mesh sees, what is + // embedded in the contact cards this device emits, and what other clients + // match against. Corrupt it once and the corruption propagates outward. + // (#636) final writer = BufferWriter(); writer.writeByte(cmdSetAdvertName); - writer.writeBytes(Uint8List.fromList(nameBytes.sublist(0, nameLen))); + writer.writeBytes(utf8TruncateToBytes(name, maxNameSize - 1)); return writer.toBytes(); } diff --git a/test/connector/utf8_name_truncation_test.dart b/test/connector/utf8_name_truncation_test.dart new file mode 100644 index 00000000..346d8a51 --- /dev/null +++ b/test/connector/utf8_name_truncation_test.dart @@ -0,0 +1,124 @@ +// Grapheme-safe name truncation (#636). +// +// The on-wire name field is 32 bytes including a null terminator, so 31 are +// usable. Before this fix the three writers copied raw bytes up to that +// boundary, cutting multi-byte characters in half and putting invalid UTF-8 on +// the wire. ASCII names were unaffected, which is why it went unnoticed. + +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/meshcore_protocol.dart'; + +/// Decodes strictly. Throws if the bytes are not valid UTF-8, which is exactly +/// the failure this fix prevents. +String strictDecode(List bytes) => + utf8.decode(bytes, allowMalformed: false); + +void main() { + group('utf8TruncateToBytes (#636)', () { + test('leaves anything that already fits completely alone', () { + for (final s in ['', 'Bob', 'Roger KY4RS', 'a' * 31]) { + expect(utf8TruncateToBytes(s, 31), utf8.encode(s), reason: s); + } + }); + + test('never emits invalid UTF-8, whatever the cut point', () { + // The core property. Sweep every budget across a string whose characters + // are 1, 3 and 4 bytes, so a byte-wise cut would land mid-character at + // many of these lengths. + const s = 'ab中文🧙cd漢字'; + for (var budget = 0; budget <= utf8.encode(s).length + 2; budget++) { + final out = utf8TruncateToBytes(s, budget); + expect(out.length, lessThanOrEqualTo(budget)); + // Would throw on a split character. + expect( + () => strictDecode(out), + returnsNormally, + reason: 'budget $budget', + ); + expect( + s.startsWith(strictDecode(out)), + isTrue, + reason: 'budget $budget', + ); + } + }); + + test('CJK truncates on a character boundary, not a byte one', () { + // 11 CJK characters is 33 bytes against a 31-byte budget. + const name = '中文节点名称测试一二三'; + expect(utf8.encode(name).length, 33); + final out = utf8TruncateToBytes(name, 31); + // 10 characters at 3 bytes each fit; the 11th does not. + expect(out.length, 30); + expect(strictDecode(out), '中文节点名称测试一二'); + }); + + test('a ZWJ emoji sequence is kept whole or dropped whole', () { + // The mage is 4 codepoints joined by ZWJ plus a variation selector, 13 + // bytes. A codepoint-safe cut would still be wrong here: it could leave a + // bare mage, a dangling joiner, or an orphaned selector. + const mage = '\u{1F9D9}‍♂️'; + expect(utf8.encode(mage).length, 13); + + // One byte short of fitting: the whole cluster must go. + expect(utf8TruncateToBytes(mage, 12), isEmpty); + // Exactly fitting: kept intact. + expect(strictDecode(utf8TruncateToBytes(mage, 13)), mage); + + // And no partial cluster survives at any budget below 13. + for (var b = 0; b < 13; b++) { + expect(utf8TruncateToBytes(mage, b), isEmpty, reason: 'budget $b'); + } + }); + + test('a zero or negative budget yields nothing', () { + expect(utf8TruncateToBytes('anything', 0), isEmpty); + expect(utf8TruncateToBytes('anything', -5), isEmpty); + }); + }); + + group('the three writers are grapheme-safe (#636)', () { + // Reads a fixed-width, null-padded name field back out. + String nameFrom(Uint8List frame, int offset, int width) { + final slice = frame.sublist(offset, offset + width); + final end = slice.indexOf(0); + return strictDecode(slice.sublist(0, end < 0 ? slice.length : end)); + } + + const cjk = '中文节点名称测试一二三'; + + test('buildSetAdvertNameFrame: own advert name survives intact', () { + // The most externally visible of the three: this is the name the whole + // mesh sees and the name embedded in emitted contact cards. + final frame = buildSetAdvertNameFrame(cjk); + final decoded = strictDecode(frame.sublist(1)); + expect(decoded, '中文节点名称测试一二'); + expect(frame.length - 1, lessThanOrEqualTo(maxNameSize - 1)); + }); + + test('buildUpdateContactPathFrame: contact name survives intact', () { + final frame = buildUpdateContactPathFrame( + Uint8List(pubKeySize), + Uint8List(0), + -1, + name: cjk, + ); + expect(nameFrom(frame, contactNameOffset, maxNameSize), '中文节点名称测试一二'); + }); + + test('buildSetChannelFrame: channel name survives intact', () { + final frame = buildSetChannelFrame(0, cjk, Uint8List(16)); + // [cmd][idx][name x32][psk x16] + expect(nameFrom(frame, 2, maxNameSize), '中文节点名称测试一二'); + }); + + test('ASCII names are byte-identical to the old behaviour', () { + // No regression for the overwhelmingly common case. + final frame = buildSetAdvertNameFrame('Roger KY4RS'); + expect(strictDecode(frame.sublist(1)), 'Roger KY4RS'); + }); + }); +}