OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[31583] avoid parsing strings to yaml
Oliver Günther269d91cfb964
diff --git a/lib/open_project/configuration.rb b/lib/open_project/configuration.rbindex 6d189837bae..9fd2621a48e 100644--- a/lib/open_project/configuration.rb+++ b/lib/open_project/configuration.rb@@ -406,15 +406,21 @@ module OpenProject# using YAML.## @param key [String] The key of the input within the source hash.- # @param value [String] The string from which to extract the actual value.+ # @param original_value [String] The string from which to extract the actual value.# @return A ruby object (e.g. Integer, Float, String, Hash, Boolean, etc.)# @raise [ArgumentError] If the string could not be parsed.- def extract_value(key, value)+ def extract_value(key, original_value)# YAML parses '' as false, but empty ENV variables will be passed as that.# To specify specific values, one can use !!str (-> '') or !!null (-> nil)- return value if value == ''+ return original_value if original_value == ''- YAML.load(value)+ parsed = YAML.load(original_value)++ if parsed.is_a?(String)+ original_value+ else+ parsed+ endrescue StandardError => eraise ArgumentError, "Configuration value for '#{key}' is invalid: #{e.message}"enddiff --git a/spec/lib/open_project/configuration_spec.rb b/spec/lib/open_project/configuration_spec.rbindex 2cb1d209690..63191b52990 100644--- a/spec/lib/open_project/configuration_spec.rb+++ b/spec/lib/open_project/configuration_spec.rb@@ -99,6 +99,7 @@ describe OpenProject::Configuration do'nil' => 'foobar','str_empty' => 'foobar','somesetting' => 'foo',+ 'invalid_yaml' => nil,'some_list_entry' => nil,'nested' => {'key' => 'value',@@ -120,7 +121,7 @@ describe OpenProject::Configuration do'SOMEEMPTYSETTING' => '','SOMESETTING' => 'bar','NIL' => '!!null',- 'STR_EMPTY' => '!!str',+ 'INVALID_YAML' => "'foo'! #234@@½%%%",'OPTEST_SOME__LIST__ENTRY' => '[foo, bar , xyz, whut wat]','OPTEST_NESTED_KEY' => 'baz','OPTEST_NESTED_DEEPLY__NESTED_KEY' => '42',@@ -135,6 +136,10 @@ describe OpenProject::Configuration doOpenProject::Configuration.send :override_config!, config, env_varsend+ it 'returns the original string, not the invalid YAML one' do+ expect(config['invalid_yaml']).to eq env_vars['INVALID_YAML']+ end+it 'should not parse the empty value' doexpect(config['someemptysetting']).to eq('')end@@ -143,10 +148,6 @@ describe OpenProject::Configuration doexpect(config['nil']).to be_nilend- it 'should parse the empty string' do- expect(config['str_empty']).to eq('')- end-it 'should override the previous setting value' doexpect(config['somesetting']).to eq('bar')end