Skip to content

Commit 1764942

Browse files
committed
Use Psych.safe_load by default
Psych.load is not safe for use with untrusted data. Too many applications make the mistake of using `Psych.load` with untrusted data and that ends up with some kind of security vulnerability. This commit changes the default `Psych.load` to use `safe_load`. Users that want to parse trusted data can use Psych.unsafe_load.
1 parent 4de7e9c commit 1764942

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

lib/psych.rb

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ module Psych
249249
#
250250
# Example:
251251
#
252-
# Psych.load("--- a") # => 'a'
253-
# Psych.load("---\n - a\n - b") # => ['a', 'b']
252+
# Psych.unsafe_load("--- a") # => 'a'
253+
# Psych.unsafe_load("---\n - a\n - b") # => ['a', 'b']
254254
#
255255
# begin
256-
# Psych.load("--- `", filename: "file.txt")
256+
# Psych.unsafe_load("--- `", filename: "file.txt")
257257
# rescue Psych::SyntaxError => ex
258258
# ex.file # => 'file.txt'
259259
# ex.message # => "(file.txt): found character that cannot start any token"
@@ -262,14 +262,14 @@ module Psych
262262
# When the optional +symbolize_names+ keyword argument is set to a
263263
# true value, returns symbols for keys in Hash objects (default: strings).
264264
#
265-
# Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
266-
# Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
265+
# Psych.unsafe_load("---\n foo: bar") # => {"foo"=>"bar"}
266+
# Psych.unsafe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
267267
#
268268
# Raises a TypeError when `yaml` parameter is NilClass
269269
#
270270
# NOTE: This method *should not* be used to parse untrusted documents, such as
271271
# YAML documents that are supplied via user input. Instead, please use the
272-
# safe_load method.
272+
# load method or the safe_load method.
273273
#
274274
def self.unsafe_load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false, freeze: false
275275
if legacy_filename != NOT_GIVEN
@@ -363,6 +363,46 @@ def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_
363363
result
364364
end
365365

366+
###
367+
# Load +yaml+ in to a Ruby data structure. If multiple documents are
368+
# provided, the object contained in the first document will be returned.
369+
# +filename+ will be used in the exception message if any exception
370+
# is raised while parsing. If +yaml+ is empty, it returns
371+
# the specified +fallback+ return value, which defaults to +false+.
372+
#
373+
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
374+
#
375+
# Example:
376+
#
377+
# Psych.load("--- a") # => 'a'
378+
# Psych.load("---\n - a\n - b") # => ['a', 'b']
379+
#
380+
# begin
381+
# Psych.load("--- `", filename: "file.txt")
382+
# rescue Psych::SyntaxError => ex
383+
# ex.file # => 'file.txt'
384+
# ex.message # => "(file.txt): found character that cannot start any token"
385+
# end
386+
#
387+
# When the optional +symbolize_names+ keyword argument is set to a
388+
# true value, returns symbols for keys in Hash objects (default: strings).
389+
#
390+
# Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
391+
# Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
392+
#
393+
# Raises a TypeError when `yaml` parameter is NilClass. This method is
394+
# similar to `safe_load` except that `Symbol` objects are allowed by default.
395+
#
396+
def self.load yaml, permitted_classes: [Symbol], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false
397+
safe_load yaml, permitted_classes: permitted_classes,
398+
permitted_symbols: permitted_symbols,
399+
aliases: aliases,
400+
filename: filename,
401+
fallback: fallback,
402+
symbolize_names: symbolize_names,
403+
freeze: freeze
404+
end
405+
366406
###
367407
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Document.
368408
# +filename+ is used in the exception message if a Psych::SyntaxError is
@@ -595,6 +635,7 @@ def self.safe_load_file filename, **kwargs
595635
self.safe_load f, filename: filename, **kwargs
596636
}
597637
end
638+
class << self; alias load_file safe_load_file end
598639

599640
# :stopdoc:
600641
def self.add_domain_type domain, type_tag, &block

0 commit comments

Comments
 (0)