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,
});
Comment on lines +25 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same thing as

Suggested change
const o = {
foo: "bar",
};
// override constructor as an enumerable property
Object.defineProperty(o, "constructor", {
value: new Date(0),
enumerable: true,
});
const o = {
foo: "bar",
constructor: new Date(0),
};

const encoded = encode(o);

throws(() => {
decode(encoded);
}, DecodeError);
Comment on lines +35 to +37

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of just testing that it fails to decode, you should've instead tested that, even in the hostile environment where you've imagined a current security issue, the library does not trigger it.

});
});

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);
});
});
});