Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Bug reading password from a buffer when reader returns EOF #11796

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions client/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package input

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -83,12 +84,25 @@ func inputIsTty() bool {
return isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
}

// readLineFromBuf reads one line from stdin.
// readLineFromBuf reads one line from reader.
// Subsequent calls reuse the same buffer, so we don't lose
// any input when reading a password twice (to verify)
func readLineFromBuf(buf *bufio.Reader) (string, error) {
pass, err := buf.ReadString('\n')
if err != nil {

switch {
case errors.Is(err, io.EOF):
// If by any chance the error is EOF, but we were actually able to read
// something from the reader then don't return the EOF error.
// If we didn't read anything from the reader and got the EOF error, then
// it's safe to return EOF back to the caller.
if len(pass) > 0 {
// exit the switch statement
break
}
return "", err

case err != nil:
Vizualni marked this conversation as resolved.
Show resolved Hide resolved
return "", err
}

Expand Down
57 changes: 57 additions & 0 deletions client/input/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package input

import (
"bufio"
"errors"
"io"
"testing"

"github.com/stretchr/testify/require"
)

type fakeReader struct {
fnc func(p []byte) (int, error)
}

func (f fakeReader) Read(p []byte) (int, error) {
return f.fnc(p)
}

var _ io.Reader = fakeReader{}

func TestReadLineFromBuf(t *testing.T) {
var fr fakeReader

t.Run("it correctly returns the password when reader returns EOF", func(t *testing.T) {
fr.fnc = func(p []byte) (int, error) {
return copy(p, []byte("hello")), io.EOF
}
buf := bufio.NewReader(fr)

pass, err := readLineFromBuf(buf)
require.NoError(t, err)
require.Equal(t, "hello", pass)
})

t.Run("it returns EOF if reader has been exhausted", func(t *testing.T) {
fr.fnc = func(p []byte) (int, error) {
return 0, io.EOF
}
buf := bufio.NewReader(fr)

_, err := readLineFromBuf(buf)
require.ErrorIs(t, err, io.EOF)
})

t.Run("it returns the error if it's not EOF regardles if it read something or not", func(t *testing.T) {
expectedErr := errors.New("oh no")
fr.fnc = func(p []byte) (int, error) {
return copy(p, []byte("hello")), expectedErr
}
buf := bufio.NewReader(fr)

_, err := readLineFromBuf(buf)
require.ErrorIs(t, err, expectedErr)
})

}