This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 563
fix: add authz ante handler #1741
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f3ffc53
imp: remove distribution perms
facs95 b015cdb
update changelog
facs95 ddfc15a
fix: add authz ante handler
facs95 68af64d
fix tests
facs95 7ad1e78
fix constants
facs95 7b1688d
remove unnecesary setup
facs95 2652fdf
fix commit
facs95 5d7c5ce
fix lint
facs95 999dc34
Merge branch 'main' into facs95/authz-ante
facs95 dbcba8e
Address changes in authz.go and partially refactor unit tests
0a1c f7cf7b6
Refactor duplicate test utils and improve naming
0a1c 4b494a2
Refactor test suite to improve code quality
0a1c 60816dd
Fix EIP-712 testutil comment header
0a1c 63865da
Add DisabledAuthzMsgs to handler options
0a1c b58be7b
Add ante test
0a1c 44da689
Merge branch 'main' into facs95/authz-ante
0a1c aeabde4
Add minor code-quality refactors
0a1c File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2021 Evmos Foundation | ||
// This file is part of Evmos' Ethermint library. | ||
// | ||
// The Ethermint library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The Ethermint library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
errortypes "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/x/authz" | ||
) | ||
|
||
// maxNestedMsgs defines a cap for the number of nested messages on a MsgExec message | ||
const maxNestedMsgs = 6 | ||
|
||
// AuthzLimiterDecorator blocks certain msg types from being granted or executed | ||
// within the authorization module. | ||
type AuthzLimiterDecorator struct { | ||
// disabledMsgs is a set that contains type urls of unauthorized msgs. | ||
disabledMsgs map[string]struct{} | ||
} | ||
|
||
// NewAuthzLimiterDecorator creates a decorator to block certain msg types | ||
// from being granted or executed within authz. | ||
func NewAuthzLimiterDecorator(disabledMsgTypes []string) AuthzLimiterDecorator { | ||
disabledMsgs := make(map[string]struct{}) | ||
for _, url := range disabledMsgTypes { | ||
disabledMsgs[url] = struct{}{} | ||
} | ||
|
||
return AuthzLimiterDecorator{ | ||
disabledMsgs: disabledMsgs, | ||
} | ||
} | ||
|
||
func (ald AuthzLimiterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
if err := ald.checkDisabledMsgs(tx.GetMsgs(), false, 0); err != nil { | ||
return ctx, errorsmod.Wrapf(errortypes.ErrUnauthorized, err.Error()) | ||
} | ||
return next(ctx, tx, simulate) | ||
} | ||
|
||
// checkDisabledMsgs iterates through the msgs and returns an error if it finds any unauthorized msgs. | ||
// | ||
// This method is recursive as MsgExec's can wrap other MsgExecs. nestedMsgs sets a reasonable limit on | ||
// the total messages, regardless of how they are nested. | ||
func (ald AuthzLimiterDecorator) checkDisabledMsgs(msgs []sdk.Msg, isAuthzInnerMsg bool, nestedMsgs int) error { | ||
if nestedMsgs >= maxNestedMsgs { | ||
return fmt.Errorf("found more nested msgs than permitted. Limit is : %d", maxNestedMsgs) | ||
} | ||
for _, msg := range msgs { | ||
switch msg := msg.(type) { | ||
case *authz.MsgExec: | ||
innerMsgs, err := msg.GetMessages() | ||
if err != nil { | ||
return err | ||
} | ||
nestedMsgs++ | ||
if err := ald.checkDisabledMsgs(innerMsgs, true, nestedMsgs); err != nil { | ||
return err | ||
} | ||
case *authz.MsgGrant: | ||
authorization, err := msg.GetAuthorization() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url := authorization.MsgTypeURL() | ||
if ald.isDisabledMsg(url) { | ||
return fmt.Errorf("found disabled msg type: %s", url) | ||
} | ||
default: | ||
url := sdk.MsgTypeURL(msg) | ||
if isAuthzInnerMsg && ald.isDisabledMsg(url) { | ||
return fmt.Errorf("found disabled msg type: %s", url) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// isDisabledMsg returns true if the given message is in the set of restricted | ||
// messages from the AnteHandler. | ||
func (ald AuthzLimiterDecorator) isDisabledMsg(msgTypeURL string) bool { | ||
_, ok := ald.disabledMsgs[msgTypeURL] | ||
return ok | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the change to 6 ?