Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveSupport::JSON fails to decode unicode-escaped newline and literal newlines

#3479

With the YAML backend, ActiveSupport::JSON decodes a unicode-escaped newline (or CRLF) as a space. It does the same with a literal newline. But, it deals correctly with a \n-escaped newline.

# unicode-escaped LF
>> ActiveSupport::JSON.decode("\"\\u000a\"")
=> " "
# unicode-escaped CRLF
>> ActiveSupport::JSON.decode("\"\\u000d\\u000a\"")
=> " "
# literal LF
>> ActiveSupport::JSON.decode("\"\n\"")
=> " "
# normal backslash-escaped LF
>> ActiveSupport::JSON.decode("\"\\n\"")
=> "\n"

The JSON gem does this correctly:

>> JSON.parse "[\"\\u000a\"]"
=> ["\n"]

(note that it's in an array because the JSON gem doesn't seem to like decoding just strings. but that's not at issue here; the YAML backend fails when it's in an array too)

This is because the method convert_json_to_yaml changes the escaped newline to a literal newline:

>> ActiveSupport::JSON::Backends::Yaml.send(:convert_json_to_yaml, "[\"\\u000a\"]")
=> "[\"\n\"]"

and then calls YAML.load("["\n"]") which changes the newline to a space:

>> YAML.load "[\"\n\"]"
=> [" "]

I'm not sure if that's correct behavior for YAML or not. But, the result is certainly incorrect for JSON.
A workaround could be to escape newlines in strings before passing them to YAML, after converting from unicode-escaped newline to literal newline. that is, change "\n" to "\n"; change "\r\n" to "\r\n", as YAML correctly converts those to literal newlines.

Reported by Ethan · November 10th, 2009 @ 11:08 PM

State: resolved
Milestone: 2.3.10
Assigned to: nobody
Importance: none

Activity

  1. Ethan
    Ethan

    formatting seems to have gotten lost; that last line was supposed to say:

    change "\n" to "\\n"; change "\r\n" to "\\r\\n", as YAML correctly converts those to literal newlines.
    

    (maybe it will work this time? where's the preview function on this thing?)

    November 11th, 2009 @ 02:01 AM

  2. Maxime RETY
    Maxime RETY
    • Tag changed from activesupport, decode, json, newline, yaml to activesupport, decode, json, newline, patch, yaml

    As you previously reported it, ActiveSupport::JSON fails to decode a unicode-escaped newline with the YAML backend :

    unicode-escaped LF

    ActiveSupport::JSON.decode(""\u000a"") => " "

    With the new Yajl backend, same code works :

    unicode-escaped LF (with Yajl backend)

    ActiveSupport::JSON.decode(""\u000a"") => "\n"

    I did not test with JSON gem, since it's not a default json backend since rails 2.3.6.

    I believe "\n" char is interpreted in YAML as a formatting instruction of the file.
    So newlines need to be escaped as "\n" before passing yaml string to "YAML.load" method.

    I also think it's a newline-only bug. Other "backslash-chars" like "\r" are correctly handled without extra escaping.

    I propose the attached patch to fix the issue in "convert_json_to_yaml" method.
    I added 3 tests to check that bug is fixed with any backend.

    Did you have any other feedback on this issue ?

    June 14th, 2010 @ 03:41 PM

  3. Maxime RETY
    Maxime RETY

    Oops i should have read formatting help more carefully !

    As you previously reported it, ActiveSupport::JSON fails to decode a unicode-escaped newline with the YAML backend :

    # unicode-escaped LF
    ActiveSupport::JSON.decode("\"\\u000a\"")
    => " "

    With the new Yajl backend, same code works :

    # unicode-escaped LF (with Yajl backend)
    ActiveSupport::JSON.decode("\"\\u000a\"")
    => "\n"

    I did not test with JSON gem, since it's not a default json backend since rails 2.3.6.

    I believe "\n" char is interpreted in YAML as a formatting instruction of the file.
    So newlines need to be escaped as "\\n" before passing yaml string to "YAML.load" method.

    I propose the attached patch to fix the issue in "convert_json_to_yaml" method.
    I added 3 tests to check that bug is fixed with any backend.

    Did you have any other feedback on this issue ?

    June 14th, 2010 @ 03:59 PM

  4. Michael Koziarski
  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10

    August 30th, 2010 @ 02:28 AM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:49 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:49 PM

  8. Ethan
    Ethan
    • State changed from stale to open

    This is still reproducing in ActiveSupport 3.0.3. Maxime's patch should be applied.

    [state:open]

    February 2nd, 2011 @ 08:38 PM

  9. Repository
    Repository
    • State changed from open to resolved

    (from [68e3fb81090ba67575e513407fc2463dba3b002b]) Fix JSON decoding of newline character with Yaml backend [#3479 state:resolved]

    Signed-off-by: Santiago Pastorino santiago@wyeworks.com
    https://github.com/rails/rails/commit/68e3fb81090ba67575e513407fc24...

    February 2nd, 2011 @ 09:03 PM

  10. bingbing