From 1ecd222cade0ee62f39bcc87c0fdcb5abadd7264 Mon Sep 17 00:00:00 2001 From: Matt <98158711+BedrockSquirrel@users.noreply.github.com> Date: Tue, 5 Dec 2023 10:00:50 +0000 Subject: [PATCH] Enclave: no panic for bad rlp format (#1679) (cherry picked from commit 4551c81b6d59dff308fa8d72fb5f7f3df2159936) --- go/enclave/rpc_server.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/go/enclave/rpc_server.go b/go/enclave/rpc_server.go index a764904045..a61401be89 100644 --- a/go/enclave/rpc_server.go +++ b/go/enclave/rpc_server.go @@ -108,8 +108,16 @@ func (s *RPCServer) InitEnclave(_ context.Context, request *generated.InitEnclav } func (s *RPCServer) SubmitL1Block(_ context.Context, request *generated.SubmitBlockRequest) (*generated.SubmitBlockResponse, error) { - bl := s.decodeBlock(request.EncodedBlock) - receipts := s.decodeReceipts(request.EncodedReceipts) + bl, err := s.decodeBlock(request.EncodedBlock) + if err != nil { + s.logger.Error("Error decoding block", log.ErrKey, err) + return nil, err + } + receipts, err := s.decodeReceipts(request.EncodedReceipts) + if err != nil { + s.logger.Error("Error decoding receipts", log.ErrKey, err) + return nil, err + } blockSubmissionResponse, err := s.enclave.SubmitL1Block(bl, receipts, request.IsLatest) if err != nil { var rejErr *errutil.BlockRejectError @@ -439,25 +447,25 @@ func (s *RPCServer) EnclavePublicConfig(_ context.Context, _ *generated.EnclaveP return &generated.EnclavePublicConfigResponse{L2MessageBusAddress: enclaveCfg.L2MessageBusAddress.Bytes()}, nil } -func (s *RPCServer) decodeBlock(encodedBlock []byte) types.Block { +func (s *RPCServer) decodeBlock(encodedBlock []byte) (types.Block, error) { block := types.Block{} err := rlp.DecodeBytes(encodedBlock, &block) if err != nil { - s.logger.Info("failed to decode block sent to enclave", log.ErrKey, err) + return types.Block{}, fmt.Errorf("unable to decode block, bytes=%x, err=%w", encodedBlock, err) } - return block + return block, nil } // decodeReceipts - converts the rlp encoded bytes to receipts if possible. -func (s *RPCServer) decodeReceipts(encodedReceipts []byte) types.Receipts { +func (s *RPCServer) decodeReceipts(encodedReceipts []byte) (types.Receipts, error) { receipts := make(types.Receipts, 0) err := rlp.DecodeBytes(encodedReceipts, &receipts) if err != nil { - s.logger.Crit("failed to decode receipts sent to enclave", log.ErrKey, err) + return nil, fmt.Errorf("unable to decode receipts, bytes=%x, err=%w", encodedReceipts, err) } - return receipts + return receipts, nil } func toRPCError(err common.SystemError) *generated.SystemError {