mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
fix: decode HEAD with a strict format-specific schema
This commit is contained in:
1 parent
e2c69c9686
commit
bb52a686b8
2 files changed
+71
-20
No files matched your search
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user