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.

[PATCH] ActiveSupport::JSON.decode explodes in certain circumstances

#2831

ActiveSupport::JSON.decode has problems with JSON strings which include both quoted slashes and dates:

>> ActiveSupport::JSON.decode('{"r":"http:\/\/foo","d":"2004-06-24"}')
IndexError: index 38 out of string
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/json/decoding.rb:75:in `[]='
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/json/decoding.rb:75:in `convert_json_to_yaml'
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/json/decoding.rb:75:in `each'
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/json/decoding.rb:75:in `convert_json_to_yaml'
    from /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/json/decoding.rb:12:in `decode'

Whether quoted slashes are actually valid JSON is not completely clear, but they are definitely seen in the wild -- the JSON gem's Hash#to_json method produces them, for example.

Reported by Martin Kleppmann · June 24th, 2009 @ 06:44 PM

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

Activity

  1. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to open

    July 2nd, 2009 @ 08:16 PM

  2. Bas van Klinkenberg
    Bas van Klinkenberg
    • Title changed from ActiveSupport::JSON.decode explodes in certain circumstances to [PATCH] ActiveSupport::JSON.decode explodes in certain circumstances

    Here is a patch for this bug. I also believe this solves bug #2273.

    The cause of the bug is that between the moment when the position of the quotes around dates are tracked, and the moment those quotes are removed, the output string has changed size due to removing backslashes and/or decoding unicode sequences. Due to this wrong characters in the string are overwritten, causing Yaml to fail consequently, which causes a ParseError exception.

    This patch moves the code that replaces the quotes above the code that handles the backslashes and unicode sequences, before the string size changes.

    August 1st, 2009 @ 01:40 AM

  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.3.2, activesupport, json to 2.3.2, activesupport, bugmash, json

    August 8th, 2009 @ 01:01 PM

  4. Nikolay Petrachkov
    Nikolay Petrachkov
    • Assigned user set to Pratik

    Patch is verified for above code.

    Before:

    ActiveSupport::JSON.decode('{"r":"http:\/\/foo","d":"2004-06-24"}')
    IndexError: index 38 out of string
        from /opt/rubyee/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/json/backends/yaml.rb:77:in `[]='
        from /opt/rubyee/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/json/backends/yaml.rb:77:in `convert_json_to_yaml'
        from /opt/rubyee/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/json/backends/yaml.rb:77:in `each'
        from /opt/rubyee/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/json/backends/yaml.rb:77:in `convert_json_to_yaml'
        from /opt/rubyee/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/json/backends/yaml.rb:16:in `decode'
        from /opt/rubyee/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/json/decoding.rb:11:in `__send__'
        from /opt/rubyee/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/json/decoding.rb:11:in `decode'
        from (irb):1
    
    

    After patch:

    ActiveSupport::JSON.decode('{"r":"http:\/\/foo","d":"2004-06-24"}')
    => {"d"=>Thu, 24 Jun 2004, "r"=>"http://foo"}
    

    August 8th, 2009 @ 01:18 PM

  5. Rizwan Reza
    Rizwan Reza

    verified

    +1 This patch works perfectly. All tests pass.

    August 8th, 2009 @ 04:25 PM

  6. Repository
    Repository
    • State changed from open to resolved

    (from [0fbeaa98e4e60ca0949be298dae8545807407e1d]) Fixed a bug in JSON decoding with Yaml backend, where a combination of dates, escaped or unicode encoded data and arrays would make the parser fail with a ParseError exception. [#2831 state:resolved]

    Signed-off-by: Yehuda Katz wycats@gmail.com
    http://github.com/rails/rails/commit/0fbeaa98e4e60ca0949be298dae854...

    August 8th, 2009 @ 04:45 PM

  7. CancelProfileIsBroken
    CancelProfileIsBroken
    • Assigned user cleared.
    • Tag changed from 2.3.2, activesupport, bugmash, json to 2.3.2, activesupport, json
    • Milestone cleared.

    August 8th, 2009 @ 11:07 PM

  8. Nathan Beyer
    Nathan Beyer

    This issue is present in the 2.3.5 release. It looks like the patch was only applied to the 2.3.2 branch and didn't make it beyond that.

    Should a new bug be opened?

    February 12th, 2010 @ 10:12 PM

  9. gregpyp
    gregpyp

    this bug is in the 2.3.5 too

    March 14th, 2010 @ 08:02 PM

  10. Nathan Beyer
    Nathan Beyer

    I created a new ticket with the patch ported to the 2-3-stable branch - https://rails.lighthouseapp.com/projects/8994/tickets/4102-ticket-2... .

    March 15th, 2010 @ 12:08 AM

  11. Repository
    Repository

    (from [b99914cc3e58e6444f66006cd4b9b4fa7368afe2]) Fixed a bug in JSON decoding with Yaml backend, where a combination of dates, escaped or unicode encoded data and arrays would make the parser fail with a ParseError exception.

    [#2831]

    Signed-off-by: Yehuda Katz wycats@gmail.com
    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/b99914cc3e58e6444f66006cd4b9b4...

    March 15th, 2010 @ 04:51 AM

  12. ssupreme11
    ssupreme11
    • Importance changed from to

    Its my first time to visit this site and as I was exploring I cant believe that this site was made up of a very informative articles that you should try to have compliment with so as what I am doing now I really love to look forward with more interesting information on this site. Dissertation Writing
    .

    May 10th, 2011 @ 10:37 PM