Skip to content

Commit 77b5290

Browse files
fix: check for unknown/None content_type within attachment handling (#3413)
* fix: check for unknown/None content_type for pastebin uploader * don't check text/plain * fix: check content_type is not None before checking charset in filtering
1 parent c1a3673 commit 77b5290

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

bot/exts/filtering/filtering.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ async def on_message(self, msg: Message) -> None:
235235

236236
text_contents = [
237237
await _extract_text_file_content(a)
238-
for a in msg.attachments if "charset" in a.content_type
238+
for a in msg.attachments if a.content_type and "charset" in a.content_type
239239
]
240240

241241
if text_contents:

bot/exts/utils/attachment_pastebin_uploader.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,21 @@ async def on_message_delete(self, message: discord.Message) -> None:
7575
@commands.Cog.listener()
7676
async def on_message(self, message: discord.Message) -> None:
7777
"""Listens for messages containing attachments and offers to upload them to the pastebin."""
78-
# Check if the message contains an embedded file and is not sent by a bot or in DMs.
79-
if message.author.bot or not message.guild or not any("charset" in a.content_type for a in message.attachments):
78+
# Check the message is not sent by a bot or in DMs.
79+
if message.author.bot or not message.guild:
80+
return
81+
82+
# Check if the message contains any text-based attachments.
83+
# we only require a charset here, as its setting is matched
84+
attachments: list[discord.Attachment] = []
85+
for attachment in message.attachments:
86+
if (
87+
attachment.content_type
88+
and "charset" in attachment.content_type
89+
):
90+
attachments.append(attachment)
91+
92+
if not attachments:
8093
return
8194

8295
log.trace(f"Offering to upload attachments for {message.author} in {message.channel}, message {message.id}")
@@ -104,11 +117,7 @@ async def on_message(self, message: discord.Message) -> None:
104117
self.pending_messages.discard(message.id)
105118

106119
# Extract the attachments.
107-
files = [
108-
await self._convert_attachment(f)
109-
for f in message.attachments
110-
if "charset" in f.content_type
111-
]
120+
files = [await self._convert_attachment(f) for f in attachments]
112121

113122
# Upload the files to the paste bin, exiting early if there's an error.
114123
log.trace(f"Attempting to upload {len(files)} file(s) to pastebin.")

0 commit comments

Comments
 (0)