T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/ofd_to_text.py:29
- Finding
- Unbounded Processing of Untrusted OFD Archives<![CDATA[ ## Vulnerability Details **File Location**: `scripts/ofd_to_text.py:29-39`; `scripts/ofd_to_markdown.py:28-34` **Vulnerability Type**: Uncontrolled resource consumption through archive decompression and XML parsing **Risk Level**: Medium ### Vulnerable Code `scripts/ofd_to_text.py:29-39`: ```python with zipfile.ZipFile(ofd_path, 'r') as zip_ref: # 获取所有 XML 文件 file_list = zip_ref.namelist() # OFD 文件结构:根目录有 OFD.xml,内容在 Doc_0/ 下 # 首先读取 OFD.xml 获取文档结构 ofd_xml_files = [f for f in file_list if f.endswith('.xml') and 'Doc_' in f] for xml_file in ofd_xml_files: try: with zip_ref.open(xml_file) as xml_file_obj: ``` `scripts/ofd_to_markdown.py:28-34`: ```python with zipfile.ZipFile(self.ofd_path, 'r') as zip_ref: file_list = zip_ref.namelist() # 查找文档内容文件 doc_files = [f for f in file_list if f.endswith('.xml') and 'Doc_' in f] for doc_file in sorted(doc_files): self._process_document(zip_ref, doc_file) ``` The selected entries are subsequently parsed using `xml.etree.ElementTree.parse()` without resource limits. ### Technical Analysis Both converters treat OFD documents as ZIP archives and process every archive entry whose name ends in `.xml` and contains `Doc_`. They do not enforce limits on: - The number of archive entries - The uncompressed size of individual entries - The aggregate uncompressed size - The ratio between compressed and uncompressed sizes - XML document size, nesting depth, or element count - Total conversion time or memory consumption An attacker can construct an OFD archive containing highly compressible XML data, a large number of qualifying entries, or XML documents with excessive structural complexity. Opening and parsing those entries can consume substantial CPU and memory even when the supplied archive itself is relatively small. The scripts do not extract entries to the filesystem, so conventional ZIP path traversal is not demonstrated. The relevant weakne ...[truncated 1111 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Inspect every candidate entry with `ZipFile.infolist()` before opening it. 2. Reject archives that exceed explicit limits for: - Total entry count - Number of XML entries - Maximum uncompressed size per entry - Maximum aggregate uncompressed size - Maximum compression ratio 3. Maintain a cumulative byte counter while reading entries rather than relying only on ZIP metadata. 4. Parse XML incrementally with `ElementTree.iterparse()` and clear processed elements to reduce memory use. 5. Enforce application-level limits on XML depth, element count, text length, and conversion time. 6. Process untrusted documents in a restricted worker with operating-system CPU and memory limits. 7. Return an explicit validation error when any configured resource limit is exceeded. ]]>
