runtime: [System.Text.Json] UnsafeRelaxedJsonEscaping can result in StackOverflowException

var test = new { Name = "测试11" };
Console.WriteLine(JsonSerializer.Serialize(test, new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));

Here’s what’s causing the StackOverflow:

image


Original text:

From @Zonciu on Tuesday, September 10, 2019 2:16:16 PM

Describe the bug

WebApi returning specific content cause stack overflow

To Reproduce

Reproduction project

WebApplication.zip

Steps to reproduce the behavior:

  1. Using this version of NET Core 3.0 Preview 9
  2. dotnet new webapi
  3. Add controller methods:
    public class Test
    {
        public string Name { get; set; }
    }

    [ApiController]
    [Route("test")]
    public class TestController : ControllerBase
    {
        [HttpGet("1")]
        public Test Get([FromServices] ILogger<Test> logger)
        {
            logger.LogInformation("Get test 2");
            return new Test {Name = "测试"};
        }

        [HttpGet("2")]
        public Test Get2([FromServices] ILogger<Test> logger)
        {
            logger.LogInformation("Get test 2");
            return new Test {Name = "测试11"};
        }
    }
  1. Access /test/1 is ok, but /test/2 causing stack overflow and crashed.

Expected behavior

Access /test/2, return {"name":"测试1"}

Screenshots

/test/1 image

/test/2 image

Additional context

dotnet --info output

.NET Core SDK(反映任何 global.json):
 Version:   3.0.100-preview9-014004
 Commit:    8e7ef240a5

运行时环境:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.0.100-preview9-014004\

Host (useful for support):
  Version: 3.0.0-preview9-19423-09
  Commit:  2be172345a

.NET Core SDKs installed:
  2.1.801 [C:\Program Files\dotnet\sdk]
  3.0.100-preview9-014004 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview9.19424.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview9-19423-09 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview9-19423-09 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Copied from original issue: aspnet/AspNetCore#13856

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@xcaptain 3.0 rc1 doesn’t contain this fix, you need to use Newtonsoft.Json, or try the daily build I’d tested it in docker (nightly build 09/17/2019), it works.