fix(render): stop mutating the DataFromReader headers map

This commit is contained in:
Suryakant Sahoo 2026-10-24 06:55:56 +05:30 committed by GitHub
parent 0f09c3a9b4
commit 20acf62450
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,10 +22,16 @@ type Reader struct {
func (r Reader) Render(w http.ResponseWriter) (err error) {
r.WriteContentType(w)
if r.ContentLength >= 0 {
if r.Headers == nil {
r.Headers = map[string]string{}
// Copy the caller's headers map before adding Content-Length so the
// map passed via DataFromReader is never mutated. Mutating it leaks a
// stale Content-Length into later responses that reuse the map and
// races when concurrent requests share it. See #4899.
headers := make(map[string]string, len(r.Headers)+1)
for k, v := range r.Headers {
headers[k] = v
}
r.Headers["Content-Length"] = strconv.FormatInt(r.ContentLength, 10)
headers["Content-Length"] = strconv.FormatInt(r.ContentLength, 10)
r.Headers = headers
}
r.writeHeaders(w)
_, err = io.Copy(w, r.Reader)