From 407a2fddf0f04554852e46f938612bc6e3bab89c Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:09:42 +0900 Subject: [PATCH 1/5] feat: prevent fswap and fbridge module in Submessages --- x/wasm/keeper/msg_dispatcher.go | 9 +++++++++ x/wasm/keeper/msg_dispatcher_test.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/x/wasm/keeper/msg_dispatcher.go b/x/wasm/keeper/msg_dispatcher.go index 15ea530e27..166e26f7ce 100644 --- a/x/wasm/keeper/msg_dispatcher.go +++ b/x/wasm/keeper/msg_dispatcher.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "sort" + "strings" abci "github.com/tendermint/tendermint/abci/types" @@ -81,6 +82,14 @@ func (d MessageDispatcher) dispatchMsgWithGasLimit(ctx sdk.Context, contractAddr func (d MessageDispatcher) DispatchSubmessages(ctx sdk.Context, contractAddr sdk.AccAddress, ibcPort string, msgs []wasmvmtypes.SubMsg) ([]byte, error) { var rsp []byte for _, msg := range msgs { + + // Prevent the use of fswap and fbridge module in Submessages + // https://github.com/Finschia/finschia-sdk/pull/1336, https://github.com/Finschia/finschia-sdk/pull/1340 + if stargateMsg := msg.Msg.Stargate; stargateMsg != nil && + (strings.Contains(stargateMsg.TypeURL, "lbm.fswap.v1") || strings.Contains(stargateMsg.TypeURL, "lbm.fbridge.v1")) { + return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fswap and fbridge not supported of Stargate") + } + switch msg.ReplyOn { case wasmvmtypes.ReplySuccess, wasmvmtypes.ReplyError, wasmvmtypes.ReplyAlways, wasmvmtypes.ReplyNever: default: diff --git a/x/wasm/keeper/msg_dispatcher_test.go b/x/wasm/keeper/msg_dispatcher_test.go index a0ce9e6655..b8032e0ad7 100644 --- a/x/wasm/keeper/msg_dispatcher_test.go +++ b/x/wasm/keeper/msg_dispatcher_test.go @@ -386,6 +386,14 @@ func TestDispatchSubmessages(t *testing.T) { sdk.NewEvent("stargate-reply"), }, }, + "stargate msg with invalid fswap TypeURL": { + msgs: []wasmvmtypes.SubMsg{{Msg: wasmvmtypes.CosmosMsg{Stargate: &wasmvmtypes.StargateMsg{TypeURL: "/lbm.fswap.v1.MsgSwapRequest"}}}}, + expErr: true, + }, + "stargate msg with invalid fbridge TypeURL": { + msgs: []wasmvmtypes.SubMsg{{Msg: wasmvmtypes.CosmosMsg{Stargate: &wasmvmtypes.StargateMsg{TypeURL: "/lbm.fbridge.v1.MsgTransfer"}}}}, + expErr: true, + }, } for name, spec := range specs { t.Run(name, func(t *testing.T) { From 7a6420f603156d61e916c9b296b36d03f1856d74 Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:20:25 +0900 Subject: [PATCH 2/5] docs: add CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b39de675f..066f726913 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased](https://github.com/Finschia/wasmd/compare/v0.3.0...HEAD) ### Features +* [\#123](https://github.com/Finschia/wasmd/pull/123) prevent fswap and fbridge module in Submessages ### Improvements From 1ce21a05eac3d5288933e0395a44d4a38822b974 Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Thu, 25 Apr 2024 16:49:14 +0900 Subject: [PATCH 3/5] fix: separate error handling for fswap and fbridge --- x/wasm/keeper/msg_dispatcher.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/x/wasm/keeper/msg_dispatcher.go b/x/wasm/keeper/msg_dispatcher.go index 166e26f7ce..b3c1e675f9 100644 --- a/x/wasm/keeper/msg_dispatcher.go +++ b/x/wasm/keeper/msg_dispatcher.go @@ -85,9 +85,13 @@ func (d MessageDispatcher) DispatchSubmessages(ctx sdk.Context, contractAddr sdk // Prevent the use of fswap and fbridge module in Submessages // https://github.com/Finschia/finschia-sdk/pull/1336, https://github.com/Finschia/finschia-sdk/pull/1340 - if stargateMsg := msg.Msg.Stargate; stargateMsg != nil && - (strings.Contains(stargateMsg.TypeURL, "lbm.fswap.v1") || strings.Contains(stargateMsg.TypeURL, "lbm.fbridge.v1")) { - return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fswap and fbridge not supported of Stargate") + if stargateMsg := msg.Msg.Stargate; stargateMsg != nil { + if strings.HasPrefix(stargateMsg.TypeURL, "/lbm.fswap.v1") { + return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fswap not supported of Stargate") + } + if strings.HasPrefix(stargateMsg.TypeURL, "/lbm.fbridge.v1") { + return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fbridge not supported of Stargate") + } } switch msg.ReplyOn { From 58714d1fb3f9f33fab3f3e69f19bd4d88320f1cf Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:11:46 +0900 Subject: [PATCH 4/5] refactor: improve error message prepositions --- x/wasm/keeper/msg_dispatcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/wasm/keeper/msg_dispatcher.go b/x/wasm/keeper/msg_dispatcher.go index b3c1e675f9..bb3df7f4f1 100644 --- a/x/wasm/keeper/msg_dispatcher.go +++ b/x/wasm/keeper/msg_dispatcher.go @@ -87,10 +87,10 @@ func (d MessageDispatcher) DispatchSubmessages(ctx sdk.Context, contractAddr sdk // https://github.com/Finschia/finschia-sdk/pull/1336, https://github.com/Finschia/finschia-sdk/pull/1340 if stargateMsg := msg.Msg.Stargate; stargateMsg != nil { if strings.HasPrefix(stargateMsg.TypeURL, "/lbm.fswap.v1") { - return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fswap not supported of Stargate") + return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fswap not supported by Stargate") } if strings.HasPrefix(stargateMsg.TypeURL, "/lbm.fbridge.v1") { - return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fbridge not supported of Stargate") + return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fbridge not supported by Stargate") } } From 2047fe71396629620afd0023f5118bfc896ca53e Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Fri, 26 Apr 2024 17:31:35 +0900 Subject: [PATCH 5/5] refactor: improve Stargate msg handling with denylist --- x/wasm/keeper/msg_dispatcher.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/x/wasm/keeper/msg_dispatcher.go b/x/wasm/keeper/msg_dispatcher.go index bb3df7f4f1..98a0f258f2 100644 --- a/x/wasm/keeper/msg_dispatcher.go +++ b/x/wasm/keeper/msg_dispatcher.go @@ -83,14 +83,14 @@ func (d MessageDispatcher) DispatchSubmessages(ctx sdk.Context, contractAddr sdk var rsp []byte for _, msg := range msgs { - // Prevent the use of fswap and fbridge module in Submessages + // Prevent the use of specific modules in Submessages // https://github.com/Finschia/finschia-sdk/pull/1336, https://github.com/Finschia/finschia-sdk/pull/1340 if stargateMsg := msg.Msg.Stargate; stargateMsg != nil { - if strings.HasPrefix(stargateMsg.TypeURL, "/lbm.fswap.v1") { - return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fswap not supported by Stargate") - } - if strings.HasPrefix(stargateMsg.TypeURL, "/lbm.fbridge.v1") { - return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, "fbridge not supported by Stargate") + deniedModules := []string{"/lbm.fswap.v1", "/lbm.fbridge.v1"} + for _, moduleName := range deniedModules { + if strings.HasPrefix(stargateMsg.TypeURL, moduleName) { + return nil, sdkerrors.Wrap(types.ErrUnsupportedForContract, moduleName+" not supported by Stargate") + } } }