Part I. Developer Cheat Sheets (Builder)

  1. Authentication Cheat Sheet

Last revision (mm/dd/yy): 02/24/2018

1.1. Introduction

Authentication is the process of verification that an individual or an entity is who it claims to be. Authentication is commonly performed by submitting a user name or ID and one or more items of private information that only a given user should know. Session Management is a process by which a server maintains the state of an entity interacting with it. This is required for a server to remember how to react to sub-sequent requests throughout a transaction. Sessions are maintained on the server by a session identifier which can be passed back and forward between the client and server when transmitting and receiving requests. Sessions should be unique per user and computationally very difficult to predict.

1.2. Authentication General Guidelines

1.2.1. User IDs

Make sure your usernames/userids are case insensitive. Regardless, it would be very strange for user ’smith’ and user ’Smith’ to be different users. Could result in serious confusion.

Email address as a User ID

Many sites use email addresses as a user id, which is a good mechanism for ensuring a unique identifier for each user without adding the burden of remembering a new username. However, many web applications do not treat email addresses correctly due to common misconceptions about what constitutes a valid address. Specifically, it is completely valid to have an mailbox address which:

  • Is case sensitive in the local-part
  • Has non-alphanumeric characters in the local-part (including + and @)
  • Has zero or more labels (though zero is admittedly not going to occur)

The local-part is the part of the mailbox address to the left of the rightmost @ char-acter. The domain is the part of the mailbox address to the right of the rightmost @ character and consists of zero or more labels joined by a period character.

At the time of writing, RFC 5321[2] is the current standard defining SMTP and what constitutes a valid mailbox address.


Many web applications contain computationally expensive and inaccurate regular expressions that attempt to validate email addresses.

Recent changes to the landscape mean that the number of false-negatives will in-crease, particularly due to:


  1. Authentication Cheat Sheet
  • Increased popularity of sub-addressing by providers such as Gmail (commonly using + as a token in the local-part to affect delivery)
  • New gTLDs with long names (many regular expressions check the number and length of each label in the domain)

Following RFC 5321, best practice for validating an email address would be to:

  • Check for presence of at least one @ symbol in the address
  • Ensure the local-part is no longer than 64 octets
  • Ensure the domain is no longer than 255 octets
  • Ensure the address is deliverable

To ensure an address is deliverable, the only way to check this is to send the user an email and have the user take action to confirm receipt. Beyond confirming that the email address is valid and deliverable, this also provides a positive acknowledgement that the user has access to the mailbox and is likely to be authorised to use it.

Address Normalisation

As the local-part of email addresses are, in fact – case sensitive, it is important to store and compare email addresses correctly. To normalise an email address input, you would convert the domain part ONLY to lowercase.

Unfortunately this does and will make input harder to normalise and correctly match to a users intent.

It is reasonable to only accept one unique capitalisation of an otherwise identical address, however in this case it is critical to:

  • Store the user-part as provided and verified by user verification
  • Perform comparisons by lowercase(provided)==lowercase(persisted)

1.2.2. Implement Proper Password Strength Controls

A key concern when using passwords for authentication is password strength. A “strong” password policy makes it difficult or even improbable for one to guess the password through either manual or automated means. The following characteristics define a strong password: Password Length

Longer passwords provide a greater combination of characters and consequently make it more difficult for an attacker to guess.

  • Minimum length of the passwords should be enforced by the application.

Passwords shorter than 10 characters are considered to be weak [3].

While minimum length enforcement may cause problems with memorizing passwords among some users, applications should encourage them to set passphrases (sen-tences or combination of words) that can be much longer than typical passwords and yet much easier to remember.

  • Maximum password length should not be set too low, as it will prevent users from creating passphrases. Typical maximum length is 128 characters.


  1. Authentication Cheat Sheet

Passphrases shorter than 20 characters are usually considered weak if they only consist of lower case Latin characters.

  • Every character counts!!

Make sure that every character the user types in is actually included in the password. We’ve seen systems that truncate the password at a length shorter than what the user provided (e.g., truncated at 15 characters when they entered 20).

This is usually handled by setting the length of ALL password input fields to be exactly the same length as the maximum length password. This is particularly important if your max password length is short, like 20-30 characters. Password Complexity

Applications should enforce password complexity rules to discourage easy to guess passwords. Password mechanisms should allow virtually any character the user can type to be part of their password, including the space character. Passwords should, obviously, be case sensitive in order to increase their complexity. Occasionally, we find systems where passwords aren’t case sensitive, frequently due to legacy system issues like old mainframes that didn’t have case sensitive passwords.

The password change mechanism should require a minimum level of complexity that makes sense for the application and its user population. For example:

  • Password must meet at least 3 out of the following 4 complexity rules

at least 1 uppercase character (A-Z)

at least 1 lowercase character (a-z)

at least 1 digit (0-9)

at least 1 special character (punctuation) — do not forget to treat space as special characters too

  • at least 10 characters
  • at most 128 characters
  • not more than 2 identical characters in a row (e.g., 111 not allowed)

As application’s require more complex password policies, they need to be very clear about what these policies are.

  • The required policy needs to be explicitly stated on the password change page

be sure to list every special character you allow, so it’s obvious to the user Recommendation:

  • Ideally, the application would indicate to the user as they type in their new password how much of the complexity policy their new password meets

In fact, the submit button should be grayed out until the new password meets the complexity policy and the 2nd copy of the new password matches the 1st. This will make it far easier for the user to understand and comply with your complexity policy.

Regardless of how the UI behaves, when a user submits their password change re-quest:


  1. Authentication Cheat Sheet
  • If the new password doesn’t comply with the complexity policy, the error mes-sage should describe EVERY complexity rule that the new password does not comply with, not just the 1st rule it doesn’t comply with

Changing passwords should be EASY, not a hunt in the dark.

1.2.3. Implement Secure Password Recovery Mechanism

It is common for an application to have a mechanism that provides a means for a user to gain access to their account in the event they forget their password. Please see Forgot Password Cheat Sheet on page 65 for details on this feature.

1.2.4. Store Passwords in a Secure Fashion

It is critical for a application to store a password using the right cryptographic tech-nique. Please see Password Storage Cheat Sheet on page 98 for details on this fea-ture.

1.2.5. Transmit Passwords Only Over TLS

See: Transport Layer Protection Cheat Sheet on page 149

The login page and all subsequent authenticated pages must be exclusively accessed over TLS. The initial login page, referred to as the “login landing page”, must be served over TLS. Failure to utilize TLS for the login landing page allows an attacker to mod-ify the login form action, causing the user’s credentials to be posted to an arbitrary location. Failure to utilize TLS for authenticated pages after the login enables an at-tacker to view the unencrypted session ID and compromise the user’s authenticated session.

1.2.6. Require Re-authentication for Sensitive Features

In order to mitigate CSRF and session hijacking, it’s important to require the current credentials for an account before updating sensitive account information such as the user’s password, user’s email, or before sensitive transactions, such as shipping a purchase to a new address. Without this countermeasure, an attacker may be able to execute sensitive transactions through a CSRF or XSS attack without needing to know the user’s current credentials. Additionally, an attacker may get temporary physical access to a user’s browser or steal their session ID to take over the user’s session.

1.2.7. Utilize Multi-Factor Authentication

Multi-factor authentication (MFA) is using more than one authentication factor to logon or process a transaction:

  • Something you know (account details or passwords)
  • Something you have (tokens or mobile phones)
  • Something you are (biometrics)

Authentication schemes such as One Time Passwords (OTP) implemented using a hardware token can also be key in fighting attacks such as CSRF and client-side malware. A number of hardware tokens suitable for MFA are available in the market that allow good integration with web applications. See [6].


  1. Authentication Cheat Sheet SSL Client Authentication

SSL Client Authentication, also known as two-way SSL authentication, consists of both, browser and server, sending their respective SSL certificates during the TLS handshake process. Just as you can validate the authenticity of a server by using the certificate and asking a well known Certificate Authority (CA) if the certificate is valid, the server can authenticate the user by receiving a certificate from the client and validating against a third party CA or its own CA. To do this, the server must provide the user with a certificate generated specifically for him, assigning values to the subject so that these can be used to determine what user the certificate should validate. The user installs the certificate on a browser and now uses it for the website. It is a good idea to do this when:

  • It is acceptable (or even preferred) that the user only has access to the website from only a single computer/browser.
  • The user is not easily scared by the process of installing SSL certificates on his browser or there will be someone, probably from IT support, that will do this for the user.
  • The website requires an extra step of security.
  • It is also a good thing to use when the website is for an intranet of a company or organization.

It is generally not a good idea to use this method for widely and publicly available

websites that will have an average user. For example, it wouldn’t be a good idea to

implement this for a website like Facebook.  While this technique can prevent the

user from having to type a password (thus protecting against an average keylogger

from stealing it), it is still considered a good idea to consider using both a password

and SSL client authentication combined.

For more information, see: [4] or [5].

1.2.8. Authentication and Error Messages

Incorrectly implemented error messages in the case of authentication functionality can be used for the purposes of user ID and password enumeration. An application should respond (both HTTP and HTML) in a generic manner. Authentication Responses

An application should respond with a generic error message regardless of whether the user ID or password was incorrect. It should also give no indication to the status of an existing account. Incorrect Response Examples

  • “Login for User foo: invalid password”
  • “Login failed, invalid user ID”
  • “Login failed; account disabled”
  • “Login failed; this user is not active”


  1. Authentication Cheat Sheet Correct Response Example

  • “Login failed; Invalid userID or password”

The correct response does not indicate if the user ID or password is the incorrect parameter and hence inferring a valid user ID. Error Codes and URLs

The application may return a different HTTP Error code depending on the authenti-cation attempt response. It may respond with a 200 for a positive result and a 403 for a negative result. Even though a generic error page is shown to a user, the HTTP response code may differ which can leak information about whether the account is valid or not.

1.2.9. Prevent Brute-Force Attacks

If an attacker is able to guess passwords without the account becoming disabled due to failed authentication attempts, the attacker has an opportunity to continue with a brute force attack until the account is compromised. Automating brute-force/password guessing attacks on web applications is a trivial challenge. Pass-word lockout mechanisms should be employed that lock out an account if more than a preset number of unsuccessful login attempts are made. Password lockout mech-anisms have a logical weakness. An attacker that undertakes a large number of authentication attempts on known account names can produce a result that locks out entire blocks of user accounts. Given that the intent of a password lockout sys-tem is to protect from brute-force attacks, a sensible strategy is to lockout accounts for a period of time (e.g., 20 minutes). This significantly slows down attackers, while allowing the accounts to reopen automatically for legitimate users.

Also, multi-factor authentication is a very powerful deterrent when trying to prevent brute force attacks since the credentials are a moving target. When multi-factor is implemented and active, account lockout may no longer be necessary.

1.3. Use of authentication protocols that require no password

While authentication through a user/password combination and using multi-factor authentication is considered generally secure, there are use cases where it isn’t con-sidered the best option or even safe. An example of this are third party applications that desire connecting to the web application, either from a mobile device, another website, desktop or other situations. When this happens, it is NOT considered safe to allow the third party application to store the user/password combo, since then it extends the attack surface into their hands, where it isn’t in your control. For this, and other use cases, there are several authentication protocols that can protect you from exposing your users’ data to attackers.

1.3.1. OAuth

Open Authorization (OAuth) is a protocol that allows an application to authenticate against a server as a user, without requiring passwords or any third party server that acts as an identity provider. It uses a token generated by the server, and provides how the authorization flows most occur, so that a client, such as a mobile application, can tell the server what user is using the service.

The recommendation is to use and implement OAuth 1.0a or OAuth 2.0, since the very first version (OAuth1.0) has been found to be vulnerable to session fixation.


  1. Authentication Cheat Sheet

OAuth 2.0 relies on HTTPS for security and is currently used and implemented by API’s from companies such as Facebook, Google, Twitter and Microsoft. OAuth1.0a is more difficult to use because it requires the use of cryptographic libraries for digital signatures, however does not rely on HTTPS for security and can therefore be more suited for higher risk transactions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.