Skip to content

Commit 5f6f5e3

Browse files
authored
console: handle undefined + null in console funcs (#21160)
1 parent d98c42c commit 5f6f5e3

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

console/bridge.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,12 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) {
233233
if len(call.Arguments) < 1 {
234234
return nil, fmt.Errorf("usage: unlockAccount(account, [ password, duration ])")
235235
}
236+
237+
account := call.Argument(0)
236238
// Make sure we have an account specified to unlock.
237-
if call.Argument(0).ExportType().Kind() != reflect.String {
239+
if goja.IsUndefined(account) || goja.IsNull(account) || account.ExportType().Kind() != reflect.String {
238240
return nil, fmt.Errorf("first argument must be the account to unlock")
239241
}
240-
account := call.Argument(0)
241242

242243
// If password is not given or is the null value, prompt the user for it.
243244
var passwd goja.Value
@@ -285,10 +286,10 @@ func (b *bridge) Sign(call jsre.Call) (goja.Value, error) {
285286
passwd = call.Argument(2)
286287
)
287288

288-
if message.ExportType().Kind() != reflect.String {
289+
if goja.IsUndefined(message) || message.ExportType().Kind() != reflect.String {
289290
return nil, fmt.Errorf("first argument must be the message to sign")
290291
}
291-
if account.ExportType().Kind() != reflect.String {
292+
if goja.IsUndefined(account) || account.ExportType().Kind() != reflect.String {
292293
return nil, fmt.Errorf("second argument must be the account to sign with")
293294
}
294295

@@ -317,10 +318,11 @@ func (b *bridge) Sleep(call jsre.Call) (goja.Value, error) {
317318
if nArgs := len(call.Arguments); nArgs < 1 {
318319
return nil, fmt.Errorf("usage: sleep(<number of seconds>)")
319320
}
320-
if !isNumber(call.Argument(0)) {
321+
sleepObj := call.Argument(0)
322+
if goja.IsUndefined(sleepObj) || goja.IsNull(sleepObj) || !isNumber(sleepObj) {
321323
return nil, fmt.Errorf("usage: sleep(<number of seconds>)")
322324
}
323-
sleep := call.Argument(0).ToFloat()
325+
sleep := sleepObj.ToFloat()
324326
time.Sleep(time.Duration(sleep * float64(time.Second)))
325327
return call.VM.ToValue(true), nil
326328
}
@@ -338,13 +340,13 @@ func (b *bridge) SleepBlocks(call jsre.Call) (goja.Value, error) {
338340
return nil, fmt.Errorf("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
339341
}
340342
if nArgs >= 1 {
341-
if !isNumber(call.Argument(0)) {
343+
if goja.IsNull(call.Argument(0)) || goja.IsUndefined(call.Argument(0)) || !isNumber(call.Argument(0)) {
342344
return nil, fmt.Errorf("expected number as first argument")
343345
}
344346
blocks = call.Argument(0).ToInteger()
345347
}
346348
if nArgs >= 2 {
347-
if !isNumber(call.Argument(1)) {
349+
if goja.IsNull(call.Argument(1)) || goja.IsUndefined(call.Argument(1)) || !isNumber(call.Argument(1)) {
348350
return nil, fmt.Errorf("expected number as second argument")
349351
}
350352
sleep = call.Argument(1).ToInteger()

console/bridge_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2020 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 console
18+
19+
import (
20+
"testing"
21+
22+
"github.com/dop251/goja"
23+
"github.com/ethereum/go-ethereum/internal/jsre"
24+
)
25+
26+
// TestUndefinedAsParam ensures that personal functions can receive
27+
// `undefined` as a parameter.
28+
func TestUndefinedAsParam(t *testing.T) {
29+
b := bridge{}
30+
call := jsre.Call{}
31+
call.Arguments = []goja.Value{goja.Undefined()}
32+
33+
b.UnlockAccount(call)
34+
b.Sign(call)
35+
b.Sleep(call)
36+
}
37+
38+
// TestNullAsParam ensures that personal functions can receive
39+
// `null` as a parameter.
40+
func TestNullAsParam(t *testing.T) {
41+
b := bridge{}
42+
call := jsre.Call{}
43+
call.Arguments = []goja.Value{goja.Null()}
44+
45+
b.UnlockAccount(call)
46+
b.Sign(call)
47+
b.Sleep(call)
48+
}

0 commit comments

Comments
 (0)