diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f37e7e324..46b39679e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +* (apps/27-gmp) [\#9077](https://github.com/cosmos/ibc-go/pull/9077) Stop validating `AccountIdentifier.Sender` as a local bech32 address in genesis validation/import; the sender is a counterparty identifier (e.g. an EVM hex address), so exported state containing ICS27 accounts could not be validated or re-imported. * (core/02-client) [\#9080](https://github.com/cosmos/ibc-go/pull/9080) Return an error from `RecoverClient` when the substitute client is of a different client type than the subject instead of panicking in the subject light client module. * (apps/pfm) [\#9006](https://github.com/cosmos/ibc-go/pull/9006) fix(apps/pfm): remove legacy params before the v3 to v4 migration * (apps/rate-limiting) [\#8767](https://github.com/cosmos/ibc-go/pull/8767) Fix string conflict in rate-limiting prefix iterator diff --git a/modules/apps/27-gmp/keeper/genesis.go b/modules/apps/27-gmp/keeper/genesis.go index 281cc1adc3..90bc393533 100644 --- a/modules/apps/27-gmp/keeper/genesis.go +++ b/modules/apps/27-gmp/keeper/genesis.go @@ -20,8 +20,8 @@ func (k *Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) erro if _, err := sdk.AccAddressFromBech32(account.AccountAddress); err != nil { return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err) } - if _, err := sdk.AccAddressFromBech32(account.AccountId.Sender); err != nil { - return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err) + if err := account.AccountId.Validate(); err != nil { + return err } ics27Account := types.ICS27Account{ diff --git a/modules/apps/27-gmp/keeper/genesis_test.go b/modules/apps/27-gmp/keeper/genesis_test.go index 6dae7b1f60..4f934c6355 100644 --- a/modules/apps/27-gmp/keeper/genesis_test.go +++ b/modules/apps/27-gmp/keeper/genesis_test.go @@ -42,9 +42,23 @@ func (s *KeeperTestSuite) TestInitGenesis() { ibcerrors.ErrInvalidAddress, }, { - "failure: invalid sender address", + "success: sender is a counterparty (non-bech32) identifier", func() { - genesisState.Ics27Accounts[0].AccountId.Sender = invalid + genesisState.Ics27Accounts[0].AccountId.Sender = "0x1234567890abcdef1234567890abcdef12345678" + }, + nil, + }, + { + "failure: empty sender address", + func() { + genesisState.Ics27Accounts[0].AccountId.Sender = " " + }, + ibcerrors.ErrInvalidAddress, + }, + { + "failure: sender address exceeds max length", + func() { + genesisState.Ics27Accounts[0].AccountId.Sender = ibctesting.GenerateString(types.MaximumSenderLength + 1) }, ibcerrors.ErrInvalidAddress, }, @@ -132,3 +146,27 @@ func (s *KeeperTestSuite) TestExportGenesis() { s.Require().Equal(sender, genesisState.Ics27Accounts[0].AccountId.Sender) s.Require().Equal([]byte(testSalt), genesisState.Ics27Accounts[0].AccountId.Salt) } + +// TestGenesisRoundTripRemoteSender ensures that an ICS27 account created for a +// sender that is not a local bech32 address (e.g. an EVM hex address) survives +// an export -> validate -> import genesis round trip. +func (s *KeeperTestSuite) TestGenesisRoundTripRemoteSender() { + s.SetupTest() + ctx := s.chainA.GetContext() + gmpKeeper := s.chainA.GetSimApp().GMPKeeper + + evmSender := "0x1234567890abcdef1234567890abcdef12345678" + packetData := types.NewGMPPacketData(evmSender, "", []byte(testSalt), []byte{}, "") + + // account creation happens before the (expected) payload execution failure + _, err := gmpKeeper.OnRecvPacket(ctx, &packetData, ibctesting.FirstClientID) + s.Require().ErrorIs(err, types.ErrInvalidPayload) + + exported, err := gmpKeeper.ExportGenesis(ctx) + s.Require().NoError(err) + s.Require().Len(exported.Ics27Accounts, 1) + s.Require().Equal(evmSender, exported.Ics27Accounts[0].AccountId.Sender) + + s.Require().NoError(exported.Validate()) + s.Require().NoError(gmpKeeper.InitGenesis(ctx, exported)) +} diff --git a/modules/apps/27-gmp/module_test.go b/modules/apps/27-gmp/module_test.go index 2cf15ea6f2..f00714a13e 100644 --- a/modules/apps/27-gmp/module_test.go +++ b/modules/apps/27-gmp/module_test.go @@ -74,14 +74,14 @@ func (s *AppModuleTestSuite) TestValidateGenesis() { true, }, { - "failure: invalid sender address", + "failure: empty sender address", &types.GenesisState{ Ics27Accounts: []types.RegisteredICS27Account{ { AccountAddress: s.chainA.SenderAccount.GetAddress().String(), AccountId: types.AccountIdentifier{ ClientId: ibctesting.FirstClientID, - Sender: "invalid", + Sender: "", Salt: []byte("salt"), }, }, diff --git a/modules/apps/27-gmp/types/account.go b/modules/apps/27-gmp/types/account.go index 5bd1662c56..1260976b29 100644 --- a/modules/apps/27-gmp/types/account.go +++ b/modules/apps/27-gmp/types/account.go @@ -29,6 +29,27 @@ func NewAccountIdentifier(clientID, sender string, salt []byte) AccountIdentifie } } +// Validate performs basic validation of the account identifier fields. +// The sender is an opaque identifier on the counterparty chain (for example an +// EVM hex address or a bech32 address with a foreign prefix), so it is only +// checked for presence and length, mirroring GMPPacketData.ValidateBasic. +func (id AccountIdentifier) Validate() error { + if err := host.ClientIdentifierValidator(id.ClientId); err != nil { + return errorsmod.Wrapf(err, "invalid client ID %s", id.ClientId) + } + if strings.TrimSpace(id.Sender) == "" { + return errorsmod.Wrap(ibcerrors.ErrInvalidAddress, "missing sender address") + } + if len(id.Sender) > MaximumSenderLength { + return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "sender address must not exceed %d bytes", MaximumSenderLength) + } + if len(id.Salt) > MaximumSaltLength { + return errorsmod.Wrapf(ErrInvalidSalt, "salt must not exceed %d bytes", MaximumSaltLength) + } + + return nil +} + // NewICS27Account creates a new ICS27Account with the given address and accountId. func NewICS27Account(addr string, accountID *AccountIdentifier) ICS27Account { return ICS27Account{ diff --git a/modules/apps/27-gmp/types/genesis.go b/modules/apps/27-gmp/types/genesis.go index 3f209f6079..c42b820f4d 100644 --- a/modules/apps/27-gmp/types/genesis.go +++ b/modules/apps/27-gmp/types/genesis.go @@ -7,7 +7,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - host "github.com/lfdt-ibc/ibc-go/v11/modules/core/24-host" ibcerrors "github.com/lfdt-ibc/ibc-go/v11/modules/core/errors" ) @@ -25,14 +24,8 @@ func (gs GenesisState) Validate() error { if _, err := sdk.AccAddressFromBech32(account.AccountAddress); err != nil { return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err) } - if _, err := sdk.AccAddressFromBech32(account.AccountId.Sender); err != nil { - return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err) - } - if err := host.ClientIdentifierValidator(account.AccountId.ClientId); err != nil { - return errorsmod.Wrapf(err, "invalid source client ID %s", account.AccountId.ClientId) - } - if len(account.AccountId.Salt) > MaximumSaltLength { - return errorsmod.Wrapf(ErrInvalidSalt, "salt must not exceed %d bytes", MaximumSaltLength) + if err := account.AccountId.Validate(); err != nil { + return err } } diff --git a/modules/apps/27-gmp/types/genesis_test.go b/modules/apps/27-gmp/types/genesis_test.go index ea2ebcc375..80f90623c1 100644 --- a/modules/apps/27-gmp/types/genesis_test.go +++ b/modules/apps/27-gmp/types/genesis_test.go @@ -64,14 +64,46 @@ func TestGenesisState_Validate(t *testing.T) { true, }, { - "failure: invalid sender address", + "success: sender is a counterparty (non-bech32) identifier", &types.GenesisState{ Ics27Accounts: []types.RegisteredICS27Account{ { AccountAddress: validAddress, AccountId: types.AccountIdentifier{ ClientId: validClientID, - Sender: "invalid", + Sender: "0x1234567890abcdef1234567890abcdef12345678", + Salt: []byte("salt"), + }, + }, + }, + }, + false, + }, + { + "failure: empty sender address", + &types.GenesisState{ + Ics27Accounts: []types.RegisteredICS27Account{ + { + AccountAddress: validAddress, + AccountId: types.AccountIdentifier{ + ClientId: validClientID, + Sender: " ", + Salt: []byte("salt"), + }, + }, + }, + }, + true, + }, + { + "failure: sender address exceeds max length", + &types.GenesisState{ + Ics27Accounts: []types.RegisteredICS27Account{ + { + AccountAddress: validAddress, + AccountId: types.AccountIdentifier{ + ClientId: validClientID, + Sender: ibctesting.GenerateString(types.MaximumSenderLength + 1), Salt: []byte("salt"), }, },