fix(desktop): cap XLSX zip entry count and decompression footprint before preflight
davoodmehraban89 committed
Oct 30, 2026 at 23:40 UTC
f21cdb6782234ed7848e01eb1f89df00f2eb4f74
1 file changed
+28
backend/src/aram_backend/desktop_pilot_core.py
+28
@@ -5,11 +5,38 @@ uploaded, auto-imported, or used to generate statements.
5
"""
6
from __future__ import annotations
7
8
+from io import BytesIO
9
from pathlib import Path
10
+from zipfile import BadZipFile, ZipFile, ZipInfo
11
12
from aram_backend.pilot_workbook import PilotPreflightResult, preflight_pilot_workbook
13
14
MAX_WORKBOOK_BYTES = 20 * 1024 * 1024
15
+MAX_ZIP_ENTRIES = 500
16
+MAX_UNCOMPRESSED_BYTES = 128 * 1024 * 1024
17
+
18
+
19
+def _check_archive_limits(members: list[ZipInfo]) -> None:
20
+ """Reject obvious XLSX expansion bombs before calling openpyxl."""
21
+ if len(members) > MAX_ZIP_ENTRIES:
22
+ raise PilotInputError("Workbook ZIP has too many internal entries")
23
+ total = 0
24
+ for item in members:
25
+ if item.file_size < 0 or item.file_size > MAX_UNCOMPRESSED_BYTES:
26
+ raise PilotInputError("Workbook ZIP declares excessive uncompressed bytes")
27
+ total += item.file_size
28
+ if total > MAX_UNCOMPRESSED_BYTES:
29
+ raise PilotInputError("Workbook ZIP exceeds the uncompressed size limit")
30
+
31
+
32
+def _preflight_archive_limits(content: bytes) -> None:
33
+ try:
34
+ with ZipFile(BytesIO(content)) as archive:
35
+ _check_archive_limits(archive.infolist())
36
+ except BadZipFile:
37
+ # Use the validator's existing structured INVALID_WORKBOOK result.
38
+ # Invalid ZIP bytes must not be treated as valid.
39
+ return
40
41
42
class PilotInputError(ValueError):
@@ -62,6 +89,7 @@ class DesktopPilotController:
89
raise PilotInputError("Selected workbook cannot be read") from exc
90
if not content or len(content) > MAX_WORKBOOK_BYTES:
91
raise PilotInputError("Workbook is empty or exceeds the 20 MiB limit")
92
+ _preflight_archive_limits(content)
93
checked = preflight_pilot_workbook(content)
94
self.result = checked
95
self.state = "VALID" if checked.valid else "INVALID"