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.

JSON parser fails to read escaped backslashes

#973

ActiveSupport::JSON cannot parse a string as a hash value that end with backslashes if there are subsequent values in the hash, ie:

string = "{[bad]("\\","trailing":""}") puts string

=> {bad

p ActiveSupport::JSON.decode(string)

=> ActiveSupport::JSON::ParseError: Invalid JSON string

The json gem parser works correctly:

require 'json' p JSON.parse(string)

=>{"bad"=>"\", "trailing"=>""}

There is no problem if the backslash is not the last character, or if there is no subsequent values in the hash:

string = "{[good]("\\x","trailing":""}") puts string

=> {good

p ActiveSupport::JSON.decode(string)

=> {"good"=>"\x", "trailing"=>""}

string = "{[good]("\\"}") puts string

=> {good

p ActiveSupport::JSON.decode(string)

=> {"good"=>"\"}

Reported by Daniel Sheppard · September 5th, 2008 @ 03:03 AM

State: committed
Milestone: 2.3.4
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Daniel Sheppard
    Daniel Sheppard

    Bah, wasn't expecting markdown.

    ActiveSupport::JSON cannot parse a string as a hash value that end with backslashes if there are subsequent values in the hash, ie:

    
    string = "{\"bad\":\"\\\\\",\"trailing\":\"\"}"
    puts string
    #=> {"bad":"\\","trailing":""}
    p ActiveSupport::JSON.decode(string)
    #=> ActiveSupport::JSON::ParseError: Invalid JSON string
    

    The json gem parser works correctly:

    
    require 'json'
    p JSON.parse(string)
    #=>{"bad"=>"\\", "trailing"=>""}
    

    There is no problem if the backslash is not the last character, or if there is no subsequent values in the hash:

    
    string = "{\"good\":\"\\\\x\",\"trailing\":\"\"}"
    puts string
    #=> {"good":"\\x","trailing":""}
    p ActiveSupport::JSON.decode(string)
    #=> {"good"=>"\\x", "trailing"=>""}
    
    string = "{\"good\":\"\\\\\"}"
    puts string
    #=> {"good":"\\"}
    p ActiveSupport::JSON.decode(string)
    #=> {"good"=>"\\"}
    

    September 5th, 2008 @ 03:07 AM

  2. Daniel Sheppard
    Daniel Sheppard
    • Title changed from JSON parser fails to read escaped backslashes to [PATCH] JSON parser fails to read escaped backslashes
    • Tag changed from activesupport to activesupport, patch, tested

    Stringscanner was thinking that it was still within quotes due to treating the second slash as an escape character. Patch fixes.

    September 5th, 2008 @ 03:37 AM

  3. bmulloy
    bmulloy

    with this patch it seems that \ becomes .

    \ -> \ (1 stays as 1) \ -> \ (2 becomes 1) \\ -> \ (3 becomes 2) \\ -> \ (4 becomes 2)

    December 1st, 2008 @ 04:42 AM

  4. bmulloy
    bmulloy

    my bad. please ignore the above comment. the patch is fine.

    December 1st, 2008 @ 04:45 AM

  5. Pratik
    Pratik
    • Title changed from [PATCH] JSON parser fails to read escaped backslashes to JSON parser fails to read escaped backslashes

    Removing '[PATCH]' prefix

    December 22nd, 2008 @ 02:37 PM

  6. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to incomplete

    I can't apply the patch anymore. Could you please upload a new patch using git-format-patch ? http://guides.rails.info/contrib... should be helpful.

    Thanks !

    March 8th, 2009 @ 01:51 PM

  7. Daniel Sheppard
    Daniel Sheppard

    I don't have an edge environment around

    It's literally a three line change (including tests) - Can't you just read the patch and replay?

    March 9th, 2009 @ 11:11 PM

  8. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activesupport, patch, tested to activesupport, bugmash, patch, tested

    August 5th, 2009 @ 03:25 PM

  9. Nikolay Petrachkov
    Nikolay Petrachkov

    Verified problem that described above.

    I've attached a patch. That is fixed patch by Daniel Sheppard.

    August 8th, 2009 @ 12:44 PM

  10. Nikolay Petrachkov
    Nikolay Petrachkov
    • Assigned user changed from Pratik to José Valim

    and i've attached a patch for master. Previous one was for 2-3-stable.

    August 9th, 2009 @ 08:34 AM

  11. Rizwan Reza
    Rizwan Reza

    verified

    +1 Both patches apply cleanly to the respective branches. All tests pass.

    August 9th, 2009 @ 04:11 PM

  12. Repository
    Repository
    • State changed from incomplete to committed

    (from [9341655fa30ffc245093493422c9abe72eeb64ed]) Fix that JSON parser fails to read escaped backslashes.

    [#973 state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/9341655fa30ffc245093493422c9ab...

    August 10th, 2009 @ 06:32 AM

  13. Repository
    Repository

    (from [b3381cacaf7735ec6eb108e378ba255ebf6ffb14]) Fix that JSON parser fails to read escaped backslashes.

    [#973 state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/b3381cacaf7735ec6eb108e378ba25...

    August 10th, 2009 @ 06:32 AM

  14. Jeremy Kemper
    Jeremy Kemper
    • Tag changed from activesupport, bugmash, patch, tested to activesupport, patch, tested
    • Milestone changed from 2.x to 2.3.4

    August 10th, 2009 @ 06:32 AM

  15. bingbing