Repository navigation
Expand file tree
/
Copy pathlimits.py
More file actions
66 lines (47 loc) · 2.41 KB
/
Copy pathlimits.py
File metadata and controls
66 lines (47 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Resource limits for untrusted file imports.
All limits are configurable via environment variables with safe defaults.
"""
from __future__ import annotations
import os
MAX_IFC_SIZE_MB: int = int(os.environ.get("MATCHLINE_MAX_IFC_SIZE_MB", 100))
MAX_IFC_ELEMENTS: int = int(os.environ.get("MATCHLINE_MAX_IFC_ELEMENTS", 500_000))
MAX_XML_SIZE_MB: int = int(os.environ.get("MATCHLINE_MAX_XML_SIZE_MB", 50))
MAX_XML_NODES: int = int(os.environ.get("MATCHLINE_MAX_XML_NODES", 100_000))
MAX_IMAGE_SIZE_MB: int = int(os.environ.get("MATCHLINE_MAX_IMAGE_SIZE_MB", 20))
def check_file_size(path: str, max_mb: int) -> None:
"""Check file size against limit, raise ValueError if exceeded."""
from pathlib import Path
file_size_mb = Path(path).stat().st_size / (1024 * 1024)
if file_size_mb > max_mb:
raise ValueError(
f"File '{path}' is {file_size_mb:.1f} MB, "
f"exceeds the {max_mb} MB limit. "
f"Set MATCHLINE_MAX_*_SIZE_MB to increase the limit."
)
class _NodeCountingParser:
"""XML parser wrapper that counts elements and raises on complexity limit."""
def __init__(self, max_nodes: int) -> None:
self.max_nodes = max_nodes
self._count = 0
def start(self, tag: str, attrib: dict) -> None:
self._count += 1
if self._count > self.max_nodes:
raise ValueError(
f"XML document has more than {self._count} elements, "
f"exceeds the {self.max_nodes} limit. "
f"Set MATCHLINE_MAX_XML_NODES to increase the limit."
)
def end(self, tag: str) -> None:
pass
def data(self, data: str) -> None:
pass
def make_node_counting_parser(max_nodes: int) -> _NodeCountingParser:
"""Create a node-counting parser for XML complexity validation."""
return _NodeCountingParser(max_nodes)
# PDF drawing sets (#737). Large-format sheets reach 48x72 in (3456x5184 pt);
# a dense CAD export can carry a few hundred thousand path objects per sheet.
MAX_PDF_SIZE_MB: int = int(os.environ.get("MATCHLINE_MAX_PDF_SIZE_MB", 500))
MAX_PDF_PAGES: int = int(os.environ.get("MATCHLINE_MAX_PDF_PAGES", 500))
MAX_PDF_PAGE_SIDE_PT: float = float(os.environ.get("MATCHLINE_MAX_PDF_PAGE_SIDE_PT", 7200))
MAX_PDF_PAGE_OBJECTS: int = int(os.environ.get("MATCHLINE_MAX_PDF_PAGE_OBJECTS", 1_000_000))
MAX_PDF_RASTER_MPX: float = float(os.environ.get("MATCHLINE_MAX_PDF_RASTER_MPX", 150))