Skip to content

Audit and fix uses of YAML.load_file and YAML.safe_load for Ruby 3.1

YAML.load_file in Ruby 3.1/Psych4 changed to default to YAML.safe_load. This breaks a number of our tests because of the reasons documented below:

  1. https://github.com/ruby/psych/issues/533
  2. https://www.ctrl.blog/entry/ruby-psych4.html

For example, in https://gitlab.com/gitlab-org/gitlab/-/jobs/3918495253 we see this Unknown alias: label_definition:

 1) ProjectsController updates Service Desk attributes
     Failure/Error: raise Gitlab::ImportExport::Error, shared.errors.to_sentence
     Gitlab::ImportExport::Error:
       Unknown alias: label_definition
     # ./app/services/projects/import_export/export_service.rb:132:in `notify_error!'
     # ./app/services/projects/import_export/export_service.rb:60:in `save_all!'
     # ./app/services/projects/import_export/export_service.rb:18:in `execute'
     # ./app/workers/project_export_worker.rb:25:in `perform'
     # ./spec/factories/projects.rb:211:in `block (4 levels) in <top (required)>'
     # ./spec/controllers/projects_controller_spec.rb:10:in `block (2 levels) in <top (required)>'

This fix is this:

diff --git a/lib/gitlab/import_export/config.rb b/lib/gitlab/import_export/config.rb
index 423e0933605e..bcd283a32b3f 100644
--- a/lib/gitlab/import_export/config.rb
+++ b/lib/gitlab/import_export/config.rb
@@ -52,7 +52,7 @@ def merge_ee?
       end
 
       def parse_yaml
-        YAML.load_file(@config)
+        YAML.load_file(@config, aliases: true)
       end
     end
   end

YAML.safe_load also needs to be checked:

RSpec::Retry: 2nd try ./spec/lib/gitlab/import_export/project/exported_relations_merger_spec.rb:58
Failures:
  1) Gitlab::Ci::YamlProcessor::Result#config_metadata returns expanded yaml config
     Failure/Error: expanded_config = YAML.safe_load(config_metadata[:merged_yaml], [Symbol])
     ArgumentError:
       wrong number of arguments (given 2, expected 1)

Failing tests:

  1. https://gitlab.com/gitlab-org/gitlab/-/jobs/3919091809: ./lib/gitlab/bullet/exclusions.rb:30
  2. https://gitlab.com/gitlab-org/gitlab/-/jobs/3919091810: ./lib/backup/manager.rb:250, Tried to load unspecified class: ActiveSupport::TimeWithZone
Edited by Stan Hu