Skip to content
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
4 changes: 2 additions & 2 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ export class Decoder<ContextType = undefined> {
continue DECODE;
}
} else if (state.type === STATE_MAP_KEY) {
if (object === "__proto__") {
throw new DecodeError("The key __proto__ is not allowed");
if (object === "__proto__" || object === "constructor" || object === "prototype") {
throw new DecodeError(`The key ${object} is not allowed`);
}

state.key = this.mapKeyConverter(object);
Expand Down
36 changes: 36 additions & 0 deletions test/prototype-pollution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,40 @@ describe("prototype pollution", () => {
}, DecodeError);
});
});

context("constructor exists as a map key", () => {
it("raises DecodeError in decoding", () => {
const o = {
foo: "bar",
};
// override constructor as an enumerable property
Object.defineProperty(o, "constructor", {
value: new Date(0),
enumerable: true,
});
const encoded = encode(o);

throws(() => {
decode(encoded);
}, DecodeError);
});
});

context("prototype exists as a map key", () => {
it("raises DecodeError in decoding", () => {
const o = {
foo: "bar",
};
// override prototype as an enumerable property
Object.defineProperty(o, "prototype", {
value: new Date(0),
enumerable: true,
});
const encoded = encode(o);

throws(() => {
decode(encoded);
}, DecodeError);
});
});
});