Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RegExps support for Mjolnir's WordList protection #546

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ protections:
# Configuration for the wordlist plugin, which can ban users based if they say certain
# blocked words shortly after joining.
wordlist:
# For advanced usage, regex can also be enabled. If left disabled Mjolnir will interpret words literally.
#
# For example, if enabled this 'word' would match all t.me links: (https?:\/\/)?t\.me\/(\+)?[a-zA-Z0-9_-]+
#
# See the following links for more information;
# - https://www.digitalocean.com/community/tutorials/an-introduction-to-regular-expressions
# - https://regexr.com/
enableRegExps: false

# A list of case-insensitive keywords that the WordList protection will watch for from new users.
#
# WordList will ban users who use these words when first joining a room, so take caution when selecting them.
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export interface IConfig {
};
protections: {
wordlist: {
enableRegExps: boolean;
words: string[];
minutesBeforeTrusting: number;
};
Expand Down Expand Up @@ -228,6 +229,7 @@ const defaultConfig: IConfig = {
},
protections: {
wordlist: {
enableRegExps: false,
words: [],
minutesBeforeTrusting: 20,
},
Expand Down
26 changes: 16 additions & 10 deletions src/protections/WordList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,9 @@ export class WordList extends Protection {
}
}
if (!this.badWords) {
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
const escapeRegExp = (string: string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};

// Create a mega-regex from all the tiny words.
const words = mjolnir.config.protections.wordlist.words
.filter((word) => word.length !== 0)
.map(escapeRegExp);
const enableRegExps = mjolnir.config.protections.wordlist.enableRegExps || false;
const words = mjolnir.config.protections.wordlist.words.filter((word) => word.length !== 0);

if (words.length === 0) {
mjolnir.managementRoomOutput.logMessage(
LogLevel.ERROR,
Expand All @@ -106,7 +100,19 @@ export class WordList extends Protection {
this.enabled = false;
return;
}
this.badWords = new RegExp(words.join("|"), "i");

if (enableRegExps) {
this.badWords = new RegExp(words.join("|"), "i");
} else {
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
const escapeRegExp = (string: string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};

// Create a mega-regex from all the tiny words.
const escapedWords = words.map(escapeRegExp);
this.badWords = new RegExp(escapedWords.join("|"), "i");
}
}

const match = this.badWords!.exec(message);
Expand Down
Loading