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

Add from/to validation to eth_sendRawTransaction #107

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
4 changes: 2 additions & 2 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (evm *EVM) Interpreter() *EVMInterpreter {
// execution error or failed value transfer.
func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas uint64, value *uint256.Int) (ret []byte, leftOverGas uint64, err error) {
// Fail if the address is not allowed to call
if evm.isDeniedToCall(addr) {
if IsDeniedToCall(evm.StateDB, addr) {
return nil, 0, ErrUnauthorizedCall
}
// Fail if we're trying to execute above the call depth limit
Expand Down Expand Up @@ -516,7 +516,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
// Create creates a new contract using code as deployment code.
func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
// Fail if the caller is not allowed to create
if !evm.isAllowedToCreate(caller.Address()) {
if !IsAllowedToCreate(evm.StateDB, caller.Address()) {
return nil, common.Address{}, 0, ErrUnauthorizedCreate
}
contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
Expand Down
18 changes: 7 additions & 11 deletions core/vm/evm_access_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts/oasys"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)

var (
Expand All @@ -21,20 +20,17 @@ var (
emptyHash common.Hash
)

// Check the `_createAllowedList` mappings in the `EVMAccessControl` contract
func (evm *EVM) isAllowedToCreate(address common.Address) bool {
if evm.chainConfig.ChainID.Cmp(params.OasysTestnetChainConfig.ChainID) == 0 {
return true // Allow all contract creation on testnet
}
hash := computeAddressMapStorageKey(address, 1)
val := evm.StateDB.GetState(evmAccessControl, hash)
// Check the `_createAllowedList` mappings in the `EVMAccessControl` contract
func IsAllowedToCreate(state StateDB, from common.Address) bool {
hash := computeAddressMapStorageKey(from, 1)
val := state.GetState(evmAccessControl, hash)
return val.Cmp(emptyHash) != 0
}

// Check the `_callAllowedList` mappings in the `EVMAccessControl` contract
func (evm *EVM) isDeniedToCall(address common.Address) bool {
hash := computeAddressMapStorageKey(address, 2)
val := evm.StateDB.GetState(evmAccessControl, hash)
func IsDeniedToCall(state StateDB, to common.Address) bool {
hash := computeAddressMapStorageKey(to, 2)
val := state.GetState(evmAccessControl, hash)
return val.Cmp(emptyHash) != 0
}

Expand Down
26 changes: 22 additions & 4 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1844,9 +1844,6 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c
// Ensure only eip155 signed transactions are submitted if EIP155Required is set.
return common.Hash{}, errors.New("only replay-protected (EIP-155) transactions allowed over RPC")
}
if err := b.SendTx(ctx, tx); err != nil {
return common.Hash{}, err
}
// Print a log with full tx details for manual investigations and interventions
head := b.CurrentBlock()
signer := types.MakeSigner(b.ChainConfig(), head.Number, head.Time)
Expand All @@ -1855,7 +1852,28 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c
return common.Hash{}, err
}

if tx.To() == nil {
// Validate the contract creator or destination contract.
to := tx.To()
state, _, err := b.StateAndHeaderByNumber(ctx, rpc.BlockNumber(head.Number.Int64()))
if err != nil {
return common.Hash{}, err
}
if state == nil {
return common.Hash{}, errors.New("state not found")
}
// Fail if the caller is not allowed to create
if to == nil && !vm.IsAllowedToCreate(state, from) {
return common.Hash{}, vm.ErrUnauthorizedCreate
Copy link
Contributor

@tak1827 tak1827 Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで、vmのエラーを返すのは違和感があります。
なので、こんな感じで丁寧にお願いします。

fmt.Errorf("the deployer address is not allowed. please submit application form. from:%s," from)

したの方は、ひとまずこんな感じで。

fmt.Errorf("the calling contract is in denlylist. to: %s", to)

(Denylistは公開して理由を添えた方が良さそう。運用の話し

Copy link
Collaborator Author

@ironbeer ironbeer Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そこは自分も思ったのですが、行っている処理に対してEVMAccessControlというコントラクト名が大げさすぎませんか?用途がEVMに限定されすぎている感じもあります。vmパッケージの外に移動することも考えて良いと思います。

}
// Fail if the address is not allowed to call
if to != nil && vm.IsDeniedToCall(state, *to) {
return common.Hash{}, vm.ErrUnauthorizedCall
}

if err := b.SendTx(ctx, tx); err != nil {
return common.Hash{}, err
}
if to == nil {
addr := crypto.CreateAddress(from, tx.Nonce())
log.Info("Submitted contract creation", "hash", tx.Hash().Hex(), "from", from, "nonce", tx.Nonce(), "contract", addr.Hex(), "value", tx.Value())
} else {
Expand Down