Because of this security advisory serialized attributes need to use YAML.safe_load with a safe list of allowed Classes.
The problem I am having is that I want to use a custom class (Foo::Bar) and it seems like at least in Rails 6.1 that you need to set the allowed classes towards the beginning of the boot process in application.rb
.
config.active_record.yaml_column_permitted_classes = [
Symbol,
String,
Foo::Bar
]
The problem is that running this (as is) inside application.rb
gives this error:
uninitialized constant AppName::Application::Foo (NameError)
If I add to the top of the application.rb
a require statement, then the application boots fine, but, in dev I then get warnings (because classes and their constants are redefined)
warning: previous definition of SomeConstant was here
The only cleanish way I've found is instead of using the config at all, you just set what rails really wants to be set which is this ->
# in some initializer
ActiveRecord::Base.yaml_column_permitted_classes = [
Symbol,
String,
Foo::Bar
]
That doesn't give any errors, but, it feels like I'm going outside of what Rails wants me to do.
Is there a correct way to initialize custom classes in the application.rb
?
config
gets passed into theafter_initialize
block so I just had to useconfig.active_record.yaml_column_permitted_classes = [ ... ]
inside of theconfig.after_initialize
and I kept it inconfig/application.rb
as well to keep it clean and consistent. – Wentzel