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
36 changes: 36 additions & 0 deletions __tests__/fastMergeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,40 @@ describe('fastMerge', () => {

expect(result).toEqual(testObject);
});

it.each([true, false])('should ignore __proto__ keys from both objects, with null removal %s', (removeNulls) => {
const target = JSON.parse('{"__proto__":{"fromTarget":true},"name":"a","nested":{"__proto__":{"fromTarget":true},"y":2}}');
const source = JSON.parse('{"__proto__":{"isAdmin":true},"role":"user","nested":{"__proto__":{"isAdmin":true},"x":1}}');
const result = fastMerge(target, source, removeNulls);

expect(Object.getPrototypeOf(result)).toBe(Object.prototype);
expect(Object.getPrototypeOf(result.nested)).toBe(Object.prototype);
expect(result.isAdmin).toBeUndefined();
expect(result.fromTarget).toBeUndefined();
expect(result.nested.isAdmin).toBeUndefined();
expect(result.nested.fromTarget).toBeUndefined();
expect(result).toEqual({name: 'a', role: 'user', nested: {x: 1, y: 2}});
expect({}.isAdmin).toBeUndefined();
});

it.each([
[JSON.parse('{"__proto__":{"injected":true}}'), {}],
[{}, JSON.parse('{"__proto__":{"injected":true}}')],
[null, JSON.parse('{"__proto__":{"injected":true}}')],
])('should ignore __proto__ on either side of a merge', (target, source) => {
const result = fastMerge(target, source);

expect(Object.getPrototypeOf(result)).toBe(Object.prototype);
expect(result.injected).toBeUndefined();
expect(Object.prototype.hasOwnProperty.call(result, '__proto__')).toBe(false);
});

it('should preserve constructor and prototype keys as own data properties', () => {
const result = fastMerge({a: 1}, JSON.parse('{"constructor":{"prototype":{"value":true}},"prototype":"p"}'));

expect(Object.prototype.hasOwnProperty.call(result, 'constructor')).toBe(true);
expect(result.constructor).toEqual({prototype: {value: true}});
expect(result.prototype).toBe('p');
expect({}.value).toBeUndefined();
});
});
8 changes: 6 additions & 2 deletions lib/fastMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ function isMergeableObject(value: unknown): value is Record<string, unknown> {
return nonNullObject && Object.prototype.toString.call(value) !== '[object RegExp]' && Object.prototype.toString.call(value) !== '[object Date]' && !Array.isArray(value);
}

function isUnsafeKey(key: string): boolean {
return key === '__proto__';
}

/**
* Merges two objects and removes null values if "shouldRemoveNullObjectValues" is set to true
*
Expand Down Expand Up @@ -40,7 +44,7 @@ function fastMerge<TObject extends Record<string, unknown>>(target: TObject, sou
const isSourceOrTargetNull = targetValue === null || sourceValue === null;
const shouldOmitSourceKey = shouldRemoveNullObjectValues && isSourceOrTargetNull;

if (!shouldOmitSourceKey) {
if (!shouldOmitSourceKey && !isUnsafeKey(key)) {
destination[key] = targetValue;
}
}
Expand All @@ -59,7 +63,7 @@ function fastMerge<TObject extends Record<string, unknown>>(target: TObject, sou
// If we pass undefined as the updated value for a key, we want to generally ignore it
const isSourceKeyUndefined = sourceValue === undefined;

if (!isSourceKeyUndefined && !shouldOmitSourceKey) {
if (!isSourceKeyUndefined && !shouldOmitSourceKey && !isUnsafeKey(key)) {
const isSourceKeyMergable = isMergeableObject(sourceValue);

if (isSourceKeyMergable && targetValue) {
Expand Down
Loading