fix(mapping-xlsx): reject hidden sheets and dimensions with bounded streaming XML
davoodmehraban89 committed
Oct 30, 2026 at 17:27 UTC
34500e8f0ba877f5f19839be8946790f5c163599
1 file changed
+47
backend/src/aram_backend/mapping_excel.py
+47
@@ -8,6 +8,7 @@ from __future__ import annotations
8
from dataclasses import dataclass
9
from io import BytesIO
10
from zipfile import BadZipFile, ZipFile
11
+from xml.etree import ElementTree
12
13
from openpyxl import Workbook, load_workbook
14
from sqlalchemy.orm import Session
@@ -46,6 +47,47 @@ def _safe_archive(content: bytes) -> None:
47
raise MappingValidationError("Invalid XLSX archive") from exc
48
49
50
+def _reject_hidden_dimensions(content: bytes, worksheet_path: str) -> None:
51
+ """Stream worksheet XML to detect hidden row/column metadata safely.
52
+
53
+ openpyxl's read-only worksheet deliberately lacks row_dimensions and
54
+ column_dimensions. Loading arbitrary XLSX in full-memory mode merely to
55
+ inspect visibility would defeat the bounded streaming parser.
56
+ """
57
+ try:
58
+ with ZipFile(BytesIO(content)) as archive:
59
+ with archive.open(worksheet_path.lstrip("/")) as worksheet:
60
+ for action, element in ElementTree.iterparse(
61
+ worksheet, events=("start", "end")
62
+ ):
63
+ if action == "start":
64
+ kind = element.tag.rsplit("}", 1)[-1]
65
+ if element.attrib.get("hidden", "").lower() not in ("1", "true"):
66
+ continue
67
+ if kind == "row":
68
+ # Any hidden row can conceal Mapping decisions, including
69
+ # a header or a visually empty spacer.
70
+ raise MappingValidationError("Hidden Mapping row is prohibited")
71
+ if kind == "col":
72
+ try:
73
+ first = int(element.attrib["min"])
74
+ last = int(element.attrib["max"])
75
+ except (ValueError, KeyError) as exc:
76
+ raise MappingValidationError(
77
+ "Hidden Mapping column range is invalid"
78
+ ) from exc
79
+ if first <= len(HEADERS) and last >= 1:
80
+ raise MappingValidationError(
81
+ "Hidden Mapping column is prohibited"
82
+ )
83
+ else:
84
+ element.clear()
85
+ except MappingValidationError:
86
+ raise
87
+ except (BadZipFile, KeyError, OSError, ValueError, ElementTree.ParseError) as exc:
88
+ raise MappingValidationError("Unreadable XLSX worksheet visibility metadata") from exc
89
+
90
+
91
def _parse_int(value: object, label: str, row_number: int) -> int:
92
if type(value) is not int or value <= 0:
93
raise MappingValidationError(f"Row {row_number}: {label} must be a positive integer")
@@ -69,6 +111,11 @@ def preview_mapping_workbook(content: bytes) -> tuple[MappingEntry, ...]:
111
if book.sheetnames != [SHEET]:
112
raise MappingValidationError("Workbook must contain only a Mapping sheet")
113
sheet = book[SHEET]
114
+ if sheet.sheet_state != "visible":
115
+ raise MappingValidationError("Hidden Mapping sheet is prohibited")
116
+ # Visibility is checked from the same bounded workbook archive, before
117
+ # any mapping entry is parsed or a database transaction can be staged.
118
+ _reject_hidden_dimensions(content, sheet._worksheet_path)
119
if sheet.max_row is not None and sheet.max_row > MAX_ROWS + 1:
120
raise MappingValidationError("Too many mapping rows")
121
if sheet.max_column is not None and sheet.max_column > len(HEADERS):