Skip to content

Commit 96e0483

Browse files
authored
Validate module names in the memiavl snapshot importer (#4469)
`MultiTreeImporter.AddModule` used the module name from the snapshot stream as a path component without validating it. Real module names are store keys, so anything other than a plain directory name means the snapshot is malformed. `AddModule` now rejects empty, `.`, `..`, absolute and multi-component names before it creates any files, so such a snapshot fails early with a clear error. Valid snapshots import exactly as before and the AppHash is unchanged.
1 parent 77a50d6 commit 96e0483

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎sei-cosmos/storev2/rootmulti/restore_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestRestoreRejectsMalformedStream(t *testing.T) {
9898
},
9999
"node after an unnamed store item": {
100100
items: []snapshottypes.SnapshotItem{storeItem(""), nodeItem(0, "k")},
101-
wantErr: "outside a named store section",
101+
wantErr: "invalid snapshot module name",
102102
},
103103
"branch node before its leaves": {
104104
items: []snapshottypes.SnapshotItem{storeItem("bank"), nodeItem(1, "k")},

‎sei-db/state_db/sc/memiavl/import.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ func (mti *MultiTreeImporter) Add(item interface{}) error {
8282
}
8383
}
8484

85+
// AddModule starts importing the tree called name into its own directory under
86+
// the import temp dir. name must be a plain directory name.
8587
func (mti *MultiTreeImporter) AddModule(name string) error {
88+
if err := validateModuleName(name); err != nil {
89+
return err
90+
}
8691
if mti.importer != nil {
8792
if err := mti.importer.Close(); err != nil {
8893
return err
@@ -92,6 +97,13 @@ func (mti *MultiTreeImporter) AddModule(name string) error {
9297
return nil
9398
}
9499

100+
func validateModuleName(name string) error {
101+
if name == "." || !filepath.IsLocal(name) || filepath.Base(name) != name {
102+
return fmt.Errorf("invalid snapshot module name %q", name)
103+
}
104+
return nil
105+
}
106+
95107
func (mti *MultiTreeImporter) AddNode(node *types.SnapshotNode) error {
96108
mti.importer.Add(node)
97109
return nil

‎sei-db/state_db/sc/memiavl/import_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package memiavl
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"testing"
79
"time"
810

@@ -37,3 +39,27 @@ func TestTreeImporterRejectsBranchBeforeLeaves(t *testing.T) {
3739
require.ErrorContains(t, imp.Close(), "pending children")
3840
require.NoError(t, imp.Close(), "a second Close must be a no-op")
3941
}
42+
43+
// TestMultiTreeImporterValidatesModuleName checks that AddModule only accepts
44+
// plain directory names.
45+
func TestMultiTreeImporterValidatesModuleName(t *testing.T) {
46+
root := t.TempDir()
47+
dir := filepath.Join(root, "db")
48+
require.NoError(t, os.Mkdir(dir, 0o750))
49+
50+
mti, err := NewMultiTreeImporter(dir, 1)
51+
require.NoError(t, err)
52+
53+
for _, name := range []string{"", ".", "..", "../../other", "a/b", "/abs", filepath.Join(root, "abs")} {
54+
require.ErrorContains(t, mti.AddModule(name), "invalid snapshot module name", "name %q", name)
55+
}
56+
require.Nil(t, mti.importer, "a rejected name must not start a tree import")
57+
58+
require.NoError(t, mti.AddModule("bank"))
59+
require.NoError(t, mti.Close())
60+
61+
_, err = os.Stat(filepath.Join(root, "other"))
62+
require.ErrorIs(t, err, os.ErrNotExist)
63+
_, err = os.Stat(filepath.Join(dir, snapshotName(1), "bank"))
64+
require.NoError(t, err)
65+
}

0 commit comments

Comments
 (0)