Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: no permissions system in cronos #795

Merged
merged 30 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f3d4444
add permissions to cronos
thomas-nguy Dec 19, 2022
acae055
fix lint
thomas-nguy Dec 19, 2022
b5fad87
fix logic
thomas-nguy Dec 19, 2022
aa53a8b
update tyoe
thomas-nguy Dec 21, 2022
b560d64
add cli cmd
thomas-nguy Dec 21, 2022
b090665
add comments
thomas-nguy Dec 22, 2022
61121ea
add unit test
thomas-nguy Dec 22, 2022
6d3bd6f
update change log
thomas-nguy Dec 22, 2022
b5dd092
add integration tests
thomas-nguy Dec 22, 2022
22459d1
fix python lint
thomas-nguy Dec 22, 2022
61cf2f7
fix lint
thomas-nguy Jan 27, 2023
3f8d051
move authorization logic to ante handler
thomas-nguy Jan 27, 2023
8e3a9cc
fix lint
thomas-nguy Jan 27, 2023
dcb53c8
rebase main
thomas-nguy Jan 30, 2023
1265646
update gomod2mix
thomas-nguy Jan 30, 2023
a696140
update buf
thomas-nguy Jan 31, 2023
c943010
regenerate proto
thomas-nguy Jan 31, 2023
7f61c18
update gomod2nix
thomas-nguy Jan 31, 2023
aa88d77
fix proto
thomas-nguy Feb 6, 2023
801550b
skip case when permission is 0
thomas-nguy Feb 6, 2023
09c55ee
simplify permissions query
thomas-nguy Feb 6, 2023
5c2a98f
Merge branch 'main' into thomas/add-permissions
thomas-nguy Feb 10, 2023
65270e6
Merge branch 'main' into thomas/add-permissions
thomas-nguy Feb 10, 2023
15fa8e3
upgrade go verison
thomas-nguy Feb 10, 2023
58a127c
upgrade protogen
thomas-nguy Feb 10, 2023
493a109
Merge branch 'main' into thomas/add-permissions
yihuang Feb 10, 2023
3c587ed
revert upgrading proto gen
thomas-nguy Feb 10, 2023
7327bb1
Merge branch 'thomas/add-permissions' of https://github.com/thomas-ng…
thomas-nguy Feb 10, 2023
3173dc5
go mod tidy in version db
thomas-nguy Feb 10, 2023
1de67b7
return error in antehandler
thomas-nguy Feb 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [cronos#781](https://github.com/crypto-org-chain/cronos/pull/781) Add prune command.
- [cronos#830](https://github.com/crypto-org-chain/cronos/pull/830) Upgrade gravity bridge for latest bugfixes, patching two important DOS vulnerabilities
- [cronos#834](https://github.com/crypto-org-chain/cronos/pull/834) Remove unsafe experimental flag.
- [cronos#795](https://github.com/crypto-org-chain/cronos/pull/795) Support permissions in cronos.

### Bug Fixes

Expand Down
53 changes: 53 additions & 0 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/crypto-org-chain/cronos/v2/x/cronos/keeper"
"github.com/crypto-org-chain/cronos/v2/x/cronos/types"
evmante "github.com/evmos/ethermint/app/ante"
)

// NewAnteHandler add additional logic on top of Ethermint's anteHandler
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
return func(
ctx sdk.Context, tx sdk.Tx, sim bool,
) (newCtx sdk.Context, err error) {
var anteHandler sdk.AnteHandler

defer evmante.Recover(ctx.Logger(), &err)

// Check msg authorization
for _, msg := range tx.GetMsgs() {
thomas-nguy marked this conversation as resolved.
Show resolved Hide resolved
var permissionToCheck uint64
var accountToCheck sdk.AccAddress

switch v := msg.(type) {
thomas-nguy marked this conversation as resolved.
Show resolved Hide resolved
case *types.MsgUpdateTokenMapping:
permissionToCheck = keeper.CanChangeTokenMapping
acc, err := sdk.AccAddressFromBech32(v.Sender)
if err != nil {
panic(err)
}
accountToCheck = acc
case *types.MsgTurnBridge:
permissionToCheck = keeper.CanTurnBridge
acc, err := sdk.AccAddressFromBech32(v.Sender)
if err != nil {
panic(err)
}
accountToCheck = acc
}

if !options.CronosKeeper.HasPermission(ctx, accountToCheck, permissionToCheck) {
return newCtx, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "msg sender is unauthorized")
}
}

anteHandler, err = evmante.NewAnteHandler(options.EvmOptions)
if err != nil {
panic(err)
}
return anteHandler(ctx, tx, sim)
}, nil
}
12 changes: 12 additions & 0 deletions app/ante/handler_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ante

import (
evmante "github.com/evmos/ethermint/app/ante"
)

// HandlerOptions extend the ethermint's AnteHandler options by adding extra keeper necessary for
// custom ante handler logics
type HandlerOptions struct {
EvmOptions evmante.HandlerOptions
CronosKeeper CronosKeeper
}
7 changes: 7 additions & 0 deletions app/ante/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ante

import sdk "github.com/cosmos/cosmos-sdk/types"

type CronosKeeper interface {
HasPermission(ctx sdk.Context, account sdk.AccAddress, permissionsToCheck uint64) bool
}
11 changes: 9 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"github.com/crypto-org-chain/cronos/v2/app/ante"
"github.com/crypto-org-chain/cronos/v2/x/cronos/middleware"
"golang.org/x/exp/slices"

Expand Down Expand Up @@ -755,7 +756,7 @@ func New(

// use Ethermint's custom AnteHandler
func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64) {
anteHandler, err := evmante.NewAnteHandler(evmante.HandlerOptions{
evmOptions := evmante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
EvmKeeper: app.EvmKeeper,
Expand All @@ -767,7 +768,13 @@ func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64) {
MaxTxGasWanted: maxGasWanted,
ExtensionOptionChecker: ethermint.HasDynamicFeeExtensionOption,
TxFeeChecker: evmante.NewDynamicFeeChecker(app.EvmKeeper),
})
}
options := ante.HandlerOptions{
EvmOptions: evmOptions,
CronosKeeper: app.CronosKeeper,
}

anteHandler, err := ante.NewAnteHandler(options)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

Loading