Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that Dict.set only accepts string keys #14597

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ class PDFDocument {
} else {
info(`Bad value in document info for "${key}".`);
}
} else if (typeof key === "string") {
} else {
// For custom values, only accept white-listed types to prevent
// errors that would occur when trying to send non-serializable
// objects to the main-thread (for example `Dict` or `Stream`).
Expand Down
11 changes: 7 additions & 4 deletions src/core/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,14 @@ class Dict {

set(key, value) {
if (
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")) &&
value === undefined
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
getVerbosityLevel,
info,
InvalidPDFException,
isString,
MissingPDFException,
PasswordException,
setVerbosityLevel,
Expand Down Expand Up @@ -639,7 +638,7 @@ class WorkerMessageHandler {
const xrefInfo = xref.trailer.get("Info") || null;
if (xrefInfo instanceof Dict) {
xrefInfo.forEach((key, value) => {
if (isString(key) && isString(value)) {
if (typeof value === "string") {
infoObj[key] = stringToPDFString(value);
}
});
Expand Down
11 changes: 11 additions & 0 deletions test/unit/primitives_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ describe("primitives", function () {
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 () {
const dict = new Dict();
expect(function () {
Expand Down