xlsongc opened a new pull request, #77475:
URL: https://github.com/apache/airflow/pull/77475

   
   ---
   
   related: #77259
   
   This is the first of two follow-ups from #77259: it checks the uncompressed 
size of a DOCX before python-docx reads it. The document-wide character budget 
will come in a separate PR.
   
   A DOCX is a zip archive, and python-docx reads every part whole, then parses 
the XML parts into lxml trees that take 13-18x their size in memory (about 50x 
for a file of empty paragraphs). The file size says little about this, because 
a small `.docx` can inflate to gigabytes. The check rejects a file with 
`ValueError` when its XML parts total more than 50 MB uncompressed, or all its 
parts more than 512 MB. Word does not open files over 512 MB, and the largest 
XML total I found in about 1,900 real files (the Apache POI and LibreOffice 
test corpora plus local Word files) was 14.6 MB.
   
   The sizes in the zip directory can be forged, so they are not enough on 
their own. `ZipFile.read` stops at the declared size, but only after it has 
inflated up to 1 GiB (2 GiB on Python 3.14) in one call, so a part declared as 
1.6 KB that holds 2.5 GiB of zeros still costs 1.3 GB, and can even load 
cleanly when its CRC matches the declared prefix. The check therefore also 
inflates every part in 64 KB chunks through a copy of its `ZipInfo` that allows 
one byte more than declared: a longer part then fails the CRC check or yields 
that extra byte.
   
   A part counts as XML when its content type ends in `xml` or it is a `.rels` 
file, which python-docx parses whatever its content type. Content types are 
looked up the way python-docx does it (overrides by part name, defaults by 
extension, case-insensitive), so a part cannot move out of the XML total by 
being renamed, for example to `word/media/a.png`. I used `endswith("xml")` 
rather than `"xml" in`, because OLE objects have types like 
`application/vnd.openxmlformats-officedocument.oleObject` and are not parsed.
   
   I did not use a compression-ratio check like Apache POI's. Real files go 
well below POI's 0.01 minimum ratio (EMF images at 0.0012, embedded OLE at 
0.0068, and Word XML down to 0.013), and an attacker can raise the ratio easily.
   
   Measured with Python 3.12, median of 3 runs, peak memory excluding the input 
bytes:
   
   | File | Before | After |
   |---|---|---|
   | 300 pages of text | 0.10 s, 18 MB | 0.10 s, 18 MB |
   | 6173 x 10 table | 2.37 s, 112 MB | 2.33 s, 112 MB |
   | 90 MB of images | 0.07 s, 106 MB | 0.09 s, 103 MB |
   | Largest real file found (14.6 MB of XML) | 0.70 s, 231 MB | 0.70 s, 232 MB 
|
   | 60 MB of empty paragraphs | 1.53 s, 1373 MB, then lxml `XPathEvalError` | 
0.04 s, 6 MB, `ValueError` |
   | Part declared as 1.6 KB, holding 2.5 GiB, CRC of the prefix | 0.55 s, 1376 
MB, loads | 0.03 s, 7 MB, `BadZipFile` |
   | Same, CRC of the full data | 0.55 s, 1318 MB, `BadZipFile` | 0.03 s, 6 MB, 
`BadZipFile` |
   
   One cost this check does not bound well: a file of 40 MB of empty paragraphs 
stays under the XML limit and still takes about 60 s and 1.4 GB, because the 
loader reads the text of each of its 7 million paragraphs through an lxml XPath 
query. The character budget will not catch it either, as the paragraphs hold no 
text, so I plan to also cap the number of paragraphs and table cells in the 
follow-up PR.
   
   The check runs after the `docx` import, so a missing extra still raises the 
optional-feature exception. A file that is not a zip, or has no 
`[Content_Types].xml`, raises the same `BadZipFile` or `KeyError` as before.
   
   The tests build real python-docx documents and edit their zip parts, with 
the limits patched down to 1 MB and 2 MB so the files stay small. Each test 
case fails without the change.
   
   Verification: the document loader tests pass on the host with Python 3.12 
and in breeze with Python 3.11, and the new tests pass on 3.11, 3.13 and 3.14 
too. `prek` pre-commit and manual hooks and `breeze run mypy` pass on the 
changed files. `breeze build-docs common.ai` builds with no spelling errors; 
the only warnings are for the existing mermaid diagrams, because mermaid-cli 
would not install in my local CI image.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Opus 5.5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to