Skip to content

Commit

Permalink
Fix V-DOGE-VUL-007 (#40)
Browse files Browse the repository at this point in the history
* Fix crash when faulty node send nil gossip transaction to peer

* Add unit test

* Fix lint error of nlreturn

* More accurate test cases of addGossipTx
  • Loading branch information
DarianShawn authored May 18, 2022
1 parent 0b6e7bf commit 9c8acff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
14 changes: 13 additions & 1 deletion txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,19 @@ func (p *TxPool) addGossipTx(obj interface{}) {
return
}

raw := obj.(*proto.Txn) // nolint:forcetypeassert
raw, ok := obj.(*proto.Txn)
if !ok {
p.logger.Warn("gossip tx(%+v) is not a transaction", obj)

return
}

if raw.Raw == nil || len(raw.Raw.Value) == 0 {
p.logger.Info("gossip tx raw data is empty")

return
}

tx := new(types.Transaction)

// decode tx
Expand Down
35 changes: 35 additions & 0 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/golang/protobuf/ptypes/any"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/anypb"
)

const (
Expand Down Expand Up @@ -390,6 +391,40 @@ func TestDropKnownGossipTx(t *testing.T) {
assert.Equal(t, uint64(1), pool.accounts.get(addr1).enqueued.length())
}

func TestAddGossipTx_ShouldNotCrash(t *testing.T) {
pool, err := newTestPool()
assert.NoError(t, err)
pool.SetSigner(&mockSigner{})

assert.NotPanics(t, func() {
// type assertion
pool.addGossipTx(&types.Block{Header: &types.Header{Number: 10}})
})

assert.Nil(t, pool.accounts.get(addr1), "addr in txpool should be nil")

assert.NotPanics(t, func() {
pool.addGossipTx(createNilRawTxn())
pool.addGossipTx(createNilRawDataTxn())
})

assert.Nil(t, pool.accounts.get(addr1), "addr in txpool should be nil")
}

func createNilRawTxn() *proto.Txn {
return &proto.Txn{
Raw: nil,
}
}

func createNilRawDataTxn() *proto.Txn {
return &proto.Txn{
Raw: &anypb.Any{
Value: nil,
},
}
}

func TestAddHandler(t *testing.T) {
t.Run("enqueue new tx with higher nonce", func(t *testing.T) {
pool, err := newTestPool()
Expand Down

0 comments on commit 9c8acff

Please sign in to comment.