@davoodmehraban89 / Aram / commits / 719d859
Fork 0

fix(security): bound mapping workbook archive expansion

davoodmehraban89 committed Nov 1, 2026 at 15:09 UTC 719d859d226740183695cc269770eec0529b2210
1 file changed +26
backend/src/aram_backend/mapping_source_parser.py
+26
@@ -3,6 +3,7 @@ from __future__ import annotations
3 from collections.abc import Iterator
4 from dataclasses import dataclass
5 from io import BytesIO
6 +from zipfile import BadZipFile, ZipFile
7 from typing import Any
8
9 from openpyxl import load_workbook
@@ -11,6 +12,9 @@ from aram_backend.workbook_inspection import inspect_xlsx
12
13
14 MAX_MAPPING_ROWS = 100_000
15 +MAX_MAPPING_INPUT_BYTES = 20 * 1024 * 1024
16 +MAX_MAPPING_ZIP_ENTRIES = 500
17 +MAX_MAPPING_EXPANDED_BYTES = 128 * 1024 * 1024
18
19 _REQUIRED_HEADERS = (
20 "کد حساب",
@@ -48,6 +52,7 @@ def parse_mapping_source(content: bytes) -> Iterator[MappingSourceRow]:
52 Formula cells are read as formulas (data_only=False), never executed.
53 """
54
55 + _guard_mapping_archive(content)
56 inspection = inspect_xlsx(content, document_type="mapping")
57 workbook = load_workbook(
58 BytesIO(content), read_only=True, data_only=False, keep_links=False
@@ -149,3 +154,24 @@ def _required_code(value: object, row_number: int, field: str) -> str:
154 if isinstance(value, float):
155 return str(int(value))
156 return result
157 +
158 +
159 +def _guard_mapping_archive(content: bytes) -> None:
160 + if not isinstance(content, bytes) or not 0 < len(content) <= MAX_MAPPING_INPUT_BYTES:
161 + raise MappingSourceParseError("mapping workbook size is invalid")
162 + try:
163 + with ZipFile(BytesIO(content)) as archive:
164 + members = archive.infolist()
165 + if len(members) > MAX_MAPPING_ZIP_ENTRIES:
166 + raise MappingSourceParseError("mapping workbook archive has too many entries")
167 + expanded = 0
168 + for member in members:
169 + if member.file_size < 0 or member.file_size > MAX_MAPPING_EXPANDED_BYTES:
170 + raise MappingSourceParseError("mapping workbook expanded size is excessive")
171 + expanded += member.file_size
172 + if expanded > MAX_MAPPING_EXPANDED_BYTES:
173 + raise MappingSourceParseError("mapping workbook expanded size is excessive")
174 + except (BadZipFile, OSError, ValueError) as exc:
175 + if isinstance(exc, MappingSourceParseError):
176 + raise
177 + raise MappingSourceParseError("mapping workbook archive is invalid") from exc