From 2903e03e4a4e2b68e1bfd2bdd14844bfb8615b46 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:03:38 -0800 Subject: [PATCH] fix(telegram): hermes send keeps an HTML tag whose quoted attribute holds `>` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _TELEGRAM_HTML_TAG_RE stopped at the first `>`, even inside a quoted attribute, so the new escaping pass turned `` into `…a>b">` and broke a tag that used to pass through. The tag pattern is now quote-aware: it ends at the first unquoted `>`. Greptile thread: "Escaping breaks valid HTML links" (4511713825). --- plugins/platforms/telegram/standalone.py | 4 +++- tests/tools/test_telegram_send_message_html_whitelist.py | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/platforms/telegram/standalone.py b/plugins/platforms/telegram/standalone.py index dae7ba7a0f..76f34b54fd 100644 --- a/plugins/platforms/telegram/standalone.py +++ b/plugins/platforms/telegram/standalone.py @@ -265,8 +265,10 @@ _TELEGRAM_HTML_TAGS = ("b", "strong", "i", "em", "u", "ins", "s", "strike", "del "code", "pre", "blockquote", "tg-spoiler", "span", "tg-emoji") # Opening tag (optionally with attributes) or closing tag. The name must end at ``>`` or # whitespace, so ```` never matches ````; longest-first keeps the alternation readable. +# Quoted attribute values may hold ``>`` (``href="a>b"``): the tag ends at the first unquoted one. _TELEGRAM_HTML_TAG_ALT = "|".join(sorted(_TELEGRAM_HTML_TAGS, key=len, reverse=True)) -_TELEGRAM_HTML_TAG_RE = re.compile(rf"]*)?>", re.IGNORECASE) +_TELEGRAM_HTML_TAG_RE = re.compile( + rf""""']++|"[^"]*+"|'[^']*+')*+)?>""", re.IGNORECASE) def _telegram_album_paths(media_files, force_document, caption): diff --git a/tests/tools/test_telegram_send_message_html_whitelist.py b/tests/tools/test_telegram_send_message_html_whitelist.py index 7ca6925bcf..5f495594bf 100644 --- a/tests/tools/test_telegram_send_message_html_whitelist.py +++ b/tests/tools/test_telegram_send_message_html_whitelist.py @@ -107,3 +107,12 @@ def test_tag_shown_only_as_markdown_code_keeps_the_markdown_branch( assert (parse_mode, has_html) == ("MarkdownV2", False) assert formatted.startswith("*Note*") and "**" not in formatted + + +def test_a_quoted_gt_inside_an_attribute_does_not_break_the_tag( + monkeypatch: pytest.MonkeyPatch) -> None: + from plugins.platforms.telegram.standalone import _telegram_format + + _install_telegram_mock(monkeypatch, MagicMock()) + markup = 'link and bold' + assert _telegram_format(markup) == (markup, "HTML", True)