Skip to content

Automate browser based workflows with AI

fix: broaden CDP download interceptor to capture XHR file responses with generic binary MIME types (#7153)

LawyZheng368d4a5453ac
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index 1b11e4724..2e2d53e30 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -1464,7 +1464,10 @@ class ScopedXhrDownloadCapture:
headers = response.headers
if not self._is_xhr_download(headers, response.status):
return
- raw_filename = extract_filename({"content-disposition": headers.get("content-disposition", "")}, "")
+ response_url = response.url
+ raw_filename = extract_filename(
+ {"content-disposition": headers.get("content-disposition", "")}, response_url
+ )
filename = normalize_download_filename(raw_filename, headers.get("content-type", ""))
if not filename or filename in self._saved:
return
diff --git a/skyvern/webeye/cdp_download_interceptor.py b/skyvern/webeye/cdp_download_interceptor.py
index c0843de1c..febdbb648 100644
--- a/skyvern/webeye/cdp_download_interceptor.py
+++ b/skyvern/webeye/cdp_download_interceptor.py
@@ -99,9 +99,26 @@ DOWNLOAD_MIME_TYPES = frozenset(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel",
"application/msword",
+ "text/csv",
+ "application/csv",
}
)
+# Literal Content-Type strings that some misconfigured servers send verbatim for
+# file bytes (e.g. the literal "application/*"). Matched by exact string equality,
+# NOT wildcard/prefix semantics. Only eligible for XHR/Fetch responses with
+# Content-Length >= MIN_XHR_DOWNLOAD_BYTES; non-XHR responses must rely on stronger
+# signals (attachment header or known download MIME).
+GENERIC_DOWNLOAD_CONTENT_TYPE_LITERALS = frozenset(
+ {
+ "application/*",
+ }
+)
+
+# Minimum response size (bytes) for XHR/Fetch responses with generic binary MIME to be
+# treated as downloads, even without Content-Disposition: attachment.
+MIN_XHR_DOWNLOAD_BYTES = 1024 # 1 KB
+
DOWNLOAD_EXTENSION_BY_MIME_TYPE = {
"application/pdf": ".pdf",
}
@@ -176,6 +193,9 @@ def is_download_response(headers: dict[str, str], status_code: int, resource_typ
2. Skip API content types (application/json, etc.)
3. For XHR/Fetch: require BOTH attachment header AND download MIME type
(prevents false positives like Google's text/plain + attachment XHR responses)
+ Exception: generic binary MIME types (like application/*) where the server
+ does not set a specific Content-Type but the response carries meaningful
+ bytes (Content-Length >= MIN_XHR_DOWNLOAD_BYTES).
4. Content-Disposition contains "attachment"
5. Content-Type is a known download MIME type
"""
@@ -193,11 +213,21 @@ def is_download_response(headers: dict[str, str], status_code: int, resource_typ
is_attachment = "attachment" in content_disposition.lower()
is_download_mime = content_type in DOWNLOAD_MIME_TYPES
+ is_generic_binary = content_type in GENERIC_DOWNLOAD_CONTENT_TYPE_LITERALS
# XHR/Fetch require both signals to avoid false positives
# (e.g. Google async requests: text/plain + attachment; filename="f.txt")
if resource_type in XHR_FETCH_RESOURCE_TYPES:
- return is_attachment and is_download_mime
+ # Primary path: attachment header + known download MIME
+ if is_attachment and is_download_mime:
+ return True
+ # Secondary path: generic binary MIME with evidence of actual file content.
+ # Some sites (e.g. report exports) return XHR file responses with
+ # Content-Type: application/* and no Content-Disposition header.
+ content_length = _parse_content_length(headers)
+ if is_generic_binary and content_length is not None and content_length >= MIN_XHR_DOWNLOAD_BYTES:
+ return True
+ return False
if is_attachment:
return True
diff --git a/tests/unit/test_cdp_download_interceptor.py b/tests/unit/test_cdp_download_interceptor.py
index eaa26feb1..c249314c5 100644
--- a/tests/unit/test_cdp_download_interceptor.py
+++ b/tests/unit/test_cdp_download_interceptor.py
@@ -126,8 +126,64 @@ class TestIsDownloadResponse:
{"content-disposition": "attachment", "content-type": "text/csv"},
200,
"XHR",
+ True,
+ id="xhr_attachment_csv_is_download",
+ ),
+ pytest.param(
+ {"content-disposition": "attachment", "content-type": "application/csv"},
+ 200,
+ "XHR",
+ True,
+ id="xhr_attachment_application_csv_is_download",
+ ),
+ pytest.param(
+ {"content-type": "application/*", "content-length": "46681129"},
+ 200,
+ "XHR",
+ True,
+ id="xhr_generic_binary_with_bytes_is_download",
+ ),
+ pytest.param(
+ {"content-type": "application/*"},
+ 200,
+ "XHR",
+ False,
+ id="xhr_generic_binary_no_length_not_download",
+ ),
+ pytest.param(
+ {"content-type": "application/*", "content-length": "6"},
+ 200,
+ "XHR",
False,
- id="xhr_attachment_csv_not_download",
+ id="xhr_generic_binary_small_body_not_download",
+ ),
+ pytest.param(
+ {"content-type": "application/*"},
+ 200,
+ "",
+ False,
+ id="non_xhr_generic_binary_no_length_not_download",
+ ),
+ pytest.param(
+ {"content-type": "application/*"},
+ 200,
+ "Document",
+ False,
+ id="non_xhr_generic_binary_document_no_length_not_download",
+ ),
+ pytest.param(
+ {"content-type": "application/*", "content-length": "9999999"},
+ 200,
+ "Other",
+ False,
+ id="non_xhr_generic_binary_other_large_not_download",
+ ),
+ pytest.param(
+ {"content-type": "application/*", "content-length": "2048"},
+ 200,
+ "Fetch",
+ True,
+ id="fetch_generic_binary_with_bytes_is_download",
),
pytest.param(
{"content-type": "application/pdf"},
diff --git a/tests/unit/test_scoped_xhr_download_capture.py b/tests/unit/test_scoped_xhr_download_capture.py
index 488ff1919..8e91bfbe3 100644
--- a/tests/unit/test_scoped_xhr_download_capture.py
+++ b/tests/unit/test_scoped_xhr_download_capture.py
@@ -17,14 +17,17 @@ def _make_response(
content_disposition: str = 'inline; filename="report.pdf"',
body: bytes = b"%PDF-1.4 fake",
content_length: str | None = None,
+ url: str = "https://example.com/api/report",
) -> 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}
+ resp.url = url
+ headers: dict[str, str] = {"content-type": content_type}
+ if content_disposition:
+ headers["content-disposition"] = content_disposition
if content_length is not None:
headers["content-length"] = content_length
resp.headers = headers
@@ -222,3 +225,78 @@ class TestScopedXhrDownloadCapture:
page = _make_page()
capture = ScopedXhrDownloadCapture(page, tmp_path)
await capture.drain()
+
+ @pytest.mark.asyncio
+ async def test_saves_xhr_generic_binary_with_large_body(self, tmp_path: Path) -> None:
+ """Production shape: XHR with application/*, large body, no Content-Disposition,
+ filename extracted from URL path."""
+ page = _make_page()
+ capture = ScopedXhrDownloadCapture(page, tmp_path)
+ response = _make_response(
+ content_type="application/*",
+ content_disposition="",
+ content_length="46681129",
+ body=b"fake-excel-bytes" * 1024,
+ url="https://example.com/report/General.xlsx",
+ )
+
+ await capture._on_response(response)
+
+ saved = tmp_path / "General.xlsx"
+ assert saved.exists()
+ assert saved.stat().st_size > 0
+
+ @pytest.mark.asyncio
+ async def test_skips_xhr_generic_binary_small_body(self, tmp_path: Path) -> None:
+ """XHR with application/* and very small Content-Length (6) should NOT save."""
+ page = _make_page()
+ capture = ScopedXhrDownloadCapture(page, tmp_path)
+ response = _make_response(
+ content_type="application/*",
+ content_disposition="",
+ content_length="6",
+ body=b"empty",
+ url="https://example.com/report/General.xlsx",
+ )
+
+ await capture._on_response(response)
+
+ assert list(tmp_path.iterdir()) == []
+
+ @pytest.mark.asyncio
+ async def test_saves_xhr_generic_binary_inline_no_filename_fallsback_to_url(self, tmp_path: Path) -> None:
+ """XHR with application/*, inline Content-Disposition (no filename), and URL fallback."""
+ page = _make_page()
+ capture = ScopedXhrDownloadCapture(page, tmp_path)
+ response = _make_response(
+ content_type="application/*",
+ content_disposition="inline",
+ content_length="99999999",
+ body=b"spreadsheet-data",
+ url="https://example.com/report/General.xlsx",
+ )
+
+ await capture._on_response(response)
+
+ saved = tmp_path / "General.xlsx"
+ assert saved.exists()
+ assert saved.stat().st_size > 0
+
+ @pytest.mark.asyncio
+ async def test_saves_xhr_generic_binary_attachment_no_filename_fallsback_to_url(self, tmp_path: Path) -> None:
+ """XHR with application/*, attachment header but no filename, and URL fallback."""
+ page = _make_page()
+ capture = ScopedXhrDownloadCapture(page, tmp_path)
+ response = _make_response(
+ content_type="application/*",
+ content_disposition="attachment",
+ content_length="99999999",
+ body=b"spreadsheet-data",
+ url="https://example.com/report/General.xlsx",
+ )
+
+ await capture._on_response(response)
+
+ saved = tmp_path / "General.xlsx"
+ assert saved.exists()
+ assert saved.stat().st_size > 0