Utf8Json icon indicating copy to clipboard operation
Utf8Json copied to clipboard

Deserialize from stream introduces a serious vulnerability due to shared buffers

Open tomasfreund opened this issue 5 years ago • 2 comments

I have encountered an issue when implementing error handling for malformed json in my web application, instead of unexpected end of string I was getting error messages with unexpected char that was not even in the json. After some digging I discovered an exploit that enables an attacker to read data from previously serialized objects. This exploit can be used to target any web application that uses the MVC formatters in this repository or parses json from stream using this library. The example below illustrates the issue.

            for (int i = 0; i < 1000; i++)
            {
                Utf8Json.JsonSerializer.Serialize(new {SomeValue = "ABCDEFGHIJKLMNOPQRSTUVWKYZ1234567890"});
            }

            for (int i = 0; i < 1000; i++)
            {
                try
                {
                    var ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("{\"SomeValue\":\""));
                    var value = Utf8Json.JsonSerializer.Deserialize<dynamic>(ms);
                    // outputs 'ABCDEFGHIJKLMNOPQRSTUVWKYZ1234567890'
                    Console.WriteLine(value["SomeValue"]);
                }
                catch (Exception ex)
                {
                    
                }
            }

This issue is caused by sharing buffers that are never cleared and could be solved by clearing the buffers or by passing the length to the reader in adittion to the buffer.

There is already an issue (#127) regarding this (6 months old) but it does not mention the security impact (especially for web applications using the formatters)

tomasfreund avatar Apr 17 '19 13:04 tomasfreund

it seems serious too me.

unikzforce avatar May 26 '19 09:05 unikzforce

Is there any update for this?

MichaelTontchev avatar Dec 29 '20 23:12 MichaelTontchev