Skip to content

string values with leading hyphenated line cause error when converted to YAML #137

@tardate

Description

@tardate

I ran into this issue with psych 1.3.4 with ActiveRecord models where string attributes contained something that 'looks like' YAML.

item = Item.last
item.description = %{- a\n2013-04-08 11:45:33}
item.to_yaml # error

Although the original issue was found in ActiveRecord, it can be shown more generally serializing a Hash:

{ 'key' => %{2013-04-08 11:45:33} }.to_yaml # ok
=> "---\nkey: '2013-04-08 11:45:33'\n"

{ 'key' => %{- 2013-04-08 11:45:33} }.to_yaml # ok
=> "---\nkey: ! '- 2013-04-08 11:45:33'\n"

{ 'key' => %{- a\n2013-04-08 11:45:33} }.to_yaml # error
TypeError: can't convert nil into Integer
from ..gems/psych-1.3.4/lib/psych/scalar_scanner.rb:111:in `utc'
from ..gems/psych-1.3.4/lib/psych/scalar_scanner.rb:111:in `parse_time'

# but if the date format parses correctly, it doesn't fail, but the string has been incorrectly converted to yaml: 
{ 'key' => %{- a\n2013-04-08 11:45} }.to_yaml
=> "---\nkey: ! '- a\n\n  2013-04-08 11:45'\n"

It seems like the same date handling error in #82 but the real issue (question?) seems to be that the leading '- string\n' sequence is causing psych to try to interpret my text as yaml when in fact it should all be treated as a string literal.

Is this expected behaviour?

Especially in the ActiveRecord case, can we control such behaviour so that user-generated content doesn't unexpectedly break psych?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions