9

When entering a username and password on a web application I have always wondered why the username often equals personal email address (which is often known or easy to guess or find). A random username for login would be more secure in my opinion.

However, I also wonder why often we show the username field in plain text and don't protect it the same against shoulder surfing, screen grabbing et cetera, by hiding what the user types using a HTML password input field instead of text input field. The very same thing really applies on 2FA codes that are usually being typed into a visible text field.

Why don't we use HTML password input fields for usernames and 2FA codes in the front-end of web applications? Why is this not a common practice? Even with a 'preview eye' button next to it, it's likely safer than just showing it in plain text.

Also are there any described best practices that recommend web developers to not use an email address as username, and hide the username and 2FA codes in the front-end too?

16
  • 7
    What's the risk of shoulder surfing to see the username?
    – schroeder
    Commented Nov 15 at 15:26
  • 2
    @schroeder I guess the risk is, that you are forced to publish a valid email address for something unrelated to receiving mails Commented Nov 15 at 22:46
  • 3
    @BernhardDöbler that's a nonsequitur ... The issue at hand is seeing the username, not that the username is an email ...
    – schroeder
    Commented Nov 16 at 0:10
  • 3
    Interestingly, another asker here seems to find many websites do mask 2FA codes during entry: security.stackexchange.com/questions/218735/… I wonder what the actual proportion would be.
    – timuzhti
    Commented Nov 16 at 1:40
  • 1
    @timuzhti: The top answer suspects cargo-cult programming for this feature, so the number of sites which implement it may not really say anything, not even about the proportion of people who actually think this is a good idea. Sometimes features are implemented without much thinking. This by itself doesn't mean it's bad to mask 2FA codes, just that there might be no conscious decision behind it in many cases.
    – Ja1024
    Commented Nov 16 at 5:14

5 Answers 5

44

When using password authentication, then the authentication element is in fact the password. The username is just an identifier which could be entirely public (unless it contains sensitive data like an e-mail address, of course). Using a random username as an additional secret makes little sense, because this forces the user to effectively manage two secrets while providing no benefit over a single strong password. There are also practical issues. Do you store the username as plaintext on the server? Then it's hardly a secret. Do you store a simple hash? Then the username will have to be very long or very complex to withstand brute-force attacks. Do you use a specialized password hash algorithm like Argon2? Then you'll need yet another identifier to look up the hash parameters. None of this is really good solution. In general, the username should be chosen by the user and be simple enough that they'll easily remember it.

The reason why 2FA codes are shown without masking is because they're only valid for a short amount of time (e.g., 30 seconds) and only work in conjunction with the password. So even if somebody sees the code, this doesn't immediately give them access to the account. It's also a matter of usability. Having to type in the code in a relatively short amount of time already creates some stress. If the user cannot even see the code and makes a mistake somewhere, this will quickly become frustrating.

If you want to increase security, then tell your users to generate strong random passwords with a password manager. Or look into passwordless alternatives like WebAuthn with passkeys.

4
  • 1
    I'm doubting 'providing no benefit over a single strong password'. Even if its stored plain text, from the outside targeting a specific user just became much harder. If the username was not a publicly known email address. And 'manage two secrets' is neglectible if the user follows the good practise of using a password manager. I would very much like to see more WebAuthn too, but the reality is that there is often an email address asked with it as a username as well, and implementations are by far user friendly for the average user.
    – Bob Ortiz
    Commented Nov 15 at 8:34
  • 16
    @BobOrtiz: If you store the “secret” username as plaintext and just hope it will never get leaked, this is security through obscurity – so no actual security. It’s also unclear what kind of attacks you’re trying to prevent with the random username when you assume the use of password manager. If the password is already randomly generated and stored in a secure manner, what’s the point of adding another random input but storing it in an insecure manner? I think the approach suffers from the more-is-better fallacy.
    – Ja1024
    Commented Nov 15 at 9:17
  • 4
    I'll add that for a surprisingly large number of sites that I use, I have multiple accounts (e.g., I have a work/professional identity and a personal identity to get to Office 365), and a privileged user in a cloud context is as likely as not to use the same authentication page, so seeing which account I am using is a strong mitigation against using the wrong account.
    – Randall
    Commented Nov 17 at 13:17
  • 1
    To add to this: Most kinds of system need accounts to have a non-secret identifier — not just in cases where it’s publicly displayed (eg social media, forums etc), but also for interaction with internal support, administration, etc. So making the “username” secret would not only add nothing (as this answer says, adding a second secret adds at best as much security as making passwords slightly longer/stronger) — it would require adding another “unique identifier” field, which would effectively just be reinventing usernames under a less-familiar term.
    – PLL
    Commented Nov 17 at 15:49
19

Usernames are intended to identify the individual user, and they're not usually considered to be secret. And this is especially true when you're threat is someone shoulder surfing, because they're standing right next to you. So you could hide the username from them on the login screen - but then then what? If they're looking over your shoulder they can probably see your username/email/whatever once you've logged in, as well as everything else that you're doing.

So you gain very little benefit from hiding it on one screen, and the penalty you get is that users are more likely to run into issues trying to login because they can't see what they're typing - which leads to more locked out accounts, more support tickets, and more frustrated users. So most people have decided that its not worth the effort for the very marginal benefits it brings.


As for OTPs, there's more of an argument that they should be hidden, and I've definitely seen some sites that do hide them. But the fact that they have such a short lifetime (and in many cases can only be used once) means that many sites don't bother doing so. It's hard enough persuading people to setup and use MFA in the first place, so adding additional layers of difficulty may end up being counterproductive.

It may also be that they've found people are more likely to make mistakes typing them than they are with passwords (as its a new code every time, and they're having to do it quickly) - but that's just speculation on my part.

6
  • 'they can probably see your username'. Not when a password manager is properly used. Copy username hidden, paste hidden. If it's unique per site there is no way they could see. Moreover, 'see everything else that you're doing' yes but seeing stuff is of a much higher risk when its regarding user credentials, that could escalate to authorization for altering rather then seeing. Imagine username, and 2FA code are visible and the user click the preview eye for the password. I often see OTPs not invalidated after used once within the valid time, there you go, sitting behind it.
    – Bob Ortiz
    Commented Nov 15 at 8:42
  • 5
    @BobOrtiz whether you're using a password manager is irrelevant if the site shows your username on all the post-login pages - like it does next to this comment as soon as I click "add comment", or shows my unique avatar in the top right of the screen.
    – Gh0stFish
    Commented Nov 15 at 8:53
  • Yes but then again, that should be a 'display name' to begin with, not a 'username' that's 1/2 (user/pass) or 1/3 (+TFA) of the pieces needed to login, in my opinion.
    – Bob Ortiz
    Commented Nov 15 at 9:04
  • 14
    @BobOrtiz: Why not argue for two passwords instead of (mis)using the username as a pseudo-secret? I think this will also be pointless and annoying, but you'll at least deal with actual secrets.
    – Ja1024
    Commented Nov 15 at 9:24
  • 2
    If the username were random, it's basically just a longer password.
    – Barmar
    Commented Nov 15 at 15:17
12

Adding to what was already said:

  • A lot of people use smartphones for internet access. Typing on a virtual keyboard makes easier to make mistakes, and having to type again a random username plus a password plus an OTP without knowing for sure which one you mistyped is frustrating. Having to click on the "eye icon" on the field on a small smartphone screen will not always be easy for all the population.

  • Usernames are not expected to be secret. It just becomes a second password, and the user will end up using his email anyway because it's easier to remember.

  • I personally dislike OTP being masked. If I mistyped it, I usually have to wait for the next token to be generated, and I spend that time complaining about the developer, the service and all. It's ephemeral, so knowing only it won't facilitate an account takeover.

  • Acording to AviD's Rule of Usability:

"Security at the expense of usability, comes at the expense of security."

If the login screen is too complex, users will try to find a shortcut that will reduce the security, like using the same password for everything, having the username the same as the email, or disabling OTP. Or give up and use another service.

1
  • 3
    You make a really good point there with usability. I dread typing in long passwords on my phone, let alone a long username, as well. And then at the same time, as pointed out the Ja1024’s answer, as long as the password is strong, then having a long username doesn’t even matter. I myself would be more likely to do something insecure to save the hassle! Commented Nov 14 at 21:42
6

We don't use password input fields for usernames because it prevents the user from correcting mistakes. This could be simple spelling mistakes but in the modern era we need to consider Apps themselves, browsers and plugins will often automate or pre-fill the username field, sometimes this is from the previous visit, other times it is simply the email address stored in the browser or plugin profile. Users with multiple identities or on devices that are used by multiple people could easily have the wrong username filled in.

Making the username field clear text reduces round trips to authenticate simply because the username was incorrect, in turn reduces user frustration in your site.

It is a bad practise to use password input fields for 2FA codes for similar reasons to above, except that at the 2FA prompt most implementations will activate stricter lockout policies if the 2FA value is incorrect a small number of times, the value itself should be single or limited time use, so the risk is lower if someone does see it, as they would have to see it, and use it before the first user. A worse issue is that some auto-filler plugin logic is very aggressive and they can treat any password input fields as passwords, and might auto-fill the password for a site into the 2FA field that was implemented as a password input, this would usually be wrong because MFA/OTP codes are usually generated for the specific session request and the pre-filling logic may not have access to this.

1
  • 1
    I had problems with my password manager autofilling "password" fields like this. On one site, the only way I could log in was by logging into my password manager (to autofill the actual password), closing the password manager, and then entering the six-digit code. If I didn't close the password manager, something about the form caused my password to be filled in automatically even after manually editing the six-digit code.
    – kaya3
    Commented Nov 17 at 17:46
0

I agree only 80% with the other answers here that cleanly conclude that usernames are non-secrets. That is true if the username is completely "independent" of the secret password. In other words, the username shouldn't provide any clues about the password's content, and it shouldn't provide an avenue for retrieving or bypassing the secret password.

The reality is that the username is often an email -- the same email used for password resets -- which breaks the assumption of independence. There's an entirely different class of attacks available to someone with that information.

Now, is this a big deal? Probably not enough to warrant hiding the username field. But it's enough that you should double-check assumptions about non-secrets in your app to make sure that's actually what they are

3
  • 4
    I don't get it: the username being the email does not reveal anything about the password itself. Attacker trying a password reset does not change anything if he does not have access to the email. Unless the username somehow is part of the password, they are independent.
    – ThoriumBR
    Commented Nov 15 at 19:57
  • 5
    If the security of an application depends on keeping the e-mails addresses secret, there's something fundamentally wrong. A lot of people happily hand out their e-mail address and publish it in all kinds of places (GitHub, personal websites, social media etc.). This is a legitimate personal decision. Of course there are also good reasons for protecting e-mail addresses (privacy, avoiding spam etc.), but this question should make no difference for the security of user accounts.
    – Ja1024
    Commented Nov 16 at 4:51
  • 3
    I also think there are two different problems. Even if we agree that the e-mail addresses should be protected, then the solution is to not use it as the username in the first place. It makes little sense to first expose the address as part of the credentials and then try to hide it in the UI (through masking or similar techniques). The question whether the username (which can be any identifier) should be protected is a separate issue.
    – Ja1024
    Commented Nov 16 at 4:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .