Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-9802): capture XHR inline file downloads during download actions (#6054)
LawyZheng2b47451399fa
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 6d29230d9..a0ce956cf 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -15,7 +15,7 @@ import structlogfrom fuzzysearch import find_near_matchesfrom opentelemetry import trace as otel_tracefrom playwright._impl._errors import Error as PlaywrightError-from playwright.async_api import Download, FileChooser, Frame, Locator, Page, TimeoutError+from playwright.async_api import Download, FileChooser, Frame, Locator, Page, Response, TimeoutErrorfrom pydantic import BaseModelfrom skyvern.config import settings@@ -111,6 +111,12 @@ from skyvern.webeye.actions.actions import ()from skyvern.webeye.actions.responses import ActionAbort, ActionFailure, ActionResult, ActionSuccessfrom skyvern.webeye.browser_factory import initialize_download_dir+from skyvern.webeye.cdp_download_interceptor import (+ DOWNLOAD_MIME_TYPES,+ MAX_FILE_SIZE_BYTES,+ extract_filename,+ is_download_response,+)from skyvern.webeye.scraper.scraped_page import (CleanupElementTreeFunc,ElementTreeBuilder,@@ -501,6 +507,105 @@ class AutoCompletionResult(BaseModel):action_result: ActionResult = ActionSuccess()+class ScopedXhrDownloadCapture:+ """Install on a page before a download action; remove after the polling window.++ Skipped when CDPDownloadInterceptor is active on the browser context+ (detected via ``_skyvern_cdp_download_active`` flag) because the CDP path+ already handles downloads at the Fetch domain level.++ Automatically attaches to new pages opened during the action window+ (e.g. target="_blank" links) so XHR responses on child tabs are captured.+ """++ def __init__(self, page: Page, download_dir: Path) -> None:+ self._page = page+ self._download_dir = download_dir+ self._saved: set[str] = set()+ self._extra_pages: list[Page] = []+ self._active = False++ def _is_xhr_download(self, headers: dict[str, str], status: int) -> bool:+ """Check if an XHR response carries a downloadable file body.++ Reuses ``is_download_response`` for attachment cases. For inline+ responses, additionally accepts download MIME + explicit filename+ (the case ``is_download_response`` intentionally rejects for the+ global CDP path to avoid false positives on PDF previews).+ """+ if is_download_response(headers, status, resource_type="XHR"):+ return True+ if status >= 400:+ return False+ content_type = headers.get("content-type", "").split(";")[0].strip().lower()+ content_disposition = headers.get("content-disposition", "")+ if content_type not in DOWNLOAD_MIME_TYPES:+ return False+ return bool(re.search(r"filename\s*[*]?\s*=", content_disposition, re.IGNORECASE))++ async def _on_response(self, response: Response) -> None:+ try:+ if response.request.resource_type not in ("xhr", "fetch"):+ return+ headers = response.headers+ if not self._is_xhr_download(headers, response.status):+ return+ raw_filename = extract_filename({"content-disposition": headers.get("content-disposition", "")}, "")+ filename = Path(raw_filename).name if raw_filename else ""+ if not filename or filename in self._saved:+ return+ content_length = headers.get("content-length", "")+ if content_length:+ try:+ if int(content_length) > MAX_FILE_SIZE_BYTES:+ return+ except ValueError:+ pass+ save_path = self._download_dir / filename+ body = await response.body()+ if len(body) > MAX_FILE_SIZE_BYTES:+ return+ try:+ with open(save_path, "xb") as f:+ f.write(body)+ except FileExistsError:+ pass+ self._saved.add(filename)+ LOG.info(+ "XHR download captured during download action",+ filename=filename,+ size=len(body),+ )+ except Exception:+ LOG.warning("Failed to capture XHR download response", exc_info=True)++ def _on_new_page(self, page: Page) -> None:+ if not self._active:+ return+ page.on("response", self._on_response)+ self._extra_pages.append(page)++ def enable(self) -> None:+ if getattr(self._page.context, "_skyvern_cdp_download_active", False):+ return+ self._page.on("response", self._on_response)+ self._page.context.on("page", self._on_new_page)+ self._active = True++ def disable(self) -> None:+ if not self._active:+ return+ self._page.remove_listener("response", self._on_response)+ self._page.context.remove_listener("page", self._on_new_page)+ for page in self._extra_pages:+ try:+ page.remove_listener("response", self._on_response)+ except Exception:+ pass+ self._extra_pages.clear()+ self._active = False++class ActionHandler:_handled_action_types: dict[ActionType,@@ -614,9 +719,11 @@ class ActionHandler:download_dir=download_dir,)+ xhr_capture = ScopedXhrDownloadCapture(page, download_dir)download_triggered = Falsepage.on("download", _capture_download_event)try:+ xhr_capture.enable()with _tracer.start_as_current_span("skyvern.agent.action.handle_inner") as _hi_span:apply_context_attrs(_hi_span)results = await ActionHandler._handle_action(@@ -810,6 +917,7 @@ class ActionHandler:return resultsfinally:+ xhr_capture.disable()if browser_state is not None and download_triggered:# get the page count after downloadpages_after_download = await browser_state.list_valid_pages()diff --git a/skyvern/webeye/browser_factory.py b/skyvern/webeye/browser_factory.pyindex a4361ea8a..b13589305 100644--- a/skyvern/webeye/browser_factory.py+++ b/skyvern/webeye/browser_factory.py@@ -850,6 +850,7 @@ async def _connect_to_cdp_browser(LOG.warning("Failed to enable CDP intercept on new page", page_url=page.url, exc_info=True)browser_context.on("page", lambda page: asyncio.ensure_future(_on_new_page(page)))+ browser_context._skyvern_cdp_download_active = True # type: ignore[attr-defined]LOG.info("CDP download interceptor enabled",download_dir=download_dir,diff --git a/tests/unit/test_cdp_download_interceptor.py b/tests/unit/test_cdp_download_interceptor.pyindex c31d9b2a4..b88f1cfc7 100644--- a/tests/unit/test_cdp_download_interceptor.py+++ b/tests/unit/test_cdp_download_interceptor.py@@ -110,15 +110,23 @@ class TestIsDownloadResponse:# XHR/Fetch without attachment header should NOT be download (MIME-only false positive)def test_xhr_mime_only_not_download(self) -> None:- """XHR with download MIME type but no attachment header should NOT be treated as download."""+ """XHR with download MIME type but no Content-Disposition at all should NOT be treated as download."""headers = {"content-type": "application/pdf"}assert is_download_response(headers, 200, resource_type="XHR") is Falsedef test_fetch_mime_only_not_download(self) -> None:- """Fetch with download MIME type but no attachment header should NOT be treated as download."""+ """Fetch with download MIME type but no Content-Disposition at all should NOT be treated as download."""headers = {"content-type": "application/octet-stream"}assert is_download_response(headers, 200, resource_type="Fetch") is False+ def test_xhr_inline_pdf_with_filename_not_download(self) -> None:+ """XHR with inline + filename is NOT a CDP download — handled by ScopedXhrDownloadCapture instead."""+ headers = {+ "content-disposition": 'inline; filename="Denali 10.pdf"',+ "content-type": "application/pdf",+ }+ assert is_download_response(headers, 200, resource_type="XHR") is False+# XHR/Fetch with attachment but API content-type should still be filtereddef test_xhr_json_attachment_not_download(self) -> None:"""XHR with JSON content-type and attachment header should NOT be download."""diff --git a/tests/unit/test_download_action_dedup.py b/tests/unit/test_download_action_dedup.pynew file mode 100644index 000000000..2852e317f--- /dev/null+++ b/tests/unit/test_download_action_dedup.py@@ -0,0 +1,157 @@+"""Tests for the download action file dedup/cleanup logic in handler.py.++These tests exercise the dedup block inline in handle_action by creating+real files in tmp_path and calling the same logic the handler uses:+re-scan → remove 0-byte → checksum dedup.+"""++import os+from pathlib import Path++from skyvern.forge.sdk.api.files import calculate_sha256_for_file, list_files_in_directory+++def _run_dedup(download_dir: Path, list_files_before: list[str]) -> list[str]:+ """Reproduce the dedup block from handle_action (post-rescan version)."""+ list_files_after = list_files_in_directory(download_dir)+ new_file_paths = set(list_files_after) - set(list_files_before)+ seen_checksums: dict[str, str] = {}+ deduplicated_paths: list[str] = []+ for fp in sorted(new_file_paths):+ if not os.path.isfile(fp):+ deduplicated_paths.append(fp)+ continue+ if os.path.getsize(fp) == 0:+ os.remove(fp)+ continue+ checksum = calculate_sha256_for_file(fp)+ if checksum in seen_checksums:+ os.remove(fp)+ else:+ seen_checksums[checksum] = fp+ deduplicated_paths.append(fp)+ return [os.path.basename(fp) for fp in deduplicated_paths]+++class TestRescanCatchesLateFiles:+ def test_file_appearing_after_initial_snapshot(self, tmp_path: Path) -> None:+ """A file that appears after the polling-loop snapshot should be+ caught by re-scanning the directory."""+ before: list[str] = []++ # Simulate: polling loop sees file_a and breaks+ (tmp_path / "file_a.pdf").write_bytes(b"content_a")+ stale_snapshot = list_files_in_directory(tmp_path)+ assert len(stale_snapshot) == 1++ # Simulate: browser-native download completes after the break+ (tmp_path / "file_b.pdf").write_bytes(b"content_b")++ # Re-scan picks up both+ result = _run_dedup(tmp_path, before)+ assert sorted(result) == ["file_a.pdf", "file_b.pdf"]++ def test_late_duplicate_removed_by_rescan(self, tmp_path: Path) -> None:+ """A late-arriving file with identical content should be deduped."""+ before: list[str] = []+ content = b"same pdf content"++ (tmp_path / "xhr_capture.pdf").write_bytes(content)+ # Browser-native download arrives later with different name+ (tmp_path / "abc123.pdf").write_bytes(content)++ result = _run_dedup(tmp_path, before)+ assert len(result) == 1+ # Only one file should remain on disk+ remaining = list(tmp_path.iterdir())+ assert len(remaining) == 1+++class TestZeroByteFileRemoval:+ def test_zero_byte_file_removed(self, tmp_path: Path) -> None:+ """A 0-byte file should be deleted, not included in results."""+ before: list[str] = []+ (tmp_path / "good.pdf").write_bytes(b"real content")+ (tmp_path / "empty.pdf").write_bytes(b"")++ result = _run_dedup(tmp_path, before)+ assert result == ["good.pdf"]+ assert not (tmp_path / "empty.pdf").exists()++ def test_only_zero_byte_file_yields_empty_result(self, tmp_path: Path) -> None:+ """If the only new file is 0 bytes, result is empty."""+ before: list[str] = []+ (tmp_path / "broken.pdf").write_bytes(b"")++ result = _run_dedup(tmp_path, before)+ assert result == []+ assert not (tmp_path / "broken.pdf").exists()+++class TestChecksumDedup:+ def test_identical_files_deduped(self, tmp_path: Path) -> None:+ """Two files with same content: keep first (sorted), remove second."""+ before: list[str] = []+ content = b"identical content"+ (tmp_path / "a_first.pdf").write_bytes(content)+ (tmp_path / "b_second.pdf").write_bytes(content)++ result = _run_dedup(tmp_path, before)+ assert result == ["a_first.pdf"]+ assert (tmp_path / "a_first.pdf").exists()+ assert not (tmp_path / "b_second.pdf").exists()++ def test_different_files_both_kept(self, tmp_path: Path) -> None:+ before: list[str] = []+ (tmp_path / "report_a.pdf").write_bytes(b"content_a")+ (tmp_path / "report_b.pdf").write_bytes(b"content_b")++ result = _run_dedup(tmp_path, before)+ assert sorted(result) == ["report_a.pdf", "report_b.pdf"]+++class TestRemoteUriSafety:+ def test_remote_uri_not_hashed_or_deleted(self, tmp_path: Path) -> None:+ """Remote URIs (s3://, azure://) should be passed through without+ hashing or deletion attempts."""+ before: list[str] = []+ (tmp_path / "local.pdf").write_bytes(b"local content")++ # Simulate browser-session files as remote URIs in list_files_after+ remote_uri = "s3://bucket/org/browser_sessions/bs_123/downloads/report.pdf"+ list_files_after = list_files_in_directory(tmp_path) + [remote_uri]++ new_file_paths = set(list_files_after) - set(before)+ deduplicated_paths: list[str] = []+ seen_checksums: dict[str, str] = {}+ for fp in sorted(new_file_paths):+ if not os.path.isfile(fp):+ # Remote URIs pass through here (not a local file)+ deduplicated_paths.append(fp)+ continue+ if os.path.getsize(fp) == 0:+ os.remove(fp)+ continue+ checksum = calculate_sha256_for_file(fp)+ if checksum in seen_checksums:+ os.remove(fp)+ else:+ seen_checksums[checksum] = fp+ deduplicated_paths.append(fp)++ basenames = sorted(os.path.basename(fp) for fp in deduplicated_paths)+ assert basenames == ["local.pdf", "report.pdf"]+ assert (tmp_path / "local.pdf").exists()+++class TestPreExistingFilesExcluded:+ def test_files_present_before_action_not_in_result(self, tmp_path: Path) -> None:+ """Files in list_files_before should not appear in new_file_paths."""+ (tmp_path / "old.pdf").write_bytes(b"old")+ before = list_files_in_directory(tmp_path)++ (tmp_path / "new.pdf").write_bytes(b"new")++ result = _run_dedup(tmp_path, before)+ assert result == ["new.pdf"]+ assert (tmp_path / "old.pdf").exists()diff --git a/tests/unit/test_scoped_xhr_download_capture.py b/tests/unit/test_scoped_xhr_download_capture.pynew file mode 100644index 000000000..0506d7c52--- /dev/null+++ b/tests/unit/test_scoped_xhr_download_capture.py@@ -0,0 +1,190 @@+"""Tests for ScopedXhrDownloadCapture — action-scoped XHR download listener."""++from pathlib import Path+from unittest.mock import AsyncMock, MagicMock++import pytest++from skyvern.webeye.actions.handler import ScopedXhrDownloadCapture+++def _make_response(+ *,+ resource_type: str = "xhr",+ status: int = 200,+ content_type: str = "application/pdf",+ content_disposition: str = 'inline; filename="report.pdf"',+ body: bytes = b"%PDF-1.4 fake",+ content_length: str | None = None,+) -> MagicMock:+ resp = AsyncMock()+ resp.status = status+ request_mock = MagicMock()+ request_mock.resource_type = resource_type+ resp.request = request_mock+ resp.url = "https://example.com/api/report"+ headers = {"content-type": content_type, "content-disposition": content_disposition}+ if content_length is not None:+ headers["content-length"] = content_length+ resp.headers = headers+ resp.body = AsyncMock(return_value=body)+ return resp+++def _make_page(*, cdp_active: bool = False) -> MagicMock:+ page = MagicMock()+ context = MagicMock()+ context._skyvern_cdp_download_active = cdp_active+ page.context = context+ return page+++class TestScopedXhrDownloadCapture:+ def test_enable_registers_listener_on_page_and_context(self) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, Path("/tmp/downloads"))+ capture.enable()+ page.on.assert_called_once_with("response", capture._on_response)+ page.context.on.assert_called_once_with("page", capture._on_new_page)++ def test_disable_removes_all_listeners(self) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, Path("/tmp/downloads"))+ capture.enable()+ capture.disable()+ page.remove_listener.assert_called_once_with("response", capture._on_response)+ page.context.remove_listener.assert_called_once_with("page", capture._on_new_page)++ def test_skips_when_cdp_interceptor_active(self) -> None:+ page = _make_page(cdp_active=True)+ capture = ScopedXhrDownloadCapture(page, Path("/tmp/downloads"))+ capture.enable()+ page.on.assert_not_called()+ assert not capture._active++ def test_disable_noop_when_not_enabled(self) -> None:+ page = _make_page(cdp_active=True)+ capture = ScopedXhrDownloadCapture(page, Path("/tmp/downloads"))+ capture.enable()+ capture.disable()+ page.remove_listener.assert_not_called()++ @pytest.mark.asyncio+ async def test_saves_xhr_inline_pdf_to_download_dir(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response = _make_response()++ await capture._on_response(response)++ saved = tmp_path / "report.pdf"+ assert saved.exists()+ assert saved.read_bytes() == b"%PDF-1.4 fake"++ @pytest.mark.asyncio+ async def test_saves_xhr_attachment_pdf(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response = _make_response(content_disposition='attachment; filename="invoice.pdf"')++ await capture._on_response(response)++ assert (tmp_path / "invoice.pdf").exists()++ @pytest.mark.asyncio+ async def test_ignores_non_xhr_response(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response = _make_response(resource_type="document")++ await capture._on_response(response)++ assert list(tmp_path.iterdir()) == []++ @pytest.mark.asyncio+ async def test_ignores_json_response(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response = _make_response(content_type="application/json", content_disposition="")++ await capture._on_response(response)++ assert list(tmp_path.iterdir()) == []++ @pytest.mark.asyncio+ async def test_ignores_xhr_pdf_without_filename(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response = _make_response(content_disposition="inline")++ await capture._on_response(response)++ assert list(tmp_path.iterdir()) == []++ @pytest.mark.asyncio+ async def test_ignores_error_responses(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response = _make_response(status=403)++ await capture._on_response(response)++ assert list(tmp_path.iterdir()) == []++ @pytest.mark.asyncio+ async def test_deduplicates_same_filename(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response1 = _make_response(body=b"first")+ response2 = _make_response(body=b"second")++ await capture._on_response(response1)+ await capture._on_response(response2)++ saved = tmp_path / "report.pdf"+ assert saved.read_bytes() == b"first"++ @pytest.mark.asyncio+ async def test_sanitizes_path_traversal(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response = _make_response(content_disposition='inline; filename="../../etc/evil.pdf"')++ await capture._on_response(response)++ assert not (tmp_path.parent.parent / "etc" / "evil.pdf").exists()+ assert (tmp_path / "evil.pdf").exists()++ @pytest.mark.asyncio+ async def test_skips_oversized_content_length(self, tmp_path: Path) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, tmp_path)+ response = _make_response(content_length="200000000")++ await capture._on_response(response)++ assert list(tmp_path.iterdir()) == []++ def test_new_page_gets_listener(self) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, Path("/tmp/downloads"))+ new_page = MagicMock()++ capture.enable()+ capture._on_new_page(new_page)++ new_page.on.assert_called_once_with("response", capture._on_response)+ assert new_page in capture._extra_pages++ def test_disable_cleans_up_extra_pages(self) -> None:+ page = _make_page()+ capture = ScopedXhrDownloadCapture(page, Path("/tmp/downloads"))+ new_page = MagicMock()++ capture.enable()+ capture._on_new_page(new_page)+ capture.disable()++ new_page.remove_listener.assert_called_once_with("response", capture._on_response)+ assert capture._extra_pages == []+ assert not capture._active