From 8be0837c205530fc61a2a888317af187e5eb5a57 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 21 Nov 2026 18:44:57 -0800 Subject: [PATCH] s3api: try the next replica when a copied encrypted chunk does not decrypt (#12872) A copy stores a per-chunk-encrypted chunk's ciphertext as-is, so a replica whose needle is cut short or diverged became the copied chunk unnoticed. Open the GCM tag of a whole-needle read and fail that replica when it does not authenticate; a compressed ciphertext has no expected length to check. Co-authored-by: Claude Opus 5.5 --- weed/s3api/s3api_object_handlers_copy.go | 7 +++ .../s3api_object_handlers_copy_stream_test.go | 56 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/weed/s3api/s3api_object_handlers_copy.go b/weed/s3api/s3api_object_handlers_copy.go index 05eb30a624..742c3eab14 100644 --- a/weed/s3api/s3api_object_handlers_copy.go +++ b/weed/s3api/s3api_object_handlers_copy.go @@ -2327,6 +2327,13 @@ func (s3a *S3ApiServer) downloadChunkDataFrom(srcUrl, fileId string, offset, siz if len(cipherKey) == 0 && int64(len(chunkData)) < size { return nil, fmt.Errorf("download chunk %s: got %d of %d bytes", srcUrl, len(chunkData), size) } + // A compressed ciphertext has no expected length; the GCM tag catches a + // truncated or diverged needle instead. + if len(cipherKey) > 0 && offset == 0 { + if _, err := util.Decrypt(chunkData, util.CipherKey(cipherKey)); err != nil { + return nil, fmt.Errorf("download chunk %s: %d encrypted bytes do not decrypt: %w", srcUrl, len(chunkData), err) + } + } return chunkData, nil } diff --git a/weed/s3api/s3api_object_handlers_copy_stream_test.go b/weed/s3api/s3api_object_handlers_copy_stream_test.go index 37fbf9e507..b7a9b737b5 100644 --- a/weed/s3api/s3api_object_handlers_copy_stream_test.go +++ b/weed/s3api/s3api_object_handlers_copy_stream_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/util" util_http "github.com/seaweedfs/seaweedfs/weed/util/http" ) @@ -214,6 +215,61 @@ func TestDownloadChunkDataFallsBackToNextReplica(t *testing.T) { } } +// An encrypted chunk is copied as ciphertext, so a replica whose needle is +// cut short or diverged must be skipped rather than stored as the copy. +func TestDownloadChunkDataSkipsUndecryptableReplica(t *testing.T) { + util_http.InitGlobalHttpClient() + plaintext := bytes.Repeat([]byte("secret chunk"), 512) + key := util.GenCipherKey() + ciphertext, err := util.Encrypt(plaintext, key) + if err != nil { + t.Fatal(err) + } + foreign, err := util.Encrypt(plaintext, util.GenCipherKey()) + if err != nil { + t.Fatal(err) + } + serve := func(data []byte) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(data)) + })) + } + healthy := serve(ciphertext) + defer healthy.Close() + short := serve(ciphertext[:len(ciphertext)-7]) + defer short.Close() + diverged := serve(foreign) + defer diverged.Close() + + s3a := &S3ApiServer{} + for _, tc := range []struct { + name string + sources []string + wantErr string + }{ + {"first replica serves a shorter needle", []string{short.URL, healthy.URL}, ""}, + {"first replica serves a diverged needle", []string{diverged.URL, healthy.URL}, ""}, + {"only replica serves a shorter needle", []string{short.URL}, "do not decrypt"}, + {"no replica decrypts", []string{diverged.URL, short.URL}, "do not decrypt"}, + } { + t.Run(tc.name, func(t *testing.T) { + data, err := s3a.downloadChunkData(tc.sources, "1,test", 0, int64(len(plaintext)), key) + if tc.wantErr != "" { + if err == nil || !strings.Contains(err.Error(), tc.wantErr) { + t.Fatalf("download error=%v, want it to mention %q", err, tc.wantErr) + } + return + } + if err != nil { + t.Fatalf("download: %v", err) + } + if !bytes.Equal(data, ciphertext) { + t.Fatalf("downloaded %d bytes that differ from the %d-byte ciphertext", len(data), len(ciphertext)) + } + }) + } +} + func TestVolumeLocationUrls(t *testing.T) { got := volumeLocationUrls([]*filer_pb.Location{{Url: "a:8080"}, {Url: ""}, nil, {Url: "b:8080"}}, "3,01637037d6") want := []string{"http://a:8080/3,01637037d6", "http://b:8080/3,01637037d6"}