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 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-21 18:44:57 -08:00
1 parent 279d2d2c03
commit 8be0837c20
2 files changed
+63

No files matched your search

+7
View File
@@ -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
}
@@ -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"}