-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IndirectObject.fully_unwrap infinite recursion guard
Ensures we only go to a maximum depth, so a malicious PDF cannot get us indefinitely stuck. Also introduces unit tests.
- Loading branch information
1 parent
ea85c75
commit 323e162
Showing
2 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
|
||
__author__ = "Mathieu Fenniak" | ||
__author_email__ = "[email protected]" | ||
MAX_INDIRECT_OBJECT_NESTING_DEPTH = 10 | ||
|
||
|
||
class PdfObject(PdfObjectProtocol): | ||
|
@@ -292,8 +293,15 @@ def fully_unwrap(obj: Optional["PdfObject"]) -> Optional["PdfObject"]: | |
Given a PdfObject that may be an IndirectObject, recursively unwrap that IndirectObject until a None or | ||
PdfObject that is not an IndirectObject is returned. | ||
""" | ||
if isinstance(obj, IndirectObject): | ||
return IndirectObject.fully_unwrap(obj.get_object()) | ||
depth = 0 | ||
while isinstance(obj, IndirectObject): | ||
if depth > MAX_INDIRECT_OBJECT_NESTING_DEPTH: | ||
raise PdfReadError( | ||
"IndirectObject nested too deep. " | ||
"If required, consider increasing MAX_INDIRECT_OBJECT_NESTING_DEPTH." | ||
) | ||
depth += 1 | ||
obj = obj.get_object() | ||
return obj | ||
|
||
def __repr__(self) -> str: | ||
|
This file contains 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