Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #9975 from mmangino/raise_when_attributes_cant_be_…
…unserialized

Unserializing YAML attributes can silently fail in development mode
  • Loading branch information
tenderlove committed Apr 2, 2013
2 parents f95111e + 1bf6b53 commit 22fee7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
20 changes: 8 additions & 12 deletions activerecord/lib/active_record/coders/yaml_column.rb
Expand Up @@ -23,19 +23,15 @@ def dump(obj)
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)

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
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
end
end
end
Expand Down
14 changes: 12 additions & 2 deletions activerecord/test/cases/coders/yaml_column_test.rb
Expand Up @@ -43,10 +43,20 @@ def test_load_handles_other_classes
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
Expand Down

0 comments on commit 22fee7c

Please sign in to comment.