Skip to content

Commit 7eca701

Browse files
CopilotMattiaFailla
andcommitted
fix: escape user-controlled fields in ban_notification() to prevent HTML injection
Co-authored-by: MattiaFailla <11872425+MattiaFailla@users.noreply.github.com>
1 parent cd6cec8 commit 7eca701

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/python_italy_bot/strings.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
and Python programming metaphors.
55
"""
66

7+
import html
8+
79
BOT_NAME = "Electus"
810

911
# =============================================================================
@@ -90,13 +92,15 @@ def ban_notification(
9092
reason: str | None,
9193
) -> str:
9294
"""Format ban notification message for admins."""
93-
text = f"<b>{chat_title}:</b>\n"
94-
text += f'Utente bannato: <a href="tg://user?id={banned_id}">{banned_name}</a> ({banned_id})\n'
95-
text += (
96-
f'Bannato da: <a href="tg://user?id={admin_id}">{admin_name}</a> ({admin_id})\n'
97-
)
95+
safe_chat_title = html.escape(chat_title, quote=True)
96+
safe_banned_name = html.escape(banned_name, quote=True)
97+
safe_admin_name = html.escape(admin_name, quote=True)
98+
safe_reason = html.escape(reason, quote=True) if reason else "Nessuno"
99+
text = f"<b>{safe_chat_title}:</b>\n"
100+
text += f'Utente bannato: <a href="tg://user?id={banned_id}">{safe_banned_name}</a> ({banned_id})\n'
101+
text += f'Bannato da: <a href="tg://user?id={admin_id}">{safe_admin_name}</a> ({admin_id})\n'
98102
text += f"Gruppi: {success_count}\n"
99-
text += f"Motivo: {reason or 'Nessuno'}"
103+
text += f"Motivo: {safe_reason}"
100104
return text
101105

102106

0 commit comments

Comments
 (0)