Merge pull request #10412 from Snuffleupagus/issue-10410

Prevent errors, in `SimpleXMLParser.onEndElement`, when the stack has already been completely parsed (issue 10410)
This commit is contained in:
Tim van der Meij 2019-01-05 12:56:43 +01:00 committed by GitHub
commit a0eb5cf9d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -287,6 +287,9 @@ class SimpleDOMNode {
return undefined; return undefined;
} }
const index = childNodes.indexOf(this); const index = childNodes.indexOf(this);
if (index === -1) {
return undefined;
}
return childNodes[index + 1]; return childNodes[index + 1];
} }
@ -364,8 +367,11 @@ class SimpleXMLParser extends XMLParserBase {
} }
onEndElement(name) { onEndElement(name) {
this._currentFragment = this._stack.pop(); this._currentFragment = this._stack.pop() || [];
const lastElement = this._currentFragment[this._currentFragment.length - 1]; const lastElement = this._currentFragment[this._currentFragment.length - 1];
if (!lastElement) {
return;
}
for (let i = 0, ii = lastElement.childNodes.length; i < ii; i++) { for (let i = 0, ii = lastElement.childNodes.length; i < ii; i++) {
lastElement.childNodes[i].parentNode = lastElement; lastElement.childNodes[i].parentNode = lastElement;
} }

View File

@ -146,4 +146,26 @@ describe('metadata', function() {
expect(metadata.getAll()).toEqual({ 'dc:title': '\'Foo bar baz\'', }); expect(metadata.getAll()).toEqual({ 'dc:title': '\'Foo bar baz\'', });
}); });
it('should gracefully handle unbalanced end tags (issue 10410)', function() {
const data = '<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>' +
'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' +
'<rdf:Description rdf:about="" ' +
'xmlns:pdf="http://ns.adobe.com/pdf/1.3/">' +
'<pdf:Producer>Soda PDF 5</pdf:Producer></rdf:Description>' +
'<rdf:Description rdf:about="" ' +
'xmlns:xap="http://ns.adobe.com/xap/1.0/">' +
'<xap:CreateDate>2018-10-02T08:14:49-05:00</xap:CreateDate>' +
'<xap:CreatorTool>Soda PDF 5</xap:CreatorTool>' +
'<xap:MetadataDate>2018-10-02T08:14:49-05:00</xap:MetadataDate> ' +
'<xap:ModifyDate>2018-10-02T08:14:49-05:00</xap:ModifyDate>' +
'</rdf:Description><rdf:Description rdf:about="" ' +
'xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">' +
'<xmpMM:DocumentID>uuid:00000000-1c84-3cf9-89ba-bef0e729c831' +
'</xmpMM:DocumentID></rdf:Description>' +
'</rdf:RDF></x:xmpmeta><?xpacket end="w"?>';
const metadata = new Metadata(data);
expect(isEmptyObj(metadata.getAll())).toEqual(true);
});
}); });