Add more validation in the PDFDocument._hasOnlyDocumentSignatures method

If this method is ever passed invalid/unexpected data, or if during the course of parsing (since it's used recursively) such data is found, it will fail in a non-graceful way.
Hence this patch, which ensures that we don't attempt to access non-existent properties and also that errors such as the one fixed in PR 12479 wouldn't have occured.
This commit is contained in:
Jonas Jenwald 2020-10-16 12:57:01 +02:00
parent 3351d3476d
commit 29af15f37e

View File

@ -687,8 +687,15 @@ class PDFDocument {
*/ */
_hasOnlyDocumentSignatures(fields, recursionDepth = 0) { _hasOnlyDocumentSignatures(fields, recursionDepth = 0) {
const RECURSION_LIMIT = 10; const RECURSION_LIMIT = 10;
if (!Array.isArray(fields)) {
return false;
}
return fields.every(field => { return fields.every(field => {
field = this.xref.fetchIfRef(field); field = this.xref.fetchIfRef(field);
if (!(field instanceof Dict)) {
return false;
}
if (field.has("Kids")) { if (field.has("Kids")) {
if (++recursionDepth > RECURSION_LIMIT) { if (++recursionDepth > RECURSION_LIMIT) {
warn("_hasOnlyDocumentSignatures: maximum recursion depth reached"); warn("_hasOnlyDocumentSignatures: maximum recursion depth reached");
@ -937,6 +944,9 @@ class PDFDocument {
: clearPrimitiveCaches(); : clearPrimitiveCaches();
} }
/**
* @private
*/
_collectFieldObjects(name, fieldRef, promises) { _collectFieldObjects(name, fieldRef, promises) {
const field = this.xref.fetchIfRef(fieldRef); const field = this.xref.fetchIfRef(fieldRef);
if (field.has("T")) { if (field.has("T")) {