fix(telegram): validate remote media URLs
This commit is contained in:
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user