Fix Unicode chat sanitization
This commit is contained in:
parent
c20bb35929
commit
91b54e4a01
2 changed files with 43 additions and 14 deletions
|
|
@ -9,15 +9,27 @@ const MAX_HISTORY: int = 100
|
||||||
const LATE_JOIN_HISTORY: int = 50
|
const LATE_JOIN_HISTORY: int = 50
|
||||||
const MAX_ID_LENGTH: int = 96
|
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 }
|
enum Kind { PLAYER, SYSTEM }
|
||||||
|
|
||||||
|
|
||||||
static func sanitize_body(value: Variant) -> String:
|
static func sanitize_body(value: Variant) -> String:
|
||||||
if typeof(value) != TYPE_STRING:
|
if typeof(value) != TYPE_STRING:
|
||||||
return ""
|
return ""
|
||||||
var strippable_whitespace = RegEx.create_from_string("^[^\\w]+|[^\\w]+$")
|
var result: String = str(value).replace("\r", " ").replace("\n", " ")
|
||||||
var result := strippable_whitespace.sub(value, "")
|
result = result.replace("\t", " ")
|
||||||
if result.is_empty() or result.length() > MAX_VISIBLE_CHARACTERS:
|
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 ""
|
return ""
|
||||||
if result.to_utf8_buffer().size() > MAX_UTF8_BYTES:
|
if result.to_utf8_buffer().size() > MAX_UTF8_BYTES:
|
||||||
return ""
|
return ""
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,38 @@
|
||||||
extends SceneTree
|
extends SceneTree
|
||||||
|
|
||||||
const MainScene: PackedScene = preload("res://main/main.tscn")
|
|
||||||
|
|
||||||
func _initialize() -> void:
|
func _initialize() -> void:
|
||||||
call_deferred("_run")
|
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
|
const em_space := " " # U+2003 EM SPACE
|
||||||
assert(NetworkChatProtocol.sanitize_body(em_space + "a") == "a")
|
const zero_width_space := "" # U+200B ZERO WIDTH SPACE
|
||||||
const braille_space := "⠀" # U+2800 BRAILLE PATTERN BLANK
|
const braille_blank := "⠀" # U+2800 BRAILLE PATTERN BLANK
|
||||||
assert(NetworkChatProtocol.sanitize_body(braille_space + "a") == "a")
|
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")
|
print("Chat validation: PASS")
|
||||||
quit(0)
|
quit(0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue