diff options
-rw-r--r-- | activesupport/lib/active_support/json/decoding.rb | 2 | ||||
-rw-r--r-- | activesupport/test/json/decoding_test.rb | 14 |
2 files changed, 14 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index 2533ff43e1..21de09c1cc 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -14,7 +14,7 @@ module ActiveSupport # ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}") # => {"team" => "rails", "players" => "36"} def decode(json, options = {}) - data = ::JSON.parse(json, options.merge(create_additions: false)) + data = ::JSON.parse(json, options.merge(create_additions: false, quirks_mode: true)) if ActiveSupport.parse_json_times convert_dates_from(data) else diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index 3ec9b06d6a..e9780b36e4 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -59,7 +59,16 @@ class TestJSONDecoding < ActiveSupport::TestCase %q({"a":"\n"}) => {"a"=>"\n"}, %q({"a":"\u000a"}) => {"a"=>"\n"}, %q({"a":"Line1\u000aLine2"}) => {"a"=>"Line1\nLine2"}, - %q({"json_class":"TestJSONDecoding::Foo"}) => {"json_class"=>"TestJSONDecoding::Foo"} + # prevent json unmarshalling + %q({"json_class":"TestJSONDecoding::Foo"}) => {"json_class"=>"TestJSONDecoding::Foo"}, + # json "fragments" - these are invalid JSON, but ActionPack relies on this + %q("a string") => "a string", + %q(1.1) => 1.1, + %q(1) => 1, + %q(-1) => -1, + %q(true) => true, + %q(false) => false, + %q(null) => nil } TESTS.each_with_index do |(json, expected), index| @@ -83,7 +92,10 @@ class TestJSONDecoding < ActiveSupport::TestCase end def test_failed_json_decoding + assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%(undefined)) } + assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({a: 1})) } assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({: 1})) } + assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%()) } end def test_cannot_force_json_unmarshalling |