Upgrading to Ruby 3.1 causes Psych::DisallowedClass exception when using YAML.load_file

Viewed 5916

When upgrading to ruby 3.1, I am seeing the following sort error message when using YAML.load_file some_file_name

 Psych::DisallowedClass:
   Tried to load unspecified class: Matrix

Other load statements cause similar errors but cite different unspecified classes e.g. OpenStruct. It appears that the latest version of YAML only loads classes from a permitted white list, so it is necessary to use a permitted_class keyword to allow other classes. I have tried

hsh = YAML.load_file some_file_name, permitted_classes: [Matrix, OpenStruct]

but this gives the error

 Psych::DisallowedClass:
   Tried to load unspecified class: Symbol

how do I fix this?

3 Answers

Symbol is also not allowed per default. Therefore just add Symbol to the permitted_classes too:

hash = YAML.load_file(
  some_file_name, 
  permitted_classes: [Matrix, OpenStruct, Symbol]
)

See the list of default permitted_classes.

The working solution is to add this line to config/application.rb

config.active_record.yaml_column_permitted_classes = [ActiveSupport::HashWithIndifferentAccess]

You can do the same with any class name, like

config.active_record.yaml_column_permitted_classes = [Symbol, Hash, Array, ActiveSupport::HashWithIndifferentAccess]

Had this on rails 6.1 upgrade. If you have no other choice, maybe this workaround will bring you some time (application.rb):

config.active_record.use_yaml_unsafe_load = true
Related