ReadPassword(3pm)

πŸ“¦
version 0.11 Β· 6 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– ReadPassword β€” the manual, but make it cute ✨

okay bestie, so this is the page for Term::ReadPassword, and it's literally all about asking the user for a password. no cap.


🌸 NAME 🌸

Term::ReadPassword β€” asking the user for a password 🫢


✨ SYNOPSIS ✨

use Term::ReadPassword;
while (1) {
  my $password = read_password('password: ');
  redo unless defined $password;
  if ($password eq 'flubber') {
    print "Access granted.\n";
    last;
  } else {
    print "Access denied.\n";
    redo;
  }
}

πŸ¦‹ DESCRIPTION πŸ¦‹

so this module lets you ask the user for a password in the traditional way β€” straight from the keyboard, without echoing. iconic behavior tbh πŸ’…

heads up bestie: this is not intended for use over the web. user authentication over the web is a whole other matter entirely. also, this module should generally be used in conjunction with Perl's crypt() function, sold separately. πŸ’€

the read_password function prompts for input, reads a line of text from the keyboard, then returns that line to the caller. and the way that the line of text doesn't include the newline character?? so there's literally no need to use chomp. we love a low-maintenance API ✨

while the user is entering the text, a few special characters get processed:

  • the character delete (or the character backspace) will back up one character, removing the last character in the input buffer (if any)
  • the character CR (or the character LF) will signal the end of input, causing the accumulated input buffer to be returned
  • Control-U will empty the input buffer
  • and, optionally, the character Control-C may be used to terminate the input operation (see details below πŸ‘‡)

all other characters, even ones which would normally have special purposes, will be added to the input buffer. periodt.


not me telling you this butβ€” it is not recommended that you use the as-yet-unspecified control characters in your passwords, because those characters may become meaningful in a future version of this module. 🚨

applications which allow the user to set their own passwords may wish to enforce this rule, perhaps with code something like this:

{
  # Naked block for scoping and redo
  my $new_pw = read_password("Enter your new password: ");
  if ($new_pw =~ /([^\x20-\x7E])/) {
    my $bad = unpack "H*", $1;
    print "Your password may not contain the ";
    print "character with hex code $bad.\n";
    redo;
  } elsif (length($new_pw) < 5) {
    print "Your password must be longer than that!\n";
    redo;
  } elsif ($new_pw ne read_password("Enter it again: ")) {
    print "Passwords don't match.\n";
    redo;
  } else {
    &change_password($new_pw);
    print "Your password is now changed.\n";
  }
}

the second parameter to read_password is the optional "idle_timeout" value. if it's a non-zero number and there is no keyboard input for that many seconds, the input operation will terminate. ⏰

note bestie: this is not an overall time limit β€” the timer is restarted with each new character. so it's giving "per-keystroke patience", not "hurry up already". πŸ™ˆ

the third parameter will optionally allow the input operation to be terminated by the user with Control-C. if this is not supplied, or is false, a typed Control-C will be entered into the input buffer just as any other character. 😭 in that case, there is no way from the keyboard to terminate the program while it is waiting for input. (that is to say, the normal ability to generate signals from the keyboard is suspended during the call to read_password.) ⚠️

if the input operation terminates early (either because the idle_timeout was exceeded, or because a Control-C was enabled and typed), the return value will be "undef". in either case, there is no way provided to discover what (if anything) was typed before the early termination, or why the input operation was terminated. 🫠

so as to discourage users from typing their passwords anywhere except at the prompt, any input which has been "typed ahead" before the prompt appears will be discarded. and whether the input operation terminates normally or not, a newline character will be printed, so that the cursor will not remain on the line after the prompt. 🌈


πŸ’€ BUGS πŸ’€

  • Windows users will want Term::ReadPassword::Win32.
  • this module has a poorly-designed interface, and should be thoroughly rethought and probably re-united with the Windows version. πŸ’… (yes, the manpage said it, not me)
  • users who wish to see password characters echoed as stars may set $Term::ReadPassword::USE_STARS to a true value. ⭐ the bugs are that some terminals may not erase stars when the user corrects an error, and that using stars leaks information to shoulder-surfers. πŸ‘€

πŸ”₯ SECURITY πŸ”₯

you would think that a module dealing with passwords would be full of security features. you'd think that, but you'd be wrong. 😭

for example, perl provides no way to erase a piece of data from memory. (it's easy to erase it so that it can't be accessed from perl, but that's not the same thing as expunging it from the actual memory.) if you've entered a password, even if the variable that contained that password has been erased, it may be possible for someone to find that password, in plaintext, in a core dump. and that's just one potential security hole. πŸ’€

in short, if serious security is an issue, don't use this module. like, fr fr. not me sugarcoating it, that's literally what it says. 🚫

πŸŽ€ LICENSE πŸŽ€

this program is free software; you may redistribute it, modify it, or both, under the same terms as Perl itself. 🫢


πŸ‘‘ AUTHOR πŸ‘‘

Tom Phoenix <rootbeer@redcat.com>. Copyright (C) 2007 Tom Phoenix.


🌟 SEE ALSO 🌟

Term::ReadLine, "crypt" in perlfunc, and your system's manpages for the low-level I/O operations used here. ✨


and that's the whole page bestie!! this one's from perl v5.36.0, dated 2022-11-20, and it's ReadPassword(3pm) β€” go forth and prompt responsibly πŸ’–πŸŒΈπŸ¦‹πŸ’…

πŸ“¦ where this page comes from