diff --git a/src/core/obj.js b/src/core/obj.js index eee8eae11..89dfa4df3 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -1260,7 +1260,7 @@ var XRef = (function XRefClosure() { this.readXRef(/* recoveryMode */ true); } // finding main trailer - var dict; + let trailerDict; for (i = 0, ii = trailers.length; i < ii; ++i) { stream.pos = trailers[i]; var parser = new Parser(new Lexer(stream), /* allowStreams = */ true, @@ -1270,18 +1270,33 @@ var XRef = (function XRefClosure() { continue; } // read the trailer dictionary - dict = parser.getObj(); + let dict = parser.getObj(); if (!isDict(dict)) { continue; } + // Do some basic validation of the trailer/root dictionary candidate. + let rootDict; + try { + rootDict = dict.get('Root'); + } catch (ex) { + if (ex instanceof MissingDataException) { + throw ex; + } + continue; + } + if (!isDict(rootDict) || !rootDict.has('Pages')) { + continue; + } // taking the first one with 'ID' if (dict.has('ID')) { return dict; } + // The current dictionary is a candidate, but continue searching. + trailerDict = dict; } - // no tailer with 'ID', taking last one (if exists) - if (dict) { - return dict; + // No trailer with 'ID', taking last one (if exists). + if (trailerDict) { + return trailerDict; } // nothing helps throw new InvalidPDFException('Invalid PDF structure'); diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index 164de3e75..1d643cb1d 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -68,6 +68,7 @@ !issue9252.pdf !issue9262_reduced.pdf !issue9291.pdf +!issue9418.pdf !issue9458.pdf !bad-PageLabels.pdf !decodeACSuccessive.pdf diff --git a/test/pdfs/issue9418.pdf b/test/pdfs/issue9418.pdf new file mode 100644 index 000000000..c52cde328 Binary files /dev/null and b/test/pdfs/issue9418.pdf differ diff --git a/test/test_manifest.json b/test/test_manifest.json index e9092caec..e80912ad4 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -748,6 +748,13 @@ "link": false, "type": "eq" }, + { "id": "issue9418", + "file": "pdfs/issue9418.pdf", + "md5": "32ecad8098acb1938539d47944ecb54b", + "rounds": 1, + "link": false, + "type": "eq" + }, { "id": "issue9262", "file": "pdfs/issue9262_reduced.pdf", "md5": "5347ce2d7b3866625c22e115fd90e0de",