Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Filter generic network auto-downloads

Magnus Müller20e70519cf14
diff --git a/browser_use/browser/watchdogs/downloads_watchdog.py b/browser_use/browser/watchdogs/downloads_watchdog.py
index 34658ba1e..176e95b1f 100644
--- a/browser_use/browser/watchdogs/downloads_watchdog.py
+++ b/browser_use/browser/watchdogs/downloads_watchdog.py
@@ -3,6 +3,7 @@
import asyncio
import json
import os
+import re
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar
@@ -30,6 +31,75 @@ if TYPE_CHECKING:
pass
+_NETWORK_DOWNLOAD_FILE_EXTENSIONS = {
+ 'pdf',
+ 'doc',
+ 'docx',
+ 'xls',
+ 'xlsx',
+ 'ppt',
+ 'pptx',
+ 'csv',
+ 'tsv',
+ 'txt',
+ 'json',
+ 'xml',
+ 'zip',
+ 'gz',
+ 'tar',
+ 'jpg',
+ 'jpeg',
+ 'png',
+ 'gif',
+ 'webp',
+}
+
+_GENERIC_TEXT_ATTACHMENT_NAMES = {'f', 'download', 'response', 'data', 'callback'}
+
+
+def _filename_from_content_disposition(content_disposition: str) -> str | None:
+ filename_match = re.search(r'filename[^;=\n]*=(([\'"]).*?\2|[^;\n]*)', content_disposition)
+ if filename_match:
+ return filename_match.group(1).strip('\'"')
+ return None
+
+
+def _has_file_extension(value: str | None) -> bool:
+ if not value:
+ return False
+ return Path(urlparse(value).path if '://' in value else value).suffix.lower().lstrip('.') in _NETWORK_DOWNLOAD_FILE_EXTENSIONS
+
+
+def _is_generic_text_attachment(url: str, content_type: str, suggested_filename: str | None) -> bool:
+ mime = content_type.split(';', 1)[0].strip().lower()
+ if mime not in {'text/plain', 'application/json', 'text/javascript', 'application/javascript'}:
+ return False
+ if _has_file_extension(url):
+ return False
+ if not suggested_filename:
+ return False
+ filename = Path(suggested_filename).name.lower()
+ stem = Path(filename).stem
+ ext = Path(filename).suffix.lower().lstrip('.')
+ return stem in _GENERIC_TEXT_ATTACHMENT_NAMES and ext in {'', 'txt', 'json'}
+
+
+def _should_auto_download_network_response(
+ url: str,
+ content_type: str,
+ is_pdf: bool,
+ is_download_attachment: bool,
+ suggested_filename: str | None,
+) -> bool:
+ if is_pdf:
+ return True
+ if not is_download_attachment:
+ return False
+ if _is_generic_text_attachment(url, content_type, suggested_filename):
+ return False
+ return _has_file_extension(suggested_filename) or _has_file_extension(url) or 'application/octet-stream' in content_type
+
+
class DownloadsWatchdog(BaseWatchdog):
"""Monitors downloads and handles file download events."""
@@ -344,6 +414,18 @@ class DownloadsWatchdog(BaseWatchdog):
if not (is_pdf or is_download_attachment):
return
+ # Extract filename from Content-Disposition if available
+ suggested_filename = _filename_from_content_disposition(content_disposition)
+
+ if not _should_auto_download_network_response(
+ url=url,
+ content_type=content_type,
+ is_pdf=is_pdf,
+ is_download_attachment=is_download_attachment,
+ suggested_filename=suggested_filename,
+ ):
+ return
+
# Check if we've already processed this URL in this session
if url in self._detected_downloads:
self.logger.debug(f'[DownloadsWatchdog] Already detected download: {url[:80]}...')
@@ -352,16 +434,6 @@ class DownloadsWatchdog(BaseWatchdog):
# Mark as detected to avoid duplicates
self._detected_downloads.add(url)
- # Extract filename from Content-Disposition if available
- suggested_filename = None
- if 'filename=' in content_disposition:
- # Parse filename from Content-Disposition header
- import re
-
- filename_match = re.search(r'filename[^;=\n]*=(([\'"]).*?\2|[^;\n]*)', content_disposition)
- if filename_match:
- suggested_filename = filename_match.group(1).strip('\'"')
-
self.logger.info(f'[DownloadsWatchdog] 🔍 Detected downloadable content via network: {url[:80]}...')
self.logger.debug(
f'[DownloadsWatchdog] Content-Type: {content_type}, Is PDF: {is_pdf}, Is Attachment: {is_download_attachment}'
diff --git a/tests/ci/test_downloads_watchdog.py b/tests/ci/test_downloads_watchdog.py
new file mode 100644
index 000000000..c416025d7
--- /dev/null
+++ b/tests/ci/test_downloads_watchdog.py
@@ -0,0 +1,41 @@
+from browser_use.browser.watchdogs.downloads_watchdog import _should_auto_download_network_response
+
+
+def test_downloads_watchdog_skips_generic_text_attachment_without_file_url():
+ assert not _should_auto_download_network_response(
+ url='https://www.google.com/complete/search?q=test&client=gws-wiz&xssi=t',
+ content_type='text/plain',
+ is_pdf=False,
+ is_download_attachment=True,
+ suggested_filename='f.txt',
+ )
+
+
+def test_downloads_watchdog_keeps_pdf_network_response():
+ assert _should_auto_download_network_response(
+ url='https://example.com/view?id=123',
+ content_type='application/pdf',
+ is_pdf=True,
+ is_download_attachment=False,
+ suggested_filename=None,
+ )
+
+
+def test_downloads_watchdog_keeps_named_file_attachment():
+ assert _should_auto_download_network_response(
+ url='https://example.com/download?id=123',
+ content_type='text/csv',
+ is_pdf=False,
+ is_download_attachment=True,
+ suggested_filename='report.csv',
+ )
+
+
+def test_downloads_watchdog_keeps_text_attachment_with_file_url():
+ assert _should_auto_download_network_response(
+ url='https://example.com/files/summary.txt?download=1',
+ content_type='text/plain',
+ is_pdf=False,
+ is_download_attachment=True,
+ suggested_filename='f.txt',
+ )