Introduction
The login form is the front door of every web app, and attackers try it constantly. They take passwords leaked from other sites and try them here (credential stuffing), guess common passwords across thousands of accounts (password spraying), or try to steal or guess the session that keeps a user logged in. OWASP files all of these under Identification and Authentication Failures.
This lesson collects the defences in one place: limiting guesses, handling sessions safely, and the small details, such as error messages and password resets, where logins usually leak.
How it works
Slow down guessing. A 4-digit PIN has only 10000 values, and a common-password list has a few thousand entries, so unlimited attempts eventually win. Two tools stop that:
- Rate limiting: allow only a few attempts per minute per account and per address.
- Increasing delays: after a few failures, make each new failure wait longer, doubling each time. Honest users who mistype twice notice nothing; a guessing script slows to a crawl.
A hard lockout after N failures also stops guessing, but it hands attackers a way to lock real users out on purpose, so many sites prefer delays plus a second factor.
Protect the session. After login, the server gives the browser a session ID, usually in a cookie, and that ID now is the user. It must be:
- long and random (at least 128 bits from a secure random generator), so it cannot be guessed;
- renewed at login, so an ID an attacker planted before login (session fixation) becomes useless;
- sent only over HTTPS, in a cookie marked
Secure,HttpOnlyandSameSite; - ended at logout and after a period of inactivity.
Leak nothing. Say "wrong user name or password", never which one was wrong. Make password-reset links single-use, long, random and short-lived. Check new passwords against lists of leaked ones, and offer a second factor.
Example
The program shows increasing delays after three failures in a row, and how fast the waits grow.
// Slowing down password guessing: after 3 failures in a row, each new failure doubles the wait.
let failures = 0;
function attempt(correct) {
if (correct) {
failures = 0;
return "logged in";
}
failures++;
const wait = failures <= 3 ? 0 : 2 ** (failures - 3);
return "wrong password (" + failures + " in a row)" + (wait ? ", wait " + wait + " s before the next try" : "");
}
for (const ok of [false, false, false, false, false, false, true]) console.log(attempt(ok));
// The waits add up fast: how long do the first 20 wrong guesses take?
let total = 0;
for (let k = 1; k <= 20; k++) total += k <= 3 ? 0 : 2 ** (k - 3);
console.log("waiting for the first 20 wrong guesses alone: " + total + " s (about " + Math.round(total / 86400) + " days)");Line by line
failurescounts wrong passwords in a row. A correct password resets it to 0.- For the first 3 failures there is no wait, so a person who mistypes is not punished.
- From the 4th failure, the wait is 2^(failures − 3) seconds: 2, 4, 8, and so on, doubling each time.
- After the right password, the count resets and the user logs in.
- The last lines add up the waits for 20 wrong guesses in a row: 2 + 4 + … + 2¹⁷ = 262142 seconds, about 3 days, for just 20 guesses.
Expected result
wrong password (1 in a row)
wrong password (2 in a row)
wrong password (3 in a row)
wrong password (4 in a row), wait 2 s before the next try
wrong password (5 in a row), wait 4 s before the next try
wrong password (6 in a row), wait 8 s before the next try
logged in
waiting for the first 20 wrong guesses alone: 262142 s (about 3 days)
In the real world
- Credential stuffing works because people reuse passwords; checking new passwords against leaked lists and offering a second factor defeat most of it.
- Spraying one common password across many accounts avoids per-account limits, so limits must also apply per source address and across the whole site.
- Session IDs in URLs leak through browser history, logs and shared links; keep them in cookies.
- Many frameworks provide secure session handling and login throttling; use them rather than writing your own.
Common mistakes
- Saying "this user does not exist" at login or reset, which lets attackers list valid accounts.
- No limit on attempts, or a limit per account only.
- Keeping the same session ID after login.
- Reset links that never expire or can be used twice.
Best practices
- Rate-limit logins per account, per address and overall, with growing delays.
- Generate session IDs with a secure random generator, renew them at login, and expire them.
- Use one generic error message for any login failure.
- Offer and encourage a second factor, preferably passkeys or an authenticator app.
Summary
Authentication failures let attackers guess, reuse or steal their way into accounts. Rate limits and increasing delays make guessing hopeless while barely touching honest users; hard lockouts work but can be abused. Session IDs must be long and random, renewed at login, kept in Secure, HttpOnly, SameSite cookies and ended on logout. Generic error messages, single-use reset links, leaked-password checks and a second factor close the remaining gaps.
Quick check
Key takeaways
- Limit attempts per account, per address and overall, with delays that double.
- Session IDs must be long, random, renewed at login and expired.
- Use one generic login error and single-use, short-lived reset links.
- Leaked-password checks and a second factor defeat most credential stuffing.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.