conradkleinespel / rpassword

Cross platform Rust library to read a password in the terminal (Linux, BSD, OSX, Windows, WASM).
Apache License 2.0
243 stars 38 forks source link

Git Bash errors with `prompt_password_stdout` or `read_password` on Windows #49

Closed bochaco closed 4 years ago

bochaco commented 4 years ago

I'm getting OS error 6 ("The handle is invalid") on Windows using Git Bash when calling prompt_password_stdout or read_password. I also tried using read_password_from_tty which not only throws that same error after hitting enter, but also the message is not shown and the characters I type are displayed on the console.

I am currently using read_password_from_tty and it works for Linux, Mac, Windows Powershell, Windows CMD, but it fails with Windows Git Bash.

Is there any workaround for this?

conradkleinespel commented 4 years ago

@bochaco Hi ! I am not aware of this bug. Unfortunately I don't use Windows so I wouldn't be able to help. Windows, with it's many shells (Powershell, CMD, etc), needs a bit more testing, as far as I'm aware.

mahkoh commented 4 years ago

In current versions of msys2 and git-bash, applications that use the windows console api must be run with winpty. E.g winpty program args instead of program args.

cygwin supports the conpty api since cygwin version 3.1 but msys2 and git-bash currently use older versions.

conradkleinespel commented 4 years ago

Would you mind checking if this bug is still present after the recent fix in #51 ?

Try this version of rpassword in Cargo.toml:

rpassword = { git = "https://github.com/conradkleinespel/rpassword", rev = "189c388" }
bochaco commented 4 years ago

prompt_password_stdout and read_password don't throw the error but the characters I type are displayed on the console.

With read_password_from_tty the characters typed are also displayed and it throws "The handle is invalid" error after hitting enter.

conradkleinespel commented 4 years ago

@Heliozoa This seems like surprising behavior, that @bochaco sees when using the fix from #51. Any idea?

Heliozoa commented 4 years ago

Looks like this is an issue with how Git Bash works. I found this issue https://github.com/SBoudrias/Inquirer.js/issues/421, and the linked issues led me to this github-for-windows FAQ answer which suggests running the program through winpty, which fixed the problems both in read_password and read_password_from_tty when I tried it. A comment on another issue makes it sound like this would be quite difficult to fix properly:

The thing to understand first is that MinTTY does not implement a Win32 Console. Instead, it uses MSYS2's pty emulation. Therefore vagrant is absolutely correct in saying that it cannot hide the password: it really cannot, unless it learns about MSYS2's pty emulation. Which would of course be asking a bit much... what other terminal emulators should vagrant support in addition to MinTTY?

I also tried the regular command line, PowerShell 5/6 and the newer Windows Terminal and they all worked as expected.

conradkleinespel commented 4 years ago

Alright, thanks a lot for your renewed help @Heliozoa !

In that case, I'm going to close this issue, @bochaco, as it looks like this problem is unrelated to rpassword but rather related to Git Bash. Maybe this should be reported to Git Bash project maintainers directly.