From 65dfa27eafaf37c0e85cb33fce85722b5286e379 Mon Sep 17 00:00:00 2001
From: Dragory <2606411+Dragory@users.noreply.github.com>
Date: Tue, 30 Apr 2019 06:05:53 +0300
Subject: [PATCH] Also apply censor filters to embeds and attachments

---
 src/plugins/Censor.ts | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/plugins/Censor.ts b/src/plugins/Censor.ts
index f20ef449..368cbb9e 100644
--- a/src/plugins/Censor.ts
+++ b/src/plugins/Censor.ts
@@ -118,14 +118,16 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
    * @return {boolean} Indicates whether the message was removed
    */
   async applyFiltersToMsg(savedMessage: SavedMessage): Promise<boolean> {
-    if (!savedMessage.data.content) return false;
-
     const config = this.getConfigForMemberIdAndChannelId(savedMessage.user_id, savedMessage.channel_id);
 
+    let messageContent = savedMessage.data.content || "";
+    if (savedMessage.data.attachments) messageContent += " " + JSON.stringify(savedMessage.data.attachments);
+    if (savedMessage.data.embeds) messageContent += " " + JSON.stringify(savedMessage.data.embeds);
+
     // Filter zalgo
     const filterZalgo = config.filter_zalgo;
     if (filterZalgo) {
-      const result = ZalgoRegex.exec(savedMessage.data.content);
+      const result = ZalgoRegex.exec(messageContent);
       if (result) {
         this.censorMessage(savedMessage, "zalgo detected");
         return true;
@@ -141,7 +143,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
       const inviteCodeBlacklist = config.invite_code_blacklist;
       const allowGroupDMInvites = config.allow_group_dm_invites;
 
-      const inviteCodes = getInviteCodesInString(savedMessage.data.content);
+      const inviteCodes = getInviteCodesInString(messageContent);
 
       let invites: Invite[] = await Promise.all(inviteCodes.map(code => this.bot.getInvite(code).catch(() => null)));
 
@@ -187,7 +189,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
       const domainWhitelist = config.domain_whitelist;
       const domainBlacklist = config.domain_blacklist;
 
-      const urls = getUrlsInString(savedMessage.data.content);
+      const urls = getUrlsInString(messageContent);
       for (const thisUrl of urls) {
         if (domainWhitelist && !domainWhitelist.includes(thisUrl.hostname)) {
           this.censorMessage(savedMessage, `domain (\`${thisUrl.hostname}\`) not found in whitelist`);
@@ -204,7 +206,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
     // Filter tokens
     const blockedTokens = config.blocked_tokens || [];
     for (const token of blockedTokens) {
-      if (savedMessage.data.content.toLowerCase().includes(token.toLowerCase())) {
+      if (messageContent.toLowerCase().includes(token.toLowerCase())) {
         this.censorMessage(savedMessage, `blocked token (\`${token}\`) found`);
         return true;
       }
@@ -214,7 +216,7 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
     const blockedWords = config.blocked_words || [];
     for (const word of blockedWords) {
       const regex = new RegExp(`\\b${escapeStringRegexp(word)}\\b`, "i");
-      if (regex.test(savedMessage.data.content)) {
+      if (regex.test(messageContent)) {
         this.censorMessage(savedMessage, `blocked word (\`${word}\`) found`);
         return true;
       }
@@ -224,7 +226,8 @@ export class CensorPlugin extends ZeppelinPlugin<ICensorPluginConfig> {
     const blockedRegex = config.blocked_regex || [];
     for (const regexStr of blockedRegex) {
       const regex = new RegExp(regexStr, "i");
-      if (regex.test(savedMessage.data.content)) {
+      // We're testing both the original content and content + attachments/embeds here so regexes that use ^ and $ still match the regular content properly
+      if (regex.test(savedMessage.data.content) || regex.test(messageContent)) {
         this.censorMessage(savedMessage, `blocked regex (\`${regexStr}\`) found`);
         return true;
       }