From 9d1d7359283ea0b7db97eb238b70befed8200a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Verstraeten?= Date: Sat, 10 Oct 2026 19:32:08 +0200 Subject: [PATCH] Report sanitized tus upload creation errors Extract tus and backend error codes from bounded response bodies to improve failure diagnostics without exposing sensitive messages. Add regression coverage for sanitization. --- machinery/src/cloud/tus_client.go | 26 +++++++++++++++++++++++++- machinery/src/cloud/tus_client_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/machinery/src/cloud/tus_client.go b/machinery/src/cloud/tus_client.go index deae1f5..f71bfe2 100644 --- a/machinery/src/cloud/tus_client.go +++ b/machinery/src/cloud/tus_client.go @@ -10,6 +10,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "sort" "strconv" "strings" @@ -49,6 +50,12 @@ func resumableUploadsEnabled() bool { const tusDefaultChunkSize int64 = 8 << 20 // 8 MiB (>= S3 multipart minimum part size) const tusProgressBucketPercent int64 = 10 +const tusErrorBodyLimit int64 = 16 << 10 + +var ( + tusErrorCodePattern = regexp.MustCompile(`\bERR_[A-Z0-9_]+\b`) + tusBackendErrorPattern = regexp.MustCompile(`\bapi error ([A-Za-z][A-Za-z0-9._-]{0,63}):`) +) // tusChunkSize returns the number of bytes to send per PATCH request. It // defaults to tusDefaultChunkSize (8 MiB) and can be overridden with the @@ -400,11 +407,17 @@ func tusCreate(client *http.Client, baseURL string, size int64, metadata string, if err != nil { return "", 0, err } - io.Copy(io.Discard, resp.Body) if resp.StatusCode != http.StatusCreated { + body, _ := io.ReadAll(io.LimitReader(resp.Body, tusErrorBodyLimit)) + _, _ = io.Copy(io.Discard, resp.Body) + details := tusErrorDetails(body) + if details != "" { + return "", resp.StatusCode, fmt.Errorf("unexpected status creating upload: %s (%s)", resp.Status, details) + } return "", resp.StatusCode, fmt.Errorf("unexpected status creating upload: %s", resp.Status) } + _, _ = io.Copy(io.Discard, resp.Body) location := resp.Header.Get("Location") if location == "" { return "", resp.StatusCode, errors.New("missing Location header in create response") @@ -412,6 +425,17 @@ func tusCreate(client *http.Client, baseURL string, size int64, metadata string, return resolveTusLocation(baseURL, location), resp.StatusCode, nil } +func tusErrorDetails(body []byte) string { + var details []string + if code := tusErrorCodePattern.Find(body); len(code) != 0 { + details = append(details, string(code)) + } + if match := tusBackendErrorPattern.FindSubmatch(body); len(match) == 2 { + details = append(details, "backend="+string(match[1])) + } + return strings.Join(details, ", ") +} + // tusHead performs the tus "offset" request (HEAD) and returns the current // server-side upload offset. func tusHead(client *http.Client, uploadURL string, setHeaders tusHeaderFunc) (int64, int, error) { diff --git a/machinery/src/cloud/tus_client_test.go b/machinery/src/cloud/tus_client_test.go index cb19114..202155a 100644 --- a/machinery/src/cloud/tus_client_test.go +++ b/machinery/src/cloud/tus_client_test.go @@ -934,6 +934,32 @@ func TestUploadKerberosHubFallsBackThroughHubAfterTusCreateServerErrors(t *testi } } +func TestTusCreateReportsSanitizedServerErrorCodes(t *testing.T) { + const sensitiveMessage = "credential=do-not-log-this" + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + _, _ = io.WriteString(w, "ERR_INTERNAL_SERVER_ERROR: operation error S3: CreateMultipartUpload, api error AccessDenied: "+sensitiveMessage) + })) + defer server.Close() + + _, status, err := tusCreate(server.Client(), server.URL, 8, "", func(http.Header, string) {}, "f.mp4") + if status != http.StatusInternalServerError { + t.Fatalf("status = %d, want %d", status, http.StatusInternalServerError) + } + if err == nil { + t.Fatal("error = nil, want create failure") + } + message := err.Error() + for _, want := range []string{"ERR_INTERNAL_SERVER_ERROR", "backend=AccessDenied"} { + if !strings.Contains(message, want) { + t.Errorf("error %q does not contain %q", message, want) + } + } + if strings.Contains(message, sensitiveMessage) { + t.Fatalf("error exposed backend message: %q", message) + } +} + func TestUploadHubResumableDoesNotFallbackAfterCreateClientError(t *testing.T) { srv := newFakeTus() srv.createStatus = http.StatusBadRequest