From 8b50a9bdba557911adf467712b4297b5bbe474ea Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 17:30:33 -0800 Subject: [PATCH 1/3] test: enumerate the VOLUME writer and crypto identity source --- .../frontend/chunkstore/layout/guard_test.go | 8 +- .../layout/volume_random_guard_test.go | 77 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 seaweed-block/core/frontend/chunkstore/layout/volume_random_guard_test.go diff --git a/seaweed-block/core/frontend/chunkstore/layout/guard_test.go b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go index c71e0be859..0f7cb4d4ff 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/guard_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go @@ -143,6 +143,9 @@ func guardFindings(code map[string]map[string]bool, decl, tests map[string]bool) if got := owners(code["UpdateHead"]); !reflect.DeepEqual(got, []string{"runTransition"}) { bad = append(bad, "UpdateHead used by "+strings.Join(got, ", ")+"; want only runTransition") } + if got := owners(code["CreateVolumeRecord"]); !reflect.DeepEqual(got, []string{"createVolume"}) { + bad = append(bad, "CreateVolumeRecord used by "+strings.Join(got, ", ")+"; want only createVolume") + } var runs []string for _, fn := range owners(code["runTransition"]) { x, ok := strings.CutPrefix(fn, "Run") @@ -426,7 +429,10 @@ func TestGuard_Controls(t *testing.T) { // A control with a file name replaces that real file (a changed // claim.go); otherwise it is added beside the real sources. for name, c := range map[string]struct{ src, want, file string }{ - "direct call": {src: `func w(ctx context.Context, st HeadStore, h Head) error { return st.UpdateHead(ctx, h.Gen, h) }`, want: "UpdateHead used by"}, + "second VOLUME writer": {src: `func w(ctx context.Context, st NewVolumeStore, v Volume) error { return st.CreateVolumeRecord(ctx,v) }`, want: "CreateVolumeRecord used by"}, + "VOLUME method alias": {src: `func w(st NewVolumeStore) { write := st.CreateVolumeRecord; _ = write }`, want: "CreateVolumeRecord used by"}, + "VOLUME method expression": {src: `var write = NewVolumeStore.CreateVolumeRecord`, want: "CreateVolumeRecord used by"}, + "direct call": {src: `func w(ctx context.Context, st HeadStore, h Head) error { return st.UpdateHead(ctx, h.Gen, h) }`, want: "UpdateHead used by"}, "method value alias": {src: `func w(ctx context.Context, st HeadStore, h Head) error { update := st.UpdateHead return update(ctx, h.Gen, h) diff --git a/seaweed-block/core/frontend/chunkstore/layout/volume_random_guard_test.go b/seaweed-block/core/frontend/chunkstore/layout/volume_random_guard_test.go new file mode 100644 index 0000000000..ff7611e61e --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/volume_random_guard_test.go @@ -0,0 +1,77 @@ +package layout + +import ( + "go/ast" + "go/parser" + "go/token" + "strconv" + "testing" +) + +// A repeated-id smoke test cannot prove a crypto source: pin the actual entropy +// call too. Full width is checked on the public VolumeID value. +func TestVolumeIDUses128CryptoRandomBits(t *testing.T) { + id, err := NewVolumeID() + if err != nil { + t.Fatal(err) + } + if len(id) != 16 || len(id.String()) != 32 { + t.Fatal("volume id is not 128 bits") + } + f, err := parser.ParseFile(token.NewFileSet(), "volume.go", nil, 0) + if err != nil { + t.Fatal(err) + } + countCryptoReads := func() int { + imports := map[string]string{} + for _, i := range f.Imports { + p, _ := strconv.Unquote(i.Path.Value) + if p == "crypto/rand" { + name := "rand" + if i.Name != nil { + name = i.Name.Name + } + imports[name] = p + } + } + calls := 0 + for _, d := range f.Decls { + fn, ok := d.(*ast.FuncDecl) + if !ok || fn.Name.Name != "NewVolumeID" { + continue + } + ast.Inspect(fn.Body, func(n ast.Node) bool { + c, ok := n.(*ast.CallExpr) + if !ok { + return true + } + sel, ok := c.Fun.(*ast.SelectorExpr) + if !ok { + return true + } + recv, ok := sel.X.(*ast.Ident) + if ok && imports[recv.Name] == "crypto/rand" && sel.Sel.Name == "Read" && len(c.Args) == 1 { + if slice, ok := c.Args[0].(*ast.SliceExpr); ok && slice.Low == nil && slice.High == nil && slice.Max == nil { + calls++ + } + } + return true + }) + } + return calls + } + calls := countCryptoReads() + if calls != 1 { + t.Fatalf("full-buffer crypto/rand.Read calls = %d, want 1", calls) + } + // A non-cryptographic replacement must fail even if ids are still 16 bytes. + for _, i := range f.Imports { + if i.Path.Value == "\"crypto/rand\"" { + i.Path.Value = "\"math/rand\"" + } + } + if countCryptoReads() != 0 { + t.Fatal("crypto-source guard accepted math/rand") + } + +} From 5be5e528c2e2ca1888f72745032284e9d6b73ec2 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 17:35:30 -0800 Subject: [PATCH 2/3] test: count repository-wide VOLUME creator and constant sites --- .../volume_writer_repository_guard_test.go | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 seaweed-block/core/frontend/chunkstore/layout/volume_writer_repository_guard_test.go diff --git a/seaweed-block/core/frontend/chunkstore/layout/volume_writer_repository_guard_test.go b/seaweed-block/core/frontend/chunkstore/layout/volume_writer_repository_guard_test.go new file mode 100644 index 0000000000..8e4964177c --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/volume_writer_repository_guard_test.go @@ -0,0 +1,152 @@ +package layout + +import ( + "go/ast" + "go/parser" + "go/token" + "io/fs" + "os" + "path/filepath" + "reflect" + "strconv" + "strings" + "testing" +) + +// Count references, not just calls: method values, method expressions and +// package-level aliases can all hide another writer. Resolve the layout import +// so unrelated structs' VolumeName fields do not count as the VOLUME constant. +func volumeCreationRefs(name string, src []byte) (map[string]int, error) { + f, err := parser.ParseFile(token.NewFileSet(), name, src, 0) + if err != nil { + return nil, err + } + layoutImports := map[string]bool{} + for _, i := range f.Imports { + p, _ := strconv.Unquote(i.Path.Value) + if p == "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" { + alias := "layout" + if i.Name != nil { + alias = i.Name.Name + } + layoutImports[alias] = true + } + } + kernel := strings.HasPrefix(name, "core/frontend/chunkstore/layout/") + got := map[string]int{} + visit := func(n ast.Node, owner string) { + ast.Inspect(n, func(n ast.Node) bool { + symbol := "" + switch x := n.(type) { + case *ast.SelectorExpr: + if x.Sel.Name == "CreateVolumeRecord" { + symbol = x.Sel.Name + } + if recv, ok := x.X.(*ast.Ident); ok && layoutImports[recv.Name] && x.Sel.Name == "VolumeName" { + symbol = x.Sel.Name + } + if symbol != "" { + got[name+" "+owner+" "+symbol]++ + } + return false + case *ast.Ident: + if x.Name == "VolumeName" && (kernel || layoutImports["."]) { + symbol = x.Name + } + } + if symbol != "" { + got[name+" "+owner+" "+symbol]++ + } + return true + }) + } + for _, d := range f.Decls { + switch x := d.(type) { + case *ast.FuncDecl: + if x.Body != nil { + visit(x.Body, x.Name.Name) + } + case *ast.GenDecl: + for _, spec := range x.Specs { + if v, ok := spec.(*ast.ValueSpec); ok { + for _, e := range v.Values { + visit(e, "") + } + } + } + } + } + return got, nil +} + +func TestVolumeWriterRepositorySites(t *testing.T) { + want := map[string]int{ + "core/frontend/chunkstore/layout/create.go createVolume CreateVolumeRecord": 1, + "core/frontend/chunkstore/layout/read.go VolumeID VolumeName": 1, + "core/frontend/chunkstore/layout/verify.go volumeID VolumeName": 1, + "core/frontend/chunkstore/layout_create.go ReadVolumeRecord VolumeName": 1, + "core/frontend/chunkstore/layout_create.go CreateVolumeRecord VolumeName": 1, + } + got := map[string]int{} + root := filepath.Clean("../../../..") + err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() { + if d.Name() == ".git" || d.Name() == "vendor" { + return filepath.SkipDir + } + return nil + } + if !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") { + return nil + } + rel, err := filepath.Rel(root, p) + if err != nil { + return err + } + src, err := os.ReadFile(p) + if err != nil { + return err + } + sites, err := volumeCreationRefs(filepath.ToSlash(rel), src) + if err != nil { + return err + } + for key, n := range sites { + got[key] += n + } + return nil + }) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("VOLUME sites = %v, want exactly %v", got, want) + } +} + +func TestVolumeWriterRepositoryDetectorControls(t *testing.T) { + for _, src := range []string{ + `func hidden(st S){st.CreateVolumeRecord(nil,v)}`, + `func hidden(st S){f:=st.CreateVolumeRecord;_ = f}`, + `var hidden = S.CreateVolumeRecord`, + `func hidden(){e:=filer.Entry{Name:k.VolumeName};_ = e}`, + `var hidden = k.VolumeName`, + } { + raw := `package p; import k "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout";` + src + got, err := volumeCreationRefs("core/elsewhere/new.go", []byte(raw)) + if err != nil || len(got) != 1 { + t.Fatalf("missed cross-package writer/constant control %s: %v %v", src, got, err) + } + } + got, err := volumeCreationRefs("core/elsewhere/new.go", []byte(`package p; func f(x S){_ = x.VolumeName}`)) + if err != nil || len(got) != 0 { + t.Fatal("unrelated field mistaken for the layout constant", got, err) + } + got, err = volumeCreationRefs("core/frontend/chunkstore/layout/create.go", []byte(`package layout; func createVolume(st S){st.CreateVolumeRecord(nil,v);st.CreateVolumeRecord(nil,v)}`)) + if err != nil || got["core/frontend/chunkstore/layout/create.go createVolume CreateVolumeRecord"] != 2 { + t.Fatal("duplicate at existing site not counted", got, err) + } +} From b2a5856e6026a90077db7cb77870d1bcc85188cf Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 17:38:18 -0800 Subject: [PATCH 3/3] docs: state the literal-entry-name guard limitation --- .../chunkstore/layout/volume_writer_repository_guard_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/seaweed-block/core/frontend/chunkstore/layout/volume_writer_repository_guard_test.go b/seaweed-block/core/frontend/chunkstore/layout/volume_writer_repository_guard_test.go index 8e4964177c..30eb2b8f84 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/volume_writer_repository_guard_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/volume_writer_repository_guard_test.go @@ -16,6 +16,7 @@ import ( // Count references, not just calls: method values, method expressions and // package-level aliases can all hide another writer. Resolve the layout import // so unrelated structs' VolumeName fields do not count as the VOLUME constant. +// A write using the literal entry name instead of the constant is not caught. func volumeCreationRefs(name string, src []byte) (map[string]int, error) { f, err := parser.ParseFile(token.NewFileSet(), name, src, 0) if err != nil {