Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore __proto__ fields in deepMerge() #2779

Merged
merged 3 commits into from
Jun 4, 2019
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
4 changes: 4 additions & 0 deletions packages/apollo-gateway/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Changelog

### vNEXT

# v0.6.2

* Resolve an issue with \__proto__ pollution in deepMerge() [#2779](https://github.com/apollographql/apollo-server/pull/2779)
59 changes: 59 additions & 0 deletions packages/apollo-gateway/src/utilities/__tests__/deepMerge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { deepMerge } from '../deepMerge';

describe('deepMerge', () => {
it('merges basic', () => {
const target = {
a: 1,
b: 2,
};

const source = {
b: 3,
c: 4,
};

expect(deepMerge(target, source)).toEqual({
a: 1,
b: 3,
c: 4,
});
});

it('merges nested objects', () => {
const target = {
a: 1,
b: {
someProperty: 1,
overwrittenProperty: 'clean',
},
};

const source = {
b: {
overwrittenProperty: 'dirty',
newProperty: 'new',
},
c: 4,
};

expect(deepMerge(target, source)).toEqual({
a: 1,
b: {
newProperty: 'new',
overwrittenProperty: 'dirty',
someProperty: 1,
},
c: 4,
});
});

it('ignores merging __proto__ fields', () => {
const target = {};

// Bypass setters on __proto__
const source = JSON.parse('{"__proto__": {"pollution": true}}');
deepMerge(target, source);

expect(Object.prototype.hasOwnProperty('pollution')).toBe(false);
});
});
2 changes: 1 addition & 1 deletion packages/apollo-gateway/src/utilities/deepMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function deepMerge(target: any, source: any): any {
if (source === undefined || source === null) return target;

for (const key of Object.keys(source)) {
if (source[key] === undefined) continue;
if (source[key] === undefined || key === '__proto__') continue;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat.


if (target[key] && isObject(source[key])) {
deepMerge(target[key], source[key]);
Expand Down