Add enforcement beyond HTTP header checks to prevent malicious falsification

This commit is contained in:
Jon_K 2026-01-03 01:36:06 -05:00
parent 0a3d1642f7
commit 5c479c9b58

View File

@ -250,13 +250,28 @@ func tusPatchHandler() handleFunc {
return http.StatusInternalServerError, fmt.Errorf("could not seek file: %w", err) return http.StatusInternalServerError, fmt.Errorf("could not seek file: %w", err)
} }
// Calculate maximum bytes we should accept to prevent quota bypass
maxBytesToWrite := uploadLength - uploadOffset
if maxBytesToWrite <= 0 {
return http.StatusBadRequest, fmt.Errorf("upload already complete")
}
// Use LimitReader to enforce the declared upload length
// This prevents clients from bypassing quota by falsifying Upload-Length header
defer r.Body.Close() defer r.Body.Close()
bytesWritten, err := io.Copy(openFile, r.Body) limitedReader := io.LimitReader(r.Body, maxBytesToWrite)
bytesWritten, err := io.Copy(openFile, limitedReader)
if err != nil { if err != nil {
return http.StatusInternalServerError, fmt.Errorf("could not write to file: %w", err) return http.StatusInternalServerError, fmt.Errorf("could not write to file: %w", err)
} }
newOffset := uploadOffset + bytesWritten newOffset := uploadOffset + bytesWritten
// Verify we haven't exceeded the declared upload length (defense in depth)
if newOffset > uploadLength {
return http.StatusBadRequest, fmt.Errorf("upload exceeded declared length")
}
w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10)) w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10))
if newOffset >= uploadLength { if newOffset >= uploadLength {