Repository: python-discord/bot

Issue:
Antimalware cog file filter triggers on bot and webhook messages
Currently the antimalware cog file filter triggers on messages from the bot, and also on webhook messages. So for the example somebody tries to send an attachment from python using `!int eval`, it will be caught by the filter
![image](https://user-images.githubusercontent.com/22353562/91752774-be76ed00-ebbe-11ea-99ac-e1147ca1d2df.png)
Also, if somebody sends a non-whitelisted attachment to the bot, (and it isn't ignored because of #1139), it will be relayed to dm-log but then caught by the filter, which will delete it and then reply to the webhook in the channel.

I think the best thing to do here would just be to ignore all webhook messages and bot messages, I can't think of any problems with this. 

I would be happy to make this change


Discussion:
Sounds good to me.

Resolving patch:
diff --git a/bot/cogs/antimalware.py b/bot/cogs/antimalware.py
--- a/bot/cogs/antimalware.py
+++ b/bot/cogs/antimalware.py
@@ -55,6 +55,10 @@ async def on_message(self, message: Message) -> None:
         if not message.attachments or not message.guild:
             return
 
+        # Ignore webhook and bot messages
+        if message.webhook_id or message.author.bot:
+            return
+
         # Check if user is staff, if is, return
         # Since we only care that roles exist to iterate over, check for the attr rather than a User/Member instance
         if hasattr(message.author, "roles") and any(role.id in STAFF_ROLES for role in message.author.roles):


Test patch:
diff --git a/tests/bot/cogs/test_antimalware.py b/tests/bot/cogs/test_antimalware.py
--- a/tests/bot/cogs/test_antimalware.py
+++ b/tests/bot/cogs/test_antimalware.py
@@ -23,6 +23,8 @@ def setUp(self):
         }
         self.cog = antimalware.AntiMalware(self.bot)
         self.message = MockMessage()
+        self.message.webhook_id = None
+        self.message.author.bot = None
         self.whitelist = [".first", ".second", ".third"]
 
     async def test_message_with_allowed_attachment(self):
@@ -48,6 +50,26 @@ async def test_direct_message_with_attachment(self):
 
         self.message.delete.assert_not_called()
 
+    async def test_webhook_message_with_illegal_extension(self):
+        """A webhook message containing an illegal extension should be ignored."""
+        attachment = MockAttachment(filename="python.disallowed")
+        self.message.webhook_id = 697140105563078727
+        self.message.attachments = [attachment]
+
+        await self.cog.on_message(self.message)
+
+        self.message.delete.assert_not_called()
+
+    async def test_bot_message_with_illegal_extension(self):
+        """A bot message containing an illegal extension should be ignored."""
+        attachment = MockAttachment(filename="python.disallowed")
+        self.message.author.bot = 409107086526644234
+        self.message.attachments = [attachment]
+
+        await self.cog.on_message(self.message)
+
+        self.message.delete.assert_not_called()
+
     async def test_message_with_illegal_extension_gets_deleted(self):
         """A message containing an illegal extension should send an embed."""
         attachment = MockAttachment(filename="python.disallowed")