##// END OF EJS Templates
Make sure that settings are unserialized as UTF-8 encoded strings (#19305)....
Jean-Philippe Lang -
r13730:ed2a3a224498
parent child
Show More
@@ -91,7 +91,10 class Setting < ActiveRecord::Base
91 def value
91 def value
92 v = read_attribute(:value)
92 v = read_attribute(:value)
93 # Unserialize serialized settings
93 # Unserialize serialized settings
94 v = YAML::load(v) if available_settings[name]['serialized'] && v.is_a?(String)
94 if available_settings[name]['serialized'] && v.is_a?(String)
95 v = YAML::load(v)
96 v = force_utf8_strings(v)
97 end
95 v = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank?
98 v = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank?
96 v
99 v
97 end
100 end
@@ -238,6 +241,25 END_SRC
238 load_plugin_settings
241 load_plugin_settings
239
242
240 private
243 private
244
245 def force_utf8_strings(arg)
246 if arg.is_a?(String)
247 arg.dup.force_encoding('UTF-8')
248 elsif arg.is_a?(Array)
249 arg.map do |a|
250 force_utf8_strings(a)
251 end
252 elsif arg.is_a?(Hash)
253 arg = arg.dup
254 arg.each do |k,v|
255 arg[k] = force_utf8_strings(v)
256 end
257 arg
258 else
259 arg
260 end
261 end
262
241 # Returns the Setting instance for the setting named name
263 # Returns the Setting instance for the setting named name
242 # (record found in database or new record with default value)
264 # (record found in database or new record with default value)
243 def self.find_or_default(name)
265 def self.find_or_default(name)
@@ -1,3 +1,5
1 # encoding: utf-8
2 #
1 # Redmine - project management software
3 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
4 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 #
5 #
@@ -101,4 +103,25 class SettingTest < ActiveSupport::TestCase
101 assert_equal [10, 25, 50], Setting.per_page_options_array
103 assert_equal [10, 25, 50], Setting.per_page_options_array
102 end
104 end
103 end
105 end
106
107 def test_setting_serialied_as_binary_should_be_loaded_as_utf8_encoded_strings
108 yaml = <<-YAML
109 ---
110 - keywords: !binary |
111 Zml4ZXMsY2xvc2VzLNC40YHQv9GA0LDQstC70LXQvdC+LNCz0L7RgtC+0LLQ
112 vizRgdC00LXQu9Cw0L3QvixmaXhlZA==
113
114 done_ratio: "100"
115 status_id: "5"
116 YAML
117
118 Setting.commit_update_keywords = {}
119 assert_equal 1, Setting.where(:name => 'commit_update_keywords').update_all(:value => yaml)
120 Setting.clear_cache
121
122 assert_equal 'UTF-8', Setting.commit_update_keywords.first['keywords'].encoding.name
123 ensure
124 Setting.where(:name => 'commit_update_keywords').delete_all
125 Setting.clear_cache
126 end
104 end
127 end
General Comments 0
You need to be logged in to leave comments. Login now