Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bot/services/ai/telegram_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from html import escape
import re

_BOLD_RE = re.compile(r"\*\*(.+?)\*\*")
Expand All @@ -15,7 +16,7 @@ def normalize_llm_output_for_telegram_html(text: str) -> str:
lines = text.splitlines()
normalized_lines: list[str] = []
for line in lines:
normalized_lines.append(_BULLET_RE.sub("β€’ ", line))
normalized_lines.append(_BULLET_RE.sub("β€’ ", escape(line, quote=False)))

normalized = "\n".join(normalized_lines)
normalized = _BOLD_RE.sub(r"<b>\1</b>", normalized)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_telegram_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ def test_markdown_bullets_to_dot_bullets() -> None:
text = "- first\n* second\n - third"
out = normalize_llm_output_for_telegram_html(text)
assert out.splitlines() == ["β€’ first", "β€’ second", "β€’ third"]


def test_escapes_raw_html_control_characters() -> None:
text = "Use x < y && y > 0 in <script>alert(1)</script>."
out = normalize_llm_output_for_telegram_html(text)
assert out == "Use x &lt; y &amp;&amp; y &gt; 0 in &lt;script&gt;alert(1)&lt;/script&gt;."


def test_markdown_formatting_escapes_inner_html() -> None:
text = "**x < y** and `a & b`"
out = normalize_llm_output_for_telegram_html(text)
assert out == "<b>x &lt; y</b> and <code>a &amp; b</code>"
Loading