Skip to content

Commit

Permalink
Enclave: no panic for bad rlp format (#1679)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4551c81)
  • Loading branch information
BedrockSquirrel authored and tudor-malene committed Dec 8, 2023
1 parent 801d560 commit 1ecd222
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions go/enclave/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1ecd222

Please sign in to comment.