storage, chunkstore: enforce the chunk key domain before persistence

Review fix for the intents seam: an intent document is JSON, which would
silently replace invalid UTF-8 in a key, while a key binding preserves the
bytes, so a protected key could list back as a different key. The chunk
key domain is now non-empty valid UTF-8, refused at Put and, for every
key, at PutIntent by the SeaweedFS remote, MemChunkRemote and the test
fake before anything is written (storage.ValidChunkKey,
ErrChunkKeyInvalid; chunkstore wraps it in ErrInvalidKey). Unicode and
owner-qualified keys round-trip byte-identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-15 04:20:06 -08:00
1 parent 008b96307b
commit eba04ab67d
7 files changed
+149 -1

No files matched your search

@@ -125,6 +125,9 @@ func CheckVolumeID(id string) error {
// keyLocation returns the directory and entry name holding key's binding:
// the owner's keys/ directory for "@<owner>/<key>", this volume's otherwise.
func (r *ChunkRemote) keyLocation(key string) (dir, name string, err error) {
if err := storage.ValidChunkKey(key); err != nil {
return "", "", fmt.Errorf("%w: %v", ErrInvalidKey, err)
}
if !strings.HasPrefix(key, "@") {
name, err := keyName(key)
return r.keyDir(), name, err
@@ -143,6 +146,9 @@ func (r *ChunkRemote) keyLocation(key string) (dir, name string, err error) {
// create. A key that is already bound fails with storage.ErrChunkKeyExists
// and its bytes are untouched.
func (r *ChunkRemote) Put(ctx context.Context, key string, data []byte) error {
if err := storage.ValidChunkKey(key); err != nil {
return fmt.Errorf("%w: %v", ErrInvalidKey, err)
}
if strings.HasPrefix(key, "@") {
return fmt.Errorf("%w: %q is owner-qualified; a volume writes only its own keys", ErrInvalidKey, key)
}
@@ -222,6 +228,11 @@ func (r *ChunkRemote) intentDir() string { return path.Join(r.root, intentsDir)
// The entry is durable when the filer's fsync rule covers the chunk path,
// the same condition as for HEAD.
func (r *ChunkRemote) PutIntent(ctx context.Context, in storage.ChunkIntent) (string, error) {
for _, k := range in.Keys {
if err := storage.ValidChunkKey(k); err != nil {
return "", fmt.Errorf("%w: %v", ErrInvalidKey, err)
}
}
id, err := storage.NewChunkIntentID(in.Gen)
if err != nil {
return "", err
@@ -0,0 +1,56 @@
package chunkstore
import (
"bytes"
"context"
"errors"
"testing"
"github.com/seaweedfs/seaweed-block/core/storage"
)
// The SeaweedFS remote enforces the chunk key domain (non-empty valid UTF-8)
// at Put and PutIntent before anything is written, and keys inside the
// domain, Unicode and owner-qualified ones included, round-trip exactly
// through the key bindings and the intent documents.
func TestChunkRemote_KeyDomainEnforcedBeforePersistence(t *testing.T) {
f, b := newFakeFiler(), newFakeBlobs()
r, err := NewChunkRemote(f, b, "/swblock/chunks/v1")
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
for _, bad := range []string{"chunks/\xff/c0", "a\xc3"} {
if err := r.Put(ctx, bad, []byte("x")); !errors.Is(err, ErrInvalidKey) {
t.Errorf("Put(%q): %v, want ErrInvalidKey", bad, err)
}
if _, err := r.PutIntent(ctx, storage.ChunkIntent{Gen: 1, Epoch: 1, Holder: "h", Keys: []string{"ok", bad}}); !errors.Is(err, ErrInvalidKey) {
t.Errorf("PutIntent with %q: %v, want ErrInvalidKey", bad, err)
}
}
if puts, _, _ := b.counts(); puts != 0 || len(f.allWritten()) != 0 {
t.Fatalf("refused calls wrote %d needles and %d entries", puts, len(f.allWritten()))
}
data := bytes.Repeat([]byte{0x33}, 4096)
if err := r.Put(ctx, "chunks/é/ü-1", data); err != nil {
t.Fatalf("Put of a Unicode key: %v", err)
}
fresh, _ := NewChunkRemote(f, b, "/swblock/chunks/v1") // no cached binding
if got, err := fresh.Get(ctx, "chunks/é/ü-1"); err != nil || !bytes.Equal(got, data) {
t.Fatalf("Get of a Unicode key through its stored binding: %v", err)
}
keys := []string{"chunks/é/ü-1", "@v0/chunks/e1/c0/x-1", "日本/鍵"}
if _, err := r.PutIntent(ctx, storage.ChunkIntent{Gen: 2, Epoch: 1, Holder: "h", Keys: keys}); err != nil {
t.Fatal(err)
}
got, err := fresh.ListIntents(ctx)
if err != nil || len(got) != 1 {
t.Fatalf("ListIntents: %+v %v", got, err)
}
for i, k := range keys {
if got[0].Keys[i] != k {
t.Fatalf("intent key %d listed as %q, stored %q", i, got[0].Keys[i], k)
}
}
}
@@ -79,6 +79,11 @@ func newFakeChunkRemote() *fakeChunkRemote {
}
func (f *fakeChunkRemote) PutIntent(ctx context.Context, in chunkIntent) (string, error) {
for _, k := range in.Keys {
if err := validChunkKey(k); err != nil {
return "", err
}
}
id, err := newChunkIntentID(in.Gen)
if err != nil {
return "", err
@@ -111,6 +116,9 @@ func (f *fakeChunkRemote) ListIntents(ctx context.Context) ([]chunkIntent, error
}
func (f *fakeChunkRemote) Put(ctx context.Context, key string, data []byte) error {
if err := validChunkKey(key); err != nil {
return err
}
entry := append([]byte(nil), data...)
f.mu.Lock()
f.ops = append(f.ops, "put:"+key)
@@ -0,0 +1,44 @@
package storage
import (
"context"
"testing"
)
// The chunk key domain is non-empty valid UTF-8, enforced at Put and at
// PutIntent by every remote, so a key protected by an intent always lists
// back byte-identical to the key stored (review of #110).
func checkKeyDomain(t *testing.T, r chunkRemote) {
t.Helper()
ctx := context.Background()
for _, bad := range []string{"", "chunks/\xff/c0", "a\xc3"} {
if err := r.Put(ctx, bad, []byte("x")); err == nil {
t.Errorf("Put(%q) accepted a key outside the domain", bad)
}
if _, err := r.PutIntent(ctx, chunkIntent{Gen: 1, Epoch: 1, Holder: "h", Keys: []string{"ok", bad}}); err == nil {
t.Errorf("PutIntent with key %q accepted a key outside the domain", bad)
}
}
if got, err := r.ListIntents(ctx); err != nil || len(got) != 0 {
t.Fatalf("a refused PutIntent left intents: %+v %v", got, err)
}
keys := []string{"chunks/é/ü-1", "@v1/chunks/e1/c0/x-1", "日本/鍵"}
if err := r.Put(ctx, keys[0], []byte("y")); err != nil {
t.Fatalf("Put of a Unicode key: %v", err)
}
if _, err := r.PutIntent(ctx, chunkIntent{Gen: 2, Epoch: 1, Holder: "h", Keys: keys}); err != nil {
t.Fatalf("PutIntent with Unicode and qualified keys: %v", err)
}
got, err := r.ListIntents(ctx)
if err != nil || len(got) != 1 || len(got[0].Keys) != len(keys) {
t.Fatalf("ListIntents: %+v %v", got, err)
}
for i, k := range keys {
if got[0].Keys[i] != k {
t.Fatalf("intent key %d listed as %q, stored %q", i, got[0].Keys[i], k)
}
}
}
func TestMemChunkRemote_KeyDomain(t *testing.T) { checkKeyDomain(t, NewMemChunkRemote()) }
func TestFakeChunkRemote_KeyDomain(t *testing.T) { checkKeyDomain(t, newFakeChunkRemote()) }
@@ -32,6 +32,11 @@ func (m *MemChunkRemote) PutIntent(ctx context.Context, in chunkIntent) (string,
if err := ctx.Err(); err != nil {
return "", err
}
for _, k := range in.Keys {
if err := validChunkKey(k); err != nil {
return "", err
}
}
id, err := newChunkIntentID(in.Gen)
if err != nil {
return "", err
@@ -72,6 +77,9 @@ func (m *MemChunkRemote) Put(ctx context.Context, key string, data []byte) error
if err := ctx.Err(); err != nil {
return err
}
if err := validChunkKey(key); err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
if m.ever[key] {
@@ -38,8 +38,13 @@ var (
ErrChunkLayerFenced = errChunkLayerFenced
ErrChunkEvictRefused = errChunkEvictRefused
ErrChunkCaptureRefused = errChunkCaptureRefused
ErrChunkKeyInvalid = errChunkKeyInvalid
)
// ValidChunkKey enforces the chunk key domain (non-empty valid UTF-8); a
// remote implementation calls it in Put and, for every key, in PutIntent.
func ValidChunkKey(key string) error { return validChunkKey(key) }
// AttachChunkLayer attaches a chunk layer to an open, recovered WALStore.
// Attach after Recover: the dirty set and the restart rules read the
// replayed dirty map. The store owns the layer from then on and closes it
+17 -1
View File
@@ -35,6 +35,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
)
// chunkSink receives walstore events.
@@ -173,7 +174,9 @@ type chunkHead struct {
Chunks map[uint64]chunkRef
}
// chunkRemote is the remote store. Keys are immutable: Put of a key that
// chunkRemote is the remote store. Keys are non-empty valid UTF-8 strings
// (validChunkKey); Put and PutIntent refuse any other key before writing
// anything. Keys are immutable: Put of a key that
// exists, or once existed, fails with errChunkKeyExists and never replaces
// stored bytes. CASHead installs next only when the stored HEAD matches
// expected and returns the new generation; a mismatch fails with
@@ -224,6 +227,18 @@ func newChunkIntentID(gen uint64) (string, error) {
return strconv.FormatUint(gen, 10) + "-" + hex.EncodeToString(b[:]), nil
}
// validChunkKey is the chunk key domain: a non-empty, valid UTF-8 string.
// Every remote refuses any other key at Put and at PutIntent, before it
// persists anything, so a key protected by an intent always lists back
// byte-identical to the key stored (an encoding that replaced invalid
// bytes would otherwise protect a different key than the one stored).
func validChunkKey(key string) error {
if key == "" || !utf8.ValidString(key) {
return fmt.Errorf("%w: %q", errChunkKeyInvalid, key)
}
return nil
}
// validChunkIntentID accepts exactly the ids newChunkIntentID produces.
func validChunkIntentID(id string) bool {
gen, nonce, ok := strings.Cut(id, "-")
@@ -335,6 +350,7 @@ var (
errChunkLayerFenced = errors.New("storage: chunk layer fenced")
errChunkEvictRefused = errors.New("storage: chunk eviction refused")
errChunkCaptureRefused = errors.New("storage: chunk capture refused")
errChunkKeyInvalid = errors.New("storage: chunk key is not non-empty valid UTF-8")
)
// attachChunkLayer attaches a chunk layer to an open store (chunk_layer.go).