Serialization 确保真正的参数不会';“t触发器”;您试图将已序列化的内容分配给值。由于安全问题,此功能被禁用。”;
我在rails中创建了一个表单生成器,允许用户构建自己的表单。许多表单输入向Rails提供直接字符串(例如文本字段)。有些提供值数组(如日期选择器)。现在我将这些值存储在一个序列化列中。它工作得很好,并且允许我在发生错误时以最小的工作量重新显示自定义表单。问题是有人进入:Serialization 确保真正的参数不会';“t触发器”;您试图将已序列化的内容分配给值。由于安全问题,此功能被禁用。”;,serialization,rails-activerecord,rails-3.0.10,Serialization,Rails Activerecord,Rails 3.0.10,我在rails中创建了一个表单生成器,允许用户构建自己的表单。许多表单输入向Rails提供直接字符串(例如文本字段)。有些提供值数组(如日期选择器)。现在我将这些值存储在一个序列化列中。它工作得很好,并且允许我在发生错误时以最小的工作量重新显示自定义表单。问题是有人进入: ----------- 在文本字段中,ActiveRecord引发了一个错误,错误是:您试图将已序列化的内容分配给value。由于安全问题,此功能被禁用。 我知道字符串看起来像YAML,但我想知道是否有一种更优雅的方式来解决这
-----------
在文本字段中,ActiveRecord引发了一个错误,错误是:您试图将已序列化的内容分配给value。由于安全问题,此功能被禁用。
我知道字符串看起来像YAML,但我想知道是否有一种更优雅的方式来解决这个问题,因为用户只是输入了一堆破折号来表示他没有电话号码。我希望它基本上能正常地失败,如果有这种情况,可能会删除该值或将其存储为序列化字符串。在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