使用 YAML.load_file 时升级到 Ruby 3.1 会导致 Psych::DisallowedClass 异常

发布于 2025-01-11 04:38:02 字数 507 浏览 0 评论 0原文

升级到 ruby​​ 3.1 时,在使用 YAML.load_file some_file_name 时,我看到以下排序错误消息。

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

其他加载语句会导致类似的错误,但引用不同的未指定类,例如 OpenStruct。看来最新版本的 YAML 只加载允许的白名单中的类,因此需要使用 allowed_class 关键字来允许其他类。我已经尝试过

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

,但这给出了错误,

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

我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦在深巷 2025-01-18 04:38:02

在 Ruby 中加载 YAML 时,默认情况下也不允许使用 Symbol。因此,在读取 YAML 文件时,您还需要将 Symbol 添加到您的情况下的 permissed_classes 中:

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

请参阅默认 permissed_classes 中Psych(Ruby 使用的 YAML 解析器)。

或者,在 Ruby on Rails 中使用时,您可以在 config/application.rb 中全局配置 Ruby on Rails 应用程序在读取 YAML 时应允许哪些类作为允许的类文件:

config.active_record.yaml_column_permitted_classes += [Matrix, OpenStruct, Symbol]

请注意,对于 Ruby on Rails 中的内部 YAML 解析, Symbol 已经是 active_record.yaml_column_permissed_classes

Symbol is also not allowed per default when loading YAML in Ruby. Therefore, you need to add Symbol to the permitted_classes in your case too when reading the YAML file:

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

See the list of default permitted_classes in Psych (the YAML parser used by Ruby).

Or, when using in Ruby on Rails, you can configure globally in your config/application.rb what classes your Ruby on Rails application should allow as permitted classes when reading a YAML files:

config.active_record.yaml_column_permitted_classes += [Matrix, OpenStruct, Symbol]

Note that for internal YAML parsing in Ruby on Rails Symbol is already the default for active_record.yaml_column_permitted_classes.

半枫 2025-01-18 04:38:02

可行的解决方案是将这一行添加到 config/application.rb

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

您可以对任何类名执行相同的操作,例如

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

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]
蓝海似她心 2025-01-18 04:38:02

在 Rails 6.1 升级上有这个。如果您没有其他选择,也许这个解决方法会给您带来一些时间(application.rb):

config.active_record.use_yaml_unsafe_load = true

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
无悔心 2025-01-18 04:38:02

您可以更改 Rails 配置以使用 YAML/Psych 的 unsafe_load (请参阅 Mohamed 和crazywulf 的回答)。我需要在不重新启动 Rails 应用程序的情况下更改此配置,因此我这样做了:

ActiveRecord.use_yaml_unsafe_load = true

请注意,这只是当前进程的临时修复。重启服务器后它就会消失。

You can change the Rails configuration to use YAML/Psych's unsafe_load (see Mohamed's & crazywulf's answer). I needed to change this config without restarting the Rails app, so I did this:

ActiveRecord.use_yaml_unsafe_load = true

Please mind, this is just a temporary fix for the current process. It will be gone as soon as you restart the server.

阳光下慵懒的猫 2025-01-18 04:38:02

直接使用 YAML.load_file 时,不使用 config.yaml_column_permissed_classes。仅当 Rails 加载 YAML(配置文件、序列化 YAML)时才使用它。

您可以:

  • a.) 将允许的类列表传递给 YAML.load_file(path, allowed_classes: [..]) 就像 @spickermann 所写的那样,或者:
  • b.) 您可以切换到 YAML.unsafe_load_file (例如,对于测试用例)。

When using the YAML.load_file directly, the config.yaml_column_permitted_classes is not used. That is only used, when Rails loads YAML (config files, serialized YAML).

You can:

  • a.) either, pass the list of allowed classes to the YAML.load_file(path, permitted_classes: [..]) like @spickermann has written, or:
  • b.) You can switch to YAML.unsafe_load_file (e.g. for Test cases).
人│生佛魔见 2025-01-18 04:38:02

“安全YAML”加载方法默认不允许所有类被反序列化。此选项允许您在应用程序中指定被视为“安全”的类。例如,如果您的应用程序在序列化数据中使用符号和时间,您可以将符号和时间添加到允许列表中。

通过将其添加到 application.rb 来修复:

config.active_record.yaml_column_permitted_classes = [Symbol, Date, Time]

The “safe YAML” loading method does not allow all classes to be deserialized by default. This option allows you to specify classes deemed “safe” in your application. For example, if your application uses Symbol and Time in serialized data, you can add Symbol and Time to the allowed list.

Fixed by adding this to application.rb:

config.active_record.yaml_column_permitted_classes = [Symbol, Date, Time]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文