Skip to content

Commit

Permalink
Merge pull request #1624 from CosmWasm/upper_limit
Browse files Browse the repository at this point in the history
Ensure some contraints and limits on pin/unpin code ids
  • Loading branch information
alpe authored Sep 18, 2023
2 parents 6a2bffd + b72d182 commit 7dba5c7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
33 changes: 28 additions & 5 deletions x/wasm/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,25 @@ func (msg MsgPinCodes) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

const maxCodeIDTotal = 50

func (msg MsgPinCodes) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return errorsmod.Wrap(err, "authority")
}
if len(msg.CodeIDs) == 0 {
return validateCodeIDs(msg.CodeIDs)
}

// ensure not empty, not duplicates and not exceeding max number
func validateCodeIDs(codeIDs []uint64) error {
switch n := len(codeIDs); {
case n == 0:
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty code ids")
case n > maxCodeIDTotal:
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "total number of code ids is greater than %d", maxCodeIDTotal)
}
if hasDuplicates(codeIDs) {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "duplicate code ids")
}
return nil
}
Expand Down Expand Up @@ -519,10 +532,7 @@ func (msg MsgUnpinCodes) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return errorsmod.Wrap(err, "authority")
}
if len(msg.CodeIDs) == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty code ids")
}
return nil
return validateCodeIDs(msg.CodeIDs)
}

func (msg MsgSudoContract) Route() string {
Expand Down Expand Up @@ -684,6 +694,7 @@ func (msg MsgRemoveCodeUploadParamsAddresses) ValidateBasic() error {
func checkDuplicatedAddresses(addresses []string) error {
index := map[string]struct{}{}
for _, addr := range addresses {
addr = strings.ToUpper(addr)
if _, err := sdk.AccAddressFromBech32(addr); err != nil {
return errorsmod.Wrap(err, "addresses")
}
Expand Down Expand Up @@ -739,3 +750,15 @@ func (msg MsgStoreAndMigrateContract) ValidateBasic() error {
}
return nil
}

// returns true when slice contains any duplicates
func hasDuplicates[T comparable](s []T) bool {
index := make(map[T]struct{}, len(s))
for _, v := range s {
if _, exists := index[v]; exists {
return true
}
index[v] = struct{}{}
}
return false
}
37 changes: 36 additions & 1 deletion x/wasm/types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,6 @@ func TestMsgRemoveCodeUploadParamsAddressesValidation(t *testing.T) {
func TestMsgPinCodesValidation(t *testing.T) {
// proper address size
goodAddress := sdk.AccAddress(make([]byte, 20)).String()

specs := map[string]struct {
src MsgPinCodes
expErr bool
Expand Down Expand Up @@ -915,6 +914,20 @@ func TestMsgPinCodesValidation(t *testing.T) {
},
expErr: true,
},
"exceeds max code ids": {
src: MsgPinCodes{
Authority: goodAddress,
CodeIDs: genCodeIDs(51),
},
expErr: true,
},
"duplicate code ids": {
src: MsgPinCodes{
Authority: goodAddress,
CodeIDs: []uint64{1, 1},
},
expErr: true,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
Expand Down Expand Up @@ -961,6 +974,20 @@ func TestMsgUnpinCodesValidation(t *testing.T) {
},
expErr: true,
},
"exceeds max code ids": {
src: MsgUnpinCodes{
Authority: goodAddress,
CodeIDs: genCodeIDs(51),
},
expErr: true,
},
"duplicate code ids": {
src: MsgUnpinCodes{
Authority: goodAddress,
CodeIDs: []uint64{1, 1},
},
expErr: true,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
Expand All @@ -974,6 +1001,14 @@ func TestMsgUnpinCodesValidation(t *testing.T) {
}
}

func genCodeIDs(max int) []uint64 {
r := make([]uint64, max)
for i := 0; i < max; i++ {
r[i] = uint64(i)
}
return r
}

func TestMsgSudoContractValidation(t *testing.T) {
badAddress := "abcd"
// proper address size
Expand Down

0 comments on commit 7dba5c7

Please sign in to comment.