haml-lint icon indicating copy to clipboard operation
haml-lint copied to clipboard

Loading YAML configuration file fails if a regex is used with Ruby 3.1

Open SeanLF opened this issue 2 years ago • 0 comments

haml-lint is crashing when loading our configuration file due the fact that we use a regular expression to exclude specific files for a rule, and happen to have upgraded to Ruby 3.1.

Ruby 3.1 bumped Psych to v4.0 (aka YAML), as opposed to v3.3 in previous versions of Ruby, which breaks the YAML.load and YAML.load_file behaviour by raising Tried to load unspecified class if a class is detected while loading and that class is not listed in the permitted_classes: [] keyword argument. You can find a more detailed explanation here: https://www.ctrl.blog/entry/ruby-psych4.html

Here is an example of a config file that breaks the loading on Ruby 3.1:

# .haml-lint.yml
inherit_from:
  - .rubocop.yml
linters:
  RuboCop:
    enabled: true
# .rubocop.yml
Company/Lint/OurCustomRule:
  Exclude:
    - !ruby/regexp /app\/views\/someregex[abcde]/

To fix this, we need to replace https://github.com/sds/haml-lint/blob/1231b394bcc2b6c9c47a7a18074b43b258a0872e/lib/haml_lint/configuration_loader.rb#L83 with the following if we want to continue using safe_load (we can add any other classes to indicate as safe to load)

if yaml = YAML.load_file(file, permitted_classes: [Regexp])

or

if yaml = YAML.unsafe_load_file(file)
  • Haml-Lint version: 0.40.0
  • Haml version: 5.2.2
  • RuboCop version: 1.32.0
  • Ruby version: 3.1.2
Tried to load unspecified class: Regexp
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/class_loader.rb:99:in `find'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/class_loader.rb:28:in `load'
(eval):2:in `regexp'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:97:in `deserialize'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:128:in `visit_Psych_Nodes_Scalar'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:30:in `visit'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:6:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:35:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:338:in `block in register_empty'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:338:in `each'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:338:in `register_empty'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:146:in `visit_Psych_Nodes_Sequence'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:30:in `visit'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:6:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:35:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:345:in `block in revive_hash'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:343:in `each'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:343:in `each_slice'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:343:in `revive_hash'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:167:in `visit_Psych_Nodes_Mapping'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:30:in `visit'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:6:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:35:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:345:in `block in revive_hash'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:343:in `each'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:343:in `each_slice'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:343:in `revive_hash'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:167:in `visit_Psych_Nodes_Mapping'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:30:in `visit'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:6:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:35:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:318:in `visit_Psych_Nodes_Document'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:30:in `visit'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/visitor.rb:6:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych/visitors/to_ruby.rb:35:in `accept'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych.rb:335:in `safe_load'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych.rb:370:in `load'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych.rb:671:in `block in load_file'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych.rb:670:in `open'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/psych.rb:670:in `load_file'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:83:in `load_from_file'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:54:in `load_file'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:121:in `resolve'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:132:in `block in resolve_inheritance'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:132:in `map'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:132:in `resolve_inheritance'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:56:in `load_file'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:121:in `resolve'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:132:in `block in resolve_inheritance'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:132:in `map'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:132:in `resolve_inheritance'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:56:in `load_file'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/configuration_loader.rb:24:in `load_applicable_config'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/runner.rb:73:in `load_applicable_config'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/runner.rb:22:in `run'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/cli.rb:108:in `scan_for_lints'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/cli.rb:53:in `act_on_options'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/lib/haml_lint/cli.rb:25:in `run'
/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/gems/haml_lint-0.40.0/bin/haml-lint:8:in `<top (required)>'
/opt/hostedtoolcache/Ruby/3.1.2/x64/bin/haml-lint:25:in `load'
/opt/hostedtoolcache/Ruby/3.1.2/x64/bin/haml-lint:25:in `<main>'

SeanLF avatar Jul 28 '22 20:07 SeanLF