Skip to content

Commit

Permalink
lncli: read password from stdin till EOF in unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedawnallah committed Apr 16, 2024
1 parent 2ccf58e commit 9ef8b33
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions cmd/lncli/cmd_walletunlocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
Expand Down Expand Up @@ -469,7 +470,9 @@ var unlockCommand = cli.Command{
"a file that can be read by another user. " +
"This flag should only be used in " +
"combination with some sort of password " +
"manager or secrets vault.",
"manager or secrets vault. The password will " +
"be read from standard input until EOF " +
"(End of File) is encountered.",
},
statelessInitFlag,
},
Expand All @@ -491,11 +494,25 @@ func unlock(ctx *cli.Context) error {
// password manager. If the user types the password instead, it will be
// echoed in the console.
case ctx.IsSet("stdin"):
reader := bufio.NewReader(os.Stdin)
pw, err = reader.ReadBytes('\n')

// Remove carriage return and newline characters.
pw = bytes.Trim(pw, "\r\n")
// Copy the password from standard input to the buffer until
// EOF (End of File) is encountered.
var buf bytes.Buffer
if _, err := io.Copy(&buf, os.Stdin); err != nil {
return err
}
pw = buf.Bytes()

// Remove carriage return.
pw = bytes.Trim(pw, "\r")

// Check if the provided password has leading or trailing
// newline characters, then issue a warning since they will
// not be trimmed.
if !bytes.Equal(pw, bytes.Trim(pw, "\n")) {
fmt.Printf("WARNING: Password contains leading or" +
"trailing newline characters and they will " +
"not be trimmed.\n")
}

// Read the password from a terminal by default. This requires the
// terminal to be a real tty and will fail if a string is piped into
Expand Down

0 comments on commit 9ef8b33

Please sign in to comment.