2022-05-06 18:41:58 +00:00
|
|
|
package ante
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2022-05-06 18:41:58 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
|
|
|
)
|
|
|
|
|
2022-06-06 16:07:31 +00:00
|
|
|
var _ sdk.AnteDecorator = AuthzLimiterDecorator{}
|
|
|
|
|
2022-05-06 18:41:58 +00:00
|
|
|
// 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 {
|
2023-04-05 23:21:59 +00:00
|
|
|
return ctx, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%v", err)
|
2022-05-06 18:41:58 +00:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
2023-04-04 00:08:45 +00:00
|
|
|
authorization, err := m.GetAuthorization()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-06 18:41:58 +00:00
|
|
|
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
|
|
|
|
}
|