From 47e95dc94d2fe84cd93b7b5c9d6b9542f7459ec7 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Mon, 26 Jun 2023 14:04:02 +0200 Subject: [PATCH] blob and namesapce constructor to forbid empty data --- blob/blob.go | 4 ++++ share/namespace.go | 5 +++-- share/namespace_test.go | 12 +++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/blob/blob.go b/blob/blob.go index 3652893e51..890601e1c8 100644 --- a/blob/blob.go +++ b/blob/blob.go @@ -3,6 +3,7 @@ package blob import ( "bytes" "encoding/json" + "fmt" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -116,6 +117,9 @@ func NewBlobV0(namespace share.Namespace, data []byte) (*Blob, error) { // NewBlob constructs a new blob from the provided Namespace, data and share version. func NewBlob(shareVersion uint8, namespace share.Namespace, data []byte) (*Blob, error) { + if len(data) == 0 || len(data) > appconsts.DefaultMaxBytes { + return nil, fmt.Errorf("blob data must be > 0 && <= %d, but it was %d bytes", appconsts.DefaultMaxBytes, len(data)) + } if err := namespace.ValidateBlobNamespace(); err != nil { return nil, err } diff --git a/share/namespace.go b/share/namespace.go index a4a2828b11..7d69bff8ff 100644 --- a/share/namespace.go +++ b/share/namespace.go @@ -30,8 +30,9 @@ type Namespace []byte // The byte slice must be <= 10 bytes. // If it is less than 10 bytes, it will be left padded to size 10 with 0s. func NewNamespaceV0(id []byte) (Namespace, error) { - if len(id) > appns.NamespaceVersionZeroIDSize { - return nil, fmt.Errorf("namespace id must be <= %v, but it was %v bytes", appns.NamespaceVersionZeroIDSize, len(id)) + if len(id) == 0 || len(id) > appns.NamespaceVersionZeroIDSize { + return nil, fmt.Errorf( + "namespace id must be > 0 && <= %d, but it was %d bytes", appns.NamespaceVersionZeroIDSize, len(id)) } n := make(Namespace, NamespaceSize) diff --git a/share/namespace_test.go b/share/namespace_test.go index 559babaafd..886161431e 100644 --- a/share/namespace_test.go +++ b/share/namespace_test.go @@ -28,7 +28,7 @@ func TestNewNamespaceV0(t *testing.T) { } testCases := []testCase{ { - name: "8 byte subNid, gets left padded", + name: "8 byte id, gets left padded", subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, expected: Namespace{ 0x0, @@ -37,7 +37,7 @@ func TestNewNamespaceV0(t *testing.T) { wantErr: false, }, { - name: "10 byte subNid, no padding", + name: "10 byte id, no padding", subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x9, 0x10}, expected: Namespace{ 0x0, // version @@ -46,11 +46,17 @@ func TestNewNamespaceV0(t *testing.T) { wantErr: false, }, { - name: "11 byte subNid", + name: "11 byte id", subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x9, 0x10, 0x11}, expected: []byte{}, wantErr: true, }, + { + name: "nil id", + subNid: nil, + expected: []byte{}, + wantErr: true, + }, } for _, tc := range testCases {