Skip to content

Commit 05ca694

Browse files
committed
Fixing GHSA-hpqf-m68j-2pfx Prototype Pollution
Signed-off-by: Charlie Fish <[email protected]>
1 parent 8408f3d commit 05ca694

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/set.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import {GeneralObject, GeneralObjectOrValue} from "./types";
22

33
export = <T>(object: GeneralObject<T>, key: string, value: any): GeneralObject<T> => {
44
const keyParts = key.split(".");
5+
6+
// Protect against prototype pollution
7+
if (keyParts.includes("__proto__") || keyParts.includes("constructor")) {
8+
// If the key is __proto__ or constructor, return the object and do nothing since this is a security risk.
9+
return object;
10+
}
11+
512
let objectRef: GeneralObjectOrValue<T> = object;
613
keyParts.forEach((part: string | number, index: number) => {
714
if (keyParts.length - 1 === index) {
@@ -16,9 +23,7 @@ export = <T>(object: GeneralObject<T>, key: string, value: any): GeneralObject<T
1623
});
1724

1825
const finalKey: string = keyParts[keyParts.length - 1];
19-
if (finalKey !== "__proto__" && finalKey !== "constructor") {
20-
objectRef[finalKey] = value;
21-
}
26+
objectRef[finalKey] = value;
2227

2328
return object;
2429
};

test/set.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ describe("utils.set", () => {
5454
expect(utils.set(...test.input)).to.eql(test.output);
5555
});
5656
});
57+
58+
it("Should protect against prototype pollution when using Reflect.apply for __proto__", () => {
59+
let obj = {};
60+
expect(JSON.stringify({}.__proto__)).to.eql("{}");
61+
try {
62+
// for multiple functions, uncomment only one for each execution.
63+
Reflect.apply(utils.set, {}, [obj, "__proto__.pollutedKey", 123]);
64+
} catch (e) {
65+
expect(e).to.not.exist();
66+
}
67+
expect(JSON.stringify({}.__proto__)).to.eql("{}");
68+
expect(Object.prototype.pollutedKey).to.be.undefined;
69+
delete Object.prototype.pollutedKey;
70+
});
5771
});

0 commit comments

Comments
 (0)