Skip to content

Commit d051274

Browse files
committed
xeth: added address hex check and length check
1 parent 9a02f53 commit d051274

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

xeth/xeth.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ package xeth
2020
import (
2121
"bytes"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"math/big"
26+
"regexp"
2527
"sync"
2628
"time"
2729

@@ -45,6 +47,7 @@ var (
4547
defaultGasPrice = big.NewInt(10000000000000) //150000000000
4648
defaultGas = big.NewInt(90000) //500000
4749
dappStorePre = []byte("dapp-")
50+
addrReg = regexp.MustCompile(`^(0x)?[a-fA-F0-9]{40}$`)
4851
)
4952

5053
// byte will be inferred
@@ -878,6 +881,10 @@ func (self *XEth) Sign(fromStr, hashStr string, didUnlock bool) (string, error)
878881
return common.ToHex(sig), nil
879882
}
880883

884+
func isAddress(addr string) bool {
885+
return addrReg.MatchString(addr)
886+
}
887+
881888
func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
882889

883890
// this minimalistic recoding is enough (works for natspec.js)
@@ -887,6 +894,10 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
887894
return "", err
888895
}
889896

897+
if !isAddress(toStr) {
898+
return "", errors.New("Invalid address")
899+
}
900+
890901
var (
891902
from = common.HexToAddress(fromStr)
892903
to = common.HexToAddress(toStr)

xeth/xeth_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package xeth
2+
3+
import "testing"
4+
5+
func TestIsAddress(t *testing.T) {
6+
for _, invalid := range []string{
7+
"0x00",
8+
"0xNN",
9+
"0x00000000000000000000000000000000000000NN",
10+
"0xAAar000000000000000000000000000000000000",
11+
} {
12+
if isAddress(invalid) {
13+
t.Error("Expected", invalid, "to be invalid")
14+
}
15+
}
16+
17+
for _, valid := range []string{
18+
"0x0000000000000000000000000000000000000000",
19+
"0xAABBbbCCccff9900000000000000000000000000",
20+
"AABBbbCCccff9900000000000000000000000000",
21+
} {
22+
if !isAddress(valid) {
23+
t.Error("Expected", valid, "to be valid")
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)