Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Add ZIP support to FileParserBlock [SKY-11711] (#7049)
Suchintanb8e789d665cd
diff --git a/skyvern-frontend/src/routes/workflows/editor/helpContent.ts b/skyvern-frontend/src/routes/workflows/editor/helpContent.tsindex 3e4450c71..532b3c3fe 100644--- a/skyvern-frontend/src/routes/workflows/editor/helpContent.ts+++ b/skyvern-frontend/src/routes/workflows/editor/helpContent.ts@@ -167,7 +167,7 @@ export const helpTooltips = {fileUrl:"Since we're in beta this section isn't fully customizable yet, contact us if you'd like to integrate it into your workflow.",fileType:- "The format of the file to parse. Auto-detected from the URL extension when possible.",+ "The format of the file to parse. Auto-detected from the URL extension when possible. ZIP archives are unzipped: without a data schema the block outputs the list of extracted files; with a schema it parses the files inside and extracts against the schema.",},wait: {...baseHelpTooltipContent,diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/FileParserNode/FileParserEditor.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/FileParserNode/FileParserEditor.tsxindex 8037f452f..4ddfaae61 100644--- a/skyvern-frontend/src/routes/workflows/editor/nodes/FileParserNode/FileParserEditor.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/FileParserNode/FileParserEditor.tsx@@ -37,6 +37,7 @@ const FILE_TYPE_OPTIONS: Array<{ value: FileParserFileType; label: string }> = [{ value: "pdf", label: "PDF" },{ value: "image", label: "Image" },{ value: "docx", label: "DOCX" },+ { value: "zip", label: "ZIP" },];const FILE_EXTENSION_TO_TYPE: Record<string, FileParserFileType> = {@@ -50,6 +51,7 @@ const FILE_EXTENSION_TO_TYPE: Record<string, FileParserFileType> = {gif: "image",webp: "image",docx: "docx",+ zip: "zip",};function detectFileTypeFromUrl(url: string): FileParserFileType | null {diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/FileParserNode/types.ts b/skyvern-frontend/src/routes/workflows/editor/nodes/FileParserNode/types.tsindex ecbfeddd0..95c08b7ab 100644--- a/skyvern-frontend/src/routes/workflows/editor/nodes/FileParserNode/types.ts+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/FileParserNode/types.ts@@ -12,7 +12,8 @@ export type FileParserFileType =| "excel"| "pdf"| "image"- | "docx";+ | "docx"+ | "zip";export type FileParserNodeData = NodeBaseData & {fileUrl: string;diff --git a/skyvern-frontend/src/routes/workflows/types/workflowTypes.ts b/skyvern-frontend/src/routes/workflows/types/workflowTypes.tsindex d0a5f719b..bb9ce1c7f 100644--- a/skyvern-frontend/src/routes/workflows/types/workflowTypes.ts+++ b/skyvern-frontend/src/routes/workflows/types/workflowTypes.ts@@ -444,7 +444,7 @@ export type SendEmailBlock = WorkflowBlockBase & {export type FileURLParserBlock = WorkflowBlockBase & {block_type: "file_url_parser";file_url: string;- file_type: "auto_detect" | "csv" | "excel" | "pdf" | "image" | "docx";+ file_type: "auto_detect" | "csv" | "excel" | "pdf" | "image" | "docx" | "zip";json_schema: Record<string, unknown> | null;};diff --git a/skyvern-frontend/src/routes/workflows/types/workflowYamlTypes.ts b/skyvern-frontend/src/routes/workflows/types/workflowYamlTypes.tsindex fd36578e5..68b1ac2aa 100644--- a/skyvern-frontend/src/routes/workflows/types/workflowYamlTypes.ts+++ b/skyvern-frontend/src/routes/workflows/types/workflowYamlTypes.ts@@ -372,7 +372,7 @@ export type SendEmailBlockYAML = BlockYAMLBase & {export type FileUrlParserBlockYAML = BlockYAMLBase & {block_type: "file_url_parser";file_url: string;- file_type: "auto_detect" | "csv" | "excel" | "pdf" | "image" | "docx";+ file_type: "auto_detect" | "csv" | "excel" | "pdf" | "image" | "docx" | "zip";json_schema?: Record<string, unknown> | null;};diff --git a/skyvern/client/types/file_type.py b/skyvern/client/types/file_type.pyindex 619ce5455..6575b7e3a 100644--- a/skyvern/client/types/file_type.py+++ b/skyvern/client/types/file_type.py@@ -2,4 +2,4 @@import typing-FileType = typing.Union[typing.Literal["auto_detect", "csv", "excel", "pdf", "image", "docx"], typing.Any]+FileType = typing.Union[typing.Literal["auto_detect", "csv", "excel", "pdf", "image", "docx", "zip"], typing.Any]diff --git a/skyvern/forge/prompts/skyvern/workflow_knowledge_base.txt b/skyvern/forge/prompts/skyvern/workflow_knowledge_base.txtindex bebc2d3d4..3b2ce32ef 100644--- a/skyvern/forge/prompts/skyvern/workflow_knowledge_base.txt+++ b/skyvern/forge/prompts/skyvern/workflow_knowledge_base.txt@@ -649,19 +649,24 @@ blocks:** FILE PARSER BLOCK (file_url_parser) **-Purpose: Parse PDFs, CSVs, Excel files, images, and DOCX files. This is the "File Parser Block" in the UI.+Purpose: Parse PDFs, CSVs, Excel files, images, DOCX files, and ZIP archives. This is the "File Parser Block" in the UI.Structure:block_type: file_url_parserlabel: <unique_label>file_url: <https_url_or_file_url> # Required: URL to the file-file_type: <auto_detect|csv|excel|pdf|image|docx> # Optional: defaults to auto_detect (infer from URL/content)+file_type: <auto_detect|csv|excel|pdf|image|docx|zip> # Optional: defaults to auto_detect (infer from URL/content)json_schema: <json_schema> # Optional: Structure of parsed outputUse Cases:- Parse a PDF invoice into structured fields- Extract rows from a CSV file- Read Excel sheets for downstream processing+- Unzip a ZIP archive of PDFs and extract structured data across all of them++ZIP behavior:+- Without json_schema: outputs the list of extracted files as [{file_name, file_path, file_size}]. Loop over the output and pass each file_path as the file_url of another file_url_parser block to parse files individually.+- With json_schema: parses every supported file inside the archive (CSV/Excel/PDF/image/DOCX) and extracts against the schema from the combined content.Example:blocks:diff --git a/skyvern/forge/sdk/api/files.py b/skyvern/forge/sdk/api/files.pyindex ffb7f6ac0..38f237e6c 100644--- a/skyvern/forge/sdk/api/files.py+++ b/skyvern/forge/sdk/api/files.py@@ -220,19 +220,20 @@ async def download_file(# Get the file nameif output_dir:- os.makedirs(output_dir, exist_ok=True)- download_dir = output_dir+ download_dir_path = Path(output_dir)+ download_dir_path.mkdir(parents=True, exist_ok=True)else:- download_dir = make_temp_directory(prefix="skyvern_downloads_")+ download_dir_path = Path(make_temp_directory(prefix="skyvern_downloads_"))- # Determine filename - use provided filename or derive from response/URL- file_name = _determine_download_filename(filename, dict(response.headers), url)- file_path = os.path.join(download_dir, file_name)- if not os.path.abspath(file_path).startswith(os.path.abspath(download_dir) + os.sep):- raise ValueError(f"Unsafe filename derived from download: {file_name!r}")+ download_dir_resolved = download_dir_path.resolve()+ 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):+ temp_file.close()+ raise ValueError("Unsafe temporary file path created for download")- LOG.info(f"Downloading file to {file_path}")- with open(file_path, "wb") as f:+ 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 filetotal_bytes_downloaded = 0async for chunk in response.content.iter_chunked(1024):@@ -242,7 +243,7 @@ async def download_file(raise DownloadFileMaxSizeExceeded(max_size_mb)LOG.info(f"File downloaded successfully to {file_path}")- return file_path+ return str(file_path)except aiohttp.ClientResponseError as e:LOG.exception(f"Failed to download file, status code: {e.status}")raise@@ -262,6 +263,27 @@ async def download_file(raise+async def resolve_local_or_download_file(+ file_url: str,+ run_id: str | None,+ organization_id: str | None = None,+ max_size_mb: int | None = None,+) -> str:+ """Resolve a file input to a local path.++ Absolute paths are validated against the run's download directory; anything else is downloaded.+ """+ # Absolute paths are the run-local convention; treating all non-remote strings as paths would misroute bad URLs.+ if file_url.startswith("/"):+ resolved = validate_local_file_path(file_url, run_id)+ if not os.path.isfile(resolved):+ raise FileNotFoundError(f"Local file not found: {file_url}")+ if max_size_mb is not None and os.path.getsize(resolved) > max_size_mb * 1024 * 1024:+ raise DownloadFileMaxSizeExceeded(max_size_mb)+ return resolved+ return await download_file(file_url, max_size_mb=max_size_mb, organization_id=organization_id)++def zip_files(files_path: str, zip_file_path: str) -> str:with zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED) as zipf:for root, dirs, files in os.walk(files_path):diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 40265cb81..275498d4d 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -15,10 +15,11 @@ import shutilimport smtplibimport textwrapimport uuid+import zipfilefrom collections import defaultdict, dequefrom datetime import date, datetime, time, timezonefrom email.message import EmailMessage-from pathlib import Path+from pathlib import Path, PurePosixPathfrom types import SimpleNamespacefrom typing import Annotated, Any, Awaitable, Callable, ClassVar, Literal, Union, castfrom urllib.parse import quote, urlparse@@ -80,6 +81,7 @@ from skyvern.forge.sdk.api.files import (get_path_for_workflow_download_directory,is_remote_url,parse_uri_to_path,+ resolve_local_or_download_file,resolve_run_download_id,validate_local_file_path,)@@ -3968,6 +3970,20 @@ async def wrapper({default_args}):uri = value.get("s3uri")if not uri or not str(uri).strip():return value+ if str(uri).startswith("/"):+ try:+ local_path = validate_local_file_path(str(uri), workflow_run_id)+ if not os.path.isfile(local_path):+ raise FileNotFoundError(f"Local file not found: {uri}")+ return local_path+ except FileNotFoundError:+ pass+ except PermissionError:+ LOG.warning(+ "CodeBlock file parameter path is outside the run's download directory; leaving the original value",+ workflow_run_id=workflow_run_id,+ )+ return valuetry:output_dir = get_download_dir(workflow_run_id)local_path = await download_file(str(uri), output_dir=output_dir, organization_id=organization_id)@@ -5099,13 +5115,14 @@ class DownloadToS3Block(Block):def format_potential_template_parameters(self, workflow_run_context: WorkflowRunContext) -> None:self.url = self.format_block_parameter_template_from_workflow_run_context(self.url, workflow_run_context)- async def _upload_file_to_s3(self, uri: str, file_path: str) -> None:+ async def _upload_file_to_s3(self, uri: str, file_path: str, cleanup_file: bool = True) -> None:try:client = self.get_async_aws_client()await client.upload_file_from_path(uri=uri, file_path=file_path)finally:# Clean up the temporary file since it's created with delete=False- os.unlink(file_path)+ if cleanup_file:+ os.unlink(file_path)async def execute(self,@@ -5141,7 +5158,11 @@ class DownloadToS3Block(Block):)try:- file_path = await download_file(self.url, max_size_mb=10, organization_id=organization_id)+ context = skyvern_context.current()+ run_id = context.run_id if context and context.run_id else workflow_run_id+ file_path = await resolve_local_or_download_file(+ self.url, run_id, organization_id=organization_id, max_size_mb=10+ )except Exception as e:LOG.error("DownloadToS3Block Failed to download file", url=self.url, error=str(e))raise e@@ -5149,7 +5170,7 @@ class DownloadToS3Block(Block):uri = Nonetry:uri = f"s3://{settings.AWS_S3_BUCKET_UPLOADS}/{settings.ENV}/{workflow_run_id}/{uuid.uuid4()}"- await self._upload_file_to_s3(uri, file_path)+ await self._upload_file_to_s3(uri, file_path, cleanup_file=not self.url.startswith("/"))except Exception as e:LOG.error("DownloadToS3Block Failed to upload file to S3", uri=uri, error=str(e))raise e@@ -6255,6 +6276,16 @@ class FileParserBlock(Block):_CSV_UTF_BOMS = (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE, codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE)# Bounded cap for legitimate wide cells (JSON blobs, long descriptions); applied only while parsing._MAX_CSV_FIELD_SIZE_BYTES = 10 * 1024 * 1024+ # ZIP extraction guards (zip-bomb protection; sizes from central-directory metadata).+ # ClassVar keeps these plain class attributes — without it pydantic wraps underscore+ # names in ModelPrivateAttr and class-level access breaks.+ _MAX_ZIP_ARCHIVE_BYTES: ClassVar[int] = 512 * 1024 * 1024+ _MAX_ZIP_ENTRIES: ClassVar[int] = 1000+ _MAX_ZIP_UNCOMPRESSED_BYTES: ClassVar[int] = 1024**3+ _ZIP_JUNK_DIRS: ClassVar[tuple[str, ...]] = ("__MACOSX",)+ _ZIP_JUNK_FILES: ClassVar[tuple[str, ...]] = (".DS_Store", "Thumbs.db")+ # Classic EOCD + max comment + ZIP64 locator + ZIP64 EOCD fixed part.+ _ZIP_EOCD_TAIL_BYTES: ClassVar[int] = 65_557 + 20 + 56file_url: strfile_type: FileType = FileType.AUTO_DETECT@@ -6333,6 +6364,8 @@ class FileParserBlock(Block):file_type=FileType.DOCX,error="Legacy .doc format (Word 97-2003) is not supported. Please convert the file to .docx format.",)+ elif suffix == ".zip":+ return FileType.ZIPelif suffix == ".csv":return FileType.CSV@@ -6365,6 +6398,9 @@ class FileParserBlock(Block):return FileType.EXCELelif mime == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":return FileType.DOCX+ elif mime == "application/zip":+ # OOXML files are ZIP containers matched before this generic branch, so only plain archives reach it.+ return FileType.ZIPelif mime.startswith("image/"):return FileType.IMAGEreturn None@@ -6463,6 +6499,11 @@ class FileParserBlock(Block):raise InvalidFileType(file_url=file_url_used, file_type=self.file_type, error=f"Invalid DOCX file format: {str(e)}")+ elif self.file_type == FileType.ZIP:+ if not zipfile.is_zipfile(file_path):+ raise InvalidFileType(+ file_url=file_url_used, file_type=self.file_type, error="File is not a valid ZIP archive"+ )async def _parse_csv_file(self, file_path: str) -> list[dict[str, Any]]:"""Parse CSV/TSV file and return list of dictionaries."""@@ -6839,6 +6880,242 @@ class FileParserBlock(Block):file_url=self.file_url, file_type=self.file_type, error=f"Failed to parse DOCX file: {str(e)}")+ @classmethod+ def _is_zip_junk_member(cls, member_name: str) -> bool:+ parts = PurePosixPath(member_name).parts+ if not parts:+ return True+ if any(part in cls._ZIP_JUNK_DIRS for part in parts):+ return True+ return parts[-1] in cls._ZIP_JUNK_FILES or parts[-1].startswith("._")++ @classmethod+ def _read_zip_total_entry_count(cls, file_path: str) -> int | None:+ try:+ file_size = os.path.getsize(file_path)+ tail_size = min(file_size, cls._ZIP_EOCD_TAIL_BYTES)+ with open(file_path, "rb") as file:+ file.seek(file_size - tail_size)+ tail = file.read(tail_size)++ eocd_index = tail.rfind(b"PK\x05\x06")+ if eocd_index < 0 or eocd_index + 22 > len(tail):+ return None++ count = int.from_bytes(tail[eocd_index + 10 : eocd_index + 12], "little")+ if count != 0xFFFF:+ return count++ zip64_eocd_index = tail.rfind(b"PK\x06\x06")+ if zip64_eocd_index < 0 or zip64_eocd_index + 40 > len(tail):+ return None+ return int.from_bytes(tail[zip64_eocd_index + 32 : zip64_eocd_index + 40], "little")+ except Exception:+ return None++ def _check_extracted_size_within_limit(self, total_bytes: int) -> None:+ if total_bytes > self._MAX_ZIP_UNCOMPRESSED_BYTES:+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error=f"ZIP archive uncompressed content exceeds the limit of {self._MAX_ZIP_UNCOMPRESSED_BYTES} bytes",+ )++ def _extract_zip_file(+ self, file_path: str, workflow_run_id: str, workflow_run_block_id: str+ ) -> list[dict[str, Any]]:+ """Extract a ZIP archive into the run's download directory.++ Returns the extracted files as {"file_name", "file_path", "file_size"} dicts sorted by+ file_name, so downstream blocks can consume the files from the local filesystem.+ """+ context = skyvern_context.current()+ run_id = context.run_id if context and context.run_id else workflow_run_id+ zip_stem = sanitize_filename(Path(file_path).stem, default="archive")+ extract_dir = (+ get_path_for_workflow_download_directory(run_id) / "unzipped" / f"{zip_stem}_{workflow_run_block_id}"+ )++ archive_size = os.path.getsize(file_path)+ if archive_size > self._MAX_ZIP_ARCHIVE_BYTES:+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error=f"ZIP archive size ({archive_size} bytes) exceeds the limit of {self._MAX_ZIP_ARCHIVE_BYTES} bytes",+ )++ declared_entry_count = self._read_zip_total_entry_count(file_path)+ if declared_entry_count is not None and declared_entry_count > self._MAX_ZIP_ENTRIES:+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error=f"ZIP archive declares {declared_entry_count} entries, exceeding the limit of {self._MAX_ZIP_ENTRIES}",+ )++ with zipfile.ZipFile(file_path) as zip_file:+ members = [+ member+ for member in zip_file.infolist()+ if not member.is_dir() and not self._is_zip_junk_member(member.filename)+ ]+ if len(members) > self._MAX_ZIP_ENTRIES:+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error=f"ZIP archive contains {len(members)} files, exceeding the limit of {self._MAX_ZIP_ENTRIES}",+ )+ total_uncompressed_bytes = sum(member.file_size for member in members)+ # The declared-size check is advisory; measured bytes after extraction are authoritative.+ if total_uncompressed_bytes > self._MAX_ZIP_UNCOMPRESSED_BYTES:+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error=f"ZIP archive uncompressed size ({total_uncompressed_bytes} bytes) exceeds the limit of {self._MAX_ZIP_UNCOMPRESSED_BYTES} bytes",+ )+ if any(member.flag_bits & 0x1 for member in members):+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error="Password-protected ZIP archives are not supported",+ )++ extract_dir.mkdir(parents=True, exist_ok=True)+ # Keyed by destination path: member names that sanitize to the same destination+ # (e.g. "a.csv", "/a.csv", "../a.csv") overwrite on disk, and per ZIP semantics the+ # last entry wins — keep one list entry per file instead of duplicates.+ # measured_total_bytes intentionally counts every member's written bytes (including+ # overwritten collisions) because it guards total write I/O, not final disk usage.+ files_by_path: dict[str, dict[str, Any]] = {}+ measured_total_bytes = 0+ for member in members:+ if member.file_size > self._MAX_ZIP_UNCOMPRESSED_BYTES - measured_total_bytes:+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error=f"ZIP archive uncompressed content exceeds the limit of {self._MAX_ZIP_UNCOMPRESSED_BYTES} bytes",+ )+ # ZipFile.extract sanitizes absolute paths and ".." components, so members cannot+ # escape extract_dir.+ extracted_path = zip_file.extract(member, path=extract_dir)+ extracted_size = Path(extracted_path).stat().st_size+ measured_total_bytes += extracted_size+ self._check_extracted_size_within_limit(measured_total_bytes)+ if extracted_path in files_by_path:+ LOG.warning(+ "FileParserBlock ZIP members collide after path sanitization, keeping the last one",+ file_url=self.file_url,+ member_name=member.filename,+ )+ files_by_path[extracted_path] = {+ "file_name": str(Path(extracted_path).relative_to(extract_dir)),+ "file_path": extracted_path,+ "file_size": extracted_size,+ }++ extracted_files = sorted(files_by_path.values(), key=lambda file_info: file_info["file_name"])+ LOG.info(+ "FileParserBlock Extracted ZIP archive",+ file_url=self.file_url,+ extract_dir=str(extract_dir),+ file_count=len(extracted_files),+ )+ return extracted_files++ async def _parse_file_of_type(+ self,+ file_type: FileType,+ file_path: str,+ workflow_run_block_id: str | None = None,+ organization_id: str | None = None,+ ) -> str | list[dict[str, Any]] | None:+ """Parse a file with the parser for its type; returns None for unsupported types."""+ if file_type == FileType.CSV:+ return await self._parse_csv_file(file_path)+ if file_type == FileType.EXCEL:+ return await self._parse_excel_file(file_path)+ if file_type == FileType.PDF:+ return await self._parse_pdf_file(+ file_path, workflow_run_block_id=workflow_run_block_id, organization_id=organization_id+ )+ if file_type == FileType.IMAGE:+ return await self._parse_image_file(+ file_path, workflow_run_block_id=workflow_run_block_id, organization_id=organization_id+ )+ if file_type == FileType.DOCX:+ return await self._parse_docx_file(file_path)+ return None++ async def _parse_zip_contents(+ self,+ extracted_files: list[dict[str, Any]],+ workflow_run_block_id: str | None = None,+ organization_id: str | None = None,+ ) -> list[dict[str, Any]]:+ """Parse every supported file extracted from a ZIP for combined AI extraction.++ Returns [{"file_name", "content"}, ...]. Unsupported or unparseable files are skipped;+ total content is truncated at a file boundary once MAX_FILE_PARSE_INPUT_TOKENS is reached.+ """+ content_entries: list[dict[str, Any]] = []+ current_tokens = 0+ for file_info in extracted_files:+ inner_path: str = file_info["file_path"]+ file_name: str = file_info["file_name"]+ try:+ inner_file_type = self._detect_file_type_from_url(inner_path, file_path=inner_path)+ except InvalidFileType as e:+ LOG.warning("FileParserBlock Skipping unsupported file in ZIP", file_name=file_name, error=str(e))+ continue+ if inner_file_type == FileType.ZIP:+ LOG.warning("FileParserBlock Skipping nested ZIP archive", file_name=file_name)+ continue+ try:+ content = await self._parse_file_of_type(+ inner_file_type,+ inner_path,+ workflow_run_block_id=workflow_run_block_id,+ organization_id=organization_id,+ )+ except Exception as e:+ LOG.warning(+ "FileParserBlock Failed to parse file in ZIP, skipping it", file_name=file_name, error=str(e)+ )+ continue+ if content is None:+ LOG.warning(+ "FileParserBlock Skipping file with unsupported type in ZIP",+ file_name=file_name,+ detected_file_type=inner_file_type,+ )+ continue+ entry = {"file_name": file_name, "content": content}+ entry_tokens = count_tokens(json.dumps(entry, separators=(",", ":"), default=str))+ if current_tokens + entry_tokens > MAX_FILE_PARSE_INPUT_TOKENS:+ if not content_entries:+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error=f"File '{file_name}' in the ZIP archive alone exceeds the maximum extraction input size",+ )+ LOG.warning(+ "FileParserBlock ZIP content exceeds token limit, truncating at file boundary",+ file_url=self.file_url,+ files_included=len(content_entries),+ total_files=len(extracted_files),+ max_tokens=MAX_FILE_PARSE_INPUT_TOKENS,+ )+ break+ current_tokens += entry_tokens+ content_entries.append(entry)++ if not content_entries:+ raise InvalidFileType(+ file_url=self.file_url,+ file_type=self.file_type,+ error="ZIP archive contains no parseable files (supported: CSV, Excel, PDF, image, DOCX)",+ )+ return content_entries+async def _extract_with_ai(self,content: str | list[dict[str, Any]],@@ -7023,12 +7300,13 @@ class FileParserBlock(Block):self.file_url = extracted_urltry:- # Download the file.- file_path = await download_file(self.file_url, organization_id=organization_id)+ context = skyvern_context.current()+ run_id = context.run_id if context and context.run_id else workflow_run_id+ file_path = await resolve_local_or_download_file(self.file_url, run_id, organization_id=organization_id)# Resolve AUTO_DETECT (and legacy CSV-as-default) via URL/magic-byte detection;- # IMAGE/EXCEL/PDF/DOCX are honored as user overrides.- if self.file_type not in (FileType.IMAGE, FileType.EXCEL, FileType.PDF, FileType.DOCX):+ # IMAGE/EXCEL/PDF/DOCX/ZIP are honored as user overrides.+ if self.file_type not in (FileType.IMAGE, FileType.EXCEL, FileType.PDF, FileType.DOCX, FileType.ZIP):self.file_type = self._detect_file_type_from_url(self.file_url, file_path=file_path)# Validate the file type@@ -7052,32 +7330,34 @@ class FileParserBlock(Block):# Parse the file based on typeparsed_data: str | list[dict[str, Any]]try:- if self.file_type == FileType.CSV:- parsed_data = await self._parse_csv_file(file_path)- elif self.file_type == FileType.EXCEL:- parsed_data = await self._parse_excel_file(file_path)- elif self.file_type == FileType.PDF:- parsed_data = await self._parse_pdf_file(- file_path,- workflow_run_block_id=workflow_run_block_id,- organization_id=organization_id,+ if self.file_type == FileType.ZIP:+ extracted_zip_files = await asyncio.to_thread(+ self._extract_zip_file, file_path, workflow_run_id, workflow_run_block_id)- elif self.file_type == FileType.IMAGE:- parsed_data = await self._parse_image_file(+ if self.json_schema:+ parsed_data = await self._parse_zip_contents(+ extracted_zip_files,+ workflow_run_block_id=workflow_run_block_id,+ organization_id=organization_id,+ )+ else:+ parsed_data = extracted_zip_files+ else:+ maybe_parsed = await self._parse_file_of_type(+ self.file_type,file_path,workflow_run_block_id=workflow_run_block_id,organization_id=organization_id,)- elif self.file_type == FileType.DOCX:- parsed_data = await self._parse_docx_file(file_path)- else:- return await self._record_failure(- workflow_run_context,- workflow_run_id,- workflow_run_block_id,- organization_id,- f"Unsupported file type: {self.file_type}",- )+ if maybe_parsed is None:+ return await self._record_failure(+ workflow_run_context,+ workflow_run_id,+ workflow_run_block_id,+ organization_id,+ f"Unsupported file type: {self.file_type}",+ )+ parsed_data = maybe_parsedexcept Exception as e:return await self._record_failure(workflow_run_context,@@ -7194,8 +7474,19 @@ class PDFParserBlock(Block):organization_id=organization_id,)- # Download the file.- file_path = await download_file(self.file_url, organization_id=organization_id)+ try:+ context = skyvern_context.current()+ run_id = context.run_id if context and context.run_id else workflow_run_id+ file_path = await resolve_local_or_download_file(self.file_url, run_id, organization_id=organization_id)+ except Exception as e:+ return await self.build_block_result(+ success=False,+ failure_reason=f"Failed to download or validate file: {str(e)}",+ output_parameter_value=None,+ status=BlockStatus.failed,+ workflow_run_block_id=workflow_run_block_id,+ organization_id=organization_id,+ )try:extracted_text = extract_pdf_file(file_path, file_identifier=self.file_url)diff --git a/skyvern/forge/sdk/workflow/models/pdf_fill_block.py b/skyvern/forge/sdk/workflow/models/pdf_fill_block.pyindex 7b59894bc..f8280ec98 100644--- a/skyvern/forge/sdk/workflow/models/pdf_fill_block.py+++ b/skyvern/forge/sdk/workflow/models/pdf_fill_block.py@@ -23,7 +23,11 @@ from skyvern.constants import GET_DOWNLOADED_FILES_TIMEOUT, SAVE_DOWNLOADED_FILEfrom skyvern.exceptions import PDFParsingErrorfrom skyvern.forge import appfrom skyvern.forge.prompts import prompt_engine-from skyvern.forge.sdk.api.files import download_file, get_path_for_workflow_download_directory+from skyvern.forge.sdk.api.files import (+ download_file,+ get_path_for_workflow_download_directory,+ validate_local_file_path,+)from skyvern.forge.sdk.api.llm.api_handler import LLMAPIHandlerfrom skyvern.forge.sdk.api.llm.api_handler_factory import LLMAPIHandlerFactoryfrom skyvern.forge.sdk.artifact.models import ArtifactType@@ -179,13 +183,18 @@ class PdfFillBlock(Block):organization_id=organization_id,)- async def _resolve_source_pdf(self, organization_id: str | None) -> tuple[str, bool]:- # Bare local paths bypass download_file's gating, so only honor them in local dev;- # in deployed environments they could read other runs' files off a shared worker.+ async def _resolve_source_pdf(self, workflow_run_id: str, organization_id: str | None) -> tuple[str, bool]:# Returns (path, is_temp): is_temp marks a download_file temp the caller must clean up;# a user-supplied local path is never deleted.if settings.ENV == "local" and os.path.exists(self.file_url):return self.file_url, False+ if self.file_url.startswith("/"):+ context = skyvern_context.current()+ run_id = context.run_id if context and context.run_id else workflow_run_id+ resolved = validate_local_file_path(self.file_url, run_id)+ if not os.path.isfile(resolved):+ raise FileNotFoundError(f"Local file not found: {self.file_url}")+ return resolved, Falsereturn await download_file(self.file_url, organization_id=organization_id), True@staticmethod@@ -854,7 +863,7 @@ class PdfFillBlock(Block):)try:- source_pdf_path, source_is_temp = await self._resolve_source_pdf(organization_id)+ source_pdf_path, source_is_temp = await self._resolve_source_pdf(workflow_run_id, organization_id)except Exception as e:return await self._record_failure(workflow_run_context,diff --git a/skyvern/schemas/workflows.py b/skyvern/schemas/workflows.pyindex 4b98e6f88..f16f4fef2 100644--- a/skyvern/schemas/workflows.py+++ b/skyvern/schemas/workflows.py@@ -491,6 +491,7 @@ class FileType(StrEnum):PDF = "pdf"IMAGE = "image"DOCX = "docx"+ ZIP = "zip"class PDFFormat(StrEnum):diff --git a/skyvern/webeye/actions/handler_utils.py b/skyvern/webeye/actions/handler_utils.pyindex d2c8d6a08..96ed79194 100644--- a/skyvern/webeye/actions/handler_utils.py+++ b/skyvern/webeye/actions/handler_utils.py@@ -7,6 +7,8 @@ from playwright.async_api import Locator, Pagefrom skyvern.config import settingsfrom skyvern.constants import TEXT_PRESS_MAX_LENGTHfrom skyvern.forge.sdk.api.files import download_file as download_file_api+from skyvern.forge.sdk.api.files import resolve_run_download_id, validate_local_file_path+from skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.event.factory import EventStrategyFactoryfrom skyvern.webeye.actions.actions import Action, KeypressAction@@ -18,6 +20,10 @@ async def download_file(action: dict[str, Any] | None = None,organization_id: str | None = None,) -> str | list[str]:+ if file_url.startswith("/"):+ run_id = resolve_run_download_id(skyvern_context.current())+ return validate_local_file_path(file_url, run_id)+try:return await download_file_api(file_url, organization_id=organization_id)except Exception:diff --git a/tests/unit/test_files_resolve_local_or_download.py b/tests/unit/test_files_resolve_local_or_download.pynew file mode 100644index 000000000..dc7b1d1d5--- /dev/null+++ b/tests/unit/test_files_resolve_local_or_download.py@@ -0,0 +1,80 @@+from __future__ import annotations++from pathlib import Path+from unittest.mock import AsyncMock++import pytest++from skyvern.config import settings+from skyvern.exceptions import DownloadFileMaxSizeExceeded+from skyvern.forge.sdk.api import files+++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))+ run_dir = download_root / run_id+ run_dir.mkdir(parents=True)+ path = run_dir / name+ path.write_text("hello")+ return path+++async def test_resolve_local_file_inside_run_download_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ run_id = "wr_local"+ path = _run_file(tmp_path, monkeypatch, run_id)++ assert await files.resolve_local_or_download_file(str(path), run_id) == str(path.resolve())+++async def test_resolve_local_file_rejects_outside_run_download_dir(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))+ outside = tmp_path / "outside.txt"+ outside.write_text("nope")++ with pytest.raises(PermissionError):+ await files.resolve_local_or_download_file(str(outside), "wr_local")+++async def test_resolve_local_file_raises_for_missing_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ run_id = "wr_missing"+ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))+ missing = tmp_path / "downloads" / run_id / "missing.txt"++ with pytest.raises(FileNotFoundError, match="Local file not found"):+ await files.resolve_local_or_download_file(str(missing), run_id)+++async def test_resolve_local_file_enforces_max_size(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ run_id = "wr_size"+ path = _run_file(tmp_path, monkeypatch, run_id)+ path.write_bytes(b"x" * 2)++ with pytest.raises(DownloadFileMaxSizeExceeded):+ await files.resolve_local_or_download_file(str(path), run_id, max_size_mb=0)+++async def test_resolve_remote_url_downloads_file(monkeypatch: pytest.MonkeyPatch) -> None:+ download_mock = AsyncMock(return_value="/tmp/downloaded.pdf")+ monkeypatch.setattr(files, "download_file", download_mock)++ result = await files.resolve_local_or_download_file(+ "https://example.com/file.pdf",+ "wr_remote",+ organization_id="org-1",+ max_size_mb=10,+ )++ assert result == "/tmp/downloaded.pdf"+ download_mock.assert_awaited_once_with(+ "https://example.com/file.pdf",+ max_size_mb=10,+ organization_id="org-1",+ )diff --git a/tests/unit/test_handler_utils_download_file.py b/tests/unit/test_handler_utils_download_file.pynew file mode 100644index 000000000..facd743e5--- /dev/null+++ b/tests/unit/test_handler_utils_download_file.py@@ -0,0 +1,34 @@+from __future__ import annotations++from pathlib import Path+from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest++from skyvern.config import settings+from skyvern.webeye.actions import handler_utils+++async def test_download_file_accepts_run_local_absolute_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ run_id = "wr_upload"+ download_root = tmp_path / "downloads"+ run_dir = download_root / run_id+ run_dir.mkdir(parents=True)+ local_file = run_dir / "upload.csv"+ local_file.write_text("name\nAlice")++ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(download_root))+ monkeypatch.setattr(+ handler_utils.skyvern_context,+ "current",+ lambda: SimpleNamespace(run_id=run_id, workflow_run_id=None, task_id=None),+ )+ download_mock = AsyncMock(side_effect=AssertionError("remote download must not be called"))+ monkeypatch.setattr(handler_utils, "download_file_api", download_mock)++ result = await handler_utils.download_file(str(local_file), action={"action_type": "upload_file"})++ assert result == str(local_file.resolve())+ download_mock.assert_not_awaited()diff --git a/tests/unit/test_pdf_fill_block.py b/tests/unit/test_pdf_fill_block.pyindex dde079ded..a638e067e 100644--- a/tests/unit/test_pdf_fill_block.py+++ b/tests/unit/test_pdf_fill_block.py@@ -69,6 +69,14 @@ def _install_context(monkeypatch: pytest.MonkeyPatch, run_id: str = "run_pdf_filreturn context+def _run_local_pdf_path(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, run_id: str, name: str) -> Path:+ download_root = tmp_path / "downloads"+ monkeypatch.setattr(pdf_fill_block.settings, "DOWNLOAD_PATH", str(download_root))+ run_dir = download_root / run_id+ run_dir.mkdir(parents=True, exist_ok=True)+ return run_dir / name++def _add_empty_appearance(writer: PdfWriter, states: list[str]) -> DictionaryObject:normal_appearances = DictionaryObject()for state in states:@@ -159,7 +167,7 @@ async def _fake_llm_response(**_: Any) -> dict[str, Any]:async def test_skyvern_engine_fills_pdf_with_llm_mapping(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:run_id = "run_skyvern_pdf_fill"_install_context(monkeypatch, run_id)- source_pdf = tmp_path / "source.pdf"+ source_pdf = _run_local_pdf_path(monkeypatch, tmp_path, run_id, "source.pdf")_write_fillable_pdf(source_pdf)assert PdfReader(str(source_pdf)).get_fields()@@ -312,7 +320,7 @@ def test_checkbox_without_known_state_is_skipped_not_silently_unchecked() -> Nonasync def test_empty_field_mapping_fails(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:run_id = "run_empty_mapping"_install_context(monkeypatch, run_id)- source_pdf = tmp_path / "source.pdf"+ source_pdf = _run_local_pdf_path(monkeypatch, tmp_path, run_id, "source.pdf")_write_fillable_pdf(source_pdf)async def _unmappable_llm_response(**_: Any) -> dict[str, Any]:@@ -330,12 +338,13 @@ async def test_empty_field_mapping_fails(monkeypatch: pytest.MonkeyPatch, tmp_pa@pytest.mark.asyncio-async def test_local_path_rejected_outside_local_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:+async def test_absolute_path_rejected_outside_run_download_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:run_id = "run_non_local_path"_install_context(monkeypatch, run_id)source_pdf = tmp_path / "source.pdf"_write_fillable_pdf(source_pdf)monkeypatch.setattr("skyvern.forge.sdk.workflow.models.pdf_fill_block.settings.ENV", "production")+ monkeypatch.setattr(pdf_fill_block.settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))block = _make_block(file_url=str(source_pdf))result = await block.execute(workflow_run_id=run_id, workflow_run_block_id="", organization_id=None)@@ -344,11 +353,28 @@ async def test_local_path_rejected_outside_local_env(monkeypatch: pytest.MonkeyPassert result.status == BlockStatus.failed+async def test_resolve_source_pdf_accepts_run_local_absolute_path_in_non_local_env(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ run_id = "run_cloud_local_path"+ _install_context(monkeypatch, run_id)+ source_pdf = _run_local_pdf_path(monkeypatch, tmp_path, run_id, "source.pdf")+ _write_fillable_pdf(source_pdf)+ monkeypatch.setattr("skyvern.forge.sdk.workflow.models.pdf_fill_block.settings.ENV", "production")++ block = _make_block(file_url=str(source_pdf))+ resolved, is_temp = await block._resolve_source_pdf(run_id, organization_id="org-1")++ assert resolved == str(source_pdf.resolve())+ assert is_temp is False++@pytest.mark.asyncioasync def test_flat_pdf_without_tesseract_fails(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:run_id = "run_flat_pdf_no_ocr"_install_context(monkeypatch, run_id)- flat_pdf = tmp_path / "flat.pdf"+ flat_pdf = _run_local_pdf_path(monkeypatch, tmp_path, run_id, "flat.pdf")_write_flat_pdf(flat_pdf)monkeypatch.setattr(PdfFillBlock, "_tesseract_available", staticmethod(lambda: False))@@ -364,7 +390,7 @@ async def test_flat_pdf_without_tesseract_fails(monkeypatch: pytest.MonkeyPatch,async def test_flat_pdf_overlay_fills_with_ocr_anchors(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:run_id = "run_flat_pdf_overlay"_install_context(monkeypatch, run_id)- flat_pdf = tmp_path / "flat.pdf"+ flat_pdf = _run_local_pdf_path(monkeypatch, tmp_path, run_id, "flat.pdf")_write_flat_pdf(flat_pdf)anchor = FlatPdfAnchor(@@ -415,7 +441,7 @@ async def test_flat_pdf_overlay_fills_with_ocr_anchors(monkeypatch: pytest.Monkeasync def test_flat_pdf_over_page_limit_fails(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:run_id = "run_flat_pdf_too_many_pages"_install_context(monkeypatch, run_id)- big_pdf = tmp_path / "big.pdf"+ big_pdf = _run_local_pdf_path(monkeypatch, tmp_path, run_id, "big.pdf")writer = PdfWriter()for _ in range(26):writer.add_blank_page(width=300, height=200)@@ -651,7 +677,7 @@ def test_parse_tesseract_tsv_groups_lines() -> None:async def test_loop_iterations_get_unique_filenames(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:run_id = "run_unique_filename"_install_context(monkeypatch, run_id)- source_pdf = tmp_path / "source.pdf"+ source_pdf = _run_local_pdf_path(monkeypatch, tmp_path, run_id, "source.pdf")_write_fillable_pdf(source_pdf)monkeypatch.setattr(PdfFillBlock, "_resolve_default_llm_handler", AsyncMock(return_value=_fake_llm_response))monkeypatch.setattr(diff --git a/tests/unit/test_workflow_system_prompt_llm_forwarding.py b/tests/unit/test_workflow_system_prompt_llm_forwarding.pyindex 08e9bdb3a..52f37ca20 100644--- a/tests/unit/test_workflow_system_prompt_llm_forwarding.py+++ b/tests/unit/test_workflow_system_prompt_llm_forwarding.py@@ -340,11 +340,7 @@ def test_pdf_parser_block_forwards_system_prompt(monkeypatch) -> None:# Patch the app-level handler directly since PDFParserBlock uses app.LLM_API_HANDLER.monkeypatch.setattr(block_module.app, "LLM_API_HANDLER", fake_llm)- monkeypatch.setattr(- block_module,- "download_file",- AsyncMock(return_value="/tmp/file.pdf"),- )+ monkeypatch.setattr("skyvern.forge.sdk.api.files.download_file", AsyncMock(return_value="/tmp/file.pdf"))monkeypatch.setattr(block_module, "extract_pdf_file", lambda *a, **k: "extracted text")workflow_run_context = MagicMock()diff --git a/tests/unit/workflow/test_file_parser_block.py b/tests/unit/workflow/test_file_parser_block.pyindex 23f64a9c4..d3165117b 100644--- a/tests/unit/workflow/test_file_parser_block.py+++ b/tests/unit/workflow/test_file_parser_block.py@@ -555,7 +555,7 @@ class TestPDFParserSchemaValidation:)mp.setattr(PDFParserBlock, "record_output_parameter_value", record_output_parameter_value)mp.setattr(PDFParserBlock, "build_block_result", fake_build_block_result)- mp.setattr("skyvern.forge.sdk.workflow.models.block.download_file", AsyncMock(return_value="/tmp/test.pdf"))+ mp.setattr("skyvern.forge.sdk.api.files.download_file", AsyncMock(return_value="/tmp/test.pdf"))mp.setattr("skyvern.forge.sdk.workflow.models.block.extract_pdf_file", MagicMock(return_value="name\nAlice"))mp.setattr("skyvern.forge.sdk.workflow.models.block.prompt_engine.load_prompt",diff --git a/tests/unit/workflow/test_file_parser_error_code.py b/tests/unit/workflow/test_file_parser_error_code.pyindex 11b6a044f..1254135b8 100644--- a/tests/unit/workflow/test_file_parser_error_code.py+++ b/tests/unit/workflow/test_file_parser_error_code.py@@ -133,7 +133,7 @@ class TestFileParserBlockExecuteErrorCodes:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",side_effect=Exception("Download failed"),):result = await block.execute(@@ -153,7 +153,7 @@ class TestFileParserBlockExecuteErrorCodes:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/bad.csv",):with patch.object(@@ -176,7 +176,7 @@ class TestFileParserBlockExecuteErrorCodes:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/test_file",):with patch.object(FileParserBlock, "_detect_file_type_from_url", return_value="unsupported_type"):@@ -201,7 +201,7 @@ class TestFileParserBlockExecuteErrorCodes:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/statement.pdf",):with patch.object(FileParserBlock, "validate_file_type"):@@ -234,7 +234,7 @@ class TestFileParserBlockExecuteErrorCodes:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/statement.pdf",):with patch.object(FileParserBlock, "validate_file_type"):@@ -262,7 +262,7 @@ class TestFileParserBlockExecuteErrorCodes:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/test.csv",):with patch.object(FileParserBlock, "validate_file_type"):@@ -286,7 +286,7 @@ class TestFileParserBlockExecuteErrorCodes:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/test.csv",):with patch.object(FileParserBlock, "validate_file_type"):@@ -412,7 +412,7 @@ class TestFileParserBlockRecordsOutputOnFailure:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",side_effect=Exception("Download failed"),):with patch.object(@@ -437,7 +437,7 @@ class TestFileParserBlockRecordsOutputOnFailure:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/test_file",):with patch.object(FileParserBlock, "_detect_file_type_from_url", return_value="unsupported_type"):@@ -464,7 +464,7 @@ class TestFileParserBlockRecordsOutputOnFailure:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/statement.pdf",):with patch.object(FileParserBlock, "validate_file_type"):@@ -496,7 +496,7 @@ class TestFileParserBlockRecordsOutputOnFailure:with patch.object(FileParserBlock, "get_workflow_run_context", return_value=mock_ctx):with patch.object(FileParserBlock, "format_potential_template_parameters"):with patch(- "skyvern.forge.sdk.workflow.models.block.download_file",+ "skyvern.forge.sdk.api.files.download_file",return_value="/tmp/test.csv",):with patch.object(FileParserBlock, "validate_file_type"):diff --git a/tests/unit/workflow/test_file_parser_zip.py b/tests/unit/workflow/test_file_parser_zip.pynew file mode 100644index 000000000..103550ff2--- /dev/null+++ b/tests/unit/workflow/test_file_parser_zip.py@@ -0,0 +1,460 @@+"""+Tests for FileParserBlock ZIP support (SKY-11711).++Covers ZIP detection, validation, safe extraction (junk filtering, zip-bomb caps,+traversal), per-file content parsing for AI extraction, and local-path file_url+handling (applies to all file types).+"""++from __future__ import annotations++import json+import zipfile+from datetime import datetime, timezone+from pathlib import Path+from typing import Any+from unittest.mock import AsyncMock, MagicMock++import docx+import pytest++from skyvern.config import settings+from skyvern.forge.sdk.workflow.exceptions import InvalidFileType+from skyvern.forge.sdk.workflow.models.block import BlockType, FileParserBlock+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterType+from skyvern.schemas.workflows import BlockResult, BlockStatus, FileType+++def _make_output_parameter(key: str) -> OutputParameter:+ return OutputParameter(+ parameter_type=ParameterType.OUTPUT,+ key=key,+ description="test",+ output_parameter_id="test-output-id",+ workflow_id="test-workflow-id",+ created_at=datetime.now(timezone.utc),+ modified_at=datetime.now(timezone.utc),+ )+++def _make_file_parser_block(file_url: str, file_type: FileType = FileType.AUTO_DETECT) -> FileParserBlock:+ return FileParserBlock(+ label="test_file_parser",+ block_type=BlockType.FILE_URL_PARSER,+ output_parameter=_make_output_parameter("test_output"),+ file_url=file_url,+ file_type=file_type,+ )+++def _create_zip(path: Path, files: dict[str, bytes]) -> Path:+ with zipfile.ZipFile(path, "w") as zf:+ for name, content in files.items():+ zf.writestr(name, content)+ return path+++def _create_docx_bytes(paragraph: str, path: Path) -> bytes:+ doc = docx.Document()+ doc.add_paragraph(paragraph)+ doc.save(str(path))+ return path.read_bytes()+++class TestZipDetection:+ def _detect(self, url: str, file_path: str | None = None) -> FileType:+ block = _make_file_parser_block(url)+ return block._detect_file_type_from_url(url, file_path=file_path)++ def test_zip_extension(self) -> None:+ assert self._detect("https://example.com/archive.zip") == FileType.ZIP++ def test_zip_extension_case_insensitive(self) -> None:+ assert self._detect("https://example.com/archive.ZIP") == FileType.ZIP++ def test_zip_extension_with_query_params(self) -> None:+ assert self._detect("https://example.com/archive.zip?token=abc") == FileType.ZIP++ def test_no_extension_with_zip_magic_bytes(self, tmp_path: Path) -> None:+ zip_path = _create_zip(tmp_path / "no_ext_file", {"a.txt": b"hello"})+ assert self._detect("https://example.com/download?id=123", file_path=str(zip_path)) == FileType.ZIP++ def test_docx_magic_bytes_not_detected_as_zip(self, tmp_path: Path) -> None:+ # DOCX is a ZIP container; the OOXML matcher must win over the generic zip matcher.+ docx_path = tmp_path / "no_ext_docx"+ _create_docx_bytes("hello", docx_path)+ assert self._detect("https://example.com/download?id=123", file_path=str(docx_path)) == FileType.DOCX+++class TestValidateZipFileType:+ def test_valid_zip(self, tmp_path: Path) -> None:+ zip_path = _create_zip(tmp_path / "valid.zip", {"a.txt": b"hello"})+ block = _make_file_parser_block("https://example.com/valid.zip", FileType.ZIP)+ block.validate_file_type("https://example.com/valid.zip", str(zip_path))++ def test_non_zip_with_zip_extension(self, tmp_path: Path) -> None:+ fake_path = tmp_path / "fake.zip"+ fake_path.write_text("this is not a zip")+ block = _make_file_parser_block("https://example.com/fake.zip", FileType.ZIP)+ with pytest.raises(InvalidFileType, match="not a valid ZIP"):+ block.validate_file_type("https://example.com/fake.zip", str(fake_path))+++class TestIsZipJunkMember:+ @pytest.mark.parametrize(+ "member_name",+ [+ "__MACOSX/report.pdf",+ "nested/__MACOSX/._report.pdf",+ "._invoice.pdf",+ "nested/._invoice.pdf",+ ".DS_Store",+ "nested/.DS_Store",+ "Thumbs.db",+ ],+ )+ def test_junk_members(self, member_name: str) -> None:+ assert FileParserBlock._is_zip_junk_member(member_name) is True++ @pytest.mark.parametrize(+ "member_name",+ ["report.pdf", "nested/data.csv", "macosx/report.pdf", "_underscore.pdf", "a._b.pdf"],+ )+ def test_real_members(self, member_name: str) -> None:+ assert FileParserBlock._is_zip_junk_member(member_name) is False+++class TestExtractZipFile:+ def _extract(self, mp: pytest.MonkeyPatch, tmp_path: Path, zip_files: dict[str, bytes]) -> list[dict[str, Any]]:+ mp.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))+ zip_path = _create_zip(tmp_path / "archive.zip", zip_files)+ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)+ return block._extract_zip_file(str(zip_path), "wr_test", "wrb_test")++ def test_extracts_files_and_returns_sorted_list(self, tmp_path: Path) -> None:+ with pytest.MonkeyPatch.context() as mp:+ extracted = self._extract(+ mp,+ tmp_path,+ {"b_second.txt": b"second", "nested/a_first.txt": b"first!"},+ )++ assert [f["file_name"] for f in extracted] == ["b_second.txt", "nested/a_first.txt"]+ for file_info in extracted:+ assert Path(file_info["file_path"]).is_file()+ assert file_info["file_path"].startswith(str(tmp_path / "downloads" / "wr_test"))+ assert extracted[0]["file_size"] == len(b"second")+ assert Path(extracted[1]["file_path"]).read_bytes() == b"first!"++ def test_junk_entries_are_not_extracted(self, tmp_path: Path) -> None:+ with pytest.MonkeyPatch.context() as mp:+ extracted = self._extract(+ mp,+ tmp_path,+ {"real.txt": b"data", "__MACOSX/._real.txt": b"junk", ".DS_Store": b"junk"},+ )++ assert [f["file_name"] for f in extracted] == ["real.txt"]+ extract_dir = Path(extracted[0]["file_path"]).parent+ assert not (extract_dir / "__MACOSX").exists()+ assert not (extract_dir / ".DS_Store").exists()++ def test_traversal_member_stays_inside_extract_dir(self, tmp_path: Path) -> None:+ with pytest.MonkeyPatch.context() as mp:+ extracted = self._extract(mp, tmp_path, {"../evil.txt": b"escape attempt"})++ assert len(extracted) == 1+ download_root = (tmp_path / "downloads").resolve()+ resolved = Path(extracted[0]["file_path"]).resolve()+ assert resolved.is_relative_to(download_root)+ assert not (tmp_path / "evil.txt").exists()++ def test_too_many_entries_raises(self, tmp_path: Path) -> None:+ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(FileParserBlock, "_MAX_ZIP_ENTRIES", 2)+ with pytest.raises(InvalidFileType, match="exceeding the limit"):+ self._extract(mp, tmp_path, {"a.txt": b"1", "b.txt": b"2", "c.txt": b"3"})++ def test_archive_size_cap_raises_before_opening_zip(self, tmp_path: Path) -> None:+ zip_path = _create_zip(tmp_path / "archive.zip", {"a.txt": b"1"})++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))+ mp.setattr(FileParserBlock, "_MAX_ZIP_ARCHIVE_BYTES", zip_path.stat().st_size - 1)+ mp.setattr(+ zipfile,+ "ZipFile",+ MagicMock(side_effect=AssertionError("ZipFile must not be opened after archive-size preflight")),+ )+ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)++ with pytest.raises(InvalidFileType, match="ZIP archive size"):+ block._extract_zip_file(str(zip_path), "wr_test", "wrb_test")++ def test_read_zip_total_entry_count(self, tmp_path: Path) -> None:+ zip_path = _create_zip(tmp_path / "archive.zip", {"a.txt": b"1", "b.txt": b"2", "c.txt": b"3"})++ assert FileParserBlock._read_zip_total_entry_count(str(zip_path)) == 3++ def test_read_zip64_total_entry_count_with_max_comment(self, tmp_path: Path) -> None:+ zip64_eocd = b"PK\x06\x06" + (b"\x00" * 28) + (70_000).to_bytes(8, "little") + (b"\x00" * 16)+ zip64_locator = b"PK\x06\x07" + (b"\x00" * 16)+ classic_eocd = (+ b"PK\x05\x06"+ + (b"\x00" * 4)+ + (0xFFFF).to_bytes(2, "little")+ + (0xFFFF).to_bytes(2, "little")+ + (b"\x00" * 8)+ + (65_535).to_bytes(2, "little")+ )+ zip_path = tmp_path / "zip64_tail.zip"+ zip_path.write_bytes(b"\x00" * 4096 + zip64_eocd + zip64_locator + classic_eocd + (b"c" * 65_535))++ assert FileParserBlock._read_zip_total_entry_count(str(zip_path)) == 70_000++ def test_declared_entry_count_cap_raises_before_opening_zip(self, tmp_path: Path) -> None:+ zip_path = _create_zip(tmp_path / "archive.zip", {"a.txt": b"1", "b.txt": b"2", "c.txt": b"3"})++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))+ mp.setattr(FileParserBlock, "_MAX_ZIP_ENTRIES", 2)+ mp.setattr(+ zipfile,+ "ZipFile",+ MagicMock(side_effect=AssertionError("ZipFile must not be opened after entry-count preflight")),+ )+ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)++ with pytest.raises(InvalidFileType, match="ZIP archive declares 3 entries"):+ block._extract_zip_file(str(zip_path), "wr_test", "wrb_test")++ def test_uncompressed_size_cap_raises(self, tmp_path: Path) -> None:+ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(FileParserBlock, "_MAX_ZIP_UNCOMPRESSED_BYTES", 10)+ with pytest.raises(InvalidFileType, match="uncompressed size"):+ self._extract(mp, tmp_path, {"big.txt": b"x" * 100})++ def test_measured_uncompressed_size_helper_raises(self) -> None:+ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(FileParserBlock, "_MAX_ZIP_UNCOMPRESSED_BYTES", 10)+ block._check_extracted_size_within_limit(10)+ with pytest.raises(InvalidFileType, match="uncompressed content exceeds the limit of 10 bytes"):+ block._check_extracted_size_within_limit(11)++ def test_encrypted_zip_raises(self, tmp_path: Path) -> None:+ zip_path = _create_zip(tmp_path / "encrypted.zip", {"secret.txt": b"secret data"})+ # zipfile cannot write encrypted archives, so set the encryption flag bit directly+ # in the local file header (PK\x03\x04 offset 6) and central directory (PK\x01\x02 offset 8).+ data = bytearray(zip_path.read_bytes())+ data[data.find(b"PK\x03\x04") + 6] |= 0x1+ data[data.find(b"PK\x01\x02") + 8] |= 0x1+ zip_path.write_bytes(bytes(data))++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))+ block = _make_file_parser_block("https://example.com/encrypted.zip", FileType.ZIP)+ with pytest.raises(InvalidFileType, match="Password-protected"):+ block._extract_zip_file(str(zip_path), "wr_test", "wrb_test")++ def test_empty_zip_returns_empty_list(self, tmp_path: Path) -> None:+ with pytest.MonkeyPatch.context() as mp:+ extracted = self._extract(mp, tmp_path, {})++ assert extracted == []++ def test_colliding_member_names_keep_last_entry_once(self, tmp_path: Path) -> None:+ # "a.txt" and "../a.txt" sanitize to the same destination; per ZIP semantics the+ # last member wins and the output must not contain duplicate entries.+ zip_path = tmp_path / "colliding.zip"+ with zipfile.ZipFile(zip_path, "w") as zf:+ zf.writestr("a.txt", b"first version")+ zf.writestr("../a.txt", b"second version")++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))+ block = _make_file_parser_block("https://example.com/colliding.zip", FileType.ZIP)+ extracted = block._extract_zip_file(str(zip_path), "wr_test", "wrb_test")++ assert [f["file_name"] for f in extracted] == ["a.txt"]+ assert Path(extracted[0]["file_path"]).read_bytes() == b"second version"+ assert extracted[0]["file_size"] == len(b"second version")+++class TestParseZipContents:+ @staticmethod+ def _file_info(path: Path, root: Path) -> dict[str, Any]:+ return {+ "file_name": str(path.relative_to(root)),+ "file_path": str(path),+ "file_size": path.stat().st_size,+ }++ async def test_parses_supported_files_and_skips_unparseable(self, tmp_path: Path) -> None:+ csv_path = tmp_path / "data.csv"+ csv_path.write_text("name,age\nAlice,30")+ docx_path = tmp_path / "doc.docx"+ _create_docx_bytes("Report body", docx_path)+ nested_zip = _create_zip(tmp_path / "inner.zip", {"x.txt": b"nested"})+ binary_path = tmp_path / "blob.bin"+ binary_path.write_bytes(b"\x00\x01\x02\x03")++ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)+ entries = await block._parse_zip_contents(+ [+ self._file_info(csv_path, tmp_path),+ self._file_info(docx_path, tmp_path),+ self._file_info(nested_zip, tmp_path),+ self._file_info(binary_path, tmp_path),+ ]+ )++ assert entries == [+ {"file_name": "data.csv", "content": [{"name": "Alice", "age": "30"}]},+ {"file_name": "doc.docx", "content": "Report body"},+ ]++ async def test_pdf_files_parsed_via_pdf_parser(self, tmp_path: Path) -> None:+ pdf_path = tmp_path / "report.pdf"+ pdf_path.write_bytes(b"%PDF-1.5 fake")++ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)+ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(FileParserBlock, "_parse_pdf_file", AsyncMock(return_value="pdf text content"))+ entries = await block._parse_zip_contents([self._file_info(pdf_path, tmp_path)])++ assert entries == [{"file_name": "report.pdf", "content": "pdf text content"}]++ async def test_truncates_at_file_boundary_on_token_limit(self, tmp_path: Path) -> None:+ first = tmp_path / "a.csv"+ first.write_text("h\n1")+ second = tmp_path / "b.csv"+ second.write_text("h\n2")++ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)+ with pytest.MonkeyPatch.context() as mp:+ mp.setattr("skyvern.forge.sdk.workflow.models.block.count_tokens", lambda text: 10)+ mp.setattr("skyvern.forge.sdk.workflow.models.block.MAX_FILE_PARSE_INPUT_TOKENS", 15)+ entries = await block._parse_zip_contents(+ [self._file_info(first, tmp_path), self._file_info(second, tmp_path)]+ )++ assert [entry["file_name"] for entry in entries] == ["a.csv"]++ async def test_no_parseable_files_raises(self, tmp_path: Path) -> None:+ binary_path = tmp_path / "blob.bin"+ binary_path.write_bytes(b"\x00\x01\x02\x03")++ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)+ with pytest.raises(InvalidFileType, match="no parseable files"):+ await block._parse_zip_contents([self._file_info(binary_path, tmp_path)])++ async def test_first_file_over_budget_raises(self, tmp_path: Path) -> None:+ csv_path = tmp_path / "data.csv"+ csv_path.write_text("h\n1")++ block = _make_file_parser_block("https://example.com/archive.zip", FileType.ZIP)+ with pytest.MonkeyPatch.context() as mp:+ mp.setattr("skyvern.forge.sdk.workflow.models.block.count_tokens", lambda text: 100)+ mp.setattr("skyvern.forge.sdk.workflow.models.block.MAX_FILE_PARSE_INPUT_TOKENS", 15)+ with pytest.raises(InvalidFileType, match="alone exceeds the maximum extraction input size"):+ await block._parse_zip_contents([self._file_info(csv_path, tmp_path)])+++class TestExecuteWithZipAndLocalPaths:+ @staticmethod+ def _patch_execute_dependencies(mp: pytest.MonkeyPatch, tmp_path: Path) -> AsyncMock:+ workflow_run_context = MagicMock()+ workflow_run_context.has_parameter.return_value = False+ record_output_parameter_value = AsyncMock()++ async def fake_build_block_result(self: FileParserBlock, **kwargs: Any) -> BlockResult:+ kwargs.pop("organization_id", None)+ kwargs.pop("error_codes", None)+ return BlockResult(output_parameter=self.output_parameter, **kwargs)++ mp.setattr(+ FileParserBlock, "get_workflow_run_context", staticmethod(lambda workflow_run_id: workflow_run_context)+ )+ mp.setattr(FileParserBlock, "record_output_parameter_value", record_output_parameter_value)+ mp.setattr(FileParserBlock, "build_block_result", fake_build_block_result)+ mp.setattr(settings, "DOWNLOAD_PATH", str(tmp_path / "downloads"))+ return record_output_parameter_value++ async def test_execute_zip_without_schema_outputs_file_list(self, tmp_path: Path) -> None:+ zip_path = _create_zip(tmp_path / "archive.zip", {"a.txt": b"hello", "b.txt": b"world"})+ block = _make_file_parser_block("https://example.com/archive.zip")++ with pytest.MonkeyPatch.context() as mp:+ self._patch_execute_dependencies(mp, tmp_path)+ mp.setattr("skyvern.forge.sdk.api.files.download_file", AsyncMock(return_value=str(zip_path)))++ result = await block.execute("wr_test", "wrb_test", organization_id="org-1")++ assert result.success is True+ assert result.status == BlockStatus.completed+ output = result.output_parameter_value+ assert isinstance(output, list)+ assert [f["file_name"] for f in output] == ["a.txt", "b.txt"]+ for file_info in output:+ assert Path(file_info["file_path"]).is_file()++ async def test_execute_local_path_inside_download_dir(self, tmp_path: Path) -> None:+ run_dir = tmp_path / "downloads" / "wr_test"+ run_dir.mkdir(parents=True)+ csv_path = run_dir / "data.csv"+ csv_path.write_text("name,age\nAlice,30")+ block = _make_file_parser_block(str(csv_path))++ with pytest.MonkeyPatch.context() as mp:+ self._patch_execute_dependencies(mp, tmp_path)+ download_mock = AsyncMock(side_effect=AssertionError("download_file must not be called"))+ mp.setattr("skyvern.forge.sdk.api.files.download_file", download_mock)++ result = await block.execute("wr_test", "wrb_test", organization_id="org-1")++ assert result.success is True+ assert result.output_parameter_value == [{"name": "Alice", "age": "30"}]+ download_mock.assert_not_awaited()++ async def test_execute_zip_with_schema_runs_combined_ai_extraction(self, tmp_path: Path) -> None:+ zip_path = _create_zip(tmp_path / "archive.zip", {"data.csv": b"name,age\nAlice,30"})+ block = _make_file_parser_block("https://example.com/archive.zip")+ block.json_schema = {"type": "object"}++ with pytest.MonkeyPatch.context() as mp:+ record_output = self._patch_execute_dependencies(mp, tmp_path)+ mp.setattr("skyvern.forge.sdk.api.files.download_file", AsyncMock(return_value=str(zip_path)))+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.LLMAPIHandlerFactory.get_override_llm_api_handler",+ lambda *a, **kw: AsyncMock(return_value={"answer": 42}),+ )+ mock_load = MagicMock(return_value="prompt")+ mp.setattr("skyvern.forge.sdk.workflow.models.block.prompt_engine.load_prompt", mock_load)++ result = await block.execute("wr_test", "wrb_test", organization_id="org-1")++ assert result.success is True+ assert result.output_parameter_value == {"answer": 42}+ _, kwargs = mock_load.call_args+ content_entries = json.loads(kwargs["extracted_text_content"])+ assert content_entries == [{"file_name": "data.csv", "content": [{"name": "Alice", "age": "30"}]}]+ record_output.assert_awaited_once()++ async def test_execute_local_path_outside_download_dir_fails(self, tmp_path: Path) -> None:+ outside_path = tmp_path / "elsewhere" / "data.csv"+ outside_path.parent.mkdir(parents=True)+ outside_path.write_text("name\nAlice")+ block = _make_file_parser_block(str(outside_path))++ with pytest.MonkeyPatch.context() as mp:+ self._patch_execute_dependencies(mp, tmp_path)++ result = await block.execute("wr_test", "wrb_test", organization_id="org-1")++ assert result.success is False+ assert result.status == BlockStatus.failed+ assert "Failed to download or validate file" in (result.failure_reason or "")