module Gem::SafeYAML
This module is used for safely loading YAML specs from a gem. The safe_load method defined on this module is specifically designed for loading Gem specifications. For loading other YAML safely, please see Psych.safe_load
Constants
- MAX_ALIAS_EXPANSION
- PERMITTED_CLASSES
- PERMITTED_SYMBOLS
Public Class Methods
Source
# File lib/rubygems/safe_yaml.rb, line 40 def self.safe_load(input) # Psych rejects legacy metadata bytes, so preserve them with the internal parser. if Gem.use_psych? && valid_encoding?(input) check_alias_expansion(input) if @aliases_enabled ::Psych.safe_load(input, permitted_classes: PERMITTED_CLASSES, permitted_symbols: PERMITTED_SYMBOLS, aliases: @aliases_enabled) else # Keeps the nil Psych returns, see Gem::YAMLSerializer.load ast = Gem::YAMLSerializer::Parser.new(input).parse Gem::YAMLSerializer::Builder.new( permitted_classes: PERMITTED_CLASSES, permitted_symbols: PERMITTED_SYMBOLS, aliases: aliases_enabled? ).build(ast) end end
Also aliased as: load
Private Class Methods
# File lib/rubygems/safe_yaml.rb, line 71 def self.check_alias_expansion(input) sizes = {} expansion = 0 count = lambda do |node| if node.is_a?(::Psych::Nodes::Alias) size = sizes.fetch(node.anchor, 0) expansion += size if expansion > MAX_ALIAS_EXPANSION raise ::Psych::BadAlias, "exceeded maximum alias expansion (#{MAX_ALIAS_EXPANSION})" end next size end size = node.is_a?(::Psych::Nodes::Scalar) ? 1 + node.value.bytesize : 1 + node.children.sum(&count) sizes[node.anchor] = size if node.respond_to?(:anchor) && node.anchor size end document = ::Psych.parse(input) count.call(document) if document end
Psych.safe_load returns shared objects for aliases with no limit, and flatten, to_ruby and to_yaml expand them later. Apply the same budget as Gem::YAMLSerializer::Builder, which keeps its own constant because Bundler ships a copy of that file.
Source
# File lib/rubygems/safe_yaml.rb, line 61 def self.valid_encoding?(input) return true unless input.is_a?(String) input.dup.force_encoding(Encoding::UTF_8).valid_encoding? end