-
Notifications
You must be signed in to change notification settings - Fork 111
/
disabled_modules_ante.go
75 lines (59 loc) · 1.66 KB
/
disabled_modules_ante.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package ante
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
)
type DisabledModulesDecorator struct {
prefixes []string
}
func NewDisabledModulesDecorator(disabledModules ...string) DisabledModulesDecorator {
return DisabledModulesDecorator{prefixes: disabledModules}
}
func (dmd DisabledModulesDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
currHeight := ctx.BlockHeight()
for _, msg := range tx.GetMsgs() {
if hasDisabledModuleMsgs(msg, dmd.prefixes...) {
return ctx, fmt.Errorf("tx contains message types from unsupported modules at height %d", currHeight)
}
// Check if there is an attempt to bypass disabled module msg
// with authz MsgExec
if nestedAuthzMsgExecCheck(msg, dmd.prefixes...) {
return ctx, fmt.Errorf("tx contains message types from unsupported modules at height %d", currHeight)
}
}
return next(ctx, tx, simulate)
}
func nestedAuthzMsgExecCheck(msg sdk.Msg, prefixes ...string) bool {
msgExec, ok := msg.(*authz.MsgExec)
if !ok {
return false
}
sdkMsgs, err := msgExec.GetMessages()
if err != nil {
return false
}
for _, msg := range sdkMsgs {
if hasDisabledModuleMsgs(msg, prefixes...) {
return true
}
// Check for nested authz msgExec
_, ok = msg.(*authz.MsgExec)
if ok {
if nestedAuthzMsgExecCheck(msg, prefixes...) {
return true
}
}
}
return false
}
func hasDisabledModuleMsgs(msg sdk.Msg, prefixes ...string) bool {
msgType := sdk.MsgTypeURL(msg)
for _, prefix := range prefixes {
if strings.HasPrefix(msgType, prefix) {
return true
}
}
return false
}