在 Rails 3.0.20 LTS 中,他们对代码进行了修补,以检查发送到序列化列的 YAML 字符串。我已经覆盖了模型上的赋值方法来修复字符串而不是引发错误:
module ActiveRecord
module AttributeMethods
module Write
extend ActiveSupport::Concern
included do
attribute_method_suffix "="
end
module ClassMethods
protected
def define_method_attribute=(attr_name)
if self.serialized_attributes[attr_name]
generated_attribute_methods.send(:define_method, "#{attr_name}=") do |new_value|
if new_value.is_a?(String) and new_value =~ /^---/
raise ActiveRecordError, "You tried to assign already serialized content to #{attr_name}. This is disabled due to security issues."
end
write_attribute(attr_name, new_value)
end
elsif attr_name =~ /^[a-zA-Z_]\w*[!?=]?$/
generated_attribute_methods.module_eval("def #{attr_name}=(new_value); write_attribute('#{attr_name}', new_value); end", __FILE__, __LINE__)
else
generated_attribute_methods.send(:define_method, "#{attr_name}=") do |new_value|
write_attribute(attr_name, new_value)
end
end
end
end
...
我想在这里使用 super(new_value) 来允许原始方法进行分配,但不幸的是它似乎绕过了检查(因此也绕过了安全措施)。
def value=(new_value)
if new_value.is_a?(String) and new_value =~ /^---/
new_value.gsub!(/^-+/, '-')
end
write_attribute(:value, new_value)
end