Skip to content

Commit f2e4b9d

Browse files
hsbtclaude
andcommitted
Route !ruby/encoding through the class loader
safe_load resolved !ruby/encoding directly via ::Encoding.find, bypassing the permitted_classes check that !ruby/object:Encoding already honors. Load it through the class loader so Encoding is only deserialized when permitted. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent fee99ea commit f2e4b9d

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/psych/class_loader.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ClassLoader # :nodoc:
99
DATA = 'Data' unless RUBY_VERSION < "3.2"
1010
DATE = 'Date'
1111
DATE_TIME = 'DateTime'
12+
ENCODING = 'Encoding'
1213
EXCEPTION = 'Exception'
1314
OBJECT = 'Object'
1415
PSYCH_OMAP = 'Psych::Omap'

lib/psych/visitors/to_ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def deserialize o
8787
DateTime.civil(*t.to_a[0, 6].reverse, Rational(t.utc_offset, 86400)) +
8888
(t.subsec/86400)
8989
when '!ruby/encoding'
90+
class_loader.encoding
9091
::Encoding.find o.value
9192
when "!ruby/object:Complex"
9293
class_loader.complex

test/psych/test_safe_load.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ def test_symbol
7474
assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', permitted_classes: [Symbol])
7575
end
7676

77+
def test_encoding
78+
yaml = "--- !ruby/encoding UTF-8\n"
79+
assert_raise(Psych::DisallowedClass) do
80+
Psych.safe_load yaml
81+
end
82+
assert_raise(Psych::DisallowedClass) do
83+
Psych.safe_load yaml, permitted_classes: []
84+
end
85+
86+
assert_equal Encoding::UTF_8, Psych.safe_load(yaml, permitted_classes: [Encoding])
87+
assert_equal Encoding::UTF_8, Psych.safe_load(yaml, permitted_classes: %w{ Encoding })
88+
end
89+
7790
def test_foo
7891
assert_raise(Psych::DisallowedClass) do
7992
Psych.safe_load '--- !ruby/object:Foo {}', permitted_classes: [Foo]

0 commit comments

Comments
 (0)