[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

🆑 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.
/🆑

* 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>
This commit is contained in:
SkyratBot
2023-07-30 03:08:03 -04:00
committed by GitHub
co-authored by GoldenAlpharex
parent 1c02ab4362
commit 0c57e70df7
+19 -8
View File
@@ -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) {