refactor(xml): add streaming iterparse metadata extractor (ReDoS-free & Decimal precision) - #80
Merged
smekcio merged 9 commits intoAug 12, 2026
Conversation
added 3 commits
August 9, 2026 12:47
added 6 commits
August 12, 2026 22:02
- Switch to defusedxml for XXE mitigation and XML bomb protection - Change str encoding to StringIO to avoid mojibake - Add length check for Decimal P_11 value to prevent CPU DoS - Fix iterparse O(1) memory leak by clearing root - Fix graceful malformed xml handling to fail-fast - Update injection immunity tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
This PR introduces a high-performance streaming metadata parser
fast_extract_ksef_metadatafor KSeF XML invoices built on Python's built-inxml.etree.ElementTree.iterparse.It incorporates the feedback and recommendations from previous code reviews:
iterparse). This structural parsing approach avoids catastrophic backtracking (ReDoS) and treats CDATA sections, XML comments, and element attributes according to XML standards.tag_name == 'P_11'), preventing inadvertent matches against sub-elements likeP_11A,P_11Vat, orP_11NettoZ.Decimal): Usesdecimal.Decimalinstead of IEEE 754 floating-point arithmetic. Automatically normalizes European decimal commas (100,50->100.50) and gracefully skips invalid numeric strings.str,bytes, orio.BufferedIOBasestream inputs.Podmiot1vsPodmiot2) to return distinctseller_nipandbuyer_nipfields alongside the flatnipslist.try-except (ET.ParseError, Exception)to prevent malformed XMLs from crashing batch pipelines, while clearing processed DOM nodes (elem.clear()) to maintain an (1)$ memory footprint.Performance Benchmark (5,000 Invoices Batch)
ET.fromstring185.0 msiterparse(fast_extract)180.80 msUnit Test Coverage
Added comprehensive unit tests in
tests/test_fast_parser.py:test_fast_extract_ksef_metadata_correctness(ExactP_11matching &Decimalsum)test_fast_extract_ksef_metadata_bytes(bytesinput support)test_fast_extract_ksef_metadata_injection_immunity(XML comment & CDATA section immunity)test_fast_extract_ksef_metadata_malformed_graceful_handling(Malformed XML error handling)