123456789_123456789_123456789_123456789_123456789_

Module: Gem::SafeYAML

Relationships & Source Files
Defined in: lib/rubygems/safe_yaml.rb

Overview

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

Constant Summary

Class Attribute Summary

Class Method Summary

Class Attribute Details

.aliases_enabled=(value) (rw)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/safe_yaml.rb', line 32

def self.aliases_enabled=(value) # :nodoc:
  @aliases_enabled = !!value
end

.aliases_enabled? ⇒ Boolean (rw)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/safe_yaml.rb', line 36

def self.aliases_enabled? # :nodoc:
  @aliases_enabled
end

Class Method Details

.check_alias_expansion(input) (private)

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 YAMLSerializer::Builder, which keeps its own constant because Bundler ships a copy of that file.

[ GitHub ]

  
# File 'lib/rubygems/safe_yaml.rb', line 71

private_class_method 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

.load(input)

Alias for .safe_load.

[ GitHub ]

  
# File 'lib/rubygems/safe_yaml.rb', line 58

alias_method :load, :safe_load

.safe_load(input) Also known as: .load

[ GitHub ]

  
# 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

.valid_encoding?(input) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubygems/safe_yaml.rb', line 61

private_class_method def self.valid_encoding?(input)
  return true unless input.is_a?(String)

  input.dup.force_encoding(Encoding::UTF_8).valid_encoding?
end