Merge pull request #14597 from Snuffleupagus/Dict-set-validate-key

Ensure that `Dict.set` only accepts string `key`s
This commit is contained in:
Tim van der Meij 2022-02-23 20:31:36 +01:00 committed by GitHub
commit 409cbfc817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 7 deletions

View File

@ -1187,7 +1187,7 @@ class PDFDocument {
} else { } else {
info(`Bad value in document info for "${key}".`); info(`Bad value in document info for "${key}".`);
} }
} else if (typeof key === "string") { } else {
// For custom values, only accept white-listed types to prevent // For custom values, only accept white-listed types to prevent
// errors that would occur when trying to send non-serializable // errors that would occur when trying to send non-serializable
// objects to the main-thread (for example `Dict` or `Stream`). // objects to the main-thread (for example `Dict` or `Stream`).

View File

@ -198,11 +198,14 @@ class Dict {
set(key, value) { set(key, value) {
if ( if (
(typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")) && PDFJSDev.test("!PRODUCTION || TESTING")
value === undefined
) { ) {
unreachable('Dict.set: The "value" cannot be undefined.'); if (typeof key !== "string") {
unreachable('Dict.set: The "key" must be a string.');
} else if (value === undefined) {
unreachable('Dict.set: The "value" cannot be undefined.');
}
} }
this._map[key] = value; this._map[key] = value;
} }

View File

@ -21,7 +21,6 @@ import {
getVerbosityLevel, getVerbosityLevel,
info, info,
InvalidPDFException, InvalidPDFException,
isString,
MissingPDFException, MissingPDFException,
PasswordException, PasswordException,
setVerbosityLevel, setVerbosityLevel,
@ -639,7 +638,7 @@ class WorkerMessageHandler {
const xrefInfo = xref.trailer.get("Info") || null; const xrefInfo = xref.trailer.get("Info") || null;
if (xrefInfo instanceof Dict) { if (xrefInfo instanceof Dict) {
xrefInfo.forEach((key, value) => { xrefInfo.forEach((key, value) => {
if (isString(key) && isString(value)) { if (typeof value === "string") {
infoObj[key] = stringToPDFString(value); infoObj[key] = stringToPDFString(value);
} }
}); });

View File

@ -149,6 +149,17 @@ describe("primitives", function () {
checkInvalidKeyValues(dictWithSizeKey); checkInvalidKeyValues(dictWithSizeKey);
}); });
it("should not accept to set a non-string key", function () {
const dict = new Dict();
expect(function () {
dict.set(123, "val");
}).toThrow(new Error('Dict.set: The "key" must be a string.'));
expect(dict.has(123)).toBeFalsy();
checkInvalidKeyValues(dict);
});
it("should not accept to set a key with an undefined value", function () { it("should not accept to set a key with an undefined value", function () {
const dict = new Dict(); const dict = new Dict();
expect(function () { expect(function () {