Files
seaweedfs/weed/storage/needle/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

106 lines
3.3 KiB
Go

package needle
import (
"bytes"
"encoding/base64"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/textproto"
"strings"
"testing"
)
func TestContentCrc32cEncoding(t *testing.T) {
// The CRC32C check value for "123456789" is 0xE3069283.
if got := ContentCrc32c([]byte("123456789")); got != "4waSgw==" {
t.Fatalf("ContentCrc32c = %q, want 4waSgw==", got)
}
c, err := ParseContentCrc32c("4waSgw==")
if err != nil || c != 0xE3069283 {
t.Fatalf("ParseContentCrc32c = %x, %v", c, err)
}
for _, bad := range []string{"", "4waSgw", "4waSgx==", "not base64!", base64.StdEncoding.EncodeToString([]byte{1, 2, 3, 4, 5})} {
if _, err := ParseContentCrc32c(bad); err == nil {
t.Errorf("ParseContentCrc32c(%q) accepted", bad)
}
}
}
// crc32cUploadReq builds the multipart upload the Go uploader sends, with
// the checksum on the request header.
func crc32cUploadReq(t *testing.T, body []byte, gzipped bool, md5, crc string) *http.Request {
t.Helper()
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="file"; filename="chunk.bin"`)
h.Set("Content-Type", "application/octet-stream")
if gzipped {
h.Set("Content-Encoding", "gzip")
}
if md5 != "" {
h.Set("Content-MD5", md5)
}
fw, err := mw.CreatePart(h)
if err != nil {
t.Fatal(err)
}
if _, err := fw.Write(body); err != nil {
t.Fatal(err)
}
if err := mw.Close(); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodPost, "/3,01637037d6", &buf)
req.Header.Set("Content-Type", mw.FormDataContentType())
if crc != "" {
req.Header.Set(ContentCrc32cHeader, crc)
}
return req
}
func TestParseUploadContentCrc32c(t *testing.T) {
data := bytes.Repeat([]byte("chunk bytes from the uploader "), 2000)
gzipped := gzipBytes(t, data)
right := ContentCrc32c(data)
wrong := ContentCrc32c(append([]byte{'x'}, data[1:]...))
md5 := base64.StdEncoding.EncodeToString(md5sum(data))
cases := []struct {
name string
body []byte
gzipped bool
md5 string
crc string
wantErr string
}{
{name: "absent", body: data},
{name: "match", body: data, crc: right},
{name: "mismatch", body: data, crc: wrong, wantErr: "did not match crc32c"},
{name: "malformed", body: data, crc: "AAAA", wantErr: "not the base64"},
{name: "gzipped match", body: gzipped, gzipped: true, crc: right},
{name: "gzipped mismatch", body: gzipped, gzipped: true, crc: wrong, wantErr: "did not match crc32c"},
{name: "gzipped with Content-MD5 match", body: gzipped, gzipped: true, md5: md5, crc: right},
{name: "gzipped with Content-MD5 mismatch", body: gzipped, gzipped: true, md5: md5, crc: wrong, wantErr: "did not match crc32c"},
{name: "corrupt gzip checks raw bytes", body: gzipped[:len(gzipped)/2], gzipped: true, crc: right, wantErr: "did not match crc32c"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
pu, err := ParseUpload(crc32cUploadReq(t, c.body, c.gzipped, c.md5, c.crc), 1<<20, &bytes.Buffer{})
if c.wantErr == "" {
if err != nil {
t.Fatalf("ParseUpload: %v", err)
}
if pu.OriginalDataSize != len(data) {
t.Errorf("OriginalDataSize = %d, want %d", pu.OriginalDataSize, len(data))
}
return
}
if err == nil || !strings.Contains(err.Error(), c.wantErr) {
t.Fatalf("ParseUpload error = %v, want %q", err, c.wantErr)
}
})
}
}