Skip to content

Commit

Permalink
go/storage/mkvs: Use cbor.UnmarshalTrusted for internal metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Apr 1, 2020
1 parent 05e3030 commit ffa4115
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions .changelog/2800.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/storage/mkvs: Use cbor.UnmarshalTrusted for internal metadata
6 changes: 3 additions & 3 deletions go/storage/mkvs/db/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (d *badgerNodeDB) load() error {
// Metadata already exists, just load it and verify that it is
// compatible with what we have here.
err = item.Value(func(data []byte) error {
return cbor.Unmarshal(data, &d.meta.value)
return cbor.UnmarshalTrusted(data, &d.meta.value)
})
if err != nil {
return err
Expand Down Expand Up @@ -303,7 +303,7 @@ func (d *badgerNodeDB) GetWriteLog(ctx context.Context, startRoot node.Root, end

var log api.HashedDBWriteLog
err = item.Value(func(data []byte) error {
return cbor.Unmarshal(data, &log)
return cbor.UnmarshalTrusted(data, &log)
})
if err != nil {
return node.Root{}, nil, err
Expand Down Expand Up @@ -464,7 +464,7 @@ func (d *badgerNodeDB) Finalize(ctx context.Context, version uint64, roots []has

var updatedNodes []updatedNode
err = item.Value(func(data []byte) error {
return cbor.Unmarshal(data, &updatedNodes)
return cbor.UnmarshalTrusted(data, &updatedNodes)
})
if err != nil {
panic(fmt.Errorf("mkvs/badger: corrupted root updated nodes index: %w", err))
Expand Down
18 changes: 18 additions & 0 deletions go/storage/mkvs/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,23 @@ func testSpecialCase5(t *testing.T, ndb db.NodeDB, factory NodeDBFactory) {
testSpecialCaseFromJSON(t, ndb, "case-5.json")
}

func testLargeUpdates(t *testing.T, ndb db.NodeDB, factory NodeDBFactory) {
ctx := context.Background()
tree := New(nil, ndb)

// The number of elements is such that it would overflow the maximum number of allowed array
// elements in the default (untrusted) CBOR decoder.
for i := 0; i < 132_000; i++ {
err := tree.Insert(ctx, []byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%d", i)))
require.NoError(t, err, "Insert")
}

_, rootHash, err := tree.Commit(ctx, testNs, 0)
require.NoError(t, err, "Commit")
err = ndb.Finalize(ctx, 0, []hash.Hash{rootHash})
require.NoError(t, err, "Finalize")
}

func testBackend(
t *testing.T,
initBackend func(t *testing.T) (NodeDBFactory, func()),
Expand Down Expand Up @@ -1929,6 +1946,7 @@ func testBackend(
{"SpecialCase3", testSpecialCase3},
{"SpecialCase4", testSpecialCase4},
{"SpecialCase5", testSpecialCase5},
{"LargeUpdates", testLargeUpdates},
{"Errors", testErrors},
}

Expand Down

0 comments on commit ffa4115

Please sign in to comment.