ruby / psych

A libyaml wrapper for Ruby
MIT License
564 stars 203 forks source link

Psych::DisallowedClass when loading fixtures #658

Closed dylan-hoefsloot closed 10 months ago

dylan-hoefsloot commented 10 months ago

Steps to reproduce

I upgraded ruby from 2.7.6 to 3.1.4

Run any rspec tests which load fixtures that have a field that is a time

Expected behavior

Fixtures are loaded and tests run

Actual behavior

I get the following error when trying to run any specs

   Failure/Error: raise DisallowedClass.new('load', klassname)

       Psych::DisallowedClass:
         Tried to load unspecified class: Time
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixture_set/file.rb:50:in `raw_rows'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixture_set/file.rb:39:in `config_row'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixture_set/file.rb:29:in `model_class'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:685:in `block (2 levels) in read_fixture_files'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixture_set/file.rb:17:in `open'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:684:in `block in read_fixture_files'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:683:in `each'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:683:in `each_with_object'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:683:in `read_fixture_files'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:631:in `initialize'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:576:in `new'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:576:in `block in read_and_insert'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:574:in `map'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:574:in `read_and_insert'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/fixtures.rb:545:in `create_fixtures'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/test_fixtures.rb:223:in `load_fixtures'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/test_fixtures.rb:120:in `setup_fixtures'
       # /usr/local/bundle/gems/activerecord-6.0.6.1/lib/active_record/test_fixtures.rb:8:in `before_setup'
       # /usr/local/bundle/gems/activejob-6.0.6.1/lib/active_job/test_helper.rb:45:in `before_setup'

System configuration

Rails version: 6.0.6.1 Ruby version: 3.1.4

Solutions I have tried

Adding the following to config/application.rb (which is suggested in every other issue pertaining to this error I can find)

config.active_record.use_yaml_unsafe_load = true
config.active_record.yaml_column_permitted_classes = [Symbol, Hash, Array, ActiveSupport::TimeWithZone, ActiveSupport::TimeZone, ActiveSupport::HashWithIndifferentAccess, ActiveModel::Attribute.const_get(:FromDatabase), Time]

Confirming it is the fixtures throwing the error by commenting out

config.global_fixtures = :all

Confirming it is the time in the fixtures by commenting out usages such as

published_at: 2010-05-16 14:00

Downgrading back to 2.7.6 works but this is not ideal as I am trying to upgrade ruby to a newer version

hsbt commented 10 months ago

It's expected behavior. see https://github.com/ruby/psych/issues/564#issuecomment-1184520981

dylan-hoefsloot commented 10 months ago

@hsbt It might be expected behaviour for these errors to be appearing by default but shouldn't setting yaml_column_permitted_classes fix this issue? or this an activerecord issue instead?

hsbt commented 10 months ago

It's rails or activerecord issues. You may resolve this with downgrading Psych version.