Skip to content

Commit c26866f

Browse files
committed
Throw an exeption in case the catalog cannot be referenced from the trailer
DEVSIX-6548
1 parent 1953b3f commit c26866f

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

kernel/src/main/java/com/itextpdf/kernel/exceptions/KernelExceptionMessageConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public final class KernelExceptionMessageConstant {
129129
"Document outline is corrupted: some outline (PDF object: \"{0}\") lacks the required parent entry.";
130130
public static final String CORRUPTED_OUTLINE_NO_TITLE_ENTRY =
131131
"Document outline is corrupted: some outline (PDF object: \"{0}\") lacks the required title entry.";
132+
public static final String CORRUPTED_ROOT_ENTRY_IN_TRAILER = "The trailer is corrupted: the catalog "
133+
+ "is corrupted or cannot be referenced from the file's trailer. The PDF cannot be opened.";
132134
public static final String DATA_HANDLER_COUNTER_HAS_BEEN_DISABLED = "Data handler counter has been disabled";
133135
public static final String DEFAULT_CRYPT_FILTER_NOT_FOUND_ENCRYPTION = "/DefaultCryptFilter not found "
134136
+ "(encryption).";

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,11 @@ protected void open(PdfVersion newPdfVersion) {
19691969

19701970
readDocumentIds();
19711971

1972-
catalog = new PdfCatalog((PdfDictionary) trailer.get(PdfName.Root, true));
1972+
PdfDictionary catalogDictionary = (PdfDictionary) trailer.get(PdfName.Root, true);
1973+
if (null == catalogDictionary) {
1974+
throw new PdfException(KernelExceptionMessageConstant.CORRUPTED_ROOT_ENTRY_IN_TRAILER);
1975+
}
1976+
catalog = new PdfCatalog(catalogDictionary);
19731977
updatePdfVersionFromCatalog();
19741978
PdfStream xmpMetadataStream = catalog.getPdfObject().getAsStream(PdfName.Metadata);
19751979
if (xmpMetadataStream != null) {

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,9 @@ protected void rebuildXref() throws IOException {
13431343
}
13441344
}
13451345
}
1346-
if (trailer == null)
1346+
if (trailer == null) {
13471347
throw new PdfException(KernelExceptionMessageConstant.TRAILER_NOT_FOUND);
1348+
}
13481349
}
13491350

13501351
protected PdfNumber getXrefPrev(PdfObject prevObjectToCheck) {

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfDocumentTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.io.logs.IoLogMessageConstant;
4646
import com.itextpdf.io.image.ImageDataFactory;
4747
import com.itextpdf.io.source.DeflaterOutputStream;
48+
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
4849
import com.itextpdf.kernel.exceptions.PdfException;
4950
import com.itextpdf.kernel.colors.ColorConstants;
5051
import com.itextpdf.kernel.geom.Rectangle;
@@ -595,6 +596,16 @@ public void mergedSiblingWidgetsRemovePageTest() throws IOException, Interrupted
595596
DESTINATION_FOLDER));
596597
}
597598

599+
@Test
600+
@LogMessages(messages = @LogMessage(messageTemplate =
601+
IoLogMessageConstant.XREF_ERROR_WHILE_READING_TABLE_WILL_BE_REBUILT))
602+
public void rootCannotBeReferenceFromTrailerTest() throws IOException {
603+
String filename = SOURCE_FOLDER + "rootCannotBeReferenceFromTrailerTest.pdf";
604+
PdfReader corruptedReader = new PdfReader(filename);
605+
Exception e = Assert.assertThrows(PdfException.class, () -> new PdfDocument(corruptedReader));
606+
Assert.assertEquals(KernelExceptionMessageConstant.CORRUPTED_ROOT_ENTRY_IN_TRAILER, e.getMessage());
607+
}
608+
598609
private static class IgnoreTagStructurePdfDocument extends PdfDocument {
599610

600611
IgnoreTagStructurePdfDocument(PdfReader reader) {

0 commit comments

Comments
 (0)