slack: Requote image_url in render_attachment.

Slack attachment urls with white spaces,
e.g. `https://example.com/some file.png`,
were rejected by `check_url`. We want to call `requote_url` to deal with
any url-quoting jankiness that may be present in the exported data.
This commit is contained in:
Mateusz Mandera
2025-10-04 03:26:42 +08:00
committed by Tim Abbott
parent d484fd95d8
commit e65fb2d051

View File

@@ -5,6 +5,7 @@ from typing import Any, Literal, TypeAlias, TypedDict, cast
import regex
from django.core.exceptions import ValidationError
from requests.utils import requote_uri
from zerver.lib.types import Validator
from zerver.lib.validator import (
@@ -423,8 +424,13 @@ def render_attachment(attachment: WildValue) -> str:
pieces.append("\n".join(fields))
if attachment.get("blocks"):
pieces += map(render_block, attachment["blocks"])
if attachment.get("image_url"):
pieces.append("[]({})".format(attachment["image_url"].tame(check_url)))
if image_url_wv := attachment.get("image_url"):
try:
image_url = image_url_wv.tame(check_url)
except ValidationError: # nocoverage
image_url = image_url_wv.tame(check_string)
image_url = requote_uri(image_url)
pieces.append(f"[]({image_url})")
if attachment.get("footer"):
pieces.append(attachment["footer"].tame(check_string))
if attachment.get("ts"):