Skip to content

Commit 1974436

Browse files
Snuffleupaguspull[bot]
authored andcommitted
Prevent circular references in Type3 fonts
In corrupt PDF documents Type3 fonts may introduce circular dependencies, thus resulting in the affected font(s) never loading and parsing/rendering never completing. Note that I've not seen any real-world examples of this kind of font corruption, but the attached PDF document was rather found in https://github.com/pdf-association/safedocs/tree/main/Miscellaneous%20Targeted%20Test%20PDFs *Please note:* That repository contains a number of reduced test-cases that are specifically intended to test interoperability (between PDF viewer) and parsing/rendering for various kinds of strange/corrupt PDF documents. Some of the test-cases found there may thus not make sense to try and "fix" upfront, in my opinion, unless the problems are also found in real-world PDF documents.
1 parent fb1f054 commit 1974436

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

src/core/evaluator.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,10 @@ class PartialEvaluator {
11551155
}
11561156
}
11571157

1158+
if (this.parsingType3Font && this.type3FontRefs.has(fontRef)) {
1159+
return errorFont();
1160+
}
1161+
11581162
if (this.fontCache.has(fontRef)) {
11591163
return this.fontCache.get(fontRef);
11601164
}
@@ -4183,6 +4187,12 @@ class TranslatedFont {
41834187
// make sense to only be able to render a Type3 glyph partially.
41844188
const type3Evaluator = evaluator.clone({ ignoreErrors: false });
41854189
type3Evaluator.parsingType3Font = true;
4190+
// Prevent circular references in Type3 fonts.
4191+
const type3FontRefs = new RefSet(evaluator.type3FontRefs);
4192+
if (this.dict.objId && !type3FontRefs.has(this.dict.objId)) {
4193+
type3FontRefs.put(this.dict.objId);
4194+
}
4195+
type3Evaluator.type3FontRefs = type3FontRefs;
41864196

41874197
const translatedFont = this.font,
41884198
type3Dependencies = this.type3Dependencies;

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
!issue7339_reduced.pdf
219219
!issue3438.pdf
220220
!issue11403_reduced.pdf
221+
!ContentStreamCycleType3insideType3.pdf
221222
!issue2074.pdf
222223
!scan-bad.pdf
223224
!issue13561_reduced.pdf
2.38 KB
Binary file not shown.

test/test_manifest.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4537,6 +4537,12 @@
45374537
"lastPage": 3,
45384538
"type": "eq"
45394539
},
4540+
{ "id": "ContentStreamCycleType3insideType3",
4541+
"file": "pdfs/ContentStreamCycleType3insideType3.pdf",
4542+
"md5": "64840fc5dd8ca06afbc86477d9121b59",
4543+
"rounds": 1,
4544+
"type": "eq"
4545+
},
45404546
{ "id": "issue3458.pdf",
45414547
"file": "pdfs/issue3458.pdf",
45424548
"md5": "dab8bd3ad1acfc8dc82a8381a3c8ff94",

0 commit comments

Comments
 (0)