Skip to content

Commit 70aa604

Browse files
committed
fix: avoids prototype pollution
1 parent 70cffe5 commit 70aa604

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/defu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function _defu<T>(
1212
return _defu(baseObject, {}, namespace, merger);
1313
}
1414

15-
const object = Object.assign({}, defaults);
15+
const object = { ...defaults };
1616

1717
for (const key in baseObject) {
1818
if (key === "__proto__" || key === "constructor") {

test/defu.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ describe("defu", () => {
110110
defu({}, payload);
111111
defu(payload, {});
112112
defu(payload, payload);
113+
114+
const malicious = JSON.parse('{"__proto__":{"isAdmin":true}}');
115+
const result = defu(malicious, { isAdmin: false });
116+
117+
expect(result.isAdmin).toBe(false);
118+
113119
// @ts-ignore
114120
expect({}.isAdmin).toBe(undefined);
115121
});

0 commit comments

Comments
 (0)