From 6824d000887d9090a1480c14829377656d7c67ef Mon Sep 17 00:00:00 2001 From: Kharec Date: Sun, 14 Dec 2025 09:32:29 +0100 Subject: [PATCH] feat: add url validation before trying to download --- auditui/downloads.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/auditui/downloads.py b/auditui/downloads.py index 7d0fd65..1cfc22b 100644 --- a/auditui/downloads.py +++ b/auditui/downloads.py @@ -3,6 +3,7 @@ import re from pathlib import Path from typing import Callable +from urllib.parse import urlparse import audible import httpx @@ -44,6 +45,11 @@ class DownloadManager: notify("Failed to get download link") return None + if not self._validate_download_url(dl_link): + if notify: + notify("Invalid download URL") + return None + if not self._download_file(dl_link, local_path, notify): if notify: notify("Download failed") @@ -66,6 +72,14 @@ class DownloadManager: except Exception: return None + def _validate_download_url(self, url: str) -> bool: + """Validate that the URL is a valid HTTP/HTTPS URL.""" + try: + parsed = urlparse(url) + return parsed.scheme in ("http", "https") and bool(parsed.netloc) + except Exception: + return False + def _sanitize_filename(self, filename: str) -> str: """Remove invalid characters from filename.""" return re.sub(r'[<>:"/\\|?*]', "_", filename) @@ -135,4 +149,3 @@ class DownloadManager: return dest_path except Exception: return None -