fix(telegram): hermes send keeps an HTML tag whose quoted attribute holds >

_TELEGRAM_HTML_TAG_RE stopped at the first `>`, even inside a quoted
attribute, so the new escaping pass turned `<a href="…a>b">` into
`…a&gt;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).
This commit is contained in:
Teknium
2026-11-09 04:03:38 -08:00
parent 27971aa4d4
commit 2903e03e4a
2 changed files with 12 additions and 1 deletions

View File

@@ -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 ``<b>`` never matches ``<body>``; 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"</?(?:{_TELEGRAM_HTML_TAG_ALT})(?:\s[^>]*)?>", re.IGNORECASE)
_TELEGRAM_HTML_TAG_RE = re.compile(
rf"""</?(?:{_TELEGRAM_HTML_TAG_ALT})(?:\s(?:[^>"']++|"[^"]*+"|'[^']*+')*+)?>""", re.IGNORECASE)
def _telegram_album_paths(media_files, force_document, caption):

View File

@@ -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 = '<a href="https://x.y/?q=a>b">link</a> and <b>bold</b>'
assert _telegram_format(markup) == (markup, "HTML", True)