From 8a886ddc3849e512dc559b367a3f81d04b36bdd2 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 17 Sep 2026 12:33:45 +0000 Subject: [PATCH] fix(webui): do not hand back an error as a .log download The download sets its attachment headers before the read, because the body streams straight from the reader and a log is unbounded input -- buffering it to size the response would defeat the reason it streams. Nothing then cleared those headers, so a 404 for an attempt with no log went out as Content-Disposition: attachment; filename="task-1-attempt-0.log" Content-Type: application/x-ndjson A browser treats that as a download: the caller gets a .log file containing the text "no log recorded for this attempt". A stale or direct download URL is enough to trigger it. The 503 for a process with no reader had the same shape. Both error paths now clear the attachment headers before writing, so the response is an error rather than a file. The successful path is unchanged. Red first: the existing test asserted the status only, which is how this passed. It now asserts the headers are cleared on a 404 and on a 503. --- internal/webui/api_tasks_log_download_test.go | 14 ++++++++++++++ internal/webui/api_tasks_logs.go | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/internal/webui/api_tasks_log_download_test.go b/internal/webui/api_tasks_log_download_test.go index 40d23a9ad..630bf05d1 100644 --- a/internal/webui/api_tasks_log_download_test.go +++ b/internal/webui/api_tasks_log_download_test.go @@ -119,6 +119,16 @@ func TestHandleTaskLogsDownloadWithoutALogAnswersNotFound(t *testing.T) { if w.Code != http.StatusNotFound { t.Fatalf("GET %s with no log = %d, want 404", url, w.Code) } + // The response is an error, not a file. Attachment headers are written + // before the read (the body streams, so the size is unknown up front), and + // leaving them on a 404 hands the caller a .log attachment containing only + // the error text. + if got := w.Header().Get("Content-Disposition"); got != "" { + t.Errorf("Content-Disposition = %q on a 404, want it cleared so the error is not presented as a download", got) + } + if got := w.Header().Get("Content-Type"); strings.Contains(got, "ndjson") { + t.Errorf("Content-Type = %q on a 404, want the error content type rather than the log's", got) + } } // TestHandleTaskLogsDownloadWithoutAReaderAnswersServiceUnavailable is the @@ -144,4 +154,8 @@ func TestHandleTaskLogsDownloadWithoutAReaderAnswersServiceUnavailable(t *testin body, _ := io.ReadAll(w.Result().Body) t.Fatalf("GET %s without a reader = %d (%s), want 503", url, w.Code, body) } + // Same rule as the 404: an unavailable reader is an error, not a file. + if got := w.Header().Get("Content-Disposition"); got != "" { + t.Errorf("Content-Disposition = %q on a 503, want it cleared so the error is not presented as a download", got) + } } diff --git a/internal/webui/api_tasks_logs.go b/internal/webui/api_tasks_logs.go index 18e045953..1e9c9a29d 100644 --- a/internal/webui/api_tasks_logs.go +++ b/internal/webui/api_tasks_logs.go @@ -98,12 +98,23 @@ func (s *Server) handleTaskLogDownload(w http.ResponseWriter, r *http.Request) { // the reader into the response: a log file is unbounded input, so // buffering it to size the response would defeat the reason the reader // streams at all. The attempt's own name is known up front. + // + // Everything after this point must therefore CLEAR them before writing an + // error, never add to them: a 404 still carrying `Content-Disposition: + // attachment` is a browser download of the error text under a .log + // filename, and the NDJSON type would misdescribe it too. w.Header().Set("Content-Type", "application/x-ndjson") w.Header().Set("Content-Disposition", `attachment; filename="`+taskLogFilename(id, attempt)+`"`) + clearAttachment := func() { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.Header().Del("Content-Disposition") + } + found, err := reader.TaskLogContent(r.Context(), id, attempt, w) if err != nil { if errors.Is(err, logging.ErrTaskLogsUnavailable) { + clearAttachment() http.Error(w, err.Error(), http.StatusServiceUnavailable) return } @@ -114,6 +125,7 @@ func (s *Server) handleTaskLogDownload(w http.ResponseWriter, r *http.Request) { return } if !found { + clearAttachment() http.Error(w, "no log recorded for this attempt", http.StatusNotFound) } }