2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2013-05-14 13:26:41 -04:00
|
|
|
require 'psych/helper'
|
|
|
|
|
|
|
|
module Psych
|
|
|
|
class TestSafeLoad < TestCase
|
2018-10-20 00:25:04 -04:00
|
|
|
def setup
|
|
|
|
@orig_verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$VERBOSE = @orig_verbose
|
|
|
|
end
|
|
|
|
|
2013-05-14 13:26:41 -04:00
|
|
|
class Foo; end
|
|
|
|
|
|
|
|
[1, 2.2, {}, [], "foo"].each do |obj|
|
|
|
|
define_method(:"test_basic_#{obj.class}") do
|
|
|
|
assert_safe_cycle obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_recursion
|
|
|
|
x = []
|
|
|
|
x << x
|
|
|
|
assert_raises(Psych::BadAlias) do
|
|
|
|
Psych.safe_load Psych.dump(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_recursion
|
|
|
|
x = []
|
|
|
|
x << x
|
2018-11-10 19:20:27 -05:00
|
|
|
assert_equal(x, Psych.safe_load(Psych.dump(x), permitted_classes: [], permitted_symbols: [], aliases: true))
|
2018-08-26 20:44:04 -04:00
|
|
|
# deprecated interface
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_equal(x, Psych.safe_load(Psych.dump(x), [], [], true))
|
|
|
|
end
|
|
|
|
|
2018-11-10 19:20:27 -05:00
|
|
|
def test_permitted_symbol
|
2013-05-14 13:26:41 -04:00
|
|
|
yml = Psych.dump :foo
|
|
|
|
assert_raises(Psych::DisallowedClass) do
|
|
|
|
Psych.safe_load yml
|
|
|
|
end
|
2018-08-26 20:44:04 -04:00
|
|
|
assert_equal(
|
|
|
|
:foo,
|
|
|
|
Psych.safe_load(
|
|
|
|
yml,
|
2018-11-10 19:20:27 -05:00
|
|
|
permitted_classes: [Symbol],
|
|
|
|
permitted_symbols: [:foo]
|
2018-08-26 20:44:04 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# deprecated interface
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_equal(:foo, Psych.safe_load(yml, [Symbol], [:foo]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_symbol
|
|
|
|
assert_raises(Psych::DisallowedClass) do
|
|
|
|
assert_safe_cycle :foo
|
|
|
|
end
|
2018-08-26 20:44:04 -04:00
|
|
|
assert_raises(Psych::DisallowedClass) do
|
2018-11-10 19:20:27 -05:00
|
|
|
Psych.safe_load '--- !ruby/symbol foo', permitted_classes: []
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# deprecated interface
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_raises(Psych::DisallowedClass) do
|
|
|
|
Psych.safe_load '--- !ruby/symbol foo', []
|
|
|
|
end
|
2018-08-26 20:44:04 -04:00
|
|
|
|
2018-11-10 19:20:27 -05:00
|
|
|
assert_safe_cycle :foo, permitted_classes: [Symbol]
|
|
|
|
assert_safe_cycle :foo, permitted_classes: %w{ Symbol }
|
|
|
|
assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', permitted_classes: [Symbol])
|
2018-08-26 20:44:04 -04:00
|
|
|
|
|
|
|
# deprecated interface
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', [Symbol])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_foo
|
2018-08-26 20:44:04 -04:00
|
|
|
assert_raises(Psych::DisallowedClass) do
|
2018-11-10 19:20:27 -05:00
|
|
|
Psych.safe_load '--- !ruby/object:Foo {}', permitted_classes: [Foo]
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# deprecated interface
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_raises(Psych::DisallowedClass) do
|
|
|
|
Psych.safe_load '--- !ruby/object:Foo {}', [Foo]
|
|
|
|
end
|
2018-08-26 20:44:04 -04:00
|
|
|
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_raises(Psych::DisallowedClass) do
|
|
|
|
assert_safe_cycle Foo.new
|
|
|
|
end
|
2018-11-10 19:20:27 -05:00
|
|
|
assert_kind_of(Foo, Psych.safe_load(Psych.dump(Foo.new), permitted_classes: [Foo]))
|
2018-08-26 20:44:04 -04:00
|
|
|
|
|
|
|
# deprecated interface
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_kind_of(Foo, Psych.safe_load(Psych.dump(Foo.new), [Foo]))
|
|
|
|
end
|
|
|
|
|
|
|
|
X = Struct.new(:x)
|
|
|
|
def test_struct_depends_on_sym
|
2018-11-10 19:20:27 -05:00
|
|
|
assert_safe_cycle(X.new, permitted_classes: [X, Symbol])
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_raises(Psych::DisallowedClass) do
|
2018-11-10 19:20:27 -05:00
|
|
|
cycle X.new, permitted_classes: [X]
|
2013-05-14 13:26:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_anon_struct
|
2018-11-10 19:20:27 -05:00
|
|
|
assert Psych.safe_load(<<-eoyml, permitted_classes: [Struct, Symbol])
|
2018-08-26 20:44:04 -04:00
|
|
|
--- !ruby/struct
|
|
|
|
foo: bar
|
|
|
|
eoyml
|
|
|
|
|
|
|
|
assert_raises(Psych::DisallowedClass) do
|
2018-11-10 19:20:27 -05:00
|
|
|
Psych.safe_load(<<-eoyml, permitted_classes: [Struct])
|
2018-08-26 20:44:04 -04:00
|
|
|
--- !ruby/struct
|
|
|
|
foo: bar
|
|
|
|
eoyml
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises(Psych::DisallowedClass) do
|
2018-11-10 19:20:27 -05:00
|
|
|
Psych.safe_load(<<-eoyml, permitted_classes: [Symbol])
|
2018-08-26 20:44:04 -04:00
|
|
|
--- !ruby/struct
|
|
|
|
foo: bar
|
|
|
|
eoyml
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecated_anon_struct
|
2013-05-14 13:26:41 -04:00
|
|
|
assert Psych.safe_load(<<-eoyml, [Struct, Symbol])
|
|
|
|
--- !ruby/struct
|
|
|
|
foo: bar
|
|
|
|
eoyml
|
|
|
|
|
|
|
|
assert_raises(Psych::DisallowedClass) do
|
|
|
|
Psych.safe_load(<<-eoyml, [Struct])
|
|
|
|
--- !ruby/struct
|
|
|
|
foo: bar
|
|
|
|
eoyml
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises(Psych::DisallowedClass) do
|
|
|
|
Psych.safe_load(<<-eoyml, [Symbol])
|
|
|
|
--- !ruby/struct
|
|
|
|
foo: bar
|
|
|
|
eoyml
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-26 20:44:04 -04:00
|
|
|
def test_safe_load_default_fallback
|
|
|
|
assert_nil Psych.safe_load("")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_load
|
|
|
|
assert_equal %w[a b], Psych.safe_load("- a\n- b")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_load_raises_on_bad_input
|
|
|
|
assert_raises(Psych::SyntaxError) { Psych.safe_load("--- `") }
|
|
|
|
end
|
|
|
|
|
2013-05-14 13:26:41 -04:00
|
|
|
private
|
|
|
|
|
2018-11-10 19:20:27 -05:00
|
|
|
def cycle object, permitted_classes: []
|
|
|
|
Psych.safe_load(Psych.dump(object), permitted_classes: permitted_classes)
|
2018-08-26 20:44:04 -04:00
|
|
|
# deprecated interface test
|
2018-11-10 19:20:27 -05:00
|
|
|
Psych.safe_load(Psych.dump(object), permitted_classes)
|
2013-05-14 13:26:41 -04:00
|
|
|
end
|
|
|
|
|
2018-11-10 19:20:27 -05:00
|
|
|
def assert_safe_cycle object, permitted_classes: []
|
|
|
|
other = cycle object, permitted_classes: permitted_classes
|
2013-05-14 13:26:41 -04:00
|
|
|
assert_equal object, other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|