psych icon indicating copy to clipboard operation
psych copied to clipboard

No way to "accept all symbols" using Psych.safe_dump

Open bjwrk opened this issue 3 years ago • 0 comments

If I call Psych.safe_load on some YAML that contains symbol keys, all the symbol keys are accepted by default:

> YAML.safe_load(":yes: 1", permitted_classes: [Symbol])
=> {:yes=>1}

They are also accepted if I pass an empty list of permitted_symbols:

> YAML.safe_load(":yes: 1", permitted_classes: [Symbol], permitted_symbols: [])
=> {:yes=>1}

However, if I try to use safe_dump, there appears to be no way to "accept all Symbols":

> YAML.safe_dump({yes: 1}, permitted_classes: [Symbol])
psych/visitors/yaml_tree.rb:580:in `visit_Symbol': Tried to dump unspecified class: Symbol(:yes) (Psych::DisallowedClass)

I have to supply the full complement of symbols via permitted_symbols in order for safe_dump to succeed:

> YAML.safe_dump({yes: 1}, permitted_classes: [Symbol], permitted_symbols: [:yes])
=> "---\n:yes: 1\n"

Psych::ClassLoader::Restricted presumes that an empty permitted_symbols array means "accept all symbols": https://github.com/ruby/psych/blob/a565e1fcec3dad4de7e753b9c3e113c7ed1a22a4/lib/psych/class_loader.rb#L83-L91

However Psych::Visitors::RestrictedYAMLTree presumes that an empty permitted_symbols array means "accept no symbols": https://github.com/ruby/psych/blob/a565e1fcec3dad4de7e753b9c3e113c7ed1a22a4/lib/psych/visitors/yaml_tree.rb#L578-L584

I might be missing something here, but I would expect permitted_symbols to mean "accept all symbols" in both load and dump.

bjwrk avatar Sep 07 '22 13:09 bjwrk