Skip to content

Commit

Permalink
Add ibc memo length check (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipsu522 authored Jun 10, 2024
1 parent e9f5246 commit 2ec5bce
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions modules/apps/transfer/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ var (
ErrSendDisabled = sdkerrors.Register(ModuleName, 7, "fungible token transfers from this chain are disabled")
ErrReceiveDisabled = sdkerrors.Register(ModuleName, 8, "fungible token transfers to this chain are disabled")
ErrMaxTransferChannels = sdkerrors.Register(ModuleName, 9, "max transfer channels")
ErrInvalidMemo = sdkerrors.Register(ModuleName, 10, "invalid memo")
)
6 changes: 5 additions & 1 deletion modules/apps/transfer/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

// msg types
const (
TypeMsgTransfer = "transfer"
TypeMsgTransfer = "transfer"
MaximumMemoLength = 32768 // maximum length of the memo in bytes (value chosen arbitrarily)
)

// NewMsgTransfer creates a new MsgTransfer instance
Expand Down Expand Up @@ -69,6 +70,9 @@ func (msg MsgTransfer) ValidateBasic() error {
if strings.TrimSpace(msg.Receiver) == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing recipient address")
}
if len(msg.Memo) > MaximumMemoLength {
return sdkerrors.Wrapf(ErrInvalidMemo, "memo must not exceed %d bytes", MaximumMemoLength)
}
return ValidateIBCDenom(msg.Token.Denom)
}

Expand Down
14 changes: 14 additions & 0 deletions modules/apps/transfer/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
"math/rand"
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -62,8 +63,20 @@ func TestMsgTransferGetSignBytes(t *testing.T) {
})
}

// GenerateString generates a random string of the given length in bytes
func GenerateString(length uint) string {
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
bytes := make([]byte, length)
for i := range bytes {
bytes[i] = charset[rand.Intn(len(charset))]
}
return string(bytes)
}

// TestMsgTransferValidation tests ValidateBasic for MsgTransfer
func TestMsgTransferValidation(t *testing.T) {
largeMemoTransfer := NewMsgTransfer(validPort, validChannel, coin, addr1, addr2, timeoutHeight, 0)
largeMemoTransfer.Memo = GenerateString(MaximumMemoLength + 1)
testCases := []struct {
name string
msg *MsgTransfer
Expand All @@ -77,6 +90,7 @@ func TestMsgTransferValidation(t *testing.T) {
{"port id contains non-alpha", NewMsgTransfer(invalidPort, validChannel, coin, addr1, addr2, timeoutHeight, 0), false},
{"too short channel id", NewMsgTransfer(validPort, invalidShortChannel, coin, addr1, addr2, timeoutHeight, 0), false},
{"too long channel id", NewMsgTransfer(validPort, invalidLongChannel, coin, addr1, addr2, timeoutHeight, 0), false},
{"too long memo", largeMemoTransfer, false},
{"channel id contains non-alpha", NewMsgTransfer(validPort, invalidChannel, coin, addr1, addr2, timeoutHeight, 0), false},
{"invalid denom", NewMsgTransfer(validPort, validChannel, invalidDenomCoin, addr1, addr2, timeoutHeight, 0), false},
{"zero coin", NewMsgTransfer(validPort, validChannel, zeroCoin, addr1, addr2, timeoutHeight, 0), false},
Expand Down
3 changes: 1 addition & 2 deletions testing/simapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package simapp
import (
"encoding/json"
"fmt"
"io/ioutil"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
"io/ioutil"

"github.com/cosmos/ibc-go/v3/testing/simapp/helpers"
)
Expand Down

0 comments on commit 2ec5bce

Please sign in to comment.