Skip to content

Commit

Permalink
fix: deny modifying the object prototype (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
WikiRik committed Nov 24, 2023
1 parent 837a3cc commit 5ce8afd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/shared/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export function deepAssign<T, S>(target: T, source: S): T & S;
export function deepAssign<S>(target: {}, source: S): S;
export function deepAssign(target: any, ...sources: any[]): any {
sources.forEach((source) => {
Object.getOwnPropertyNames(source).forEach((key) => assign(key, target, source));
Object.getOwnPropertyNames(source).forEach(
(key) =>
!['__proto__', 'constructor', 'prototype'].includes(key) && assign(key, target, source)
);
/* istanbul ignore next */
if (Object.getOwnPropertySymbols) {
Object.getOwnPropertySymbols(source).forEach((key) => assign(key, target, source));
Expand Down
9 changes: 9 additions & 0 deletions test/specs/utils/object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { deepAssign } from '../../../src/shared/object';
import { addScopeOptions } from '../../../src/scopes/scope-service';

describe('utils', () => {
describe('object', () => {
Expand Down Expand Up @@ -109,6 +110,14 @@ describe('utils', () => {
expect(copy.test).to.have.property('protoFn').that.is.a('function');
});

it('ignore prototype property', () => {
const BAD_JSON = JSON.parse('{"__proto__":{"polluted":true}}');
const empty_scope = {};

addScopeOptions(empty_scope, BAD_JSON);
expect(empty_scope).not.to.have.property('polluted');
});

if (Object.getOwnPropertySymbols) {
it('should copy symbol based objects', () => {
const symbol = Symbol('test');
Expand Down

0 comments on commit 5ce8afd

Please sign in to comment.