From 74b9fa134866daee3cb8d7322462f787cf92b398 Mon Sep 17 00:00:00 2001 From: Percy Wegmann Date: Wed, 14 Aug 2024 12:10:57 -0500 Subject: [PATCH] ipn/localapi: only flush relevant data in multiFilePostResponseWriter.Flush() This prevents two things: 1. Crashing if there's no response body 2. Sending a nonsensical 0 response status code Updates tailscale/corp#22357 Signed-off-by: Percy Wegmann --- ipn/localapi/localapi.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ipn/localapi/localapi.go b/ipn/localapi/localapi.go index 565bb7b4e..3be469193 100644 --- a/ipn/localapi/localapi.go +++ b/ipn/localapi/localapi.go @@ -1752,10 +1752,17 @@ func (ww *multiFilePostResponseWriter) Write(p []byte) (int, error) { } func (ww *multiFilePostResponseWriter) Flush(w http.ResponseWriter) error { - maps.Copy(w.Header(), ww.Header()) - w.WriteHeader(ww.statusCode) - _, err := io.Copy(w, ww.body) - return err + if ww.header != nil { + maps.Copy(w.Header(), ww.header) + } + if ww.statusCode > 0 { + w.WriteHeader(ww.statusCode) + } + if ww.body != nil { + _, err := io.Copy(w, ww.body) + return err + } + return nil } func (h *Handler) singleFilePut(