From 346810e02af17e2991255543e8ec1e99530237e9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 19 Jun 2018 11:04:30 +0200 Subject: [PATCH] Add basic validation of the 'Root' dictionary in `XRef.parse` and try to recover when possible Note that the `Catalog` constructor, and some of its methods, are already enforcing that the 'Root' dictionary is valid/well-formed. However, by doing additional validation already in `XRef.parse` there's a slightly larger chance that corrupt PDF files could be successfully parsed/rendered. --- src/core/obj.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/core/obj.js b/src/core/obj.js index 620efbf4e..eee8eae11 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -877,8 +877,22 @@ var XRef = (function XRefClosure() { this.pdfManager.password); } - // get the root dictionary (catalog) object - if (!(this.root = trailerDict.get('Root'))) { + // Get the root dictionary (catalog) object, and do some basic validation. + let root; + try { + root = trailerDict.get('Root'); + } catch (ex) { + if (ex instanceof MissingDataException) { + throw ex; + } + warn(`XRef.parse - Invalid "Root" reference: "${ex}".`); + } + if (isDict(root) && root.has('Pages')) { + this.root = root; + } else { + if (!recoveryMode) { + throw new XRefParseException(); + } throw new FormatError('Invalid root reference'); } },