Skip to content

Commit 0808376

Browse files
Merge pull request #14599 from Snuffleupagus/Cmd-Name-validate-arg
Ensure that `Cmd`/`Name` is only initialized with string arguments
2 parents 4157d77 + ec87995 commit 0808376

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/core/cmap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ const CMapFactory = (function CMapFactoryClosure() {
901901

902902
function parseCMapName(cMap, lexer) {
903903
const obj = lexer.getObj();
904-
if (obj instanceof Name && isString(obj.name)) {
904+
if (obj instanceof Name) {
905905
cMap.name = obj.name;
906906
}
907907
}

src/core/primitives.js

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ const Name = (function NameClosure() {
2424
// eslint-disable-next-line no-shadow
2525
class Name {
2626
constructor(name) {
27+
if (
28+
(typeof PDFJSDev === "undefined" ||
29+
PDFJSDev.test("!PRODUCTION || TESTING")) &&
30+
typeof name !== "string"
31+
) {
32+
unreachable('Name: The "name" must be a string.');
33+
}
2734
this.name = name;
2835
}
2936

@@ -47,6 +54,13 @@ const Cmd = (function CmdClosure() {
4754
// eslint-disable-next-line no-shadow
4855
class Cmd {
4956
constructor(cmd) {
57+
if (
58+
(typeof PDFJSDev === "undefined" ||
59+
PDFJSDev.test("!PRODUCTION || TESTING")) &&
60+
typeof cmd !== "string"
61+
) {
62+
unreachable('Cmd: The "cmd" must be a string.');
63+
}
5064
this.cmd = cmd;
5165
}
5266

test/unit/primitives_spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ describe("primitives", function () {
5555
expect(firstEmpty).toBe(secondEmpty);
5656
expect(firstEmpty).not.toBe(normalName);
5757
});
58+
59+
it("should not accept to create a non-string name", function () {
60+
expect(function () {
61+
Name.get(123);
62+
}).toThrow(new Error('Name: The "name" must be a string.'));
63+
});
5864
});
5965

6066
describe("Cmd", function () {
@@ -74,6 +80,12 @@ describe("primitives", function () {
7480
expect(firstET).toBe(secondET);
7581
expect(firstBT).not.toBe(firstET);
7682
});
83+
84+
it("should not accept to create a non-string cmd", function () {
85+
expect(function () {
86+
Cmd.get(123);
87+
}).toThrow(new Error('Cmd: The "cmd" must be a string.'));
88+
});
7789
});
7890

7991
describe("Dict", function () {

0 commit comments

Comments
 (0)