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) } }