|
11 | 11 | import pytest |
12 | 12 |
|
13 | 13 | from pypdf import PdfReader, PdfWriter |
14 | | -from pypdf.errors import PdfReadError |
| 14 | +from pypdf.errors import LimitReachedError, PdfReadError |
| 15 | +from pypdf.filters import FlateDecode |
15 | 16 | from pypdf.generic import ( |
16 | 17 | ArrayObject, |
17 | 18 | DictionaryObject, |
18 | 19 | EmbeddedFile, |
| 20 | + EncodedStreamObject, |
19 | 21 | NameObject, |
20 | 22 | NullObject, |
21 | 23 | TextStringObject, |
@@ -552,3 +554,30 @@ def test_get_outline__cyclic_references__nested_handling(caplog): |
552 | 554 | ] |
553 | 555 | ] |
554 | 556 | assert caplog.messages[0].startswith("Detected cycle in outline structure for {") |
| 557 | + |
| 558 | + |
| 559 | +def test_xfa__decompression_limit(): |
| 560 | + payload = b"A" * 100_0000 |
| 561 | + compressed = FlateDecode.encode(payload, 9) |
| 562 | + |
| 563 | + writer = PdfWriter() |
| 564 | + writer.add_blank_page(width=72, height=72) |
| 565 | + |
| 566 | + stream = EncodedStreamObject() |
| 567 | + stream._data = compressed |
| 568 | + stream[NameObject("/Filter")] = NameObject("/FlateDecode") |
| 569 | + stream_reference = writer._add_object(stream) |
| 570 | + |
| 571 | + acro = DictionaryObject() |
| 572 | + acro[NameObject("/XFA")] = ArrayObject([TextStringObject("datasets"), stream_reference]) |
| 573 | + writer.root_object[NameObject("/AcroForm")] = writer._add_object(acro) |
| 574 | + |
| 575 | + data = BytesIO() |
| 576 | + writer.write(data) |
| 577 | + data.flush() |
| 578 | + |
| 579 | + reader = PdfReader(data) |
| 580 | + with mock.patch("pypdf.filters.ZLIB_MAX_OUTPUT_LENGTH", 75_000), pytest.raises( |
| 581 | + expected_exception=LimitReachedError, match=r"^Limit reached while decompressing. 902 bytes remaining.$" |
| 582 | + ): |
| 583 | + _ = reader.xfa |
0 commit comments