From a21c5dde4c267671d53b40583e43e936a684a466 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 22 Nov 2017 18:46:40 +0900 Subject: [PATCH] ssh/terminal: handle non-ASCII characters when reading passwords ReadPassword uses Windows ReadFile to read from console handle. But ReadFile does not split input into UTF-8 characters, so ReadFile only works when input is ASCII. Use os.File instead of Windows ReadFile, because os.File reads console and parses it into UTF-8. Fixes golang/go#22828 Change-Id: Ifeed3e8048b51f46706c28d4154a3e4b10111a3e Reviewed-on: https://go-review.googlesource.com/79335 Reviewed-by: Alex Brainman Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- util_windows.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/util_windows.go b/util_windows.go index 60979cc..92944f3 100644 --- a/util_windows.go +++ b/util_windows.go @@ -17,6 +17,8 @@ package terminal import ( + "os" + "golang.org/x/sys/windows" ) @@ -71,13 +73,6 @@ func GetSize(fd int) (width, height int, err error) { return int(info.Size.X), int(info.Size.Y), nil } -// passwordReader is an io.Reader that reads from a specific Windows HANDLE. -type passwordReader int - -func (r passwordReader) Read(buf []byte) (int, error) { - return windows.Read(windows.Handle(r), buf) -} - // ReadPassword reads a line of input from a terminal without local echo. This // is commonly used for inputting passwords and other sensitive data. The slice // returned does not include the \n. @@ -98,5 +93,5 @@ func ReadPassword(fd int) ([]byte, error) { windows.SetConsoleMode(windows.Handle(fd), old) }() - return readPasswordLine(passwordReader(fd)) + return readPasswordLine(os.NewFile(uintptr(fd), "stdin")) }