mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #9975 from mmangino/raise_when_attributes_cant_be_unserialized
Unserializing YAML attributes can silently fail in development mode
This commit is contained in:
commit
22fee7cbe3
2 changed files with 19 additions and 13 deletions
|
@ -23,19 +23,15 @@ module ActiveRecord
|
|||
def load(yaml)
|
||||
return object_class.new if object_class != Object && yaml.nil?
|
||||
return yaml unless yaml.is_a?(String) && yaml =~ /^---/
|
||||
begin
|
||||
obj = YAML.load(yaml)
|
||||
obj = YAML.load(yaml)
|
||||
|
||||
unless obj.is_a?(object_class) || obj.nil?
|
||||
raise SerializationTypeMismatch,
|
||||
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
|
||||
end
|
||||
obj ||= object_class.new if object_class != Object
|
||||
|
||||
obj
|
||||
rescue ArgumentError, Psych::SyntaxError
|
||||
yaml
|
||||
unless obj.is_a?(object_class) || obj.nil?
|
||||
raise SerializationTypeMismatch,
|
||||
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
|
||||
end
|
||||
obj ||= object_class.new if object_class != Object
|
||||
|
||||
obj
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -43,10 +43,20 @@ module ActiveRecord
|
|||
assert_equal [], coder.load([])
|
||||
end
|
||||
|
||||
def test_load_swallows_yaml_exceptions
|
||||
def test_load_doesnt_swallow_yaml_exceptions
|
||||
coder = YAMLColumn.new
|
||||
bad_yaml = '--- {'
|
||||
assert_equal bad_yaml, coder.load(bad_yaml)
|
||||
assert_raises(Psych::SyntaxError) do
|
||||
coder.load(bad_yaml)
|
||||
end
|
||||
end
|
||||
|
||||
def test_load_doesnt_handle_undefined_class_or_module
|
||||
coder = YAMLColumn.new
|
||||
missing_class_yaml = '--- !ruby/object:DoesNotExistAndShouldntEver {}\n'
|
||||
assert_raises(ArgumentError) do
|
||||
coder.load(missing_class_yaml)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue