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: Consider root objects without catalog type as fallback #3175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,16 @@ def root_object(self) -> DictionaryObject:
self._validated_root = o
logger_warning(f"Root found at {o.indirect_reference!r}", __name__)
break
if self._validated_root is None:
if self._validated_root is None:
if not is_null_or_none(root) and "/Pages" in cast(DictionaryObject, cast(PdfObject, root).get_object()):
logger_warning(
f"Possible root found at {cast(PdfObject, root).indirect_reference!r}, but missing /Catalog key",
__name__
)
self._validated_root = cast(
DictionaryObject, cast(PdfObject, root).get_object()
)
else:
raise PdfReadError("Cannot find Root object in pdf")
return self._validated_root

Expand Down
25 changes: 21 additions & 4 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ def test_repair_root(caplog):
caplog.clear()
reader = PdfReader(
BytesIO(
b.replace(b"/Root 1 0 R", b"/Root 2 0 R").replace(b"/Catalog", b"/Catalo ")
b.replace(b"/Root 1 0 R", b"/Root 2 0 R").replace(b"/Catalog/Pages 3 0 R", b"/Catalo ")
)
)
with pytest.raises(PdfReadError):
Expand All @@ -1775,9 +1775,9 @@ def test_repair_root(caplog):

# Invalid /Root Entry + error in get_object
caplog.clear()
b = b.replace(b"/Root 1 0 R", b"/Root 2 0 R").replace(b"/Catalog", b"/Catalo ")
b = b[:5124] + b"A" + b[5125:]
reader = PdfReader(BytesIO(b))
data = b.replace(b"/Root 1 0 R", b"/Root 2 0 R").replace(b"/Catalog/Pages 3 0 R", b"/Catalo ")
data = data[:5124] + b"A" + data[5125:]
reader = PdfReader(BytesIO(data))
with pytest.raises(PdfReadError):
len(reader.pages)
assert all(
Expand All @@ -1788,6 +1788,23 @@ def test_repair_root(caplog):
)
)

# Invalid /Root Entry without /Type, but /Pages.
caplog.clear()
reader = PdfReader(
BytesIO(
b.replace(b"/Root 1 0 R", b"/Root 2 0 R").replace(b"/Catalog", b"/Catalo ")
)
)
assert len(reader.pages) == 1
assert all(
msg in caplog.text
for msg in (
"Invalid Root object in trailer",
'Searching object with "/Catalog" key',
f"Possible root found at IndirectObject(2, 0, {id(reader)}), but missing /Catalog key"
)
)


@pytest.mark.enable_socket
def test_issue3151(caplog):
Expand Down
Loading