mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-10-01 01:36:06 -04:00
WordList was matching everything.
Made several improvements to catch edge cases.
This commit is contained in:
parent
4b357732eb
commit
dc054f7bcb
@ -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"
|
|
||||||
);
|
|
||||||
} catch (ex) {
|
|
||||||
await mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "WordList", `Could not produce a regex from the word list:\n${ex}.`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!message) {
|
// Create a mega-regex from all the tiny words.
|
||||||
|
const words = mjolnir.config.protections.wordlist.words.filter(word => word.length !== 0).map(escapeRegExp);
|
||||||
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
this.badWords = new RegExp(words.join("|"), "i");
|
||||||
|
}
|
||||||
|
|
||||||
const matches = message.match(this.badWords!);
|
const match = this.badWords!.exec(message);
|
||||||
if (matches) {
|
if (match) {
|
||||||
const reason = `bad word: ${matches[0]}`;
|
const reason = `bad word: ${match[0]}`;
|
||||||
return [new ConsequenceBan(reason), new ConsequenceRedact(reason)];
|
return [new ConsequenceBan(reason), new ConsequenceRedact(reason)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user