module Psych
def self.safe_load yaml, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false, strict_integer: false
Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
true value, returns symbols for keys in Hash objects (default: strings).
When the optional +symbolize_names+ keyword argument is set to a
while parsing.
+filename+ will be used in the exception message if any exception is raised
but the +aliases+ keyword argument is set to false.
A Psych::AliasesNotEnabled exception will be raised if the yaml contains aliases
class that isn't in the +permitted_classes+ list.
A Psych::DisallowedClass exception will be raised if the yaml contains a
Psych.safe_load yaml, aliases: true # => loads the aliases
Psych.safe_load yaml # => raises an exception
yaml = Psych.dump x
x << x
x = []
For example:
Aliases can be explicitly allowed by changing the +aliases+ keyword argument.
Now the Date class can be loaded in addition to the classes listed above.
Psych.safe_load(yaml, permitted_classes: [Date])
additive. For example, to allow Date deserialization:
can be allowed by adding those classes to the +permitted_classes+ keyword argument. They are
Recursive data structures are not allowed by default. Arbitrary classes
* Hash
* Array
* String
* Float
* Integer
* NilClass
* FalseClass
* TrueClass
classes are allowed to be deserialized:
Safely load the yaml string in +yaml+. By default, only the following
##
def self.safe_load yaml, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false, strict_integer: false result = parse(yaml, filename: filename) return fallback unless result class_loader = ClassLoader::Restricted.new(permitted_classes.map(&:to_s), permitted_symbols.map(&:to_s)) scanner = ScalarScanner.new class_loader, strict_integer: strict_integer visitor = if aliases Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze else Visitors::NoAliasRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze end result = visitor.accept result result end