Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROB: Raise proper error on infinite loop when reading objects #3169

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
Iterable,
List,
Optional,
Set,
Tuple,
Type,
Union,
Expand Down Expand Up @@ -133,6 +134,7 @@ def __init__(
self._validated_root: Optional[DictionaryObject] = None

self._initialize_stream(stream)
self._known_objects: Set[Tuple[int, int]] = set()

self._override_encryption = False
self._encryption: Optional[Encryption] = None
Expand Down Expand Up @@ -447,7 +449,13 @@ def get_object(
)
if self.strict:
assert generation == indirect_reference.generation

current_object = (indirect_reference.idnum, indirect_reference.generation)
if current_object in self._known_objects:
raise PdfReadError(f"Detected loop with self reference for {indirect_reference!r}.")
self._known_objects.add(current_object)
retval = read_object(self.stream, self) # type: ignore
self._known_objects.remove(current_object)

# override encryption is used for the /Encrypt dictionary
if not self._override_encryption and self._encryption is not None:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1806,3 +1806,15 @@ def test_issue2886(caplog):

with pytest.raises(PdfReadError, match="Unexpected empty line in Xref table."):
_ = PdfReader(BytesIO(get_data_from_url(url, name=name)))


@pytest.mark.enable_socket
def test_infinite_loop_for_length_value():
"""Tests for #3112"""
url = "https://github.com/user-attachments/files/19106009/Special.n.15.du.jeudi.22.fevrier.2024.pdf"
name = "issue3112.pdf"

reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
writer = PdfWriter()
with pytest.raises(PdfReadError, match=r"^Detected loop with self reference for IndirectObject\(165, 0, \d+\)\.$"):
writer.add_page(reader.pages[0])
Loading