Skip to content

Commit

Permalink
[TRA-671] Prevent connect messages in x/authz (#2434)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyaoy authored Oct 3, 2024
1 parent 52d48f8 commit dbd45b2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions protocol/lib/ante/nested_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

const DYDX_MSG_PREFIX = "/" + constants.AppName
const CONNECT_MSG_PREFIX = "/connect"

// IsNestedMsg returns true if the given msg is a nested msg.
func IsNestedMsg(msg sdk.Msg) bool {
Expand All @@ -32,6 +33,11 @@ func IsDydxMsg(msg sdk.Msg) bool {
return strings.HasPrefix(sdk.MsgTypeURL(msg), DYDX_MSG_PREFIX)
}

// IsConnectMsg returns true if the given msg is a Connect custom msg.
func IsConnectMsg(msg sdk.Msg) bool {
return strings.HasPrefix(sdk.MsgTypeURL(msg), CONNECT_MSG_PREFIX)
}

// ValidateNestedMsg returns err if the given msg is an invalid nested msg.
func ValidateNestedMsg(msg sdk.Msg) error {
if !IsNestedMsg(msg) {
Expand Down Expand Up @@ -80,6 +86,9 @@ func validateInnerMsg(msg sdk.Msg) error {
if IsDydxMsg(inner) {
return fmt.Errorf("Invalid nested msg for MsgExec: dydx msg type")
}
if IsConnectMsg(inner) {
return fmt.Errorf("Invalid nested msg for MsgExec: Connect msg type")
}
}

// For "internal msgs", we allow them, because they are designed to be nested.
Expand Down
35 changes: 35 additions & 0 deletions protocol/lib/ante/nested_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
invalidInnerMsgErr_AppInjected = fmt.Errorf("Invalid nested msg: app-injected msg type")
invalidInnerMsgErr_Nested = fmt.Errorf("Invalid nested msg: double-nested msg type")
invalidInnerMsgErr_Dydx = fmt.Errorf("Invalid nested msg for MsgExec: dydx msg type")
invalidInnerMsgErr_Connect = fmt.Errorf("Invalid nested msg for MsgExec: Connect msg type")
)

func TestIsNestedMsg_Empty(t *testing.T) {
Expand Down Expand Up @@ -106,6 +107,36 @@ func TestIsDydxMsg_Valid(t *testing.T) {
}
}

func TestIsConnectMsg_Invalid(t *testing.T) {
allConnectMsgs := lib.MergeAllMapsMustHaveDistinctKeys(
appmsgs.NormalMsgsConnect,
)
allMsgsMinusConnect := lib.MergeAllMapsMustHaveDistinctKeys(appmsgs.AllowMsgs, appmsgs.DisallowMsgs)
for key := range allConnectMsgs {
delete(allMsgsMinusConnect, key)
}
allNonNilSampleMsgs := testmsgs.GetNonNilSampleMsgs(allMsgsMinusConnect)

for _, sampleMsg := range allNonNilSampleMsgs {
t.Run(sampleMsg.Name, func(t *testing.T) {
require.False(t, ante.IsConnectMsg(sampleMsg.Msg))
})
}
}

func TestIsConnectMsg_Valid(t *testing.T) {
allConnectMsgs := lib.MergeAllMapsMustHaveDistinctKeys(
appmsgs.NormalMsgsConnect,
)
allNonNilSampleMsgs := testmsgs.GetNonNilSampleMsgs(allConnectMsgs)

for _, sampleMsg := range allNonNilSampleMsgs {
t.Run(sampleMsg.Name, func(t *testing.T) {
require.True(t, ante.IsConnectMsg(sampleMsg.Msg))
})
}
}

func TestValidateNestedMsg(t *testing.T) {
tests := map[string]struct {
msg sdk.Msg
Expand Down Expand Up @@ -143,6 +174,10 @@ func TestValidateNestedMsg(t *testing.T) {
msg: &testmsgs.MsgExecWithDydxMessage,
expectedErr: invalidInnerMsgErr_Dydx,
},
"Invalid MsgExec: Connect custom msg": {
msg: &testmsgs.MsgExecWithConnectMessage,
expectedErr: invalidInnerMsgErr_Connect,
},
"Valid: empty inner msg": {
msg: testmsgs.MsgSubmitProposalWithEmptyInner,
expectedErr: nil,
Expand Down
10 changes: 10 additions & 0 deletions protocol/testutil/msgs/nested_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/testutil/encoding"
prices "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
sending "github.com/dydxprotocol/v4-chain/protocol/x/sending/types"
marketmap "github.com/skip-mev/connect/v2/x/marketmap/types"
)

func init() {
Expand Down Expand Up @@ -46,6 +47,9 @@ func init() {
_ = testTxBuilder.SetMsgs(&MsgExecWithDydxMessage)
MsgExecWithDydxMessageTxBytes, _ = testEncodingCfg.TxConfig.TxEncoder()(testTxBuilder.GetTx())

_ = testTxBuilder.SetMsgs(&MsgExecWithConnectMessage)
MsgExecWithConnectMessageTxBytes, _ = testEncodingCfg.TxConfig.TxEncoder()(testTxBuilder.GetTx())

_ = testTxBuilder.SetMsgs(MsgSubmitProposalWithUpgrade)
MsgSubmitProposalWithUpgradeTxBytes, _ = testEncodingCfg.TxConfig.TxEncoder()(testTxBuilder.GetTx())

Expand Down Expand Up @@ -120,6 +124,12 @@ var (
)
MsgExecWithDydxMessageTxBytes []byte

MsgExecWithConnectMessage = authz.NewMsgExec(
constants.AliceAccAddress,
[]sdk.Msg{&marketmap.MsgUpsertMarkets{}},
)
MsgExecWithConnectMessageTxBytes []byte

// Valid MsgSubmitProposals
MsgSubmitProposalWithUpgrade, _ = gov.NewMsgSubmitProposal(
[]sdk.Msg{MsgSoftwareUpgrade}, nil, testProposer, testMetadata, testTitle, testSummary, false)
Expand Down

0 comments on commit dbd45b2

Please sign in to comment.