mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
Merge pull request #627 from seaweedfs/test/volume-identity-creation-guards
test: guard VOLUME creation and cryptographic layout ids
This commit is contained in:
commit
f9c7fbf1fd
3 files changed
+237
-1
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
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.
|
||||
// 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 {
|
||||
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, "<package>")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user