From df4b77bbeb3bbf846d5a2e57039e2c6c16af574a 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 & tx unmarshal --- vochain/cometbft.go | 15 +++++++++++++++ vochain/transaction/vochaintx/vochaintx.go | 10 +++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/vochain/cometbft.go b/vochain/cometbft.go index 5dd97d0f5..36543c374 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"), + }, fmt.Errorf("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..08fc0fc80 100644 --- a/vochain/transaction/vochaintx/vochaintx.go +++ b/vochain/transaction/vochaintx/vochaintx.go @@ -36,7 +36,15 @@ func (tx *Tx) Unmarshal(content []byte, chainID string) error { if err != nil { return fmt.Errorf("failed to unmarshal transaction: %w", err) } - tx.TxModelType = string(tx.Tx.ProtoReflect().WhichOneof(tx.Tx.ProtoReflect().Descriptor().Oneofs().Get(0)).Name()) + txReflectDescriptor := tx.Tx.ProtoReflect().Descriptor().Oneofs().Get(0) + if txReflectDescriptor == nil { + return fmt.Errorf("failed to determine transaction type") + } + whichOneTxModelType := tx.Tx.ProtoReflect().WhichOneof(txReflectDescriptor) + if whichOneTxModelType == nil { + return fmt.Errorf("failed to determine transaction type") + } + tx.TxModelType = string(whichOneTxModelType.Name()) tx.Signature = stx.GetSignature() tx.TxID = TxKey(content) return nil