8000 feat: add hashed address check to hook receiver validation by sh-cha · Pull Request #399 · initia-labs/initia · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add hashed address check to hook receiver validation #399

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

Merged
merged 3 commits into from
May 8, 2025
Merged
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
4 changes: 2 additions & 2 deletions x/ibc-hooks/move-hooks/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (h MoveHooks) onRecvIcs20Packet(
}

// Validate whether the receiver is correctly specified or not.
if err := validateReceiver(msg, data.Receiver); err != nil {
if err := validateReceiver(msg, data.Receiver, h.ac); err != nil {
return newEmitErrorAcknowledgement(err)
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func (h MoveHooks) onRecvIcs721Packet(
}

// Validate whether the receiver is correctly specified or not.
if err := validateReceiver(msg, data.Receiver); err != nil {
if err := validateReceiver(msg, data.Receiver, h.ac); err != nil {
return newEmitErrorAcknowledgement(err)
}

Expand Down
51 changes: 51 additions & 0 deletions x/ibc-hooks/move-hooks/receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,57 @@ func Test_onReceiveIcs20Packet_memo(t *testing.T) {
require.Equal(t, "\"1\"", queryRes.Ret)
}

func Test_onReceiveIcs20Packet_memo_with_hashed_receiver(t *testing.T) {
ctx, input := createDefaultTestInput(t)
_, _, addr := keyPubAddr()

data := transfertypes.FungibleTokenPacketData{
Denom: "foo",
Amount: "10000",
Sender: addr.String(),
Receiver: "cosmos1w53w03gsuvwazjx7jkq530q2l4e496m00hcx2rkj43gvl4vx9zrs65nfw5",
Memo: `{
"move": {
"message": {
"module_address": "0x1",
"module_name": "Counter",
"function_name": "increase"
}
}
}`,
}

dataBz, err := json.Marshal(&data)
require.NoError(t, err)

// failed to due to acl
ack := input.IBCHooksMiddleware.OnRecvPacket(ctx, channeltypes.Packet{
Data: dataBz,
}, addr)
require.False(t, ack.Success())

// set acl
require.NoError(t, input.IBCHooksKeeper.SetAllowed(ctx, movetypes.ConvertVMAddressToSDKAddress(vmtypes.StdAddress), true))

// success
ack = input.IBCHooksMiddleware.OnRecvPacket(ctx, channeltypes.Packet{
Data: dataBz,
}, addr)
require.True(t, ack.Success())

// check the contract state
queryRes, _, err := input.MoveKeeper.ExecuteViewFunction(
ctx,
vmtypes.StdAddress,
"Counter",
"get",
[]vmtypes.TypeTag{},
[][]byte{},
)
require.NoError(t, err)
require.Equal(t, "\"1\"", queryRes.Ret)
}

func Test_OnReceivePacket_ICS721(t *testing.T) {
ctx, input := createDefaultTestInput(t)
_, _, addr := keyPubAddr()
Expand Down
14 changes: 11 additions & 3 deletions x/ibc-hooks/move-hooks/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package move_hooks

import (
"crypto/sha256"
"encoding/json"
"fmt"
"strings"
Expand All @@ -16,6 +17,8 @@ import (

nfttransfertypes "github.com/initia-labs/initia/x/ibc/nft-transfer/types"
movetypes "github.com/initia-labs/initia/x/move/types"

coreaddress "cosmossdk.io/core/address"
)

const senderPrefix = "ibc-move-hook-intermediary"
Expand Down Expand Up @@ -74,12 +77,17 @@ func validateAndParseMemo(memo string) (
return
}

func validateReceiver(msg *movetypes.MsgExecute, receiver string) error {
func validateReceiver(msg *movetypes.MsgExecute, receiver string, ac coreaddress.Codec) error {
functionIdentifier := fmt.Sprintf("%s::%s::%s", msg.ModuleAddress, msg.ModuleName, msg.FunctionName)
if receiver != functionIdentifier {
return errors.Wrap(channeltypes.ErrInvalidPacket, "receiver is not properly set")
if receiver == functionIdentifier {
return nil
}

hashedFunctionIdentifier := sha256.Sum256([]byte(functionIdentifier))
hashedFunctionIdentifierString, err := ac.BytesToString(hashedFunctionIdentifier[:])
if err != nil || receiver != hashedFunctionIdentifierString {
return errors.Wrap(channeltypes.ErrInvalidPacket, "receiver is not properly set")
}
return nil
}

Expand Down
27 changes: 24 additions & 3 deletions x/ibc-hooks/move-hooks/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
movetypes "github.com/initia-labs/initia/x/move/types"
vmtypes "github.com/initia-labs/movevm/types"

sdk "github.com/cosmos/cosmos-sdk/types"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -45,6 +47,7 @@ func Test_isIcs721Packet(t *testing.T) {
}

func Test_validateAndParseMemo_without_callback(t *testing.T) {
ac := authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix())

argBz, err := vmtypes.SerializeUint64(100)
require.NoError(t, err)
Expand Down Expand Up @@ -74,18 +77,19 @@ func Test_validateAndParseMemo_without_callback(t *testing.T) {
},
AsyncCallback: nil,
}, hookData)
require.NoError(t, validateReceiver(hookData.Message, "0x1::dex::swap"))
require.NoError(t, validateReceiver(hookData.Message, "0x1::dex::swap", ac))

// invalid receiver
require.NoError(t, err)
require.Error(t, validateReceiver(hookData.Message, "0x2::dex::swap"))
require.Error(t, validateReceiver(hookData.Message, "0x2::dex::swap", ac))

isMoveRouted, _, err = validateAndParseMemo("hihi")
require.False(t, isMoveRouted)
require.NoError(t, err)
}

func Test_validateAndParseMemo_with_callback(t *testing.T) {
ac := authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix())

argBz, err := vmtypes.SerializeUint64(100)
require.NoError(t, err)
Expand Down Expand Up @@ -124,5 +128,22 @@ func Test_validateAndParseMemo_with_callback(t *testing.T) {
ModuleName: "dex",
},
}, hookData)
require.NoError(t, validateReceiver(hookData.Message, "0x1::dex::swap"))
require.NoError(t, validateReceiver(hookData.Message, "0x1::dex::swap", ac))
}

func Test_validateReceiver(t *testing.T) {
ac := authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix())

hookData := HookData{
Message: &movetypes.MsgExecute{
ModuleAddress: "0x1",
ModuleName: "dex",
FunctionName: "swap",
TypeArgs: []string{},
Args: [][]byte{},
},
}

require.NoError(t, validateReceiver(hookData.Message, "cosmos14ve5y0rgh6aaa45k0g99ctj4la0hw3prr6h7e57mzqx86eg63r6s9yz06a", ac))
require.NoError(t, validateReceiver(hookData.Message, "0x1::dex::swap", ac))
}
Loading
0