Skip to content

Commit 7a7a5ac

Browse files
Bas van Kervelobscuren
authored andcommitted
[release 1.4.5] eth/filter: bugfix which can cause a nil pointer crash when parsing filter arguments
(cherry picked from commit 67cd4ee)
1 parent 66d74df commit 7a7a5ac

File tree

2 files changed

+234
-38
lines changed

2 files changed

+234
-38
lines changed

eth/filters/api.go

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ func (s *PublicFilterAPI) newLogFilter(earliest, latest int64, addresses []commo
233233
return id, nil
234234
}
235235

236+
// Logs creates a subscription that fires for all new log that match the given filter criteria.
236237
func (s *PublicFilterAPI) Logs(ctx context.Context, args NewFilterArgs) (rpc.Subscription, error) {
237238
notifier, supported := rpc.NotifierFromContext(ctx)
238239
if !supported {
@@ -291,12 +292,13 @@ type NewFilterArgs struct {
291292
Topics [][]common.Hash
292293
}
293294

295+
// UnmarshalJSON sets *args fields with given data.
294296
func (args *NewFilterArgs) UnmarshalJSON(data []byte) error {
295297
type input struct {
296298
From *rpc.BlockNumber `json:"fromBlock"`
297299
ToBlock *rpc.BlockNumber `json:"toBlock"`
298300
Addresses interface{} `json:"address"`
299-
Topics interface{} `json:"topics"`
301+
Topics []interface{} `json:"topics"`
300302
}
301303

302304
var raw input
@@ -321,7 +323,6 @@ func (args *NewFilterArgs) UnmarshalJSON(data []byte) error {
321323
if raw.Addresses != nil {
322324
// raw.Address can contain a single address or an array of addresses
323325
var addresses []common.Address
324-
325326
if strAddrs, ok := raw.Addresses.([]interface{}); ok {
326327
for i, addr := range strAddrs {
327328
if strAddr, ok := addr.(string); ok {
@@ -352,56 +353,53 @@ func (args *NewFilterArgs) UnmarshalJSON(data []byte) error {
352353
args.Addresses = addresses
353354
}
354355

356+
// helper function which parses a string to a topic hash
355357
topicConverter := func(raw string) (common.Hash, error) {
356358
if len(raw) == 0 {
357359
return common.Hash{}, nil
358360
}
359-
360361
if len(raw) >= 2 && raw[0] == '0' && (raw[1] == 'x' || raw[1] == 'X') {
361362
raw = raw[2:]
362363
}
363-
364+
if len(raw) != 2 * common.HashLength {
365+
return common.Hash{}, errors.New("invalid topic(s)")
366+
}
364367
if decAddr, err := hex.DecodeString(raw); err == nil {
365368
return common.BytesToHash(decAddr), nil
366369
}
367-
368-
return common.Hash{}, errors.New("invalid topic given")
369-
}
370-
371-
// topics is an array consisting of strings or arrays of strings
372-
if raw.Topics != nil {
373-
topics, ok := raw.Topics.([]interface{})
374-
if ok {
375-
parsedTopics := make([][]common.Hash, len(topics))
376-
for i, topic := range topics {
377-
if topic == nil {
378-
parsedTopics[i] = []common.Hash{common.StringToHash("")}
379-
} else if strTopic, ok := topic.(string); ok {
380-
if t, err := topicConverter(strTopic); err != nil {
381-
return fmt.Errorf("invalid topic on index %d", i)
382-
} else {
383-
parsedTopics[i] = []common.Hash{t}
384-
}
385-
} else if arrTopic, ok := topic.([]interface{}); ok {
386-
parsedTopics[i] = make([]common.Hash, len(arrTopic))
387-
for j := 0; j < len(parsedTopics[i]); i++ {
388-
if arrTopic[j] == nil {
389-
parsedTopics[i][j] = common.StringToHash("")
390-
} else if str, ok := arrTopic[j].(string); ok {
391-
if t, err := topicConverter(str); err != nil {
392-
return fmt.Errorf("invalid topic on index %d", i)
393-
} else {
394-
parsedTopics[i] = []common.Hash{t}
395-
}
396-
} else {
397-
return fmt.Errorf("topic[%d][%d] not a string", i, j)
370+
return common.Hash{}, errors.New("invalid topic(s)")
371+
}
372+
373+
// topics is an array consisting of strings and/or arrays of strings.
374+
// JSON null values are converted to common.Hash{} and ignored by the filter manager.
375+
if len(raw.Topics) > 0 {
376+
args.Topics = make([][]common.Hash, len(raw.Topics))
377+
for i, t := range raw.Topics {
378+
if t == nil { // ignore topic when matching logs
379+
args.Topics[i] = []common.Hash{common.Hash{}}
380+
} else if topic, ok := t.(string); ok { // match specific topic
381+
top, err := topicConverter(topic)
382+
if err != nil {
383+
return err
384+
}
385+
args.Topics[i] = []common.Hash{top}
386+
} else if topics, ok := t.([]interface{}); ok { // or case e.g. [null, "topic0", "topic1"]
387+
for _, rawTopic := range topics {
388+
if rawTopic == nil {
389+
args.Topics[i] = append(args.Topics[i], common.Hash{})
390+
} else if topic, ok := rawTopic.(string); ok {
391+
parsed, err := topicConverter(topic)
392+
if err != nil {
393+
return err
398394
}
395+
args.Topics[i] = append(args.Topics[i], parsed)
396+
} else {
397+
return fmt.Errorf("invalid topic(s)")
399398
}
400-
} else {
401-
return fmt.Errorf("topic[%d] invalid", i)
402399
}
400+
} else {
401+
return fmt.Errorf("invalid topic(s)")
403402
}
404-
args.Topics = parsedTopics
405403
}
406404
}
407405

eth/filters/api_test.go

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package filters_test
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/ethereum/go-ethereum/common"
25+
"github.com/ethereum/go-ethereum/eth/filters"
26+
"github.com/ethereum/go-ethereum/rpc"
27+
)
28+
29+
func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
30+
var (
31+
fromBlock rpc.BlockNumber = 0x123435
32+
toBlock rpc.BlockNumber = 0xabcdef
33+
address0 = common.StringToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")
34+
address1 = common.StringToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")
35+
topic0 = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")
36+
topic1 = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")
37+
topic2 = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")
38+
nullTopic = common.Hash{}
39+
)
40+
41+
// default values
42+
var test0 filters.NewFilterArgs
43+
if err := json.Unmarshal([]byte("{}"), &test0); err != nil {
44+
t.Fatal(err)
45+
}
46+
if test0.FromBlock != rpc.LatestBlockNumber {
47+
t.Fatalf("expected %d, got %d", rpc.LatestBlockNumber, test0.FromBlock)
48+
}
49+
if test0.ToBlock != rpc.LatestBlockNumber {
50+
t.Fatalf("expected %d, got %d", rpc.LatestBlockNumber, test0.ToBlock)
51+
}
52+
if len(test0.Addresses) != 0 {
53+
t.Fatalf("expected 0 addresses, got %d", len(test0.Addresses))
54+
}
55+
if len(test0.Topics) != 0 {
56+
t.Fatalf("expected 0 topics, got %d topics", len(test0.Topics))
57+
}
58+
59+
// from, to block number
60+
var test1 filters.NewFilterArgs
61+
vector := fmt.Sprintf(`{"fromBlock":"0x%x","toBlock":"0x%x"}`, fromBlock, toBlock)
62+
if err := json.Unmarshal([]byte(vector), &test1); err != nil {
63+
t.Fatal(err)
64+
}
65+
if test1.FromBlock != fromBlock {
66+
t.Fatalf("expected FromBlock %d, got %d", fromBlock, test1.FromBlock)
67+
}
68+
if test1.ToBlock != toBlock {
69+
t.Fatalf("expected ToBlock %d, got %d", toBlock, test1.ToBlock)
70+
}
71+
72+
// single address
73+
var test2 filters.NewFilterArgs
74+
vector = fmt.Sprintf(`{"address": "%s"}`, address0.Hex())
75+
if err := json.Unmarshal([]byte(vector), &test2); err != nil {
76+
t.Fatal(err)
77+
}
78+
if len(test2.Addresses) != 1 {
79+
t.Fatalf("expected 1 address, got %d address(es)", len(test2.Addresses))
80+
}
81+
if test2.Addresses[0] != address0 {
82+
t.Fatalf("expected address %x, got %x", address0, test2.Addresses[0])
83+
}
84+
85+
// multiple address
86+
var test3 filters.NewFilterArgs
87+
vector = fmt.Sprintf(`{"address": ["%s", "%s"]}`, address0.Hex(), address1.Hex())
88+
if err := json.Unmarshal([]byte(vector), &test3); err != nil {
89+
t.Fatal(err)
90+
}
91+
if len(test3.Addresses) != 2 {
92+
t.Fatalf("expected 2 addresses, got %d address(es)", len(test3.Addresses))
93+
}
94+
if test3.Addresses[0] != address0 {
95+
t.Fatalf("expected address %x, got %x", address0, test3.Addresses[0])
96+
}
97+
if test3.Addresses[1] != address1 {
98+
t.Fatalf("expected address %x, got %x", address1, test3.Addresses[1])
99+
}
100+
101+
// single topic
102+
var test4 filters.NewFilterArgs
103+
vector = fmt.Sprintf(`{"topics": ["%s"]}`, topic0.Hex())
104+
if err := json.Unmarshal([]byte(vector), &test4); err != nil {
105+
t.Fatal(err)
106+
}
107+
if len(test4.Topics) != 1 {
108+
t.Fatalf("expected 1 topic, got %d", len(test4.Topics))
109+
}
110+
if len(test4.Topics[0]) != 1 {
111+
t.Fatalf("expected len(topics[0]) to be 1, got %d", len(test4.Topics[0]))
112+
}
113+
if test4.Topics[0][0] != topic0 {
114+
t.Fatalf("got %x, expected %x", test4.Topics[0][0], topic0)
115+
}
116+
117+
// test multiple "AND" topics
118+
var test5 filters.NewFilterArgs
119+
vector = fmt.Sprintf(`{"topics": ["%s", "%s"]}`, topic0.Hex(), topic1.Hex())
120+
if err := json.Unmarshal([]byte(vector), &test5); err != nil {
121+
t.Fatal(err)
122+
}
123+
if len(test5.Topics) != 2 {
124+
t.Fatalf("expected 2 topics, got %d", len(test5.Topics))
125+
}
126+
if len(test5.Topics[0]) != 1 {
127+
t.Fatalf("expected 1 topic, got %d", len(test5.Topics[0]))
128+
}
129+
if test5.Topics[0][0] != topic0 {
130+
t.Fatalf("got %x, expected %x", test5.Topics[0][0], topic0)
131+
}
132+
if len(test5.Topics[1]) != 1 {
133+
t.Fatalf("expected 1 topic, got %d", len(test5.Topics[1]))
134+
}
135+
if test5.Topics[1][0] != topic1 {
136+
t.Fatalf("got %x, expected %x", test5.Topics[1][0], topic1)
137+
}
138+
139+
// test optional topic
140+
var test6 filters.NewFilterArgs
141+
vector = fmt.Sprintf(`{"topics": ["%s", null, "%s"]}`, topic0.Hex(), topic2.Hex())
142+
if err := json.Unmarshal([]byte(vector), &test6); err != nil {
143+
t.Fatal(err)
144+
}
145+
if len(test6.Topics) != 3 {
146+
t.Fatalf("expected 3 topics, got %d", len(test6.Topics))
147+
}
148+
if len(test6.Topics[0]) != 1 {
149+
t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0]))
150+
}
151+
if test6.Topics[0][0] != topic0 {
152+
t.Fatalf("got %x, expected %x", test6.Topics[0][0], topic0)
153+
}
154+
if len(test6.Topics[1]) != 1 {
155+
t.Fatalf("expected 1 topic, got %d", len(test6.Topics[1]))
156+
}
157+
if test6.Topics[1][0] != nullTopic {
158+
t.Fatalf("got %x, expected empty hash", test6.Topics[1][0])
159+
}
160+
if len(test6.Topics[2]) != 1 {
161+
t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2]))
162+
}
163+
if test6.Topics[2][0] != topic2 {
164+
t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2)
165+
}
166+
167+
// test OR topics
168+
var test7 filters.NewFilterArgs
169+
vector = fmt.Sprintf(`{"topics": [["%s", "%s"], null, ["%s", null]]}`, topic0.Hex(), topic1.Hex(), topic2.Hex())
170+
if err := json.Unmarshal([]byte(vector), &test7); err != nil {
171+
t.Fatal(err)
172+
}
173+
if len(test7.Topics) != 3 {
174+
t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics))
175+
}
176+
if len(test7.Topics[0]) != 2 {
177+
t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0]))
178+
}
179+
if test7.Topics[0][0] != topic0 || test7.Topics[0][1] != topic1 {
180+
t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]",
181+
topic0, topic1, test7.Topics[0][0], test7.Topics[0][1],
182+
)
183+
}
184+
if len(test7.Topics[1]) != 1 {
185+
t.Fatalf("expected 1 topic, got %d topics", len(test7.Topics[1]))
186+
}
187+
if test7.Topics[1][0] != nullTopic {
188+
t.Fatalf("expected empty hash, got %x", test7.Topics[1][0])
189+
}
190+
if len(test7.Topics[2]) != 2 {
191+
t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[2]))
192+
}
193+
if test7.Topics[2][0] != topic2 || test7.Topics[2][1] != nullTopic {
194+
t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]",
195+
topic2, nullTopic, test7.Topics[2][0], test7.Topics[2][1],
196+
)
197+
}
198+
}

0 commit comments

Comments
 (0)