From bb52a686b8dbb7f9039db6ab6ed11dc53a7c2184 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 19:11:42 -0800 Subject: [PATCH] fix: decode HEAD with a strict format-specific schema --- .../core/frontend/chunkstore/chunk_remote.go | 22 +----- .../core/frontend/chunkstore/head_decode.go | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 seaweed-block/core/frontend/chunkstore/head_decode.go diff --git a/seaweed-block/core/frontend/chunkstore/chunk_remote.go b/seaweed-block/core/frontend/chunkstore/chunk_remote.go index 48c67a869f..b477841b8b 100644 --- a/seaweed-block/core/frontend/chunkstore/chunk_remote.go +++ b/seaweed-block/core/frontend/chunkstore/chunk_remote.go @@ -507,28 +507,10 @@ func decodeHeadDoc(p string, content []byte) (storage.ChunkHead, error) { fail := func(format string, a ...any) (storage.ChunkHead, error) { return storage.ChunkHead{}, fmt.Errorf("chunkstore: HEAD %s: "+format, append([]any{p}, a...)...) } - var doc headDoc - if err := decodeManifestJSON(content, &doc); err != nil { + doc, err := decodeFormatHead(content) + if err != nil { return fail("decode: %v", err) } - var fields map[string]json.RawMessage - if err := json.Unmarshal(content, &fields); err != nil { - return fail("decode representation: %v", err) - } - for name := range fields { - // encoding/json also matches tagged names case-insensitively; reject - // every such alias rather than let it bypass representation checks. - field := strings.ToLower(name) - if doc.Format == 1 && (field == "segments" || field == "chunk_bytes" || field == "chunk_count") { - return fail("flat HEAD contains segment field %s", name) - } - if doc.Format == 2 && field == "chunks" { - return fail("segmented HEAD contains flat chunks field") - } - } - if doc.Format != headFormat && doc.Format != 2 { - return fail("format %d not supported (want %d)", doc.Format, headFormat) - } if doc.Gen == 0 { return fail("generation 0") } diff --git a/seaweed-block/core/frontend/chunkstore/head_decode.go b/seaweed-block/core/frontend/chunkstore/head_decode.go new file mode 100644 index 0000000000..fca0c37eff --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/head_decode.go @@ -0,0 +1,69 @@ +package chunkstore + +import ( + "bytes" + "encoding/json" + "fmt" + + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// Only identity/proof fields are shared between persisted representations. +// Format-specific structs deliberately have no field for the other format: +// DisallowUnknownFields rejects even encoding/json's Unicode-folded aliases. +type headIdentityDoc struct { + Format int `json:"format"` + Gen uint64 `json:"gen"` + Epoch uint64 `json:"epoch"` + Holder string `json:"holder"` + RemoteLSN uint64 `json:"remote_lsn"` + State string `json:"state,omitempty"` + DetachedBy *detachedByDoc `json:"detachedBy,omitempty"` +} +type flatHeadDoc struct { + headIdentityDoc + Chunks map[uint64]refDoc `json:"chunks"` +} +type segmentedHeadDoc struct { + headIdentityDoc + Segments map[uint64]storage.ChunkSegment `json:"segments"` + ChunkBytes uint64 `json:"chunk_bytes"` + ChunkCount int `json:"chunk_count"` +} + +func (d headIdentityDoc) document() headDoc { + return headDoc{Format: d.Format, Gen: d.Gen, Epoch: d.Epoch, Holder: d.Holder, RemoteLSN: d.RemoteLSN, State: d.State, DetachedBy: d.DetachedBy} +} + +func decodeFormatHead(content []byte) (headDoc, error) { + var discriminator struct { + Format int `json:"format"` + } + // Preserve duplicate/nesting/byte/trailing-byte checks before selecting the + // schema. Unknown fields are allowed ONLY for this discriminator read. + if err := decodeManifestJSON(content, &discriminator); err != nil { + return headDoc{}, err + } + decoder := json.NewDecoder(bytes.NewReader(content)) + decoder.DisallowUnknownFields() + switch discriminator.Format { + case 1: + var flat flatHeadDoc + if err := decoder.Decode(&flat); err != nil { + return headDoc{}, err + } + doc := flat.headIdentityDoc.document() + doc.Chunks = flat.Chunks + return doc, nil + case 2: + var segmented segmentedHeadDoc + if err := decoder.Decode(&segmented); err != nil { + return headDoc{}, err + } + doc := segmented.headIdentityDoc.document() + doc.Segments, doc.ChunkBytes, doc.ChunkCount = segmented.Segments, segmented.ChunkBytes, segmented.ChunkCount + return doc, nil + default: + return headDoc{}, fmt.Errorf("chunkstore: unsupported HEAD format %d", discriminator.Format) + } +}