Skip to content

Commit

Permalink
imp: check length of slices of messages (#6256)
Browse files Browse the repository at this point in the history
* imp: check length of slices of messages

* add changelog

* change test limits

(cherry picked from commit 478f4c6)

# Conflicts:
#	CHANGELOG.md
#	modules/apps/27-interchain-accounts/host/types/params.go
#	modules/apps/27-interchain-accounts/host/types/params_test.go
#	modules/core/02-client/types/params.go
#	modules/core/02-client/types/params_test.go
  • Loading branch information
Carlos Rodriguez authored and mergify[bot] committed May 8, 2024
1 parent 3ef2a97 commit 87dd82a
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

<<<<<<< HEAD
* (apps/27-interchain-accounts) [\#6147](https://github.com/cosmos/ibc-go/pull/6147) Emit an event signalling that the host submodule is disabled.
* (testing) [\#6180](https://github.com/cosmos/ibc-go/pull/6180) Add version to tm abci headers in ibctesting.
=======
* (apps/27-interchain-accounts) [\#5533](https://github.com/cosmos/ibc-go/pull/5533) ICA host sets the host connection ID on `OnChanOpenTry`, so that ICA controller implementations are not obliged to set the value on `OnChanOpenInit` if they are not able.
* (core/02-client, core/03-connection, apps/27-interchain-accounts) [\#6256](https://github.com/cosmos/ibc-go/pull/6256) Add length checking of array fields in messages.
>>>>>>> 478f4c60 (imp: check length of slices of messages (#6256))
* (apps/27-interchain-accounts, apps/tranfer, apps/29-fee) [\#6253](https://github.com/cosmos/ibc-go/pull/6253) Allow channel handshake to succeed if fee middleware is wired up on one side, but not the other.

### Features
Expand Down
12 changes: 12 additions & 0 deletions modules/apps/27-interchain-accounts/host/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
const (
// DefaultHostEnabled is the default value for the host param (set to true)
DefaultHostEnabled = true
// Maximum length of the allowlist
MaxAllowListLength = 500
)

var (
Expand Down Expand Up @@ -50,6 +52,7 @@ func (p Params) Validate() error {
return nil
}

<<<<<<< HEAD

Check failure on line 55 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

syntax error: non-declaration statement outside function body

Check failure on line 55 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

syntax error: non-declaration statement outside function body

Check failure on line 55 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

syntax error: non-declaration statement outside function body

Check failure on line 55 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: non-declaration statement outside function body

Check failure on line 55 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: non-declaration statement outside function body
// ParamSetPairs implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
Expand All @@ -71,6 +74,15 @@ func validateAllowlist(i interface{}) error {
allowMsgs, ok := i.([]string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
=======

Check failure on line 77 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected ==, expecting }
func validateAllowlist(allowMsgs []string) error {
if len(allowMsgs) > MaxAllowListLength {
return fmt.Errorf("allow list length must not exceed %d items", MaxAllowListLength)
}

if slices.Contains(allowMsgs, AllowAllHostMsgs) && len(allowMsgs) > 1 {
return fmt.Errorf("allow list must have only one element because the allow all host messages wildcard (%s) is present", AllowAllHostMsgs)
>>>>>>> 478f4c60 (imp: check length of slices of messages (#6256))

Check failure on line 85 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected >>, expecting }

Check failure on line 85 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'
}

for _, typeURL := range allowMsgs {

Check failure on line 88 in modules/apps/27-interchain-accounts/host/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: non-declaration statement outside function body
Expand Down
7 changes: 7 additions & 0 deletions modules/apps/27-interchain-accounts/host/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ import (
func TestValidateParams(t *testing.T) {
require.NoError(t, types.DefaultParams().Validate())
require.NoError(t, types.NewParams(false, []string{}).Validate())
<<<<<<< HEAD

Check failure on line 14 in modules/apps/27-interchain-accounts/host/types/params_test.go

View workflow job for this annotation

GitHub Actions / tests (00)

expected statement, found '<<'
=======
require.Error(t, types.NewParams(true, []string{""}).Validate())
require.Error(t, types.NewParams(true, []string{" "}).Validate())
require.Error(t, types.NewParams(true, []string{"*", "/cosmos.bank.v1beta1.MsgSend"}).Validate())
require.Error(t, types.NewParams(true, make([]string, types.MaxAllowListLength+1)).Validate())
>>>>>>> 478f4c60 (imp: check length of slices of messages (#6256))
}
21 changes: 21 additions & 0 deletions modules/core/02-client/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)

<<<<<<< HEAD

Check failure on line 12 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

syntax error: non-declaration statement outside function body

Check failure on line 12 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

syntax error: non-declaration statement outside function body

Check failure on line 12 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

syntax error: non-declaration statement outside function body

Check failure on line 12 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: non-declaration statement outside function body

Check failure on line 12 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: non-declaration statement outside function body

Check failure on line 12 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: non-declaration statement outside function body

Check failure on line 12 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body
var (
// DefaultAllowedClients are the default clients for the AllowedClients parameter.
DefaultAllowedClients = []string{exported.Solomachine, exported.Tendermint, exported.Localhost}
Expand All @@ -21,6 +22,14 @@ var (
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
=======

Check failure on line 25 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

syntax error: non-declaration statement outside function body

Check failure on line 25 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

syntax error: non-declaration statement outside function body

Check failure on line 25 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

syntax error: non-declaration statement outside function body

Check failure on line 25 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: non-declaration statement outside function body

Check failure on line 25 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: non-declaration statement outside function body

Check failure on line 25 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: non-declaration statement outside function body

Check failure on line 25 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body
// Maximum length of the allowed clients list
const MaxAllowedClientsLength = 200

// DefaultAllowedClients are the default clients for the AllowedClients parameter.
// By default it allows all client types.
var DefaultAllowedClients = []string{AllowAllClients}
>>>>>>> 478f4c60 (imp: check length of slices of messages (#6256))

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

syntax error: non-declaration statement outside function body

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

invalid character U+0023 '#'

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

syntax error: non-declaration statement outside function body

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

invalid character U+0023 '#'

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

syntax error: non-declaration statement outside function body

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

invalid character U+0023 '#'

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: non-declaration statement outside function body

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

invalid character U+0023 '#'

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: non-declaration statement outside function body

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

invalid character U+0023 '#'

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: non-declaration statement outside function body

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body

Check failure on line 32 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

invalid character U+0023 '#'

// NewParams creates a new parameter configuration for the ibc client module
func NewParams(allowedClients ...string) Params {
Expand All @@ -46,6 +55,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
}
}

<<<<<<< HEAD

Check failure on line 58 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

syntax error: non-declaration statement outside function body

Check failure on line 58 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

syntax error: non-declaration statement outside function body

Check failure on line 58 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

syntax error: non-declaration statement outside function body

Check failure on line 58 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: non-declaration statement outside function body

Check failure on line 58 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: non-declaration statement outside function body

Check failure on line 58 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body
// IsAllowedClient checks if the given client type is registered on the allowlist.
func (p Params) IsAllowedClient(clientType string) bool {
for _, allowedClient := range p.AllowedClients {
Expand All @@ -60,6 +70,17 @@ func validateClients(i interface{}) error {
clients, ok := i.([]string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
=======

Check failure on line 73 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

syntax error: unexpected ==, expecting }

Check failure on line 73 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

syntax error: unexpected ==, expecting }

Check failure on line 73 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

syntax error: unexpected ==, expecting }

Check failure on line 73 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected ==, expecting }

Check failure on line 73 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected ==, expecting }

Check failure on line 73 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected ==, expecting }
// validateClients checks that the given clients are not blank and there are no duplicates.
// If AllowAllClients wildcard (*) is used, then there should no other client types in the allow list
func validateClients(clients []string) error {
if len(clients) > MaxAllowedClientsLength {
return fmt.Errorf("allowed clients length must not exceed %d items", MaxAllowedClientsLength)
}

if slices.Contains(clients, AllowAllClients) && len(clients) > 1 {
return fmt.Errorf("allow list must have only one element because the allow all clients wildcard (%s) is present", AllowAllClients)
>>>>>>> 478f4c60 (imp: check length of slices of messages (#6256))

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

syntax error: unexpected >>, expecting }

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

invalid character U+0023 '#'

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

syntax error: unexpected >>, expecting }

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

invalid character U+0023 '#'

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

syntax error: unexpected >>, expecting }

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

invalid character U+0023 '#'

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected >>, expecting }

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

invalid character U+0023 '#'

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected >>, expecting }

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

invalid character U+0023 '#'

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected >>, expecting }

Check failure on line 83 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

invalid character U+0023 '#'
}

for i, clientType := range clients {

Check failure on line 86 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm64)

syntax error: non-declaration statement outside function body

Check failure on line 86 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (arm)

syntax error: non-declaration statement outside function body

Check failure on line 86 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / build (amd64)

syntax error: non-declaration statement outside function body

Check failure on line 86 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: non-declaration statement outside function body

Check failure on line 86 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: non-declaration statement outside function body

Check failure on line 86 in modules/core/02-client/types/params.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body) (typecheck)
Expand Down
6 changes: 6 additions & 0 deletions modules/core/02-client/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func TestValidateParams(t *testing.T) {
{"default params", DefaultParams(), true},
{"custom params", NewParams(exported.Tendermint), true},
{"blank client", NewParams(" "), false},
<<<<<<< HEAD

Check failure on line 20 in modules/core/02-client/types/params_test.go

View workflow job for this annotation

GitHub Actions / tests (01)

expected operand, found '<<'
=======
{"duplicate clients", NewParams(exported.Tendermint, exported.Tendermint), false},
{"allow all clients plus valid client", NewParams(AllowAllClients, exported.Tendermint), false},
{"too many allowed clients", NewParams(make([]string, MaxAllowedClientsLength+1)...), false},
>>>>>>> 478f4c60 (imp: check length of slices of messages (#6256))
}

for _, tc := range testCases {
Expand Down
3 changes: 3 additions & 0 deletions modules/core/03-connection/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func (msg MsgConnectionOpenTry) ValidateBasic() error {
if len(msg.CounterpartyVersions) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidVersion, "empty counterparty versions")
}
if len(msg.CounterpartyVersions) > MaxCounterpartyVersionsLength {
return errorsmod.Wrapf(ibcerrors.ErrInvalidVersion, "counterparty versions must not exceed %d items", MaxCounterpartyVersionsLength)
}
for i, version := range msg.CounterpartyVersions {
if err := ValidateVersion(version); err != nil {
return sdkerrors.Wrapf(err, "basic validation failed on version with index %d", i)
Expand Down
4 changes: 3 additions & 1 deletion modules/core/03-connection/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ func (suite *MsgTestSuite) TestNewMsgConnectionOpenTry() {
{"empty proofConsensus", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{ibctesting.ConnectionVersion}, 500, suite.proof, suite.proof, emptyProof, clientHeight, clientHeight, signer), false},
{"invalid consensusHeight", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{ibctesting.ConnectionVersion}, 500, suite.proof, suite.proof, suite.proof, clientHeight, clienttypes.ZeroHeight(), signer), false},
{"empty singer", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{ibctesting.ConnectionVersion}, 500, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, ""), false},
{"success", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{ibctesting.ConnectionVersion}, 500, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, signer), true},
{"invalid version", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{{}}, 500, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, signer), false},
{"too many counterparty versions", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, make([]*types.Version, types.MaxCounterpartyVersionsLength+1), 500, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, signer), false},
{"too many features in counterparty version", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{{"v1", make([]string, types.MaxFeaturesLength+1)}}, 500, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, signer), false},
{"success", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{ibctesting.ConnectionVersion}, 500, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, signer), true},
}

for _, tc := range testCases {
Expand Down
8 changes: 8 additions & 0 deletions modules/core/03-connection/types/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ var (
allowNilFeatureSet = map[string]bool{
DefaultIBCVersionIdentifier: false,
}

// MaxVersionsLength is the maximum number of versions that can be supported
MaxCounterpartyVersionsLength = 100
// MaxFeaturesLength is the maximum number of features that can be supported
MaxFeaturesLength = 100
)

var _ exported.Version = &Version{}
Expand Down Expand Up @@ -59,6 +64,9 @@ func ValidateVersion(version *Version) error {
if strings.TrimSpace(version.Identifier) == "" {
return sdkerrors.Wrap(ErrInvalidVersion, "version identifier cannot be blank")
}
if len(version.Features) > MaxFeaturesLength {
return errorsmod.Wrapf(ErrInvalidVersion, "features length must not exceed %d items", MaxFeaturesLength)
}
for i, feature := range version.Features {
if strings.TrimSpace(feature) == "" {
return sdkerrors.Wrapf(ErrInvalidVersion, "feature cannot be blank, index %d", i)
Expand Down

0 comments on commit 87dd82a

Please sign in to comment.