File tree 2 files changed +16
-4
lines changed 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -241,12 +241,21 @@ file.readJSON = function(filepath, options) {
241
241
} ;
242
242
243
243
// Read a YAML file, parse its contents, return an object.
244
- file . readYAML = function ( filepath , options ) {
244
+ file . readYAML = function ( filepath , options , yamlOptions ) {
245
+ if ( ! options ) { options = { } ; }
246
+ if ( ! yamlOptions ) { yamlOptions = { } ; }
247
+
245
248
var src = file . read ( filepath , options ) ;
246
249
var result ;
247
250
grunt . verbose . write ( 'Parsing ' + filepath + '...' ) ;
248
251
try {
249
- result = YAML . load ( src ) ;
252
+ // use the recommended way of reading YAML files
253
+ // https://github.com/nodeca/js-yaml#safeload-string---options-
254
+ if ( yamlOptions . unsafeLoad ) {
255
+ result = YAML . load ( src ) ;
256
+ } else {
257
+ result = YAML . safeLoad ( src ) ;
258
+ }
250
259
grunt . verbose . ok ( ) ;
251
260
return result ;
252
261
} catch ( e ) {
Original file line number Diff line number Diff line change @@ -452,10 +452,13 @@ exports.file = {
452
452
test . done ( ) ;
453
453
} ,
454
454
'readYAML' : function ( test ) {
455
- test . expect ( 4 ) ;
455
+ test . expect ( 5 ) ;
456
456
var obj ;
457
457
obj = grunt . file . readYAML ( 'test/fixtures/utf8.yaml' ) ;
458
- test . deepEqual ( obj , this . object , 'file should be read as utf8 by default and parsed correctly.' ) ;
458
+ test . deepEqual ( obj , this . object , 'file should be safely read as utf8 by default and parsed correctly.' ) ;
459
+
460
+ obj = grunt . file . readYAML ( 'test/fixtures/utf8.yaml' , null , { unsafeLoad : true } ) ;
461
+ test . deepEqual ( obj , this . object , 'file should be unsafely read as utf8 by default and parsed correctly.' ) ;
459
462
460
463
obj = grunt . file . readYAML ( 'test/fixtures/iso-8859-1.yaml' , { encoding : 'iso-8859-1' } ) ;
461
464
test . deepEqual ( obj , this . object , 'file should be read using the specified encoding.' ) ;
You can’t perform that action at this time.
0 commit comments