Add stricter validation in Catalog.readPageLabels

The current PageLabel dictionary validation code won't catch some (unlikely) forms of corruption. For example: a `Type`/`S` entry being `null`/`0`/empty string, a `P`/`St` entry being `null`/`0`.

Please note: I'm not aware of any bugs caused by the old code, but I've had this patch sitting locally for some time and figured it couldn't hurt to submit it.
This commit is contained in:
Jonas Jenwald 2018-03-21 14:23:51 +01:00
parent b67f117b56
commit 374d074f6e

View File

@ -263,33 +263,45 @@ var Catalog = (function CatalogClosure() {
for (var i = 0, ii = this.numPages; i < ii; i++) {
if (i in nums) {
var labelDict = nums[i];
const labelDict = nums[i];
if (!isDict(labelDict)) {
throw new FormatError('The PageLabel is not a dictionary.');
}
var type = labelDict.get('Type');
if (type && !isName(type, 'PageLabel')) {
if (labelDict.has('Type') &&
!isName(labelDict.get('Type'), 'PageLabel')) {
throw new FormatError('Invalid type in PageLabel dictionary.');
}
var s = labelDict.get('S');
if (s && !isName(s)) {
if (labelDict.has('S')) {
const s = labelDict.get('S');
if (!isName(s)) {
throw new FormatError('Invalid style in PageLabel dictionary.');
}
style = s ? s.name : null;
style = s.name;
} else {
style = null;
}
var p = labelDict.get('P');
if (p && !isString(p)) {
if (labelDict.has('P')) {
const p = labelDict.get('P');
if (!isString(p)) {
throw new FormatError('Invalid prefix in PageLabel dictionary.');
}
prefix = p ? stringToPDFString(p) : '';
prefix = stringToPDFString(p);
} else {
prefix = '';
}
var st = labelDict.get('St');
if (st && !(Number.isInteger(st) && st >= 1)) {
if (labelDict.has('St')) {
const st = labelDict.get('St');
if (!(Number.isInteger(st) && st >= 1)) {
throw new FormatError('Invalid start in PageLabel dictionary.');
}
currentIndex = st || 1;
currentIndex = st;
} else {
currentIndex = 1;
}
}
switch (style) {
@ -320,10 +332,10 @@ var Catalog = (function CatalogClosure() {
throw new FormatError(
`Invalid style "${style}" in PageLabel dictionary.`);
}
}
pageLabels[i] = prefix + currentLabel;
currentLabel = '';
}
pageLabels[i] = prefix + currentLabel;
currentIndex++;
}
return pageLabels;