Skip to content

Commit

Permalink
Problem: no max length validation for blob msg
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Sep 30, 2024
1 parent 3766e6a commit cc638bf
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [#1610](https://github.com/crypto-org-chain/cronos/pull/1610) Sync e2ee module with v1.3.x branch.
* [#1612](https://github.com/crypto-org-chain/cronos/pull/1612) Support ibc channel upgrade related methods.
* [#1614](https://github.com/crypto-org-chain/cronos/pull/1614) Bump cosmos-sdk to v0.50.10.
* [#1613](https://github.com/crypto-org-chain/cronos/pull/1613) Add max length validation for blob msg.

### Bug Fixes

Expand Down
9 changes: 8 additions & 1 deletion x/cronos/types/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,25 @@ func NewMsgStoreBlockList(from string, blob []byte) *MsgStoreBlockList {

var errDummyIdentity = stderrors.New("dummy")

const MaximumBlobLength = 20480

type dummyIdentity struct{}

func (i *dummyIdentity) Unwrap(stanzas []*age.Stanza) ([]byte, error) {
return nil, errDummyIdentity
}

func (msg *MsgStoreBlockList) ValidateBasic() error {
length := len(msg.Blob)
if length > MaximumBlobLength {
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "block length %d must not exceed %d", length, MaximumBlobLength)
}
_, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}

// skip heavy operation in Decrypt by early return with errDummyIdentity in
// https://github.com/FiloSottile/age/blob/v1.1.1/age.go#L197
_, err = age.Decrypt(bytes.NewBuffer(msg.Blob), new(dummyIdentity))
if err != nil && err != errDummyIdentity {
return err
Expand Down
83 changes: 81 additions & 2 deletions x/cronos/types/messages_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package types_test

import (
"bytes"
"fmt"
"log"
"testing"

"github.com/crypto-org-chain/cronos/v2/app"
"filippo.io/age"
sdk "github.com/cosmos/cosmos-sdk/types"
cmdcfg "github.com/crypto-org-chain/cronos/v2/cmd/cronosd/config"
"github.com/crypto-org-chain/cronos/v2/x/cronos/types"
"github.com/stretchr/testify/require"
)

func TestValidateMsgUpdateTokenMapping(t *testing.T) {
app.SetConfig()
cmdcfg.SetBech32Prefixes(sdk.GetConfig())

testCases := []struct {
name string
Expand Down Expand Up @@ -54,3 +58,78 @@ func TestValidateMsgUpdateTokenMapping(t *testing.T) {
})
}
}

func TestValidateMsgStoreBlockList(t *testing.T) {
cmdcfg.SetBech32Prefixes(sdk.GetConfig())

publicKey := "age1cy0su9fwf3gf9mw868g5yut09p6nytfmmnktexz2ya5uqg9vl9sss4euqm"
recipient, err := age.ParseX25519Recipient(publicKey)
if err != nil {
log.Fatalf("Failed to parse public key %q: %v", publicKey, err)
}

from := "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp"
blob := []byte("valid blob data")
testCases := []struct {
name string
msg *types.MsgStoreBlockList
noEncrypt bool
expectError bool
errorMsg string
}{
{
"valid message",
types.NewMsgStoreBlockList(from, blob),
false,
false,
"",
},
{
"blob exceeds maximum length",
types.NewMsgStoreBlockList(
from,
make([]byte, types.MaximumBlobLength+1),
),
false,
true,
fmt.Sprintf("must not exceed %d", types.MaximumBlobLength),
},
{
"invalid sender address",
types.NewMsgStoreBlockList("invalid", blob),
false,
true,
"invalid sender address",
},
{
"decryption error",
types.NewMsgStoreBlockList(from, blob),
true,
true,
"failed to read header",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if !tc.noEncrypt {
out := new(bytes.Buffer)
w, err := age.Encrypt(out, recipient)
require.NoError(t, err)
_, err = w.Write(tc.msg.Blob)
require.NoError(t, err)
err = w.Close()
require.NoError(t, err)
tc.msg.Blob = out.Bytes()
}

err = tc.msg.ValidateBasic()
if tc.expectError {
require.Error(t, err)
require.Contains(t, err.Error(), tc.errorMsg)
} else {
require.NoError(t, err)
}
})
}
}

0 comments on commit cc638bf

Please sign in to comment.