From e89c60b4e488f6d8f1a3498ca6d47d6fe0f75568 Mon Sep 17 00:00:00 2001 From: RubenPari Date: Sat, 8 Aug 2026 11:07:39 +0200 Subject: [PATCH 1/2] fix(render): encode non-BMP runes as UTF-16 surrogate pairs in AsciiJSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AsciiJSON escaped every non-ASCII rune with \u%04x, but %04x is a minimum width โ€” for code points above U+FFFF it produced 5+ hex digits, creating a malformed \uXXXXX escape. JSON \u escapes must be exactly 4 hex digits; non-BMP characters must be encoded as UTF-16 surrogate pairs per RFC 8259 ยง7. For example, U+1F600 (๐Ÿ˜€) was emitted as \u1f600 (which decodes to U+1F60 + literal '0') instead of \ud83d\ude00. Fixes #4688 --- render/json.go | 11 +++++++++-- render/render_test.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/render/json.go b/render/json.go index 2f98676c..86fae1f5 100644 --- a/render/json.go +++ b/render/json.go @@ -10,6 +10,7 @@ import ( "html/template" "net/http" "unicode" + "unicode/utf16" "github.com/gin-gonic/gin/codec/json" "github.com/gin-gonic/gin/internal/bytesconv" @@ -164,8 +165,14 @@ func (r AsciiJSON) Render(w http.ResponseWriter) error { for _, r := range bytesconv.BytesToString(ret) { if r > unicode.MaxASCII { - escapeBuf = fmt.Appendf(escapeBuf[:0], "\\u%04x", r) // Reuse escapeBuf - buffer.Write(escapeBuf) + if r <= 0xFFFF { + escapeBuf = fmt.Appendf(escapeBuf[:0], "\\u%04x", r) + buffer.Write(escapeBuf) + } else { + r1, r2 := utf16.EncodeRune(r) + escapeBuf = fmt.Appendf(escapeBuf[:0], "\\u%04x\\u%04x", r1, r2) + buffer.Write(escapeBuf) + } } else { buffer.WriteByte(byte(r)) } diff --git a/render/render_test.go b/render/render_test.go index f63878b9..8acf7da4 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -5,6 +5,7 @@ package render import ( + "encoding/json" "encoding/xml" "errors" "html/template" @@ -269,6 +270,22 @@ func TestRenderAsciiJSONFail(t *testing.T) { require.Error(t, (AsciiJSON{data}).Render(w)) } +func TestRenderAsciiJSONNonBMP(t *testing.T) { + w := httptest.NewRecorder() + data := map[string]string{"msg": "๐Ÿ˜€"} + + err := (AsciiJSON{Data: data}).Render(w) + require.NoError(t, err) + + // Non-BMP characters must be encoded as UTF-16 surrogate pairs + assert.Equal(t, `{"msg":"\ud83d\ude00"}`, w.Body.String()) + + // Verify round-trip: json.Unmarshal should recover the original emoji + var decoded map[string]string + require.NoError(t, json.Unmarshal(w.Body.Bytes(), &decoded)) + assert.Equal(t, "๐Ÿ˜€", decoded["msg"]) +} + func TestRenderPureJSON(t *testing.T) { w := httptest.NewRecorder() data := map[string]any{ From 9aeac9b417ade22ac2dce0d468e52bc0a3f79abd Mon Sep 17 00:00:00 2001 From: RubenPari Date: Sun, 9 Aug 2026 22:37:11 +0200 Subject: [PATCH 2/2] test(render): satisfy testifylint for AsciiJSON non-BMP check Use assert.Contains for the exact UTF-16 surrogate pair wire encoding and assert.JSONEq for semantic equality, instead of assert.Equal on a JSON string which testifylint rejects. --- render/render_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/render/render_test.go b/render/render_test.go index 8acf7da4..4fd4be3a 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -277,8 +277,10 @@ func TestRenderAsciiJSONNonBMP(t *testing.T) { err := (AsciiJSON{Data: data}).Render(w) require.NoError(t, err) - // Non-BMP characters must be encoded as UTF-16 surrogate pairs - assert.Equal(t, `{"msg":"\ud83d\ude00"}`, w.Body.String()) + body := w.Body.String() + // Non-BMP characters must be encoded as UTF-16 surrogate pairs on the wire. + assert.Contains(t, body, `\ud83d\ude00`) + assert.JSONEq(t, `{"msg":"๐Ÿ˜€"}`, body) // Verify round-trip: json.Unmarshal should recover the original emoji var decoded map[string]string