Skip to content
Open
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
11 changes: 7 additions & 4 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1356,10 +1356,13 @@ export class Parser {
}

if (isHash) {
// defineProperty so that keys like "__proto__" become own properties
ctx.pushCode(
`Object.defineProperty(${lhs}, ${item}.${key}, { value: ${item}, enumerable: true, writable: true, configurable: true });`,
);
// Reject "__proto__" since assigning it would replace the prototype
const hashKey = ctx.generateTmpVariable();
ctx.pushCode(`var ${hashKey} = ${item}.${key};`);
ctx.pushCode(`if (${hashKey} === "__proto__") {`);
ctx.generateError(`"Invalid key: __proto__"`);
ctx.pushCode("}");
ctx.pushCode(`${lhs}[${hashKey}] = ${item};`);
} else {
ctx.pushCode(`${lhs}.push(${item});`);
}
Expand Down
12 changes: 2 additions & 10 deletions test/composite_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function compositeParserTests(
},
});
});
it("should store __proto__ key as an own property of associative arrays", () => {
it("should reject __proto__ key of associative arrays", () => {
const parser = Parser.start().array("a", {
type: Parser.start()
.uint8("l")
Expand All @@ -313,16 +313,8 @@ function compositeParserTests(
...Array.from("__proto__", (c) => c.charCodeAt(0)),
1,
]);
const result = parser.parse(buffer);

expect(Object.keys(result.a)).toStrictEqual(["__proto__"]);
expect(Object.getPrototypeOf(result.a)).toBe(Object.prototype);
expect(result.a.evil).toBeUndefined();
expect(result.a["__proto__"]).toStrictEqual({
l: 9,
k: "__proto__",
evil: 1,
});
expect(() => parser.parse(buffer)).toThrow("Invalid key: __proto__");
});
it("should use formatter to transform parsed array", () => {
const parser = Parser.start().array("data", {
Expand Down
Loading