Skip to content

Automate browser based workflows with AI

fix(SKY-11982): restore real filenames for downloaded files (upload regression) (#7156)

Aaron Pereze2edbd9aeae9
diff --git a/skyvern/forge/sdk/api/files.py b/skyvern/forge/sdk/api/files.py
index bdd143f14..a19d48840 100644
--- a/skyvern/forge/sdk/api/files.py
+++ b/skyvern/forge/sdk/api/files.py
@@ -44,7 +44,7 @@ def get_file_name_and_suffix_from_headers(headers: CIMultiDictProxy[str] | dict[
# retrieve the suffix from Content-Type
content_type = headers.get("Content-Type")
if content_type:
- if file_suffix := mimetypes.guess_extension(content_type):
+ if file_suffix := mimetypes.guess_extension(content_type.split(";")[0].strip()):
return file_stem, file_suffix
return file_stem, file_suffix or ""
@@ -66,7 +66,7 @@ def is_valid_mime_type(file_path: str) -> bool:
def _determine_download_filename(
filename: str | None,
- response_headers: dict,
+ response_headers: CIMultiDictProxy[str] | dict[str, str],
url: str,
) -> str:
"""Determine the filename for a downloaded file."""
@@ -226,6 +226,22 @@ async def download_file(
download_dir_path = Path(make_temp_directory(prefix="skyvern_downloads_"))
download_dir_resolved = download_dir_path.resolve()
+ # response.headers stays a CIMultiDictProxy: dict() would make header
+ # lookups case-sensitive and miss lowercase wire headers.
+ file_name = _determine_download_filename(filename, response.headers, url)
+ allowed_dir = os.path.realpath(download_dir_resolved)
+ resolved_final_path = os.path.realpath(os.path.join(allowed_dir, file_name))
+ # sanitize_filename strips separators but keeps dots, so a dots-only name can
+ # still resolve outside the download dir; require a direct child. Checked
+ # before streaming so a rejected name downloads zero bytes.
+ if (
+ resolved_final_path == allowed_dir
+ or not resolved_final_path.startswith(allowed_dir + os.sep)
+ or os.path.dirname(resolved_final_path) != allowed_dir
+ ):
+ raise ValueError(f"Unsafe filename derived from download: {file_name!r}")
+ final_path = Path(resolved_final_path)
+
temp_file = tempfile.NamedTemporaryFile(mode="wb", dir=download_dir_resolved, delete=False)
file_path = Path(temp_file.name).resolve()
if file_path != download_dir_resolved and not file_path.is_relative_to(download_dir_resolved):
@@ -233,17 +249,25 @@ async def download_file(
raise ValueError("Unsafe temporary file path created for download")
LOG.info("Downloading file to temporary path", file_path=str(file_path))
- with temp_file as f:
- # Write the content of the request into the file
- total_bytes_downloaded = 0
- async for chunk in response.content.iter_chunked(1024):
- f.write(chunk)
- total_bytes_downloaded += len(chunk)
- if max_size_mb and total_bytes_downloaded > max_size_mb * 1024 * 1024:
- raise DownloadFileMaxSizeExceeded(max_size_mb)
-
- LOG.info(f"File downloaded successfully to {file_path}")
- return str(file_path)
+ try:
+ with temp_file as f:
+ # Write the content of the request into the file
+ total_bytes_downloaded = 0
+ async for chunk in response.content.iter_chunked(1024):
+ f.write(chunk)
+ total_bytes_downloaded += len(chunk)
+ if max_size_mb and total_bytes_downloaded > max_size_mb * 1024 * 1024:
+ raise DownloadFileMaxSizeExceeded(max_size_mb)
+
+ file_path.replace(final_path)
+ except BaseException:
+ # An orphaned temp file in a run download dir would get synced to storage
+ # under the tmpXXXX name; drop it on any failure (incl. cancellation).
+ file_path.unlink(missing_ok=True)
+ raise
+
+ LOG.info(f"File downloaded successfully to {final_path}")
+ return str(final_path)
except aiohttp.ClientResponseError as e:
LOG.exception(f"Failed to download file, status code: {e.status}")
raise
diff --git a/tests/unit/test_files_resolve_local_or_download.py b/tests/unit/test_files_resolve_local_or_download.py
index dc7b1d1d5..d5535b360 100644
--- a/tests/unit/test_files_resolve_local_or_download.py
+++ b/tests/unit/test_files_resolve_local_or_download.py
@@ -1,15 +1,62 @@
from __future__ import annotations
+from collections.abc import AsyncIterator
from pathlib import Path
from unittest.mock import AsyncMock
import pytest
+from multidict import CIMultiDict, CIMultiDictProxy
from skyvern.config import settings
from skyvern.exceptions import DownloadFileMaxSizeExceeded
from skyvern.forge.sdk.api import files
+class _FakeDownloadResponse:
+ def __init__(self, data: bytes, headers: dict[str, str] | None = None, advertise_length: bool = True) -> None:
+ self._data = data
+ # aiohttp exposes headers as a case-insensitive CIMultiDictProxy; mirror that.
+ self.headers = CIMultiDictProxy(CIMultiDict(headers or {}))
+ self.content_length = len(data) if advertise_length else None
+ self.content = self
+ self.body_read = False
+
+ async def iter_chunked(self, chunk_size: int) -> AsyncIterator[bytes]:
+ self.body_read = True
+ yield self._data
+
+ async def __aenter__(self) -> _FakeDownloadResponse:
+ return self
+
+ async def __aexit__(self, *exc: object) -> None:
+ return None
+
+
+class _FakeDownloadSession:
+ def __init__(self, response: _FakeDownloadResponse) -> None:
+ self._response = response
+
+ def get(self, url: object, headers: dict[str, str] | None = None) -> _FakeDownloadResponse:
+ return self._response
+
+ async def __aenter__(self) -> _FakeDownloadSession:
+ return self
+
+ async def __aexit__(self, *exc: object) -> None:
+ return None
+
+
+def _patch_download_session(
+ monkeypatch: pytest.MonkeyPatch,
+ data: bytes,
+ headers: dict[str, str] | None = None,
+ advertise_length: bool = True,
+) -> _FakeDownloadResponse:
+ response = _FakeDownloadResponse(data, headers, advertise_length=advertise_length)
+ monkeypatch.setattr(files.aiohttp, "ClientSession", lambda **kwargs: _FakeDownloadSession(response))
+ return response
+
+
def _run_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, run_id: str, name: str = "data.txt") -> Path:
download_root = tmp_path / "downloads"
monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(download_root))
@@ -78,3 +125,71 @@ async def test_resolve_remote_url_downloads_file(monkeypatch: pytest.MonkeyPatch
max_size_mb=10,
organization_id="org-1",
)
+
+
+async def test_download_file_preserves_url_filename(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ _patch_download_session(monkeypatch, b"resume-bytes")
+
+ result = await files.download_file("https://example.com/files/Resume_Final.docx", output_dir=str(tmp_path))
+
+ assert Path(result).name == "Resume_Final.docx"
+ assert Path(result).parent == tmp_path.resolve()
+ assert Path(result).read_bytes() == b"resume-bytes"
+
+
+async def test_download_file_uses_content_disposition_filename(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ _patch_download_session(
+ monkeypatch, b"pdf-bytes", headers={"Content-Disposition": 'attachment; filename="candidate resume.pdf"'}
+ )
+
+ result = await files.download_file("https://example.com/f/abc123", output_dir=str(tmp_path))
+
+ assert Path(result).name == "candidate resume.pdf"
+ assert Path(result).read_bytes() == b"pdf-bytes"
+
+
+async def test_download_file_uses_lowercase_wire_headers(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ _patch_download_session(
+ monkeypatch, b"pdf-bytes", headers={"content-disposition": 'attachment; filename="report.pdf"'}
+ )
+
+ result = await files.download_file("https://example.com/f/abc123", output_dir=str(tmp_path))
+
+ assert Path(result).name == "report.pdf"
+
+
+async def test_download_file_derives_extension_from_content_type_with_params(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _patch_download_session(monkeypatch, b"pdf-bytes", headers={"Content-Type": "application/pdf; charset=utf-8"})
+
+ result = await files.download_file("https://example.com/f/abc123", output_dir=str(tmp_path))
+
+ assert Path(result).name == "abc123.pdf"
+
+
+async def test_download_file_rejects_path_escaping_filename(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ response = _patch_download_session(monkeypatch, b"x")
+
+ with pytest.raises(ValueError, match="Unsafe filename"):
+ await files.download_file("https://example.com/f/abc123", output_dir=str(tmp_path), filename="..")
+
+ assert list(tmp_path.iterdir()) == []
+ assert not response.body_read
+
+
+async def test_download_file_cleans_up_temp_file_when_max_size_exceeded_mid_stream(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _patch_download_session(monkeypatch, b"x" * (1024 * 1024 + 1), advertise_length=False)
+
+ with pytest.raises(DownloadFileMaxSizeExceeded):
+ await files.download_file("https://example.com/files/big.bin", output_dir=str(tmp_path), max_size_mb=1)
+
+ assert list(tmp_path.iterdir()) == []