Skip to content

Commit 7e669ac

Browse files
authored
Merge pull request #1719 from gruntjs/yaml-refactor
Switch to use `safeLoad` for loading YML files via `file.readYAML`.
2 parents 7125f49 + e350cea commit 7e669ac

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/grunt/file.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,21 @@ file.readJSON = function(filepath, options) {
241241
};
242242

243243
// 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+
245248
var src = file.read(filepath, options);
246249
var result;
247250
grunt.verbose.write('Parsing ' + filepath + '...');
248251
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+
}
250259
grunt.verbose.ok();
251260
return result;
252261
} catch (e) {

test/grunt/file_test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,13 @@ exports.file = {
452452
test.done();
453453
},
454454
'readYAML': function(test) {
455-
test.expect(4);
455+
test.expect(5);
456456
var obj;
457457
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.');
459462

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

0 commit comments

Comments
 (0)