From 73655e2d6e9ea9975b7a2f6616a0a3ad47f53e67 Mon Sep 17 00:00:00 2001 From: tianrking <10758833+tianrking@users.noreply.github.com> Date: Mon, 5 Oct 2026 04:53:32 +0800 Subject: [PATCH] fix(render): isolate Content-Type response header values --- context_test.go | 36 +++++++++++++ render/render.go | 7 ++- render/render_test.go | 116 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 2 deletions(-) diff --git a/context_test.go b/context_test.go index b82e04d5..7435811c 100644 --- a/context_test.go +++ b/context_test.go @@ -4055,3 +4055,39 @@ func BenchmarkGetMapFromFormData(b *testing.B) { }) } } + +type contentTypeCaseWriter struct { + ResponseWriter +} + +func (w *contentTypeCaseWriter) Write(data []byte) (int, error) { + values := w.Header()["Content-Type"] + for i, value := range values { + values[i] = strings.ReplaceAll(value, "charset=utf-8", "charset=UTF-8") + } + return w.ResponseWriter.Write(data) +} + +func TestContextRendererContentTypeIsolation(t *testing.T) { + router := New() + router.GET("/normalized", func(c *Context) { + c.Writer = &contentTypeCaseWriter{ResponseWriter: c.Writer} + c.JSON(http.StatusOK, H{"message": "ok"}) + }) + router.GET("/plain", func(c *Context) { + c.JSON(http.StatusOK, H{"message": "ok"}) + }) + first := httptest.NewRecorder() + router.ServeHTTP(first, httptest.NewRequest(http.MethodGet, "/normalized", nil)) + t.Cleanup(func() { + first.Header()["Content-Type"][0] = "application/json; charset=utf-8" + }) + require.Equal(t, http.StatusOK, first.Code) + require.Equal(t, "application/json; charset=UTF-8", first.Header().Get("Content-Type")) + second := httptest.NewRecorder() + router.ServeHTTP(second, httptest.NewRequest(http.MethodGet, "/plain", nil)) + require.Equal(t, http.StatusOK, second.Code) + require.Equal(t, "application/json; charset=utf-8", second.Header().Get("Content-Type")) + require.JSONEq(t, first.Body.String(), second.Body.String()) + require.Equal(t, "application/json; charset=UTF-8", first.Header().Get("Content-Type")) +} diff --git a/render/render.go b/render/render.go index 28bc0f5d..77dd85c1 100644 --- a/render/render.go +++ b/render/render.go @@ -4,7 +4,10 @@ package render -import "net/http" +import ( + "net/http" + "slices" +) // Render interface is to be implemented by JSON, XML, HTML, YAML and so on. type Render interface { @@ -37,6 +40,6 @@ var ( func writeContentType(w http.ResponseWriter, value []string) { header := w.Header() if val := header["Content-Type"]; len(val) == 0 { - header["Content-Type"] = value + header["Content-Type"] = slices.Clone(value) } } diff --git a/render/render_test.go b/render/render_test.go index a3f90a3f..f33922e1 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -7,6 +7,7 @@ package render import ( "encoding/xml" "errors" + "fmt" "html/template" "io" "net" @@ -14,6 +15,7 @@ import ( "net/http/httptest" "strconv" "strings" + "sync" "testing" testdata "github.com/gin-gonic/gin/testdata/protoexample" @@ -815,3 +817,117 @@ func TestRenderWriteError(t *testing.T) { require.Error(t, err) assert.Equal(t, "write error", err.Error()) } + +func TestRendererContentTypeResponseIsolation(t *testing.T) { + tests := []struct { + name string + renderer Render + contentType string + }{ + {"JSON", JSON{}, "application/json; charset=utf-8"}, + {"IndentedJSON", IndentedJSON{}, "application/json; charset=utf-8"}, + {"SecureJSON", SecureJSON{}, "application/json; charset=utf-8"}, + {"PureJSON", PureJSON{}, "application/json; charset=utf-8"}, + {"JSONP", JsonpJSON{}, "application/javascript; charset=utf-8"}, + {"AsciiJSON", AsciiJSON{}, "application/json"}, + {"XML", XML{}, "application/xml; charset=utf-8"}, + {"YAML", YAML{}, "application/yaml; charset=utf-8"}, + {"TOML", TOML{}, "application/toml; charset=utf-8"}, + {"ProtoBuf", ProtoBuf{}, "application/x-protobuf"}, + {"BSON", BSON{}, "application/bson"}, + {"HTML", HTML{}, "text/html; charset=utf-8"}, + {"String", String{}, "text/plain; charset=utf-8"}, + {"PDF", PDF{}, "application/pdf"}, + {"Data", Data{ContentType: "application/custom"}, "application/custom"}, + {"Reader", Reader{ContentType: "application/custom"}, "application/custom"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + first := httptest.NewRecorder() + second := httptest.NewRecorder() + tt.renderer.WriteContentType(first) + tt.renderer.WriteContentType(second) + require.Equal(t, tt.contentType, first.Header().Get("Content-Type")) + original := first.Header()["Content-Type"] + t.Cleanup(func() { original[0] = tt.contentType }) + original[0] = "application/first-response" + require.Equal(t, tt.contentType, second.Header().Get("Content-Type")) + second.Header()["Content-Type"][0] = "application/second-response" + require.Equal(t, "application/first-response", first.Header().Get("Content-Type")) + fresh := httptest.NewRecorder() + tt.renderer.WriteContentType(fresh) + require.Equal(t, tt.contentType, fresh.Header().Get("Content-Type")) + }) + } +} + +func TestWriteContentTypeInputIsolation(t *testing.T) { + source := []string{"application/custom", "application/fallback"} + first := httptest.NewRecorder() + second := httptest.NewRecorder() + writeContentType(first, source) + writeContentType(second, source) + source[0] = "application/source-change" + require.Equal(t, []string{"application/custom", "application/fallback"}, first.Header()["Content-Type"]) + require.Equal(t, []string{"application/custom", "application/fallback"}, second.Header()["Content-Type"]) + first.Header()["Content-Type"][1] = "application/response-change" + require.Equal(t, "application/fallback", source[1]) + require.Equal(t, "application/fallback", second.Header()["Content-Type"][1]) + require.Equal(t, "application/source-change", source[0]) +} + +func TestWriteContentTypeExistingValues(t *testing.T) { + tests := []struct { + name string + initial []string + expected []string + }{ + {"nil", nil, []string{"application/default", "application/fallback"}}, + {"empty", []string{}, []string{"application/default", "application/fallback"}}, + {"explicit", []string{"application/explicit"}, []string{"application/explicit"}}, + {"multiple", []string{"application/explicit", "application/other"}, []string{"application/explicit", "application/other"}}, + {"empty value", []string{""}, []string{""}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := httptest.NewRecorder() + w.Header()["Content-Type"] = tt.initial + source := []string{"application/default", "application/fallback"} + writeContentType(w, source) + require.Equal(t, tt.expected, w.Header()["Content-Type"]) + require.Equal(t, []string{"application/default", "application/fallback"}, source) + }) + } +} + +func TestRendererContentTypeConcurrentResponses(t *testing.T) { + const workers = 8 + errors := make(chan string, workers) + var wg sync.WaitGroup + for worker := range workers { + wg.Go(func() { + for range 32 { + w := httptest.NewRecorder() + JSON{}.WriteContentType(w) + if got := w.Header().Get("Content-Type"); got != "application/json; charset=utf-8" { + errors <- fmt.Sprintf("worker %d inherited %q", worker, got) + return + } + w.Header()["Content-Type"][0] = fmt.Sprintf("application/worker-%d", worker) + w.WriteHeader(http.StatusOK) + if got := w.Result().Header.Get("Content-Type"); got != fmt.Sprintf("application/worker-%d", worker) { + errors <- fmt.Sprintf("worker %d response was changed to %q", worker, got) + return + } + } + }) + } + wg.Wait() + close(errors) + for err := range errors { + t.Error(err) + } + fresh := httptest.NewRecorder() + JSON{}.WriteContentType(fresh) + require.Equal(t, "application/json; charset=utf-8", fresh.Header().Get("Content-Type")) +}