fix(telegram): validate remote media URLs

This commit is contained in:
Xubin Ren
2026-03-18 23:12:11 +08:00
committed by Xubin Ren
parent a7bd0f2957
commit 4b052287cb
2 changed files with 81 additions and 1 deletions
+9 -1
View File
@@ -19,6 +19,7 @@ from nanobot.bus.queue import MessageBus
from nanobot.channels.base import BaseChannel
from nanobot.config.paths import get_media_dir
from nanobot.config.schema import Base
from nanobot.security.network import validate_url_target
from nanobot.utils.helpers import split_message
TELEGRAM_MAX_MESSAGE_LEN = 4000 # Telegram message character limit
@@ -313,6 +314,10 @@ class TelegramChannel(BaseChannel):
return "audio"
return "document"
@staticmethod
def _is_remote_media_url(path: str) -> bool:
return path.startswith(("http://", "https://"))
async def send(self, msg: OutboundMessage) -> None:
"""Send a message through Telegram."""
if not self._app:
@@ -356,7 +361,10 @@ class TelegramChannel(BaseChannel):
param = "photo" if media_type == "photo" else media_type if media_type in ("voice", "audio") else "document"
# Telegram Bot API accepts HTTP(S) URLs directly for media params.
if media_path.startswith(("http://", "https://")):
if self._is_remote_media_url(media_path):
ok, error = validate_url_target(media_path)
if not ok:
raise ValueError(f"unsafe media URL: {error}")
await sender(
chat_id=chat_id,
**{param: media_path},