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

52 lines
1.9 KiB
Go

package needle
import (
"encoding/base64"
"encoding/binary"
"fmt"
)
// ContentCrc32cHeader is the request header an uploader sets to the CRC32C
// (Castagnoli) of a chunk's file data, so the volume server and its replicas
// refuse bytes that changed in transit. It covers the bytes Content-MD5 is
// checked against: the file data after undoing Content-Encoding: gzip. The
// value is the std-base64 of the big-endian checksum, the encoding of S3's
// x-amz-checksum-crc32c. The name stays outside PairNamePrefix, so it is never
// stored as a needle pair.
const ContentCrc32cHeader = "X-Seaweed-Content-Crc32c"
// EncodeContentCrc32c formats a checksum as a ContentCrc32cHeader value.
func EncodeContentCrc32c(c CRC) string {
var b [4]byte
binary.BigEndian.PutUint32(b[:], uint32(c))
return base64.StdEncoding.EncodeToString(b[:])
}
// ContentCrc32c is the ContentCrc32cHeader value for data.
func ContentCrc32c(data []byte) string {
return EncodeContentCrc32c(NewCRC(data))
}
// ParseContentCrc32c reads a ContentCrc32cHeader value. Strict decoding takes
// only the canonical form, as the Rust volume server does.
func ParseContentCrc32c(value string) (CRC, error) {
b, err := base64.StdEncoding.Strict().DecodeString(value)
if err != nil || len(b) != 4 {
return 0, fmt.Errorf("%s [%s] is not the base64 of a 4-byte crc32c", ContentCrc32cHeader, value)
}
return CRC(binary.BigEndian.Uint32(b)), nil
}
// checkContentCrc32c refuses file data whose checksum differs from the one
// the uploader sent, in the style of the Content-MD5 check.
func checkContentCrc32c(expected string, actual CRC, size int) error {
want, err := ParseContentCrc32c(expected)
if err != nil {
return err
}
if want != actual {
return fmt.Errorf("%s did not match crc32c of file data expected [%s] received [%s] size %d", ContentCrc32cHeader, expected, EncodeContentCrc32c(actual), size)
}
return nil
}