From c2f7942aea82fbbde79abf36d1f5a6fa323bad9b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 8 Jul 2022 12:06:25 +0200 Subject: [PATCH] Ensure that the /Resources-entry is actually a dictionary (issue 15150) Prevent issues in *corrupt* PDF documents, if the /Resources-entry is not of the correct and expected type. --- src/core/document.js | 4 +++- test/pdfs/.gitignore | 1 + test/pdfs/issue15150.pdf | 24 ++++++++++++++++++++++++ test/unit/api_spec.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 test/pdfs/issue15150.pdf diff --git a/src/core/document.js b/src/core/document.js index c89078d90..2d6328af4 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -132,10 +132,12 @@ class Page { // For robustness: The spec states that a \Resources entry has to be // present, but can be empty. Some documents still omit it; in this case // we return an empty dictionary. + const resources = this._getInheritableProperty("Resources"); + return shadow( this, "resources", - this._getInheritableProperty("Resources") || Dict.empty + resources instanceof Dict ? resources : Dict.empty ); } diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index f71f69d37..2c0abecc0 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -509,6 +509,7 @@ !poppler-67295-0.pdf !poppler-85140-0.pdf !issue15012.pdf +!issue15150.pdf !poppler-395-0-fuzzed.pdf !GHOSTSCRIPT-698804-1-fuzzed.pdf !issue14814.pdf diff --git a/test/pdfs/issue15150.pdf b/test/pdfs/issue15150.pdf new file mode 100644 index 000000000..c332caf2b --- /dev/null +++ b/test/pdfs/issue15150.pdf @@ -0,0 +1,24 @@ +%PDF-1.4 +1 0 obj <> +endobj +2 0 obj <> +endobj +3 0 obj<> +endobj +4 0 obj +<> +stream +0.5 w 1 0 0 RG 0 9.75 m 0.5 9.75 l s +endstream +endobj +xref +0 5 +0000000000 65535 f +0000000009 00000 n +0000000056 00000 n +0000000111 00000 n +0000000210 00000 n +trailer <> +startxref +294 +%%EOF diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index e9eb7c59e..f932ec378 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -673,6 +673,38 @@ describe("api", function () { await Promise.all([loadingTask1.destroy(), loadingTask2.destroy()]); }); + + it("creates pdf doc from PDF file with bad /Resources entry", async function () { + const loadingTask = getDocument(buildGetDocumentParams("issue15150.pdf")); + expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true); + + const pdfDocument = await loadingTask.promise; + expect(pdfDocument.numPages).toEqual(1); + + const page = await pdfDocument.getPage(1); + expect(page instanceof PDFPageProxy).toEqual(true); + + const opList = await page.getOperatorList(); + expect(opList.fnArray).toEqual([ + OPS.setLineWidth, + OPS.setStrokeRGBColor, + OPS.constructPath, + OPS.closeStroke, + ]); + expect(opList.argsArray).toEqual([ + [0.5], + new Uint8ClampedArray([255, 0, 0]), + [ + [OPS.moveTo, OPS.lineTo], + [0, 9.75, 0.5, 9.75], + [0, 0.5, 9.75, 9.75], + ], + null, + ]); + expect(opList.lastChunk).toEqual(true); + + await loadingTask.destroy(); + }); }); describe("PDFWorker", function () {