Skip to content

Commit e0987f6

Browse files
holimanligi
andauthored
cmd/clef, signer/core: password input fixes (#20960)
* cmd/clef, signer/core: use better terminal input for passwords, make it possible to avoid boot-up warning * all: move commonly used prompter to isolated (small) package * cmd/clef: Add new --acceptWarn to clef README * cmd/clef: rename flag 'acceptWarn' to 'suppress-bootwarn' Co-authored-by: ligi <ligi@ligi.de>
1 parent 3666da8 commit e0987f6

13 files changed

Lines changed: 65 additions & 54 deletions

File tree

‎cmd/clef/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ GLOBAL OPTIONS:
4646
--stdio-ui Use STDIN/STDOUT as a channel for an external UI. This means that an STDIN/STDOUT is used for RPC-communication with a e.g. a graphical user interface, and can be used when Clef is started by an external process.
4747
--stdio-ui-test Mechanism to test interface between Clef and UI. Requires 'stdio-ui'.
4848
--advanced If enabled, issues warnings instead of rejections for suspicious requests. Default off
49+
--suppress-bootwarn If set, does not show the warning during boot
4950
--help, -h show help
5051
--version, -v print the version
5152
```

‎cmd/clef/main.go‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
"github.com/ethereum/go-ethereum/cmd/utils"
4141
"github.com/ethereum/go-ethereum/common"
4242
"github.com/ethereum/go-ethereum/common/hexutil"
43-
"github.com/ethereum/go-ethereum/console"
43+
"github.com/ethereum/go-ethereum/console/prompt"
4444
"github.com/ethereum/go-ethereum/core/types"
4545
"github.com/ethereum/go-ethereum/crypto"
4646
"github.com/ethereum/go-ethereum/internal/ethapi"
@@ -82,6 +82,10 @@ var (
8282
Name: "advanced",
8383
Usage: "If enabled, issues warnings instead of rejections for suspicious requests. Default off",
8484
}
85+
acceptFlag = cli.BoolFlag{
86+
Name: "suppress-bootwarn",
87+
Usage: "If set, does not show the warning during boot",
88+
}
8589
keystoreFlag = cli.StringFlag{
8690
Name: "keystore",
8791
Value: filepath.Join(node.DefaultDataDir(), "keystore"),
@@ -196,6 +200,7 @@ The delpw command removes a password for a given address (keyfile).
196200
logLevelFlag,
197201
keystoreFlag,
198202
utils.LightKDFFlag,
203+
acceptFlag,
199204
},
200205
Description: `
201206
The newaccount command creates a new keystore-backed account. It is a convenience-method
@@ -235,6 +240,7 @@ func init() {
235240
stdiouiFlag,
236241
testFlag,
237242
advancedMode,
243+
acceptFlag,
238244
}
239245
app.Action = signer
240246
app.Commands = []cli.Command{initCommand,
@@ -433,8 +439,10 @@ func initialize(c *cli.Context) error {
433439
if c.GlobalBool(stdiouiFlag.Name) {
434440
logOutput = os.Stderr
435441
// If using the stdioui, we can't do the 'confirm'-flow
436-
fmt.Fprint(logOutput, legalWarning)
437-
} else {
442+
if !c.GlobalBool(acceptFlag.Name) {
443+
fmt.Fprint(logOutput, legalWarning)
444+
}
445+
} else if !c.GlobalBool(acceptFlag.Name) {
438446
if !confirm(legalWarning) {
439447
return fmt.Errorf("aborted by user")
440448
}
@@ -910,14 +918,14 @@ func testExternalUI(api *core.SignerAPI) {
910918
// getPassPhrase retrieves the password associated with clef, either fetched
911919
// from a list of preloaded passphrases, or requested interactively from the user.
912920
// TODO: there are many `getPassPhrase` functions, it will be better to abstract them into one.
913-
func getPassPhrase(prompt string, confirmation bool) string {
914-
fmt.Println(prompt)
915-
password, err := console.Stdin.PromptPassword("Password: ")
921+
func getPassPhrase(query string, confirmation bool) string {
922+
fmt.Println(query)
923+
password, err := prompt.Stdin.PromptPassword("Password: ")
916924
if err != nil {
917925
utils.Fatalf("Failed to read password: %v", err)
918926
}
919927
if confirmation {
920-
confirm, err := console.Stdin.PromptPassword("Repeat password: ")
928+
confirm, err := prompt.Stdin.PromptPassword("Repeat password: ")
921929
if err != nil {
922930
utils.Fatalf("Failed to read password confirmation: %v", err)
923931
}

‎cmd/devp2p/dnscmd.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
"github.com/ethereum/go-ethereum/accounts/keystore"
2929
"github.com/ethereum/go-ethereum/common"
30-
"github.com/ethereum/go-ethereum/console"
30+
"github.com/ethereum/go-ethereum/console/prompt"
3131
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
3232
"github.com/ethereum/go-ethereum/p2p/enode"
3333
cli "gopkg.in/urfave/cli.v1"
@@ -226,7 +226,7 @@ func loadSigningKey(keyfile string) *ecdsa.PrivateKey {
226226
if err != nil {
227227
exit(fmt.Errorf("failed to read the keyfile at '%s': %v", keyfile, err))
228228
}
229-
password, _ := console.Stdin.PromptPassword("Please enter the password for '" + keyfile + "': ")
229+
password, _ := prompt.Stdin.PromptPassword("Please enter the password for '" + keyfile + "': ")
230230
key, err := keystore.DecryptKey(keyjson, password)
231231
if err != nil {
232232
exit(fmt.Errorf("error decrypting key: %v", err))

‎cmd/ethkey/utils.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ import (
2323
"strings"
2424

2525
"github.com/ethereum/go-ethereum/cmd/utils"
26-
"github.com/ethereum/go-ethereum/console"
26+
"github.com/ethereum/go-ethereum/console/prompt"
2727
"github.com/ethereum/go-ethereum/crypto"
2828
"gopkg.in/urfave/cli.v1"
2929
)
3030

3131
// promptPassphrase prompts the user for a passphrase. Set confirmation to true
3232
// to require the user to confirm the passphrase.
3333
func promptPassphrase(confirmation bool) string {
34-
passphrase, err := console.Stdin.PromptPassword("Password: ")
34+
passphrase, err := prompt.Stdin.PromptPassword("Password: ")
3535
if err != nil {
3636
utils.Fatalf("Failed to read password: %v", err)
3737
}
3838

3939
if confirmation {
40-
confirm, err := console.Stdin.PromptPassword("Repeat password: ")
40+
confirm, err := prompt.Stdin.PromptPassword("Repeat password: ")
4141
if err != nil {
4242
utils.Fatalf("Failed to read password confirmation: %v", err)
4343
}

‎cmd/geth/accountcmd.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/accounts"
2424
"github.com/ethereum/go-ethereum/accounts/keystore"
2525
"github.com/ethereum/go-ethereum/cmd/utils"
26-
"github.com/ethereum/go-ethereum/console"
26+
prompt2 "github.com/ethereum/go-ethereum/console/prompt"
2727
"github.com/ethereum/go-ethereum/crypto"
2828
"github.com/ethereum/go-ethereum/log"
2929
"gopkg.in/urfave/cli.v1"
@@ -247,12 +247,12 @@ func getPassPhrase(prompt string, confirmation bool, i int, passwords []string)
247247
if prompt != "" {
248248
fmt.Println(prompt)
249249
}
250-
password, err := console.Stdin.PromptPassword("Password: ")
250+
password, err := prompt2.Stdin.PromptPassword("Password: ")
251251
if err != nil {
252252
utils.Fatalf("Failed to read password: %v", err)
253253
}
254254
if confirmation {
255-
confirm, err := console.Stdin.PromptPassword("Repeat password: ")
255+
confirm, err := prompt2.Stdin.PromptPassword("Repeat password: ")
256256
if err != nil {
257257
utils.Fatalf("Failed to read password confirmation: %v", err)
258258
}

‎cmd/geth/chaincmd.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

2929
"github.com/ethereum/go-ethereum/cmd/utils"
3030
"github.com/ethereum/go-ethereum/common"
31-
"github.com/ethereum/go-ethereum/console"
31+
"github.com/ethereum/go-ethereum/console/prompt"
3232
"github.com/ethereum/go-ethereum/core"
3333
"github.com/ethereum/go-ethereum/core/rawdb"
3434
"github.com/ethereum/go-ethereum/core/state"
@@ -521,7 +521,7 @@ func removeDB(ctx *cli.Context) error {
521521
// confirmAndRemoveDB prompts the user for a last confirmation and removes the
522522
// folder if accepted.
523523
func confirmAndRemoveDB(database string, kind string) {
524-
confirm, err := console.Stdin.PromptConfirm(fmt.Sprintf("Remove %s (%s)?", kind, database))
524+
confirm, err := prompt.Stdin.PromptConfirm(fmt.Sprintf("Remove %s (%s)?", kind, database))
525525
switch {
526526
case err != nil:
527527
utils.Fatalf("%v", err)

‎cmd/geth/main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/accounts/keystore"
3434
"github.com/ethereum/go-ethereum/cmd/utils"
3535
"github.com/ethereum/go-ethereum/common"
36-
"github.com/ethereum/go-ethereum/console"
36+
"github.com/ethereum/go-ethereum/console/prompt"
3737
"github.com/ethereum/go-ethereum/eth"
3838
"github.com/ethereum/go-ethereum/eth/downloader"
3939
"github.com/ethereum/go-ethereum/ethclient"
@@ -258,7 +258,7 @@ func init() {
258258
}
259259
app.After = func(ctx *cli.Context) error {
260260
debug.Exit()
261-
console.Stdin.Close() // Resets terminal mode.
261+
prompt.Stdin.Close() // Resets terminal mode.
262262
return nil
263263
}
264264
}

‎cmd/wnode/main.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"github.com/ethereum/go-ethereum/cmd/utils"
3939
"github.com/ethereum/go-ethereum/common"
4040
"github.com/ethereum/go-ethereum/common/hexutil"
41-
"github.com/ethereum/go-ethereum/console"
41+
"github.com/ethereum/go-ethereum/console/prompt"
4242
"github.com/ethereum/go-ethereum/crypto"
4343
"github.com/ethereum/go-ethereum/log"
4444
"github.com/ethereum/go-ethereum/p2p"
@@ -210,7 +210,7 @@ func initialize() {
210210

211211
if *mailServerMode {
212212
if len(msPassword) == 0 {
213-
msPassword, err = console.Stdin.PromptPassword("Please enter the Mail Server password: ")
213+
msPassword, err = prompt.Stdin.PromptPassword("Please enter the Mail Server password: ")
214214
if err != nil {
215215
utils.Fatalf("Failed to read Mail Server password: %s", err)
216216
}
@@ -346,7 +346,7 @@ func configureNode() {
346346
if *requestMail {
347347
p2pAccept = true
348348
if len(msPassword) == 0 {
349-
msPassword, err = console.Stdin.PromptPassword("Please enter the Mail Server password: ")
349+
msPassword, err = prompt.Stdin.PromptPassword("Please enter the Mail Server password: ")
350350
if err != nil {
351351
utils.Fatalf("Failed to read Mail Server password: %s", err)
352352
}
@@ -355,7 +355,7 @@ func configureNode() {
355355

356356
if !*asymmetricMode && !*forwarderMode {
357357
if len(symPass) == 0 {
358-
symPass, err = console.Stdin.PromptPassword("Please enter the password for symmetric encryption: ")
358+
symPass, err = prompt.Stdin.PromptPassword("Please enter the password for symmetric encryption: ")
359359
if err != nil {
360360
utils.Fatalf("Failed to read password: %v", err)
361361
}

‎console/bridge.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ import (
2828
"github.com/ethereum/go-ethereum/accounts/scwallet"
2929
"github.com/ethereum/go-ethereum/accounts/usbwallet"
3030
"github.com/ethereum/go-ethereum/common/hexutil"
31+
"github.com/ethereum/go-ethereum/console/prompt"
3132
"github.com/ethereum/go-ethereum/internal/jsre"
3233
"github.com/ethereum/go-ethereum/rpc"
3334
)
3435

3536
// bridge is a collection of JavaScript utility methods to bride the .js runtime
3637
// environment and the Go RPC connection backing the remote method calls.
3738
type bridge struct {
38-
client *rpc.Client // RPC client to execute Ethereum requests through
39-
prompter UserPrompter // Input prompter to allow interactive user feedback
40-
printer io.Writer // Output writer to serialize any display strings to
39+
client *rpc.Client // RPC client to execute Ethereum requests through
40+
prompter prompt.UserPrompter // Input prompter to allow interactive user feedback
41+
printer io.Writer // Output writer to serialize any display strings to
4142
}
4243

4344
// newBridge creates a new JavaScript wrapper around an RPC client.
44-
func newBridge(client *rpc.Client, prompter UserPrompter, printer io.Writer) *bridge {
45+
func newBridge(client *rpc.Client, prompter prompt.UserPrompter, printer io.Writer) *bridge {
4546
return &bridge{
4647
client: client,
4748
prompter: prompter,

‎console/console.go‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"syscall"
3030

3131
"github.com/dop251/goja"
32+
"github.com/ethereum/go-ethereum/console/prompt"
3233
"github.com/ethereum/go-ethereum/internal/jsre"
3334
"github.com/ethereum/go-ethereum/internal/jsre/deps"
3435
"github.com/ethereum/go-ethereum/internal/web3ext"
@@ -52,34 +53,34 @@ const DefaultPrompt = "> "
5253
// Config is the collection of configurations to fine tune the behavior of the
5354
// JavaScript console.
5455
type Config struct {
55-
DataDir string // Data directory to store the console history at
56-
DocRoot string // Filesystem path from where to load JavaScript files from
57-
Client *rpc.Client // RPC client to execute Ethereum requests through
58-
Prompt string // Input prompt prefix string (defaults to DefaultPrompt)
59-
Prompter UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter)
60-
Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout)
61-
Preload []string // Absolute paths to JavaScript files to preload
56+
DataDir string // Data directory to store the console history at
57+
DocRoot string // Filesystem path from where to load JavaScript files from
58+
Client *rpc.Client // RPC client to execute Ethereum requests through
59+
Prompt string // Input prompt prefix string (defaults to DefaultPrompt)
60+
Prompter prompt.UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter)
61+
Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout)
62+
Preload []string // Absolute paths to JavaScript files to preload
6263
}
6364

6465
// Console is a JavaScript interpreted runtime environment. It is a fully fledged
6566
// JavaScript console attached to a running node via an external or in-process RPC
6667
// client.
6768
type Console struct {
68-
client *rpc.Client // RPC client to execute Ethereum requests through
69-
jsre *jsre.JSRE // JavaScript runtime environment running the interpreter
70-
prompt string // Input prompt prefix string
71-
prompter UserPrompter // Input prompter to allow interactive user feedback
72-
histPath string // Absolute path to the console scrollback history
73-
history []string // Scroll history maintained by the console
74-
printer io.Writer // Output writer to serialize any display strings to
69+
client *rpc.Client // RPC client to execute Ethereum requests through
70+
jsre *jsre.JSRE // JavaScript runtime environment running the interpreter
71+
prompt string // Input prompt prefix string
72+
prompter prompt.UserPrompter // Input prompter to allow interactive user feedback
73+
histPath string // Absolute path to the console scrollback history
74+
history []string // Scroll history maintained by the console
75+
printer io.Writer // Output writer to serialize any display strings to
7576
}
7677

7778
// New initializes a JavaScript interpreted runtime environment and sets defaults
7879
// with the config struct.
7980
func New(config Config) (*Console, error) {
8081
// Handle unset config values gracefully
8182
if config.Prompter == nil {
82-
config.Prompter = Stdin
83+
config.Prompter = prompt.Stdin
8384
}
8485
if config.Prompt == "" {
8586
config.Prompt = DefaultPrompt

0 commit comments

Comments
 (0)