From 6833b75a5f26b1eba58a7901a457622cf180e6fc Mon Sep 17 00:00:00 2001 From: Jordi Pinyana Date: Thu, 23 Nov 2023 16:22:14 +0100 Subject: [PATCH] add checks for nil/empty requests on cometbft --- vochain/cometbft.go | 15 +++++++++++++++ vochain/transaction/vochaintx/vochaintx.go | 3 +++ 2 files changed, 18 insertions(+) diff --git a/vochain/cometbft.go b/vochain/cometbft.go index 5dd97d0f5..d3dc79bbb 100644 --- a/vochain/cometbft.go +++ b/vochain/cometbft.go @@ -168,6 +168,12 @@ func (app *BaseApplication) InitChain(_ context.Context, // CheckTx unmarshals req.Tx and checks its validity func (app *BaseApplication) CheckTx(_ context.Context, req *abcitypes.RequestCheckTx) (*abcitypes.ResponseCheckTx, error) { + if req == nil || req.Tx == nil { + return &abcitypes.ResponseCheckTx{ + Code: 1, + Data: []byte("nil request or tx"), + }, errors.New("nil request or tx") + } txReference := vochaintx.TxKey(req.Tx) ref, ok := app.txReferences.Load(txReference) if !ok { @@ -219,6 +225,9 @@ func (app *BaseApplication) FinalizeBlock(_ context.Context, req *abcitypes.RequestFinalizeBlock) (*abcitypes.ResponseFinalizeBlock, error) { app.prepareProposalLock.Lock() defer app.prepareProposalLock.Unlock() + if req == nil { + return nil, errors.New("nil request") + } start := time.Now() height := uint32(req.GetHeight()) @@ -327,6 +336,9 @@ func (app *BaseApplication) PrepareProposal(ctx context.Context, req *abcitypes.RequestPrepareProposal) (*abcitypes.ResponsePrepareProposal, error) { app.prepareProposalLock.Lock() defer app.prepareProposalLock.Unlock() + if req == nil { + return nil, errors.New("nil request") + } startTime := time.Now() type txInfo struct { @@ -431,6 +443,9 @@ func (app *BaseApplication) ProcessProposal(_ context.Context, req *abcitypes.RequestProcessProposal) (*abcitypes.ResponseProcessProposal, error) { app.prepareProposalLock.Lock() defer app.prepareProposalLock.Unlock() + if req == nil { + return nil, errors.New("nil request") + } // Check if the node is a validator, if not, just accept the proposal and return (nothing to say) validator, err := app.State.Validator(app.NodeAddress, true) if err != nil { diff --git a/vochain/transaction/vochaintx/vochaintx.go b/vochain/transaction/vochaintx/vochaintx.go index 014967274..fc647121b 100644 --- a/vochain/transaction/vochaintx/vochaintx.go +++ b/vochain/transaction/vochaintx/vochaintx.go @@ -27,6 +27,9 @@ type Tx struct { // Extracts the signature. Prepares the signed body (ready to be checked) and // computes the transaction ID (a hash of the data). func (tx *Tx) Unmarshal(content []byte, chainID string) error { + if content == nil { + return fmt.Errorf("nil content") + } stx := new(models.SignedTx) if err := proto.Unmarshal(content, stx); err != nil { return fmt.Errorf("failed to unmarshal signed transaction: %w", err)