WordList was matching everything.

Made several improvements to catch edge cases.
This commit is contained in:
gnuxie 2022-11-18 16:48:27 +00:00
parent 4b357732eb
commit dc054f7bcb

View File

@ -64,6 +64,9 @@ export class WordList extends Protection {
if (event['type'] === 'm.room.message') { if (event['type'] === 'm.room.message') {
const message = content['formatted_body'] || content['body'] || null; const message = content['formatted_body'] || content['body'] || null;
if (!message) {
return;
}
// Check conditions first // Check conditions first
if (minsBeforeTrusting > 0) { if (minsBeforeTrusting > 0) {
@ -84,25 +87,25 @@ export class WordList extends Protection {
return return
} }
} }
if (this.badWords === null) { if (!this.badWords) {
// Create a mega-regex from all the tiny baby regexs // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
try { const escapeRegExp = (string: string) => {
this.badWords = new RegExp( return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
"(" + mjolnir.config.protections.wordlist.words.join(")|(") + ")", };
"i"
); // Create a mega-regex from all the tiny words.
} catch (ex) { const words = mjolnir.config.protections.wordlist.words.filter(word => word.length !== 0).map(escapeRegExp);
await mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "WordList", `Could not produce a regex from the word list:\n${ex}.`) if (words.length === 0) {
mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "WordList", `Someone turned on the word list protection without configuring any words. Disabling.`);
this.enabled = false;
return;
} }
this.badWords = new RegExp(words.join("|"), "i");
} }
if (!message) { const match = this.badWords!.exec(message);
return; if (match) {
} const reason = `bad word: ${match[0]}`;
const matches = message.match(this.badWords!);
if (matches) {
const reason = `bad word: ${matches[0]}`;
return [new ConsequenceBan(reason), new ConsequenceRedact(reason)]; return [new ConsequenceBan(reason), new ConsequenceRedact(reason)];
} }
} }