Files
seaweedfs/weed/operation/upload_content_crc32c_test.go
T
7c33a8449f volume: check a CRC32C of each chunk upload, in every digest mode (#12999)
* volume: check a CRC32C of each chunk upload, in every digest mode

In sha256 digest mode the uploader sends no Content-MD5, so the volume
server and its replicas store a chunk without checking that its bytes
arrived as sent. The uploader now sends X-Seaweed-Content-Crc32c, the
std-base64 of the big-endian CRC32C of the bytes Content-MD5 is checked
against (the file data after undoing Content-Encoding: gzip; the
ciphertext for ciphered chunks), in both digest modes. Uploads that
forward a stored needle (IsReplication) carry only a value the primary
volume server checked.

The Go and Rust volume servers refuse a mismatch or a malformed value
with 400, as they refuse a wrong Content-MD5, and forward the checked
value to replicas, which check it again. A gzipped upload without
Content-MD5 is checksummed while its size is counted, without holding
the uncompressed bytes. Servers that predate the header ignore it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rs): keep the first part's headers when no part has a filename

Go's parseUpload falls back to the first multipart part with its
Content-Type, Content-MD5 and Content-Encoding. The Rust fallback dropped
them, so a gzipped part without a filename was stored as plain bytes and
its X-Seaweed-Content-Crc32c was checked against the compressed body.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rs): read the RFC 2231 filename* of a multipart part

Go's uploader names a part with mime.FormatMediaType, which writes
filename*=utf-8''... for a non-ASCII name, and Go's Part.FileName()
decodes it. multer reads only filename, so the Rust volume server missed
the file part and lost its name, gzip encoding and Content-MD5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume: send replicas the CRC32C of a rewritten JPEG

Once the orientation fix rewrites a JPEG, the client's CRC32C no longer
covers the bytes the primary forwards, so the replica got no checksum.
Send the CRC32C of the stored bytes instead, in Go and Rust.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rs): read filename* only outside quoted parameter values

A quoted name such as "note; filename*=utf-8''decoy" split at its
semicolon and was taken for the part's filename.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rs): refuse a decoded filename* with control characters

A percent-encoded CR/LF in the name would end the header block of the
part the primary forwards to its replicas.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume: read the forwarded CRC32C with the replica response in the test

A failing assertion left the CRC32C in the one-slot channel, so the next
subtest's replica blocked instead of the test reporting the failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* volume(rs): use naïve for the non-ASCII filename test data

codespell reads the ASCII part of café as a misspelling of calf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-25 12:53:04 -08:00

81 lines
2.9 KiB
Go

package operation
import (
"bytes"
"context"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// TestUploadSendsContentCrc32c checks that each kind of chunk upload carries
// the checksum of the bytes the volume server verifies, and that the volume
// server's parser accepts it.
func TestUploadSendsContentCrc32c(t *testing.T) {
random := make([]byte, 64*1024)
if _, err := rand.Read(random); err != nil {
t.Fatal(err)
}
text := bytes.Repeat([]byte("compressible text chunk "), 4096)
pregzipped, err := util.GzipData(text)
if err != nil {
t.Fatal(err)
}
randomMd5 := md5.Sum(random)
textCrc := needle.ContentCrc32c(text)
// want "any" stands for the ciphertext's checksum, which only the
// volume server's acceptance can confirm.
cases := []struct {
name string
data []byte
option UploadOption
want string
}{
{name: "binary", data: random, option: UploadOption{Filename: "a.bin"}, want: needle.ContentCrc32c(random)},
{name: "gzipped by the uploader", data: text, option: UploadOption{Filename: "a.txt"}, want: textCrc},
{name: "gzipped by the caller", data: pregzipped, option: UploadOption{Filename: "a.txt", IsInputCompressed: true}, want: textCrc},
{name: "with Content-MD5", data: random, option: UploadOption{Filename: "a.bin", Md5: base64.StdEncoding.EncodeToString(randomMd5[:])}, want: needle.ContentCrc32c(random)},
{name: "ciphered", data: text, option: UploadOption{Filename: "a.txt", Cipher: true}, want: "any"},
{name: "replica forward", data: pregzipped, option: UploadOption{IsInputCompressed: true, IsReplication: true, ContentCrc32c: textCrc}, want: textCrc},
{name: "replica forward without one", data: pregzipped, option: UploadOption{IsInputCompressed: true, IsReplication: true}, want: ""},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var gotCrc string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotCrc = r.Header.Get(needle.ContentCrc32cHeader)
if _, _, _, err := needle.CreateNeedleFromRequest(r, false, 1<<20, &bytes.Buffer{}); err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = io.WriteString(w, `{"error":"refused"}`)
t.Errorf("volume server refused the upload: %v", err)
return
}
w.WriteHeader(http.StatusCreated)
_, _ = io.WriteString(w, `{}`)
}))
defer server.Close()
option := c.option
option.UploadUrl = server.URL + "/3,01637037d6"
option.MaxAttempts = 1
if _, err := newUploader(server.Client()).UploadData(context.Background(), c.data, &option); err != nil {
t.Fatalf("UploadData: %v", err)
}
if c.want == "any" && gotCrc != "" {
return
}
if gotCrc != c.want {
t.Fatalf("%s = %q, want %q", needle.ContentCrc32cHeader, gotCrc, c.want)
}
})
}
}