naruto-lgtm opened a new pull request, #1582:
URL: https://github.com/apache/poi/pull/1582

   Repro: open a .docx, .xlsx or .pptx whose main part has 980 child parts and 
100,000 external relationships (713 KB). `new XWPFDocument(...)` takes 194 s on 
trunk; with 20,000 relationships it takes 25 s. The same relationships without 
the child parts load in about 1.7 s, and the parts without the relationships in 
1.3 s.
   Cause: `POIXMLDocumentPart.read` calls `POIXMLFactory.createDocumentPart` 
for every new child part, and `getPackageRelationship` finds that part's 
relationship by copying the parent's whole relationship collection 
(`getRelationships()`) and resolving every target URI. Loading is therefore 
O(child parts x relationships). Both counts come from the file: 
`MAX_FILE_COUNT` caps the parts at 1000, but nothing caps the relationships, 
and a long .rels part compresses well within the inflate-ratio limit. This runs 
on every `POIXMLDocument` load, before any extractor code, so a service that 
opens uploaded OOXML files can be stalled with a small upload 
(algorithmic-complexity DoS, CWE-407).
   Fix: while `read()` creates the child parts, the parent indexes those 
relationships by target once and `getPackageRelationship` looks the part up 
there, falling back to the existing search on a miss or for a relationship the 
part no longer has. It still returns the first relationship in id order whose 
target matches the part name ignoring case. `createDocumentPart(parent, part)` 
keeps its signature, so `XDGFFactory` and other factories that override it 
still run. The same 713 KB file now loads in 2.8 s.
   
   The index only lives for the duration of the child-part loop in `read()`, so 
later calls to `getPackageRelationship` (after relations are added or removed) 
behave exactly as before. 
`TestPOIXMLDocument.testManyChildPartsAndRelationships` builds a 900-part 
package with 50,000 external relationships and fails on trunk (20 s timeout). 
`./gradlew :poi-ooxml:check -x spotbugsMain -x spotbugsTest` passes locally.
   


-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to