CAPSTONE: HARDEN A LOGIN FLOW

Everything in this path, in one sign-in form

L320 MINSECURITY
PREDICT

Your login returns "Invalid username or password" for both an unknown user and a wrong password. It hashes the password only when the user exists, with an 80 ms hash. Can an attacker tell which usernames exist?

A login form is the place where every earlier lesson meets. Each layer stops one attack and nothing else. A parameterized query stops injection. A salted slow hash protects passwords if the table leaks. One error message, with the same work on both paths, stops enumeration. A per-account+IP limit stops guessing without letting an attacker lock the owner out. HttpOnly stops a script from stealing the session cookie, though not from using the session in place. SameSite stops other sites from riding it. Skip any one, and the attacker goes through that gap; the others never get asked.

▶ SIMULATION
ALICE'S BROWSER
cookie: —
readable by scripts: —
sent on cross-site POST: —
SERVER: USERS TABLE
—
query: concatenated · errors: distinct
limit: none
ATTACKER
usernames learned: —
guessing: —
acted as alice: 0×
last—
✓ attacker cannot act as alice without her password✓ attacker cannot learn which usernames exist
SCHEDULE
01[SERVER]server: concatenated SQL, plaintext, distinct errors, no limit, bare cookie
02[ALICE]alice signs in from 1.2.3.4
03[ATTACKER]attacker tries alice/x and zed/x and compares the responses
04[ATTACKER]attacker signs in as "zzz' UNION SELECT 'alice','pwned' --" / pwned
05[ATTACKER]attacker tries 20 common passwords on alice from 6.6.6.6
06[ATTACKER]injected script on the site's own forum page reads document.cookie to exfiltrate the session
07[ATTACKER]evil.com auto-submits POST /account/email in alice's browser (no Lax-by-default)
08[ALICE]alice signs in again from 1.2.3.4
09[SERVER]report
Start of naive login scenario. 9 steps.
app.post('/login', async (req, res) => {
  const { username, password } = req.body;
  const [user] = await db.query(
    `SELECT * FROM users WHERE username = '${username}'`);   // injection
  if (!user) return res.status(401).send('Unknown user');      // enumeration
  if (user.password !== password)                              // plaintext
    return res.status(401).send('Wrong password');
  res.cookie('sid', newSession(user.id));                      // script-readable, sent cross-site
  res.send('Welcome');
});                                                            // no rate limit

Watch the ATTACKER box and both invariants. NAIVE LOGIN fails both: the error message names the missing user, the injection signs in as alice, the twelfth guess works, and the cookie is read by a script and sent with a cross-site POST. HARDENED LOGIN runs the same six attacks and keeps both green, and alice still signs in at the end.

BREAK IT NOT YET

Every layer is in place except one. Pick the attack and the moment that gets the attacker into alice's account.

GOAL: the attacker acts as alice

FIX IT NOT YET

Harden login. Use a parameterized query (? or $1 placeholder); compare hashes with db.equal (digests are objects, so === never matches); return the same status, body and work for an unknown user as for a wrong password; allow 5 attempts per username and IP with limiter.hit(key); set an HttpOnly; Secure; SameSite=Lax cookie. Time is virtual: a query costs 1 ms, db.hash 80 ms.

Mental model:

Treat login as a chain: input to query, query to hash, hash to response, response to cookie. Every link has its own attack, and the attacker picks the weakest. Make every failure look and cost the same, and remember the session cookie is the password after sign-in.

IN PRODUCTION

Every sign-in endpoint, password reset (“if that email exists, we sent a link”), and sign-up (“username taken” is enumeration too). Framework pieces: ORM parameter binding, argon2/bcrypt libraries, rate limiters keyed on account and IP, Set-Cookie: HttpOnly; Secure; SameSite=Lax. OWASP's Authentication Cheat Sheet lists the same layers.

IN INTERVIEWS

“Design a secure login endpoint” or “Review this login code.” Walk the request in order: limiter, parameterized lookup, slow hash with a dummy for unknown users, constant-time compare, one generic error, session cookie flags. Then say what each layer does not cover: MFA for stolen passwords, breached-password checks, and distributed guessing across many IPs.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: JWT PITFALLS →