Skip to content

Commit 52f4ddb

Browse files
author
dzmitry.kachkou
committed
Add check for length of arrays and dictionaries
DEVSIX-4050
1 parent 21a8606 commit 52f4ddb

File tree

9 files changed

+720
-356
lines changed

9 files changed

+720
-356
lines changed

pdfa/src/main/java/com/itextpdf/pdfa/PdfAConformanceException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public class PdfAConformanceException extends PdfException {
113113
public static final String THE_DOCUMENT_DOES_NOT_CONTAIN_A_PDFA_OUTPUTINTENT_BUT_PAGE_CONTAINS_TRANSPARENCY_AND_DOES_NOT_CONTAIN_BLENDING_COLOR_SPACE = "If the document does not contain a OutputIntent, then page with transparency shall include the dictionary with Group key that include a CS with blending colour space";
114114
public static final String JPEG2000_ENUMERATED_COLOUR_SPACE_19_CIEJAB_SHALL_NOT_BE_USED = "jpeg2000 enumerated colour space 19 (CIEJab) shall not be used";
115115
public static final String LZWDECODE_FILTER_IS_NOT_PERMITTED = "LZWDecode filter is not permitted";
116+
public static final String MAXIMUM_ARRAY_CAPACITY_IS_EXCEEDED = "Maximum array capacity is exceeded";
117+
public static final String MAXIMUM_DICTIONARY_CAPACITY_IS_EXCEEDED = "Maximum dictionary capacity is exceeded";
116118
public static final String MIME_TYPE_SHALL_BE_SPECIFIED_USING_THE_SUBTYPE_KEY_OF_THE_FILE_SPECIFICATION_STREAM_DICTIONARY = "Mime type shall be specified using the subtype key of the file specification stream dictionary";
117119
public static final String N_KEY_SHALL_BE_APPEARANCE_SUBDICTIONARY = "If an annotation dictionary's Subtype key has a value of Widget and its FT key has a value of Btn, the value of the N key shall be an appearance subdictionary";
118120
public static final String NAMED_ACTION_TYPE_0_IS_NOT_ALLOWED = "Named action type {0} not allowed";

pdfa/src/main/java/com/itextpdf/pdfa/checker/PdfA1Checker.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,15 @@ protected void checkContentStreamObject(PdfObject object) {
309309
checkPdfString((PdfString) object);
310310
break;
311311
case PdfObject.ARRAY:
312-
for (PdfObject obj : (PdfArray) object) {
312+
PdfArray array = (PdfArray) object;
313+
checkPdfArray(array);
314+
for (PdfObject obj : array) {
313315
checkContentStreamObject(obj);
314316
}
315317
break;
316318
case PdfObject.DICTIONARY:
317319
PdfDictionary dictionary = (PdfDictionary) object;
320+
checkPdfDictionary(dictionary);
318321
for (PdfObject obj : dictionary.values()) {
319322
checkContentStreamObject(obj);
320323
}
@@ -451,8 +454,24 @@ protected double getMaxRealValue() {
451454
return 32767;
452455
}
453456

457+
@Override
458+
protected void checkPdfArray(PdfArray array) {
459+
if (array.size() > getMaxArrayCapacity()) {
460+
throw new PdfAConformanceException(PdfAConformanceException.MAXIMUM_ARRAY_CAPACITY_IS_EXCEEDED);
461+
}
462+
}
463+
464+
@Override
465+
protected void checkPdfDictionary(PdfDictionary dictionary) {
466+
if (dictionary.size() > getMaxDictionaryCapacity()) {
467+
throw new PdfAConformanceException(PdfAConformanceException.MAXIMUM_DICTIONARY_CAPACITY_IS_EXCEEDED);
468+
}
469+
}
470+
454471
@Override
455472
protected void checkPdfStream(PdfStream stream) {
473+
checkPdfDictionary(stream);
474+
456475
if (stream.containsKey(PdfName.F) || stream.containsKey(PdfName.FFilter) || stream.containsKey(PdfName.FDecodeParams)) {
457476
throw new PdfAConformanceException(PdfAConformanceException.STREAM_OBJECT_DICTIONARY_SHALL_NOT_CONTAIN_THE_F_FFILTER_OR_FDECODEPARAMS_KEYS);
458477
}
@@ -647,4 +666,12 @@ protected PdfArray getFormFields(PdfArray array) {
647666
}
648667
return fields;
649668
}
669+
670+
private int getMaxArrayCapacity() {
671+
return 8191;
672+
}
673+
674+
private int getMaxDictionaryCapacity() {
675+
return 4095;
676+
}
650677
}

pdfa/src/main/java/com/itextpdf/pdfa/checker/PdfA2Checker.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,15 @@ protected double getMaxRealValue() {
332332
protected int getMaxStringLength() {
333333
return 32767;
334334
}
335+
@Override
336+
protected void checkPdfArray(PdfArray array) {
337+
// currently no validation for arrays is implemented for PDF/A 2
338+
}
339+
340+
@Override
341+
protected void checkPdfDictionary(PdfDictionary dictionary) {
342+
// currently no validation for dictionaries is implemented for PDF/A 2
343+
}
335344

336345
@Override
337346
protected void checkAnnotation(PdfDictionary annotDic) {
@@ -547,6 +556,7 @@ protected void checkFileSpec(PdfDictionary fileSpec) {
547556

548557
@Override
549558
protected void checkPdfStream(PdfStream stream) {
559+
checkPdfDictionary(stream);
550560

551561
if (stream.containsKey(PdfName.F) || stream.containsKey(PdfName.FFilter) || stream.containsKey(PdfName.FDecodeParams)) {
552562
throw new PdfAConformanceException(PdfAConformanceException.STREAM_OBJECT_DICTIONARY_SHALL_NOT_CONTAIN_THE_F_FFILTER_OR_FDECODEPARAMS_KEYS);

pdfa/src/main/java/com/itextpdf/pdfa/checker/PdfAChecker.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,17 @@ public void checkPdfObject(PdfObject obj) {
202202
checkPdfString((PdfString) obj);
203203
break;
204204
case PdfObject.ARRAY:
205-
checkArrayRecursively((PdfArray) obj);
205+
PdfArray array = (PdfArray) obj;
206+
checkPdfArray(array);
207+
checkArrayRecursively(array);
206208
break;
207209
case PdfObject.DICTIONARY:
208210
PdfDictionary dict = (PdfDictionary) obj;
209211
PdfName type = dict.getAsName(PdfName.Type);
210212
if (PdfName.Filespec.equals(type)) {
211213
checkFileSpec(dict);
212214
}
215+
checkPdfDictionary(dict);
213216
checkDictionaryRecursively(dict);
214217
break;
215218
case PdfObject.STREAM:
@@ -415,6 +418,8 @@ protected void checkContentStreamObject(PdfObject object) {
415418
protected abstract void checkOutputIntents(PdfDictionary catalog);
416419
protected abstract void checkPageObject(PdfDictionary page, PdfDictionary pageResources);
417420
protected abstract void checkPageSize(PdfDictionary page);
421+
protected abstract void checkPdfArray(PdfArray array);
422+
protected abstract void checkPdfDictionary(PdfDictionary dictionary);
418423
protected abstract void checkPdfNumber(PdfNumber number);
419424
protected abstract void checkPdfStream(PdfStream stream);
420425
protected abstract void checkPdfString(PdfString string);

0 commit comments

Comments
 (0)