From 91b54e4a01515416465ce63fce0dc222f4e6d9e5 Mon Sep 17 00:00:00 2001 From: Voyager Date: Fri, 21 Aug 2026 09:23:28 -0400 Subject: [PATCH] Fix Unicode chat sanitization --- network/network_chat_protocol.gd | 18 ++++++++++++--- tests/chat_validation.gd | 39 +++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/network/network_chat_protocol.gd b/network/network_chat_protocol.gd index 389260d..2e91432 100644 --- a/network/network_chat_protocol.gd +++ b/network/network_chat_protocol.gd @@ -9,15 +9,27 @@ const MAX_HISTORY: int = 100 const LATE_JOIN_HISTORY: int = 50 const MAX_ID_LENGTH: int = 96 +static var _invisible_edge_pattern: RegEx = RegEx.create_from_string( + "^[\\s\\p{Z}\\p{Cf}\\x{2800}]+|[\\s\\p{Z}\\p{Cf}\\x{2800}]+$" +) +static var _visible_content_pattern: RegEx = RegEx.create_from_string( + "[^\\s\\p{Z}\\p{Cf}\\x{2800}]" +) + enum Kind { PLAYER, SYSTEM } static func sanitize_body(value: Variant) -> String: if typeof(value) != TYPE_STRING: return "" - var strippable_whitespace = RegEx.create_from_string("^[^\\w]+|[^\\w]+$") - var result := strippable_whitespace.sub(value, "") - if result.is_empty() or result.length() > MAX_VISIBLE_CHARACTERS: + var result: String = str(value).replace("\r", " ").replace("\n", " ") + result = result.replace("\t", " ") + result = _invisible_edge_pattern.sub(result, "", true) + if ( + result.is_empty() + or _visible_content_pattern.search(result) == null + or result.length() > MAX_VISIBLE_CHARACTERS + ): return "" if result.to_utf8_buffer().size() > MAX_UTF8_BYTES: return "" diff --git a/tests/chat_validation.gd b/tests/chat_validation.gd index aed8f17..fa0cd8e 100644 --- a/tests/chat_validation.gd +++ b/tests/chat_validation.gd @@ -1,21 +1,38 @@ extends SceneTree -const MainScene: PackedScene = preload("res://main/main.tscn") - func _initialize() -> void: call_deferred("_run") -func _run() -> void: - ## Regular whitespace should be stripped - assert(NetworkChatProtocol.sanitize_body(" a") == "a", "Leading space not trimmed") - assert(NetworkChatProtocol.sanitize_body("b ") == "b", "Trailing space not trimmed") - assert(NetworkChatProtocol.sanitize_body("a b") == "a b", "Innocent whitespace culled") - ## Some examples of Unicode whitespace that should be stripped +func _run() -> void: + assert(NetworkChatProtocol.sanitize_body(42).is_empty()) + assert(NetworkChatProtocol.sanitize_body(" a ") == "a") + assert(NetworkChatProtocol.sanitize_body("a b") == "a b") + assert(NetworkChatProtocol.sanitize_body("a\tb\nc") == "a b c") + assert(NetworkChatProtocol.sanitize_body(" !hello!? ") == "!hello!?") + assert(NetworkChatProtocol.sanitize_body("!!!") == "!!!") + const em_space := " " # U+2003 EM SPACE - assert(NetworkChatProtocol.sanitize_body(em_space + "a") == "a") - const braille_space := "⠀" # U+2800 BRAILLE PATTERN BLANK - assert(NetworkChatProtocol.sanitize_body(braille_space + "a") == "a") + const zero_width_space := "​" # U+200B ZERO WIDTH SPACE + const braille_blank := "⠀" # U+2800 BRAILLE PATTERN BLANK + const invisible_padding := em_space + zero_width_space + braille_blank + assert( + NetworkChatProtocol.sanitize_body(invisible_padding + "a" + invisible_padding) + == "a" + ) + assert(NetworkChatProtocol.sanitize_body(invisible_padding).is_empty()) + assert(NetworkChatProtocol.sanitize_body(" \t\n" + invisible_padding).is_empty()) + + assert( + NetworkChatProtocol.sanitize_body("a".repeat( + NetworkChatProtocol.MAX_VISIBLE_CHARACTERS + )).length() == NetworkChatProtocol.MAX_VISIBLE_CHARACTERS + ) + assert( + NetworkChatProtocol.sanitize_body("a".repeat( + NetworkChatProtocol.MAX_VISIBLE_CHARACTERS + 1 + )).is_empty() + ) print("Chat validation: PASS") quit(0)