Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 7981d4e

Browse files
authored
Merge pull request #259 from ckeditor/t/257
Other: Configuration options should be cloned to prevent features from altering the original values. Closes #257.
2 parents 0c5db2a + 0f94c4d commit 7981d4e

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @module utils/config
88
*/
99

10-
import { isPlainObject } from 'lodash-es';
10+
import { isPlainObject, cloneDeep } from 'lodash-es';
1111

1212
/**
1313
* Handles a configuration dictionary.
@@ -198,7 +198,7 @@ export default class Config {
198198
}
199199

200200
// Always returns undefined for non existing configuration
201-
return source ? source[ name ] : undefined;
201+
return source ? cloneDeep( source[ name ] ) : undefined;
202202
}
203203

204204
/**

tests/config.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ describe( 'Config', () => {
1919
path: 'xyz'
2020
}
2121
},
22-
toolbar: 'top'
22+
toolbar: 'top',
23+
options: {
24+
foo: [
25+
{ bar: 'b' },
26+
{ bar: 'a' },
27+
{ bar: 'z' }
28+
]
29+
}
2330
} );
2431
} );
2532

@@ -334,7 +341,7 @@ describe( 'Config', () => {
334341
expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
335342
} );
336343

337-
it( 'should retrieve a object of the configuration', () => {
344+
it( 'should retrieve an object of the configuration', () => {
338345
const resize = config.get( 'resize' );
339346

340347
expect( resize ).to.be.an( 'object' );
@@ -371,5 +378,33 @@ describe( 'Config', () => {
371378
config.resize.maxHeight;
372379
} ).to.throw();
373380
} );
381+
382+
it( 'should not be possible to alter config object by altering returned value', () => {
383+
expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
384+
385+
const icon = config.get( 'resize.icon' );
386+
icon.path = 'foo/bar';
387+
388+
expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
389+
390+
const resize = config.get( 'resize' );
391+
resize.icon.path = 'foo/baz';
392+
393+
expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
394+
} );
395+
396+
it( 'should not be possible to alter array in config by altering returned value', () => {
397+
expect( config.get( 'options.foo' ) ).to.deep.equal( [ { bar: 'b' }, { bar: 'a' }, { bar: 'z' } ] );
398+
399+
const fooOptions = config.get( 'options.foo' );
400+
fooOptions.pop();
401+
402+
expect( config.get( 'options.foo' ) ).to.deep.equal( [ { bar: 'b' }, { bar: 'a' }, { bar: 'z' } ] );
403+
404+
const options = config.get( 'options' );
405+
options.foo.pop();
406+
407+
expect( config.get( 'options.foo' ) ).to.deep.equal( [ { bar: 'b' }, { bar: 'a' }, { bar: 'z' } ] );
408+
} );
374409
} );
375410
} );

0 commit comments

Comments
 (0)