Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the responsibility of safe loading to Node #643

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions lib/psych.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,10 @@ def self.unsafe_load yaml, filename: nil, fallback: false, symbolize_names: fals
# Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
# Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
#
def self.safe_load yaml, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false, strict_integer: false
def self.safe_load yaml, filename: nil, fallback: nil, **kwargs
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
result.to_safe_ruby(**kwargs)
end

###
Expand Down
17 changes: 17 additions & 0 deletions lib/psych/nodes/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ def to_ruby(symbolize_names: false, freeze: false, strict_integer: false)
end
alias :transform :to_ruby

###
# Safely convert this node to Ruby.
#
# See also Psych.safe_load
#
def to_safe_ruby(permitted_classes: [], permitted_symbols: [], aliases: false, symbolize_names: false, freeze: false, strict_integer: false)
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_class = aliases ? Visitors::ToRuby : Visitors::NoAliasRuby

visitor_class.new(scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze).accept(self)
end
alias :safe_transform :to_safe_ruby

###
# Convert this node to YAML.
#
Expand Down