From 0c57e70df71d003f0e7a211ce8e2b948db832557 Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Sun, 30 Jul 2023 09:08:03 +0200 Subject: [PATCH] [MIRROR] Fixes chat highlights with broken RegEx expressions and escapes RegEx characters from non-RegEx highlights [MDB IGNORE] (#22780) * Fixes chat highlights with broken RegEx expressions and escapes RegEx characters from non-RegEx highlights (#77228) ## About The Pull Request Yeah, so, if you made a mistake in your chat highlight's regex, it would actually just straight up not work anymore, constant bluescreen pointing out your error, and there isn't a simple way for users to fix that issue. I'm not giving users an easy way to flush their highlights, but I'm making it far more robust so it shouldn't happen anymore. Now, what I'm doing is that it's preventing the chat from breaking if the chat regex is broken, and so it's just `null` instead. Not only that, but it's now also escaping anything that would be valid for RegEx from non-RegEx strings, so that stuff like `[AI Private]` don't make your whole client freeze to death. Initially caught by https://github.com/Skyrat-SS13/Skyrat-tg/issues/22774. ## Why It's Good For The Game Users not being stuck with a perma-bluescreened chat is good, methinks. ## Changelog :cl: GoldenAlpharex fix: Chat highlights now escape special RegEx characters from non-RegEx highlights. fix: Broken RegEx expressions no longer cause the chat to bluescreen, allowing you to properly fix them. /:cl: * Fixes chat highlights with broken RegEx expressions and escapes RegEx characters from non-RegEx highlights --------- Co-authored-by: GoldenAlpharex <58045821+GoldenAlpharex@users.noreply.github.com> --- tgui/packages/tgui-panel/chat/renderer.js | 27 ++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/tgui/packages/tgui-panel/chat/renderer.js b/tgui/packages/tgui-panel/chat/renderer.js index f7ce9277cf6..7a528cd4fd7 100644 --- a/tgui/packages/tgui-panel/chat/renderer.js +++ b/tgui/packages/tgui-panel/chat/renderer.js @@ -193,6 +193,7 @@ class ChatRenderer { const matchWord = setting.matchWord; const matchCase = setting.matchCase; const allowedRegex = /^[a-z0-9_\-$/^[\s\]\\]+$/gi; + const regexEscapeCharacters = /[!#$%^&*)(+=.<>{}[\]:;'"|~`_\-\\/]/g; const lines = String(text) .split(',') .map((str) => str.trim()) @@ -228,19 +229,29 @@ class ChatRenderer { if (!highlightWords) { highlightWords = []; } + // We're not going to let regex characters fuck up our RegEx operation. + line = line.replace(regexEscapeCharacters, '\\$&'); + highlightWords.push(line); } } const regexStr = regexExpressions.join('|'); const flags = 'g' + (matchCase ? '' : 'i'); - // setting regex overrides matchword - if (regexStr) { - highlightRegex = new RegExp('(' + regexStr + ')', flags); - } else { - const pattern = `${matchWord ? '\\b' : ''}(${lines.join('|')})${ - matchWord ? '\\b' : '' - }`; - highlightRegex = new RegExp(pattern, flags); + // We wrap this in a try-catch to ensure that broken regex doesn't break + // the entire chat. + try { + // setting regex overrides matchword + if (regexStr) { + highlightRegex = new RegExp('(' + regexStr + ')', flags); + } else { + const pattern = `${matchWord ? '\\b' : ''}(${highlightWords.join( + '|' + )})${matchWord ? '\\b' : ''}`; + highlightRegex = new RegExp(pattern, flags); + } + } catch { + // We just reset it if it's invalid. + highlightRegex = null; } // Lazy init if (!this.highlightParsers) {