From d407799f4a271c999988c57eb29ddd04385575e9 Mon Sep 17 00:00:00 2001 From: GoldenAlpharex <58045821+GoldenAlpharex@users.noreply.github.com> Date: Wed, 10 Nov 2021 13:26:53 -0500 Subject: [PATCH] The word filter will now be less scared of double or trailing spaces (#62703) It kept on tripping because the RegEx expression was malformed due to an absence of words in to_join_on_whitespace_splits, which caused it to filter out spaces that were at the beginning or at the end of a message, or if there was two spaces one by the other. Also prevents people from sending a message that's only spaces in OOC, because that's a little silly. --- code/controllers/configuration/configuration.dm | 6 ++++-- code/modules/client/verbs/ooc.dm | 2 +- code/modules/unit_tests/chat_filter.dm | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/code/controllers/configuration/configuration.dm b/code/controllers/configuration/configuration.dm index d1b5c728da1..97e835d7d6e 100644 --- a/code/controllers/configuration/configuration.dm +++ b/code/controllers/configuration/configuration.dm @@ -460,9 +460,11 @@ Example config: else to_join_on_whitespace_splits += REGEX_QUOTE(banned_word) - var/whitespace_split = @"(?:(?:^|\s+)(" + jointext(to_join_on_whitespace_splits, "|") + @")(?:$|\s+))" + // We don't want a whitespace_split part if there's no stuff that requires it + var/whitespace_split = to_join_on_whitespace_splits.len > 0 ? @"(?:(?:^|\s+)(" + jointext(to_join_on_whitespace_splits, "|") + @")(?:$|\s+))" : "" var/word_bounds = @"(\b(" + jointext(to_join_on_word_bounds, "|") + @")\b)" - return regex("([whitespace_split]|[word_bounds])", "i") + var/regex_filter = whitespace_split != "" ? "([whitespace_split]|[word_bounds])" : word_bounds + return regex(regex_filter, "i") //Message admins when you can. /datum/controller/configuration/proc/DelayedMessageAdmins(text) diff --git a/code/modules/client/verbs/ooc.dm b/code/modules/client/verbs/ooc.dm index 4cf58f9db79..07b5233f103 100644 --- a/code/modules/client/verbs/ooc.dm +++ b/code/modules/client/verbs/ooc.dm @@ -28,7 +28,7 @@ GLOBAL_VAR_INIT(normal_ooc_colour, "#002eb8") if(QDELETED(src)) return - msg = copytext_char(sanitize(msg), 1, MAX_MESSAGE_LEN) + msg = trim(copytext_char(sanitize(msg), 1, MAX_MESSAGE_LEN)) var/raw_msg = msg var/list/filter_result = is_ooc_filtered(msg) diff --git a/code/modules/unit_tests/chat_filter.dm b/code/modules/unit_tests/chat_filter.dm index 6a2476f0639..226416f68ab 100644 --- a/code/modules/unit_tests/chat_filter.dm +++ b/code/modules/unit_tests/chat_filter.dm @@ -50,6 +50,14 @@ BLOCKED_SHARED, ) + test_filter( + " This message has a space at the beginning, a double space, and a space at the end, but it's fine! ", + null, + null, + null, + null, + ) + /datum/unit_test/chat_filter_sanity/proc/test_filter( message, blocked_word,