Skip to content

Commit 02ef196

Browse files
Copilotseven7ty
andauthored
fix: harden commit codeblock output against fence breakout and mention abuse
Agent-Logs-Url: https://github.com/statch/gitbot/sessions/71de78f4-95f9-4b00-bbde-4a621af540f1 Co-authored-by: seven7ty <63970738+seven7ty@users.noreply.github.com>
1 parent 9bbec65 commit 02ef196

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

cogs/github/numbered/commits.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ async def commit_command(self,
115115
message: str = (f"{self.bot.mgr.truncate(commit['messageBody'], 247, full_word=True)}"
116116
if commit['messageBody'] and commit['messageBody'] != commit['messageHeadline']
117117
else '')
118+
full_headline: str = self.bot.mgr.sanitize_codeblock_content(full_headline)
119+
message: str = self.bot.mgr.sanitize_codeblock_content(message)
118120
empty: str = ctx.l.commit.fields.message.empty if not full_headline and not message else ''
119121
message: str = '```' + full_headline + message + empty + '```'
120122
embed.add_field(name=f':notepad_spiral: {ctx.l.commit.fields.message.name}:', value=message)

lib/manager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ def truncate(str_: str, length: int, ending: str = '...', full_word: bool = Fals
231231
return str_[:length - len(ending)] + ending
232232
return str_
233233

234+
@staticmethod
235+
def sanitize_codeblock_content(content: str, neutralize_mentions: bool = True) -> str:
236+
"""
237+
Harden untrusted text for Discord fenced code blocks.
238+
239+
:param content: The text to sanitize
240+
:param neutralize_mentions: Whether to prevent mention abuse
241+
:return: The sanitized text
242+
"""
243+
content: str = content.replace('```', '`\u200b``')
244+
if neutralize_mentions:
245+
content = content.replace('@', '@\u200b')
246+
return content
247+
234248
@staticmethod
235249
def flatten(iterable: Iterable) -> Iterable:
236250
return list(iterable | traverse)

0 commit comments

Comments
 (0)