Skip to content

feat: enable masking password inputs instead of blocking echo #17469

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

Merged
merged 13 commits into from
Apr 24, 2025
Merged
Prev Previous commit
Next Next commit
windows writer handling
  • Loading branch information
ibetitsmike committed Apr 22, 2025
commit 7d956d35dae26ede376b4bdb2d0600d970e603ab
12 changes: 6 additions & 6 deletions cli/cliui/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Prompt(inv *serpent.Invocation, opts PromptOptions) (string, error) {

inFile, isInputFile := inv.Stdin.(*os.File)
if opts.Secret && isInputFile && isatty.IsTerminal(inFile.Fd()) {
line, err = readSecretInput(inFile)
line, err = readSecretInput(inFile, inv.Stdout)
} else {
signal.Notify(interrupt, os.Interrupt)
defer signal.Stop(interrupt)
Expand Down Expand Up @@ -209,7 +209,7 @@ func readUntil(r io.Reader, delim byte) (string, error) {
// readSecretInput reads secret input from the terminal character by character,
// masking the input with asterisks. It handles special characters like backspace
// and enter appropriately.
func readSecretInput(f *os.File) (string, error) {
func readSecretInput(f *os.File, w io.Writer) (string, error) {
// Set terminal to raw mode
oldState, err := pty.MakeInputRaw(f.Fd())
if err != nil {
Expand All @@ -231,23 +231,23 @@ func readSecretInput(f *os.File) (string, error) {
// Handle special characters
switch buf[0] {
case '\r', '\n': // Enter
_, _ = f.Write([]byte("\n"))
_, _ = w.Write([]byte("\n"))
return line, nil
case 3: // Ctrl+C
_, _ = f.Write([]byte("\n"))
_, _ = w.Write([]byte("\n"))
return "", ErrCanceled
case 8, 127: // Backspace/Delete
if len(line) > 0 {
line = line[:len(line)-1]
// Move cursor back, print space, move cursor back again
_, _ = f.Write([]byte("\b \b"))
_, _ = w.Write([]byte("\b \b"))
}
default:
// Only append printable characters
if buf[0] >= 32 && buf[0] <= 126 {
line += string(buf[0])
// Print the mask character
_, _ = f.Write([]byte("*"))
_, _ = w.Write([]byte("*"))
}
}
}
Expand Down