From 64adf33bfdd2722e7ac8e652f8b6a8f43992e1ad Mon Sep 17 00:00:00 2001 From: harryob <55142896+harryob@users.noreply.github.com> Date: Thu, 7 Dec 2023 16:18:20 +0000 Subject: [PATCH] reports the fixes to the feature that we sent upstream --- tgui/packages/tgui-panel/chat/renderer.js | 29 +++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tgui/packages/tgui-panel/chat/renderer.js b/tgui/packages/tgui-panel/chat/renderer.js index f7ce9277cf68..fe175ee6d94e 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,27 @@ 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) {