diff --git a/seaweed-block/core/frontend/chunkstore/layout/head.go b/seaweed-block/core/frontend/chunkstore/layout/head.go index 29af651c3c..e98edb7ed6 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 } @@ -271,6 +292,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 3dc507c6dc..ea91d3505b 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/head_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/head_test.go @@ -329,3 +329,26 @@ func TestRunAbort_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) + } +}