Skip to content

Commit cfde1e3

Browse files
Copilothustcc
andauthored
fix: prevent prototype pollution in mix function
Agent-Logs-Url: https://github.com/antvis/util/sessions/7731b37f-784f-4d46-bfc5-ea4bf127da73 Co-authored-by: hustcc <7856674+hustcc@users.noreply.github.com>
1 parent 879be4a commit cfde1e3

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

__tests__/unit/lodash/mix.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import mix from '../../../src/lodash/mix';
2+
3+
describe('mix', () => {
4+
it('merges plain objects', () => {
5+
const result = mix({} as any, { a: 1 }, { b: 2 });
6+
expect(result).toEqual({ a: 1, b: 2 });
7+
});
8+
9+
it('does not pollute Object.prototype via __proto__', () => {
10+
const payload = JSON.parse('{"__proto__": {"polluted": true}}');
11+
mix({}, payload);
12+
expect((Object.prototype as any).polluted).toBeUndefined();
13+
});
14+
15+
it('does not pollute via constructor key', () => {
16+
const payload = JSON.parse('{"constructor": {"prototype": {"polluted": true}}}');
17+
mix({}, payload);
18+
expect((Object.prototype as any).polluted).toBeUndefined();
19+
});
20+
21+
it('does not pollute via prototype key', () => {
22+
mix({} as any, { prototype: { polluted: true } } as any);
23+
expect((Object.prototype as any).polluted).toBeUndefined();
24+
});
25+
});

src/lodash/mix.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
// FIXME: Mutable param should be forbidden in static lang.
22
function _mix<Base, Source>(dist: Base & Source, obj: Source): void {
33
for (const key in obj) {
4-
if (obj.hasOwnProperty(key) && key !== 'constructor' && obj[key] !== undefined) {
4+
// Prevent prototype pollution by skipping dangerous keys
5+
if (
6+
key === '__proto__' ||
7+
key === 'constructor' ||
8+
key === 'prototype'
9+
) {
10+
continue;
11+
}
12+
if (obj.hasOwnProperty(key) && obj[key] !== undefined) {
513
(<any>dist)[key] = obj[key];
614
}
715
}

0 commit comments

Comments
 (0)