Skip to content

Commit

Permalink
Switch to use safeLoad for loading YML files via file.readYAML.
Browse files Browse the repository at this point in the history
For previous behaviour please use the following:

```
readYAML('test/fixtures/utf8.yaml', null, {unsafeLoad: true});
```
  • Loading branch information
vladikoff committed Aug 17, 2020
1 parent 7125f49 commit e350cea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,21 @@ file.readJSON = function(filepath, options) {
};

// Read a YAML file, parse its contents, return an object.
file.readYAML = function(filepath, options) {
file.readYAML = function(filepath, options, yamlOptions) {
if (!options) { options = {}; }
if (!yamlOptions) { yamlOptions = {}; }

var src = file.read(filepath, options);
var result;
grunt.verbose.write('Parsing ' + filepath + '...');
try {
result = YAML.load(src);
// use the recommended way of reading YAML files
// https://github.com/nodeca/js-yaml#safeload-string---options-
if (yamlOptions.unsafeLoad) {
result = YAML.load(src);
} else {
result = YAML.safeLoad(src);
}
grunt.verbose.ok();
return result;
} catch (e) {
Expand Down
7 changes: 5 additions & 2 deletions test/grunt/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,13 @@ exports.file = {
test.done();
},
'readYAML': function(test) {
test.expect(4);
test.expect(5);
var obj;
obj = grunt.file.readYAML('test/fixtures/utf8.yaml');
test.deepEqual(obj, this.object, 'file should be read as utf8 by default and parsed correctly.');
test.deepEqual(obj, this.object, 'file should be safely read as utf8 by default and parsed correctly.');

obj = grunt.file.readYAML('test/fixtures/utf8.yaml', null, {unsafeLoad: true});
test.deepEqual(obj, this.object, 'file should be unsafely read as utf8 by default and parsed correctly.');

obj = grunt.file.readYAML('test/fixtures/iso-8859-1.yaml', {encoding: 'iso-8859-1'});
test.deepEqual(obj, this.object, 'file should be read using the specified encoding.');
Expand Down

0 comments on commit e350cea

Please sign in to comment.