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.
This commit is contained in:
Cédric Verstraeten
2026-10-10 19:32:08 +02:00
parent 7418aedd91
commit 9d1d735928
2 changed files with 51 additions and 1 deletions
+25 -1
View File
@@ -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) {
+26
View File
@@ -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