1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/psych/test_deprecated.rb
Aaron Patterson c7c2ad5749
[ruby/psych] Introduce Psych.unsafe_load
In future versions of Psych, the `load` method will be mostly the same
as the `safe_load` method.  In other words, the `load` method won't
allow arbitrary object deserialization (which can be used to escalate to
an RCE).  People that need to load *trusted* documents can use the
`unsafe_load` method.

This commit introduces the `unsafe_load` method so that people can
incrementally upgrade.  For example, if they try to upgrade to 4.0.0 and
something breaks, they can downgrade, audit callsites, change to
`safe_load` or `unsafe_load` as required, and then upgrade to 4.0.0
smoothly.

https://github.com/ruby/psych/commit/cb50aa8d3f
2021-05-17 11:20:45 +09:00

91 lines
2.1 KiB
Ruby

# frozen_string_literal: true
require_relative 'helper'
module Psych
class TestDeprecated < TestCase
def teardown
$VERBOSE = @orig_verbose
Psych.domain_types.clear
end
class QuickEmitter; end
def setup
@orig_verbose, $VERBOSE = $VERBOSE, false
end
class QuickEmitterEncodeWith
attr_reader :name
attr_reader :value
def initialize
@name = 'hello!!'
@value = 'Friday!'
end
def encode_with coder
coder.map do |map|
map.add 'name', @name
map.add 'value', nil
end
end
def to_yaml opts = {}
raise
end
end
###
# An object that defines both to_yaml and encode_with should only call
# encode_with.
def test_recursive_quick_emit_encode_with
qeew = QuickEmitterEncodeWith.new
hash = { :qe => qeew }
hash2 = Psych.unsafe_load Psych.dump hash
qe = hash2[:qe]
assert_equal qeew.name, qe.name
assert_instance_of QuickEmitterEncodeWith, qe
assert_nil qe.value
end
class YamlInitAndInitWith
attr_reader :name
attr_reader :value
def initialize
@name = 'shaners'
@value = 'Friday!'
end
def init_with coder
coder.map.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' }
end
def yaml_initialize tag, vals
raise
end
end
###
# An object that implements both yaml_initialize and init_with should not
# receive the yaml_initialize call.
def test_yaml_initialize_and_init_with
hash = { :yi => YamlInitAndInitWith.new }
hash2 = Psych.unsafe_load Psych.dump hash
yi = hash2[:yi]
assert_equal 'TGIF!', yi.name
assert_equal 'TGIF!', yi.value
assert_instance_of YamlInitAndInitWith, yi
end
def test_coder_scalar
coder = Psych::Coder.new 'foo'
coder.scalar('tag', 'some string', :plain)
assert_equal 'tag', coder.tag
assert_equal 'some string', coder.scalar
assert_equal :scalar, coder.type
end
end
end