From 8ec99b200c5c149480b0834f98e70f3f5dab3dce Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 17 Jul 2018 23:18:31 +0200 Subject: [PATCH] Prevent Metadata/XML parsing from breaking `PDFDocumentProxy.getMetadata` when no XML root document is found (issue 8884) With the new XML parser, see PR 9573, the referenced PDF file now causes `getMetadata` to fail when incomplete XML tags are encountered. This provides a simple, and hopefully generally useful, work-around that may also help prevent future bugs. (Without being able to reproduce nor even understand the other (non XML) errors mentioned in issue 8884, I'd say that this patch is enough to close that one as fixed.) --- src/display/xml_parser.js | 3 +++ test/unit/metadata_spec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/display/xml_parser.js b/src/display/xml_parser.js index 207f928a6e3b1..1cbb019c51306 100644 --- a/src/display/xml_parser.js +++ b/src/display/xml_parser.js @@ -321,6 +321,9 @@ class SimpleXMLParser extends XMLParserBase { // We should only have one root. const [documentElement] = this._currentFragment; + if (!documentElement) { + return undefined; // Return undefined if no root was found. + } return { documentElement, }; } diff --git a/test/unit/metadata_spec.js b/test/unit/metadata_spec.js index e3c136a468060..e818d93e5e4c4 100644 --- a/test/unit/metadata_spec.js +++ b/test/unit/metadata_spec.js @@ -13,6 +13,7 @@ * limitations under the License. */ +import { isEmptyObj } from '../../src/shared/util'; import { Metadata } from '../../src/display/metadata'; describe('metadata', function() { @@ -96,4 +97,34 @@ describe('metadata', function() { 'xap:creatortool': 'PDFCreator Version 0.9.6', }); }); + + it('should gracefully handle incomplete tags (issue 8884)', function() { + let data = '' + + '' + + '' + + '' + + '' + + '2010-03-25T11:20:09-04:00' + + '2010-03-25T11:20:09-04:00' + + '2010-03-25T11:20:09-04:00' + + '' + + '' + + 'application/pdf' + + '' + + '' + + '1' + + 'A' + + '' + + '' + + '' + + ''; + let metadata = new Metadata(data); + + expect(isEmptyObj(metadata.getAll())).toEqual(true); + }); });