Closed
Description
On go1.4.2 windows/amd64
with golang.org/x/crypto/ssh/terminal
revision 56474dfd625f18739b46f075b138fd0133717491
.
ReadPassword seems to be broken.
Reproduction Case:
package main
import (
"fmt"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
fmt.Print("password: ")
password, err := terminal.ReadPassword(0)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(password))
}
When I run this code on Windows 10, ReadPassword()
throws the following error: The handle is invalid.
The same code works on linux
and osx
.
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
[-]x/tools/crypto/ssh/terminal: ReadPassword broken on windows[/-][+]x/crypto/ssh/terminal: ReadPassword broken on windows[/+]ianlancetaylor commentedon Jul 29, 2015
Does it work if you use terminal.ReadPassword(syscall.Stdin) ?
The fact that 0 == syscall.Stdin on Unix system does not mean that that holds true on Windows.
Joshua-Anderson commentedon Jul 29, 2015
You're correct, I made the mistake of assuming that
syscall.Stdin
was0
on windows. I never considered that it would be different on windows (silly windows), and the docs on stdin were a little misleading. Thank you for the clarification!ianlancetaylor commentedon Apr 14, 2020
A lightly edited note from @cadethacker:
There are actually two reason this might be caused.
The first is documented above and correct that the handle ids on Windows are not the same as Unix.
But the second reason is…. if you are using Cygwin/mintty/git-bash on Windows, those Windows shells are unable to reach down to the OS API, and will throw the exact same error of the “handle is invalid”.
This issue is not directly fixable and really not an issue with Go. If you switch to Powershell or even CMD then executing
ReadPassword
will work as expected. You may then switch back to your shell for all other commands that don’t invokeReadPassword
. If you must stay in Cygwin/minty/git-bash then take a look at https://github.com/rprichard/winpty project, it might solve your issue.