From ad5ed370597fcbdcc8efc925a6e7c9dd6729f2a3 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 13 Dec 2017 14:22:39 +0100 Subject: [PATCH] Handle broken, Ghostscript generated, Metadata that contains HTML character names (bug 1424938) Please note that while this could be considered a regression in user-facing behaviour, I'm not convinced that it's really a regression as such since prior to PR 8912 the Metadata would fail to parse (with an XML error) and thus be ignored when setting the viewer title. With the refactored Metadata parsing we're now able to parse this, which uncovered issues with a subset of broken Ghostscript Metadata that uses HTML character names. Fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1424938 --- src/display/metadata.js | 14 ++++++++++++ test/unit/metadata_spec.js | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/display/metadata.js b/src/display/metadata.js index 3ef5c7d67..af58fb5c3 100644 --- a/src/display/metadata.js +++ b/src/display/metadata.js @@ -37,6 +37,20 @@ class Metadata { let bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function(code, d1, d2, d3) { return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1); + }).replace(/&(amp|apos|gt|lt|quot);/g, function(str, name) { + switch (name) { + case 'amp': + return '&'; + case 'apos': + return '\''; + case 'gt': + return '>'; + case 'lt': + return '<'; + case 'quot': + return '\"'; + } + throw new Error(`_repair: ${name} isn't defined.`); }); let chars = ''; diff --git a/test/unit/metadata_spec.js b/test/unit/metadata_spec.js index f7fa947fa..e3c136a46 100644 --- a/test/unit/metadata_spec.js +++ b/test/unit/metadata_spec.js @@ -49,4 +49,51 @@ describe('metadata', function() { expect(metadata.getAll()).toEqual({ 'dc:title': 'PDF&', }); }); + + it('should repair and handle invalid metadata (bug 1424938)', function() { + let invalidData = '' + + '' + + '' + + '' + + '\\376\\377\\000P\\000D\\000F\\000C\\000r\\000e\\000a' + + '\\000t\\000o\\000r\\000 \\000V\\000e\\000r\\000s\\000i\\000o\\000n' + + '\\000 \\0000\\000.\\0009\\000.\\0006' + + '' + + '' + + '\\376\\377\\000L\\000'\\000O\\000d' + + '\\000i\\000s\\000s\\000e\\000e\\000 \\000t\\000h\\000\\351\\000m\\000a' + + '\\000t\\000i\\000q\\000u\\000e\\000 \\000l\\000o\\000g\\000o\\000 ' + + '\\000O\\000d\\000i\\000s\\000s\\000\\351\\000\\351\\000 \\000-\\000 ' + + '\\000d\\000\\351\\000c\\000e\\000m\\000b\\000r\\000e\\000 \\0002\\0000' + + '\\0000\\0008\\000.\\000p\\000u\\000b' + + '\\376\\377\\000O\\000D\\000I\\000S' + + '' + + ''; + let metadata = new Metadata(invalidData); + + expect(metadata.has('dc:title')).toBeTruthy(); + expect(metadata.has('dc:qux')).toBeFalsy(); + + expect(metadata.get('dc:title')).toEqual( + 'L\'Odissee thématique logo Odisséé - décembre 2008.pub'); + expect(metadata.get('dc:qux')).toEqual(null); + + expect(metadata.getAll()).toEqual({ + 'dc:creator': 'ODIS', + 'dc:title': 'L\'Odissee thématique logo Odisséé - décembre 2008.pub', + 'xap:creatortool': 'PDFCreator Version 0.9.6', + }); + }); });