From 84746790e9770ee6249e57c4a90dcb5dd81f1ca7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 12:47:56 -0800 Subject: [PATCH] layout: refuse HEAD names that are not valid UTF-8 The JSON encoding turns invalid UTF-8 into U+FFFD and still re-encodes the same, so HEAD would store another holder, session, snapshot or dependent name than the caller gave and a later lookup by that name would find nothing. check() refuses such names, and runTransition checks the next HEAD before writing it. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/layout/head.go | 24 +++++++++++++++++++ .../frontend/chunkstore/layout/head_test.go | 23 ++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/seaweed-block/core/frontend/chunkstore/layout/head.go b/seaweed-block/core/frontend/chunkstore/layout/head.go index d93c76a41d..77e1b82158 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/head.go +++ b/seaweed-block/core/frontend/chunkstore/layout/head.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "unicode/utf8" ) // HEAD is the one mutable entry of a volume: who holds it (epoch, holder), @@ -63,7 +64,21 @@ type snapDoc struct { Dependents []string `json:"dependents,omitempty"` } +// validName refuses a string the JSON encoding would not keep byte for +// byte (invalid UTF-8 becomes U+FFFD). +func validName(field, v string) error { + if !utf8.ValidString(v) { + return fmt.Errorf("%w: %s %q is not valid UTF-8", ErrMalformed, field, v) + } + return nil +} + func (h Head) check() error { + for field, v := range map[string]string{"holder": h.Holder, "session": h.Activity.Session} { + if err := validName(field, v); err != nil { + return err + } + } switch h.Activity.Kind { case ActIdle: if h.Activity.Session != "" { @@ -86,7 +101,13 @@ func (h Head) check() error { if n == "" { return fmt.Errorf("%w: unnamed snapshot", ErrMalformed) } + if err := validName("snapshot", n); err != nil { + return err + } for i, d := range s.Dependents { + if err := validName("dependent", d); err != nil { + return err + } if err := checkOwner(d); err != nil { return err } @@ -240,6 +261,9 @@ func runTransition(ctx context.Context, st HeadStore, t func(Head) (Head, bool, if err != nil || !changed { return next, err } + if err := next.check(); err != nil { + return Head{}, err + } if err := st.UpdateHead(ctx, h.Gen, next); err != nil { return Head{}, err } diff --git a/seaweed-block/core/frontend/chunkstore/layout/head_test.go b/seaweed-block/core/frontend/chunkstore/layout/head_test.go index 3e03960115..7440bcc3d9 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/head_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/head_test.go @@ -235,3 +235,26 @@ func TestRunBegin_InterruptAtEveryStep(t *testing.T) { } } } + +// A name that is not valid UTF-8 would be stored as U+FFFD by the JSON +// encoding (and still re-encode the same), so HEAD would hold another name +// than the caller gave: refused on every string field. +func TestHead_RefusesInvalidUTF8(t *testing.T) { + rn := RootName{Hash: Sum([]byte("r"))} + bad := "a\xff" + for name, h := range map[string]Head{ + "holder": {Epoch: 1, Holder: bad, Activity: Activity{Kind: ActIdle}}, + "session": {Epoch: 1, Holder: "a", Activity: Activity{Kind: ActSnap, Session: bad}}, + "snapshot": {Epoch: 1, Holder: "a", Activity: Activity{Kind: ActIdle}, Snapshots: map[string]Snapshot{bad: {Root: rn}}}, + "dependent": {Epoch: 1, Holder: "a", Activity: Activity{Kind: ActIdle}, Snapshots: map[string]Snapshot{"n": {Root: rn, Dependents: []string{bad}}}}, + } { + if _, err := h.Encode(); !errors.Is(err, ErrMalformed) { + t.Errorf("%s %q: %v; want ErrMalformed", name, bad, err) + } + } + h := idleHead() + st := &memHead{h: &h} + if _, err := RunBegin(context.Background(), st, me, bad); err == nil || st.h.Activity.Kind != ActIdle { + t.Fatalf("Begin with an invalid UTF-8 session: %v, HEAD %+v; want refused, nothing written", err, st.h.Activity) + } +}