SafeYAML

This needs to be defined up front in case any internal classes need to base their behavior off of this.

Public Instance Methods

predefined_tags() click to toggle source
# File lib/safe_yaml.rb, line 80
def predefined_tags
  if @predefined_tags.nil?
    @predefined_tags = {}

    if YAML_ENGINE == "syck"
      YAML.tagged_classes.each do |tag, klass|
        @predefined_tags[klass] = tag
      end

    else
      # Special tags appear to be hard-coded in Psych:
      # https://github.com/tenderlove/psych/blob/v1.3.4/lib/psych/visitors/to_ruby.rb
      # Fortunately, there aren't many that SafeYAML doesn't already support.
      @predefined_tags.merge!({
        Exception => "!ruby/exception",
        Range     => "!ruby/range",
        Regexp    => "!ruby/regexp",
      })
    end
  end

  @predefined_tags
end
restore_defaults!() click to toggle source
# File lib/safe_yaml.rb, line 39
def restore_defaults!
  OPTIONS.clear.merge!(Deep.copy(DEFAULT_OPTIONS))
end
tag_is_explicitly_trusted?(tag) click to toggle source
# File lib/safe_yaml.rb, line 105
def tag_is_explicitly_trusted?(tag)
  false
end
tag_safety_check!(tag, options) click to toggle source
# File lib/safe_yaml.rb, line 43
def tag_safety_check!(tag, options)
  return if tag.nil? || tag == "!"
  if options[:raise_on_unknown_tag] && !options[:whitelisted_tags].include?(tag) && !tag_is_explicitly_trusted?(tag)
    raise "Unknown YAML tag '#{tag}'"
  end
end
whitelist!(*classes) click to toggle source
# File lib/safe_yaml.rb, line 50
def whitelist!(*classes)
  classes.each do |klass|
    whitelist_class!(klass)
  end
end
whitelist_class!(klass) click to toggle source
# File lib/safe_yaml.rb, line 56
def whitelist_class!(klass)
  raise "#{klass} not a Class" unless klass.is_a?(::Class)

  klass_name = klass.name
  raise "#{klass} cannot be anonymous" if klass_name.nil? || klass_name.empty?

  # Whitelist any built-in YAML tags supplied by Syck or Psych.
  predefined_tag = predefined_tags[klass]
  if predefined_tag
    OPTIONS[:whitelisted_tags] << predefined_tag
    return
  end

  # Exception is exceptional (har har).
  tag_class  = klass < Exception ? "exception" : "object"

  tag_prefix = case YAML_ENGINE
               when "psych" then "!ruby/#{tag_class}"
               when "syck"  then "tag:ruby.yaml.org,2002:#{tag_class}"
               else raise "unknown YAML_ENGINE #{YAML_ENGINE}"
               end
  OPTIONS[:whitelisted_tags] << "#{tag_prefix}:#{klass_name}"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.