[BUGFIX] Prototype pollution via unsafe object merge#7
Open
GAURAV-1313 wants to merge 1 commit into
Open
Conversation
✅ Deploy Preview for nofriction canceled.
|
Owner
Author
AI Code ReviewSummaryThe PR adds a simple object merging function that performs a shallow copy of source properties into a target object. The method lacks input validation and does not guard against potential prototype pollution or mutation hazards, presenting a moderate risk if used unguarded in production. Key FindingsBugs
Security
Recommended FixesGeneral
Security Fixes
Suggested Tests
const { mergeObjects } = require('./merge');
describe('mergeObjects', () => {
let target;
beforeEach(() => {
target = { a: 1, b: 2 };
});
test('should merge properties from source into target (happy path)', () => {
const source = { b: 3, c: 4 };
const result = mergeObjects(target, source);
expect(result).toEqual({ a: 1, b: 3, c: 4 });
expect(result).toBe(target); // returns the original target object
});
test('should handle empty source object without modifying target', () => {
const source = {};
const originalTarget = { ...target };
const result = mergeObjects(target, source);
expect(result).toEqual(originalTarget);
expect(result).toBe(target);
});
test('should handle source with a single key', () => {
const source = { c: 5 };
const result = mergeObjects(target, source);
expect(result).toEqual({ a: 1, b: 2, c: 5 });
});
test('should overwrite existing target keys with source keys', () => {
const source = { a: 100, b: 200 };
const result = mergeObjects(target, source);
expect(result).toEqual({ a: 100, b: 200 });
});
test('should handle source with keys having undefined or null values', () => {
const source = { a: undefined, d: null };
const result = mergeObjects(target, source);
expect(result).toEqual({ a: undefined, b: 2, d: null });
});
test('should handle source with numeric and symbol keys', () => {
const symbolKey = Symbol('sym');
const source = { 42: 'numberKey', [symbolKey]: 'symbolValue' };
const result = mergeObjects(target, source);
expect(result[42]).toBe('numberKey');
expect(result[symbolKey]).toBe('symbolValue');
});
test('should copy all enumerable own properties including inherited enumerable properties if any', () => {
const parent = { inherited: 'yes' };
const source = Object.create(parent);
source.own = 'ownValue';
// for..in includes inherited enumerable keys
const result = mergeObjects({}, source);
expect(result).toHaveProperty('own', 'ownValue');
expect(result).toHaveProperty('inherited', 'yes');
});
test('should not mutate the source object', () => {
const source = { c: 4 };
const sourceCopy = { ...source };
mergeObjects(target, source);
expect(source).toEqual(sourceCopy);
});
test('should throw if target is null or undefined', () => {
expect(() => mergeObjects(null, { a: 1 })).toThrow(TypeError);
expect(() => mergeObjects(undefined, { a: 1 })).toThrow(TypeError);
});
test('should throw if source is null or undefined', () => {
expect(() => mergeObjects({}, null)).toThrow(TypeError);
expect(() => mergeObjects({}, undefined)).toThrow(TypeError);
});
test('should handle source with non-object values gracefully', () => {
expect(() => mergeObjects({}, 123)).toThrow(TypeError);
expect(() => mergeObjects({}, 'string')).toThrow(TypeError);
expect(() => mergeObjects({}, true)).toThrow(TypeError);
});
});Auto-Fix AvailableAdd the 3 issues found. Reviewed by RepoSpace AI. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merges without checking for proto keys.