Security (3/5): generic.py parse limits & cycle guard — CVE-2026-27024, CVE-2026-31826, CVE-2026-33123#3
Merged
Merged
Conversation
A crafted document outline whose /Next chain forms a cycle that never reaches /Last made TreeObject.children() iterate forever (CWE-835). Snapshot /Last once and track visited node ids; stop (with a warning) if a node repeats. Mirrors upstream pypdf 6.7.1 (PR py-pdf#3645). 1.28.6 keeps the PEP 479 StopIteration branch for old Pythons, so the guard returns/raises the same way the existing terminations do. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A crafted stream dictionary with an enormous /Length (e.g. 2 GB) made DictionaryObject.read_from_stream call stream.read(length), pre-allocating a huge buffer and exhausting memory (CWE-770). Reject a declared /Length above MAX_DECLARED_STREAM_LENGTH (75 MB) with PdfReadError before the read. Mirrors upstream pypdf 6.8.0 (PR py-pdf#3675), reusing PdfReadError instead of LimitReachedError. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A content stream supplied as an array of many stream objects (or whose elements concatenate to an enormous size) made ContentStream.__init__ build the combined buffer with an unbounded `data += ...` loop, causing CPU/memory exhaustion (CWE-400/CWE-407). Cap the number of array elements (CONTENT_STREAM_ARRAY_MAX_LENGTH = 10000) and the total concatenated size (MAX_ARRAY_BASED_STREAM_OUTPUT_LENGTH = 75 MB), raising PdfReadError when exceeded, and join via a list to avoid the quadratic concatenation. Mirrors upstream pypdf 6.9.1 (PR py-pdf#3686). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Backports upstream pypdf security hardenings into PyPDF2/generic.py to mitigate parsing-related DoS vectors (cycle in outline traversal, oversized declared stream lengths, and array-based ContentStream exhaustion), plus adds targeted regression tests.
Changes:
- Add global parse-limit constants and enforce a maximum declared stream
/Lengthduring dictionary/stream parsing. - Add cycle detection to
TreeObject.children()to prevent infinite/Nextchains. - Bound array-based
ContentStreamconcatenation by element count and total output size; add regression tests covering all three CVEs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
PyPDF2/generic.py |
Adds caps/guards for declared stream length, outline traversal cycles, and array-based content-stream concatenation. |
Tests/test_security_generic.py |
Adds regression tests exercising the new limits and cycle guard behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Part 3 of 5 of the PyPDF2 1.28.6 security backport, targeting
1.28.6.x. Scoped toPyPDF2/generic.py.TreeObject.childrencyclic/Nextguard/Length(75 MB)Backported from upstream pypdf 6.7.1 / 6.8.0 / 6.9.1; Py2.7-safe. New tests:
Tests/test_security_generic.py(7). Validated under Python 2.7.18 — no new failures vs baseline.🤖 Generated with Claude Code