Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(statemachine)!: add check for length of version fields in connection handshake messages #4878

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/core/03-connection/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/cosmos/ibc-go/v8/modules/core/exported"
)

const MaximumVersionFieldsLength = 4096 // maximum length in bytes for the fields in version

var (
_ sdk.Msg = (*MsgConnectionOpenInit)(nil)
_ sdk.Msg = (*MsgConnectionOpenConfirm)(nil)
Expand Down
6 changes: 6 additions & 0 deletions modules/core/03-connection/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (suite *MsgTestSuite) TestNewMsgConnectionOpenInit() {
{"invalid counterparty connection ID", &types.MsgConnectionOpenInit{connectionID, types.NewCounterparty("clienttotest", "connectiontotest", prefix), version, 500, signer}, false},
{"empty counterparty prefix", types.NewMsgConnectionOpenInit("clienttotest", "clienttotest", emptyPrefix, version, 500, signer), false},
{"supplied version fails basic validation", types.NewMsgConnectionOpenInit("clienttotest", "clienttotest", prefix, &types.Version{}, 500, signer), false},
{"too long version identifier", types.NewMsgConnectionOpenInit("clienttotest", "clienttotest", prefix, types.NewVersion(ibctesting.GenerateString(types.MaximumVersionFieldsLength+1), []string{"ORDER_ORDERED"}), 500, signer), false},
{"too long version feature", types.NewMsgConnectionOpenInit("clienttotest", "clienttotest", prefix, types.NewVersion(types.DefaultIBCVersionIdentifier, []string{ibctesting.GenerateString(types.MaximumVersionFieldsLength + 1)}), 500, signer), false},
{"empty singer", types.NewMsgConnectionOpenInit("clienttotest", "clienttotest", prefix, version, 500, ""), false},
{"success", types.NewMsgConnectionOpenInit("clienttotest", "clienttotest", prefix, version, 500, signer), true},
}
Expand Down Expand Up @@ -158,6 +160,8 @@ func (suite *MsgTestSuite) TestNewMsgConnectionOpenTry() {
{"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 long version identifier", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{types.NewVersion(ibctesting.GenerateString(types.MaximumVersionFieldsLength+1), []string{"ORDER_ORDERED"})}, 500, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, signer), false},
{"too long version feature", types.NewMsgConnectionOpenTry("clienttotesta", "connectiontotest", "clienttotest", clientState, prefix, []*types.Version{types.NewVersion(types.DefaultIBCVersionIdentifier, []string{ibctesting.GenerateString(types.MaximumVersionFieldsLength + 1)})}, 500, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, signer), false},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -204,6 +208,8 @@ func (suite *MsgTestSuite) TestNewMsgConnectionOpenAck() {
{"empty proofConsensus", types.NewMsgConnectionOpenAck(connectionID, connectionID, clientState, suite.proof, suite.proof, emptyProof, clientHeight, clientHeight, ibctesting.ConnectionVersion, signer), false},
{"invalid consensusHeight", types.NewMsgConnectionOpenAck(connectionID, connectionID, clientState, suite.proof, suite.proof, suite.proof, clientHeight, clienttypes.ZeroHeight(), ibctesting.ConnectionVersion, signer), false},
{"invalid version", types.NewMsgConnectionOpenAck(connectionID, connectionID, clientState, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, &types.Version{}, signer), false},
{"too long version identifier", types.NewMsgConnectionOpenAck(connectionID, connectionID, clientState, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, types.NewVersion(ibctesting.GenerateString(types.MaximumVersionFieldsLength+1), []string{"ORDER_ORDERED"}), signer), false},
{"too long version feature", types.NewMsgConnectionOpenAck(connectionID, connectionID, clientState, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, types.NewVersion(types.DefaultIBCVersionIdentifier, []string{ibctesting.GenerateString(types.MaximumVersionFieldsLength + 1)}), signer), false},
{"empty signer", types.NewMsgConnectionOpenAck(connectionID, connectionID, clientState, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, ibctesting.ConnectionVersion, ""), false},
{"success", types.NewMsgConnectionOpenAck(connectionID, connectionID, clientState, suite.proof, suite.proof, suite.proof, clientHeight, clientHeight, ibctesting.ConnectionVersion, signer), true},
}
Expand Down
6 changes: 6 additions & 0 deletions modules/core/03-connection/types/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ func ValidateVersion(version *Version) error {
if strings.TrimSpace(version.Identifier) == "" {
return errorsmod.Wrap(ErrInvalidVersion, "version identifier cannot be blank")
}
if len(version.Identifier) > MaximumVersionFieldsLength {
return errorsmod.Wrapf(ErrInvalidVersion, "version identifier must not exceed %d bytes", MaximumVersionFieldsLength)
}
for i, feature := range version.Features {
if strings.TrimSpace(feature) == "" {
return errorsmod.Wrapf(ErrInvalidVersion, "feature cannot be blank, index %d", i)
}
if len(feature) > MaximumVersionFieldsLength {
return errorsmod.Wrapf(ErrInvalidVersion, "feature must not exceed %d bytes, index %d", MaximumVersionFieldsLength, i)
}
}

return nil
Expand Down