[前][次][番号順一覧][スレッド一覧]

ruby-changes:72867

From: Alexander <ko1@a...>
Date: Tue, 9 Aug 2022 01:32:54 +0900 (JST)
Subject: [ruby-changes:72867] 54219ae8c4 (master): [ruby/psych] Raise specific error when aliases are not enabled

https://git.ruby-lang.org/ruby.git/commit/?id=54219ae8c4

From 54219ae8c46bc431782caf01142883ce7e8b970b Mon Sep 17 00:00:00 2001
From: Alexander Momchilov <alexander.momchilov@s...>
Date: Thu, 21 Jul 2022 15:07:39 -0400
Subject: [ruby/psych] Raise specific error when aliases are not enabled

https://github.com/ruby/psych/commit/0c11ddcf46
---
 ext/psych/lib/psych.rb                  | 2 +-
 ext/psych/lib/psych/exception.rb        | 7 +++++++
 ext/psych/lib/psych/visitors/to_ruby.rb | 2 +-
 test/psych/helper.rb                    | 6 +++---
 test/psych/test_array.rb                | 2 +-
 test/psych/test_hash.rb                 | 2 +-
 test/psych/test_merge_keys.rb           | 2 +-
 test/psych/test_object.rb               | 2 +-
 test/psych/test_safe_load.rb            | 2 +-
 9 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb
index 42d79efb83..4a2ab58514 100644
--- a/ext/psych/lib/psych.rb
+++ b/ext/psych/lib/psych.rb
@@ -307,7 +307,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych.rb#L307
   # A Psych::DisallowedClass exception will be raised if the yaml contains a
   # class that isn't in the +permitted_classes+ list.
   #
-  # A Psych::BadAlias exception will be raised if the yaml contains aliases
+  # A Psych::AliasesNotEnabled exception will be raised if the yaml contains aliases
   # but the +aliases+ keyword argument is set to false.
   #
   # +filename+ will be used in the exception message if any exception is raised
diff --git a/ext/psych/lib/psych/exception.rb b/ext/psych/lib/psych/exception.rb
index f473b95a3b..04a9a906a4 100644
--- a/ext/psych/lib/psych/exception.rb
+++ b/ext/psych/lib/psych/exception.rb
@@ -6,6 +6,13 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/exception.rb#L6
   class BadAlias < Exception
   end
 
+  # Subclasses `BadAlias` for backwards compatibility
+  class AliasesNotEnabled < BadAlias
+    def initialize
+      super "Alias parsing was not enabled. To enable it, pass `aliases: true` to `Psych::load` or `Psych::safe_load`."
+    end
+  end
+
   class DisallowedClass < Exception
     def initialize action, klass_name
       super "Tried to #{action} unspecified class: #{klass_name}"
diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb
index 935bc74f21..0bf5198ccc 100644
--- a/ext/psych/lib/psych/visitors/to_ruby.rb
+++ b/ext/psych/lib/psych/visitors/to_ruby.rb
@@ -427,7 +427,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L427
 
     class NoAliasRuby < ToRuby
       def visit_Psych_Nodes_Alias o
-        raise BadAlias, "Unknown alias: #{o.anchor}"
+        raise AliasesNotEnabled
       end
     end
   end
diff --git a/test/psych/helper.rb b/test/psych/helper.rb
index 0643139d8c..4e82887c6d 100644
--- a/test/psych/helper.rb
+++ b/test/psych/helper.rb
@@ -51,7 +51,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/helper.rb#L51
           :UseVersion => true, :UseHeader => true, :SortKeys => true
         )
       ))
-    rescue Psych::DisallowedClass, Psych::BadAlias
+    rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
       assert_to_yaml obj, yaml, :unsafe_load
     end
 
@@ -61,7 +61,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/helper.rb#L61
     def assert_parse_only( obj, yaml )
       begin
         assert_equal obj, Psych::load( yaml )
-      rescue Psych::DisallowedClass, Psych::BadAlias
+      rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
         assert_equal obj, Psych::unsafe_load( yaml )
       end
       assert_equal obj, Psych::parse( yaml ).transform
@@ -79,7 +79,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/helper.rb#L79
           assert_equal(obj, Psych.load(v.tree.yaml))
           assert_equal(obj, Psych::load(Psych.dump(obj)))
           assert_equal(obj, Psych::load(obj.to_yaml))
-        rescue Psych::DisallowedClass, Psych::BadAlias
+        rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
           assert_equal(obj, Psych.unsafe_load(v.tree.yaml))
           assert_equal(obj, Psych::unsafe_load(Psych.dump(obj)))
           assert_equal(obj, Psych::unsafe_load(obj.to_yaml))
diff --git a/test/psych/test_array.rb b/test/psych/test_array.rb
index 6a9931ab2f..0dc82439d4 100644
--- a/test/psych/test_array.rb
+++ b/test/psych/test_array.rb
@@ -68,7 +68,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_array.rb#L68
     def test_recursive_array_uses_alias
       @list << @list
 
-      assert_raise(BadAlias) do
+      assert_raise(AliasesNotEnabled) do
         Psych.load(Psych.dump(@list), aliases: false)
       end
     end
diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb
index 0555f6e034..0efa21160f 100644
--- a/test/psych/test_hash.rb
+++ b/test/psych/test_hash.rb
@@ -125,7 +125,7 @@ eoyml https://github.com/ruby/ruby/blob/trunk/test/psych/test_hash.rb#L125
       h = { }
       h["recursive_reference"] = h
 
-      assert_raise(BadAlias) do
+      assert_raise(AliasesNotEnabled) do
         Psych.load(Psych.dump(h), aliases: false)
       end
     end
diff --git a/test/psych/test_merge_keys.rb b/test/psych/test_merge_keys.rb
index dcf4f1fce3..8d2fceabf5 100644
--- a/test/psych/test_merge_keys.rb
+++ b/test/psych/test_merge_keys.rb
@@ -117,7 +117,7 @@ development: https://github.com/ruby/ruby/blob/trunk/test/psych/test_merge_keys.rb#L117
 bar:
   << : *foo
       eoyml
-      exp = assert_raise(Psych::BadAlias) { Psych.load yaml }
+      exp = assert_raise(Psych::BadAlias) { Psych.load(yaml, aliases: true) }
       assert_match 'foo', exp.message
     end
 
diff --git a/test/psych/test_object.rb b/test/psych/test_object.rb
index 227a1d1d53..21c27794ea 100644
--- a/test/psych/test_object.rb
+++ b/test/psych/test_object.rb
@@ -46,7 +46,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_object.rb#L46
       foo = Foo.new(nil)
       foo.parent = foo
 
-      assert_raise(BadAlias) do
+      assert_raise(AliasesNotEnabled) do
         Psych.load(Psych.dump(foo), permitted_classes: [Foo], aliases: false)
       end
     end
diff --git a/test/psych/test_safe_load.rb b/test/psych/test_safe_load.rb
index e57dbcb2f5..a9ed737528 100644
--- a/test/psych/test_safe_load.rb
+++ b/test/psych/test_safe_load.rb
@@ -28,7 +28,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_safe_load.rb#L28
         b: *ABC
       YAML
 
-      assert_raise(Psych::BadAlias) do
+      assert_raise(Psych::AliasesNotEnabled) do
         Psych.safe_load(yaml_with_aliases)
       end
     end
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]