Skip to content

Commit 142fbcf

Browse files
internal/ethapi: reject non-replay-protected txs over RPC (#22339)
This PR prevents users from submitting transactions without EIP-155 enabled. This behaviour can be overridden by specifying the flag --rpc.allow-unprotected-txs=true.
1 parent c4a2b68 commit 142fbcf

9 files changed

Lines changed: 39 additions & 10 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ var (
199199
utils.InsecureUnlockAllowedFlag,
200200
utils.RPCGlobalGasCapFlag,
201201
utils.RPCGlobalTxFeeCapFlag,
202+
utils.AllowUnprotectedTxs,
202203
}
203204

204205
whisperFlags = []cli.Flag{

‎cmd/utils/flags.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ var (
593593
Name: "preload",
594594
Usage: "Comma separated list of JavaScript files to preload into the console",
595595
}
596+
AllowUnprotectedTxs = cli.BoolFlag{
597+
Name: "rpc.allow-unprotected-txs",
598+
Usage: "Allow for unprotected (non EIP155 signed) transactions to be submitted via RPC",
599+
}
596600

597601
// Network Settings
598602
MaxPeersFlag = cli.IntFlag{
@@ -966,6 +970,9 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
966970
if ctx.GlobalIsSet(HTTPPathPrefixFlag.Name) {
967971
cfg.HTTPPathPrefix = ctx.GlobalString(HTTPPathPrefixFlag.Name)
968972
}
973+
if ctx.GlobalIsSet(AllowUnprotectedTxs.Name) {
974+
cfg.AllowUnprotectedTxs = ctx.GlobalBool(AllowUnprotectedTxs.Name)
975+
}
969976
}
970977

971978
// setGraphQL creates the GraphQL listener interface string from the set

‎eth/api_backend.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ import (
4141

4242
// EthAPIBackend implements ethapi.Backend for full nodes
4343
type EthAPIBackend struct {
44-
extRPCEnabled bool
45-
eth *Ethereum
46-
gpo *gasprice.Oracle
44+
extRPCEnabled bool
45+
allowUnprotectedTxs bool
46+
eth *Ethereum
47+
gpo *gasprice.Oracle
4748
}
4849

4950
// ChainConfig returns the active chain configuration.
@@ -292,6 +293,10 @@ func (b *EthAPIBackend) ExtRPCEnabled() bool {
292293
return b.extRPCEnabled
293294
}
294295

296+
func (b *EthAPIBackend) UnprotectedAllowed() bool {
297+
return b.allowUnprotectedTxs
298+
}
299+
295300
func (b *EthAPIBackend) RPCGasCap() uint64 {
296301
return b.eth.config.RPCGasCap
297302
}

‎eth/backend.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
222222
eth.miner = miner.New(eth, &config.Miner, chainConfig, eth.EventMux(), eth.engine, eth.isLocalBlock)
223223
eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData))
224224

225-
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), eth, nil}
225+
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil}
226+
if eth.APIBackend.allowUnprotectedTxs {
227+
log.Info("Unprotected transactions allowed")
228+
}
226229
gpoParams := config.GPO
227230
if gpoParams.Default == nil {
228231
gpoParams.Default = config.Miner.GasPrice

‎internal/ethapi/api.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,10 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c
15551555
if err := checkTxFee(tx.GasPrice(), tx.Gas(), b.RPCTxFeeCap()); err != nil {
15561556
return common.Hash{}, err
15571557
}
1558+
if !b.UnprotectedAllowed() && !tx.Protected() {
1559+
// Ensure only eip155 signed transactions are submitted if EIP155Required is set.
1560+
return common.Hash{}, errors.New("only replay-protected (EIP-155) transactions allowed over RPC")
1561+
}
15581562
if err := b.SendTx(ctx, tx); err != nil {
15591563
return common.Hash{}, err
15601564
}

‎internal/ethapi/backend.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ type Backend interface {
4545
ChainDb() ethdb.Database
4646
AccountManager() *accounts.Manager
4747
ExtRPCEnabled() bool
48-
RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
49-
RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
48+
RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
49+
RPCTxFeeCap() float64 // global tx fee cap for all transaction related APIs
50+
UnprotectedAllowed() bool // allows only for EIP155 transactions.
5051

5152
// Blockchain API
5253
SetHead(number uint64)

‎les/api_backend.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ import (
4040
)
4141

4242
type LesApiBackend struct {
43-
extRPCEnabled bool
44-
eth *LightEthereum
45-
gpo *gasprice.Oracle
43+
extRPCEnabled bool
44+
allowUnprotectedTxs bool
45+
eth *LightEthereum
46+
gpo *gasprice.Oracle
4647
}
4748

4849
func (b *LesApiBackend) ChainConfig() *params.ChainConfig {
@@ -263,6 +264,10 @@ func (b *LesApiBackend) ExtRPCEnabled() bool {
263264
return b.extRPCEnabled
264265
}
265266

267+
func (b *LesApiBackend) UnprotectedAllowed() bool {
268+
return b.allowUnprotectedTxs
269+
}
270+
266271
func (b *LesApiBackend) RPCGasCap() uint64 {
267272
return b.eth.config.RPCGasCap
268273
}

‎les/client.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) {
157157
rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig)
158158
}
159159

160-
leth.ApiBackend = &LesApiBackend{stack.Config().ExtRPCEnabled(), leth, nil}
160+
leth.ApiBackend = &LesApiBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, leth, nil}
161161
gpoParams := config.GPO
162162
if gpoParams.Default == nil {
163163
gpoParams.Default = config.Miner.GasPrice

‎node/config.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ type Config struct {
191191
staticNodesWarning bool
192192
trustedNodesWarning bool
193193
oldGethResourceWarning bool
194+
195+
// AllowUnprotectedTxs allows non EIP-155 protected transactions to be send over RPC.
196+
AllowUnprotectedTxs bool `toml:",omitempty"`
194197
}
195198

196199
// IPCEndpoint resolves an IPC endpoint based on a configured value, taking into

0 commit comments

Comments
 (0)