-
Notifications
You must be signed in to change notification settings - Fork 0
/
authz.go
86 lines (75 loc) · 2.6 KB
/
authz.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
76
77
78
79
80
81
82
83
84
85
86
package ante
import (
"fmt"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
)
var _ sdk.AnteDecorator = AuthzLimiterDecorator{}
// AuthzLimiterDecorator blocks certain msg types from being granted or executed within authz.
type AuthzLimiterDecorator struct {
// disabledMsgTypes is the type urls of the msgs to block.
disabledMsgTypes []string
}
// NewAuthzLimiterDecorator creates a decorator to block certain msg types from being granted or executed within authz.
func NewAuthzLimiterDecorator(disabledMsgTypes ...string) AuthzLimiterDecorator {
return AuthzLimiterDecorator{
disabledMsgTypes: disabledMsgTypes,
}
}
func (ald AuthzLimiterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
err = ald.checkForDisabledMsg(tx.GetMsgs(), true)
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%v", err)
}
return next(ctx, tx, simulate)
}
// checkForDisabledMsg iterates through the msgs and returns an error if it finds any unauthorized msgs.
//
// When searchOnlyInAuthzMsgs is enabled, only authz MsgGrant and MsgExec are blocked, if they contain unauthorized msg types.
// Otherwise any msg matching the disabled types are blocked, regardless of being in an authz msg or not.
//
// This method is recursive as MsgExec's can wrap other MsgExecs.
func (ald AuthzLimiterDecorator) checkForDisabledMsg(msgs []sdk.Msg, searchOnlyInAuthzMsgs bool) error {
for _, msg := range msgs {
typeURL := sdk.MsgTypeURL(msg)
switch {
case !searchOnlyInAuthzMsgs && ald.isDisabled(typeURL):
return fmt.Errorf("found disabled msg type: %s", typeURL)
case typeURL == sdk.MsgTypeURL(&authz.MsgGrant{}):
m, ok := msg.(*authz.MsgGrant)
if !ok {
panic("unexpected msg type")
}
authorization, err := m.GetAuthorization()
if err != nil {
return err
}
if ald.isDisabled(authorization.MsgTypeURL()) {
return fmt.Errorf("found disabled msg type in MsgGrant: %s", authorization.MsgTypeURL())
}
case typeURL == sdk.MsgTypeURL(&authz.MsgExec{}):
m, ok := msg.(*authz.MsgExec)
if !ok {
panic("unexpected msg type")
}
innerMsgs, err := m.GetMessages()
if err != nil {
return err
}
if err := ald.checkForDisabledMsg(innerMsgs, false); err != nil {
return err
}
}
}
return nil
}
func (ald AuthzLimiterDecorator) isDisabled(msgTypeURL string) bool {
for _, disabledType := range ald.disabledMsgTypes {
if msgTypeURL == disabledType {
return true
}
}
return false
}