Protect link URLs from Telegram inline formatting
Build custom container images / build (map[base_image:php:8-fpm-alpine build_args:PHP_VERSION=8 context:php8-pgsql fingerprint_command:{ apk info -v | LC_ALL=C sort; find /usr/local/lib/php/extensions /usr/local/etc/php/conf.d -type f -exec sha256sum {} + | LC_ALL=C sort; } name:ph… (push) Successful in 39s
Build custom container images / build (map[base_image:postgres:18 build_args:PG_VERSION=18 POSTGIS_VERSION=3 VCHORD_VERSION=0.5.3 context:postgres fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; find /usr/lib/postgresql -type f -exec sha256su… (push) Successful in 48s
Build custom container images / build (map[base_image:python:3 build_args:PYTHON_VERSION=3 context:python-tools fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:python-tools oci_labels:org.opencontainers.ima… (push) Failing after 1m48s
Build custom container images / build (map[base_image:python:3.12-slim build_args:PYTHON_VERSION=3.12-slim context:linkki-tiedotus fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:linkki-tiedotus oci_labels:… (push) Successful in 26s
Build custom container images / build (map[base_image:python:3.12-slim build_args:PYTHON_VERSION=3.12-slim context:timepoll fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:timepoll oci_labels:org.opencontai… (push) Successful in 34s

This commit is contained in:
ajp_anton committed 2026-10-21 22:08:51 +00:00
1 parent 8a73c13b12
commit e54f56d910
2 files changed
+65 -8

No files matched your search

+22 -8
View File
@@ -95,7 +95,18 @@ def markdown_subset_to_html(markdown_text: str) -> str:
escaped_text = _map_blockquotes(escaped_text) escaped_text = _map_blockquotes(escaped_text)
escaped_text = _map_headings(escaped_text) escaped_text = _map_headings(escaped_text)
escaped_text = _map_lists(escaped_text) escaped_text = _map_lists(escaped_text)
escaped_text = _map_standard_links(escaped_text) escaped_text, link_token_values = _stash_standard_links(escaped_text)
escaped_text = _map_inline_styles(escaped_text)
for token, value in link_token_values.items():
escaped_text = escaped_text.replace(token, value)
for token, value in escape_token_values.items():
escaped_text = escaped_text.replace(token, value)
for token, value in code_token_values.items():
escaped_text = escaped_text.replace(token, value)
return escaped_text
def _map_inline_styles(escaped_text: str) -> str:
escaped_text = _map_style_pairs(escaped_text, r"\|\|([\s\S]+?)\|\|", "tg-spoiler") escaped_text = _map_style_pairs(escaped_text, r"\|\|([\s\S]+?)\|\|", "tg-spoiler")
escaped_text = _map_style_pairs(escaped_text, r"\*\*(.+?)\*\*", "b") escaped_text = _map_style_pairs(escaped_text, r"\*\*(.+?)\*\*", "b")
escaped_text = _map_style_pairs(escaped_text, r"__(.+?)__", "b") escaped_text = _map_style_pairs(escaped_text, r"__(.+?)__", "b")
@@ -103,10 +114,6 @@ def markdown_subset_to_html(markdown_text: str) -> str:
escaped_text = _map_style_pairs(escaped_text, r"~~(.+?)~~", "s") escaped_text = _map_style_pairs(escaped_text, r"~~(.+?)~~", "s")
escaped_text = _map_style_pairs(escaped_text, r"(?<!\*)\*([^*\n]+)\*(?!\*)", "i") escaped_text = _map_style_pairs(escaped_text, r"(?<!\*)\*([^*\n]+)\*(?!\*)", "i")
escaped_text = _map_style_pairs(escaped_text, r"(?<!_)_([^_\n]+)_(?!_)", "i") escaped_text = _map_style_pairs(escaped_text, r"(?<!_)_([^_\n]+)_(?!_)", "i")
for token, value in escape_token_values.items():
escaped_text = escaped_text.replace(token, value)
for token, value in code_token_values.items():
escaped_text = escaped_text.replace(token, value)
return escaped_text return escaped_text
@@ -208,13 +215,20 @@ def _list_indent(level: int) -> str:
return "\u00a0" * (level * 4) return "\u00a0" * (level * 4)
def _map_standard_links(text: str) -> str: def _stash_standard_links(text: str) -> tuple[str, dict[str, str]]:
token_values: dict[str, str] = {}
def repl(match: re.Match[str]) -> str: def repl(match: re.Match[str]) -> str:
label = match.group(1) label = match.group(1)
url = match.group(2) url = match.group(2)
return f'<a href="{html.escape(url, quote=True)}">{label}</a>' # URL labels and destinations are literal, not inline Markdown.
if label != url and not re.match(r"[a-z][a-z0-9+.-]*://", label, re.IGNORECASE):
label = _map_inline_styles(label)
token = f"ZZLINK{len(token_values)}ZZ"
token_values[token] = f'<a href="{html.escape(html.unescape(url), quote=True)}">{label}</a>'
return token
return re.sub(r"\[([^\]]+)\]\(([^)\s]+)\)", repl, text) return re.sub(r"\[([^\]]+)\]\(([^)\s]+)\)", repl, text), token_values
def _map_style_pairs(text: str, pattern: str, html_tag: str) -> str: def _map_style_pairs(text: str, pattern: str, html_tag: str) -> str:
+43
View File
@@ -1,3 +1,7 @@
import html
import pytest
from linkki_poster.formatting import ( from linkki_poster.formatting import (
CAPTION_LIMIT, CAPTION_LIMIT,
estimate_caption_length, estimate_caption_length,
@@ -123,3 +127,42 @@ def test_apostrophes_are_not_escaped_in_headings_or_plain_text() -> None:
assert "&#x27;" not in heading assert "&#x27;" not in heading
assert "&#x27;" not in plain_heading_char assert "&#x27;" not in plain_heading_char
assert "&#x27;" not in bold assert "&#x27;" not in bold
@pytest.mark.parametrize("url", [
"https://example.org/ril_gaala",
"https://example.org/a__b++c~~d||e*f",
"https://example.org/event?name=ril_gaala&language=fi",
])
def test_url_labels_and_destinations_are_not_parsed_as_styles(url: str) -> None:
rendered = render_template(parse_markdown_template(f"[{url}]({url})"), {})
assert rendered == f'<a href="{html.escape(url, quote=True)}">{html.escape(url, quote=False)}</a>'
def test_url_label_remains_literal_when_destination_differs() -> None:
rendered = render_template(parse_markdown_template(
"[https://example.org/a_b_c](https://example.org/register)",
), {})
assert rendered == '<a href="https://example.org/register">https://example.org/a_b_c</a>'
def test_formatted_link_label_and_surrounding_styles_still_work() -> None:
rendered = render_template(parse_markdown_template(
"_Read [**bold** ++underlined++](https://example.org/a_b_c) now_",
), {})
assert rendered == '<i>Read <a href="https://example.org/a_b_c"><b>bold</b> <u>underlined</u></a> now</i>'
def test_url_underscore_does_not_consume_later_italic_marker() -> None:
rendered = render_template(parse_markdown_template(
"[Register](https://example.org/ril_gaala) and _italic_",
), {})
assert rendered == '<a href="https://example.org/ril_gaala">Register</a> and <i>italic</i>'
def test_escaped_link_markers_are_restored_inside_protected_links() -> None:
rendered = render_template(parse_markdown_template(
r"[https://example.org/ril\_gaala](https://example.org/ril\_gaala)",
), {})
assert rendered == '<a href="https://example.org/ril_gaala">https://example.org/ril_gaala</a>'