Skip to content

Commit

Permalink
fixed prototype polution
Browse files Browse the repository at this point in the history
  • Loading branch information
llGaetanll committed Apr 6, 2024
1 parent 1cc97a0 commit 984ad92
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* prevent prototype polution */
Object.freeze(Object.prototype)

/* type checker */
const t = (d) =>
d instanceof Function
Expand Down
40 changes: 40 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,3 +1204,43 @@ describe("add", () => {
expect(obx.eq(a, res)).toBe(true);
});
});

describe("prototype polution", () => {
const BAD_JSON = JSON.parse('{"__proto__":{"polluted":true}}');

test("add", () => {
const victim = {};

try {
obx.add({}, BAD_JSON);
} catch (e) { }

expect(Object.keys(victim.__proto__).length).toBe(0);

delete Object.prototype.polluted;
});

test("cp", () => {
const victim = {};

try {
obx.cp({ "__proto__.polluted": true });
} catch (e) { }

expect(Object.keys(victim.__proto__).length).toBe(0);

delete Object.prototype.polluted;
})

test("set", () => {
const victim = {};

try {
obx.set({}, "__proto__.polluted", true);
} catch (e) { }

expect(Object.keys(victim.__proto__).length).toBe(0);

delete Object.prototype.polluted;
})
});

0 comments on commit 984ad92

Please sign in to comment.