Pattern: Passwords and login
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. All numbers are example values to tune per project.
- Core ideas: long passwords without composition rules, a modern slow hash, one uniform login error, attempt limits at several levels, and no leak of which part was wrong.
- Related: sign-up, sessions, strong sign-in.
1. Problem
Login is attacked constantly: guessed passwords, passwords leaked from other sites (credential stuffing), and probing to learn which accounts exist. Weak rules, fast hashes and informative error messages each make this easier.
2. Options
| Topic | Option | Upside | Downside |
|---|---|---|---|
| Rules | Length minimum, no composition rules (recommended) | Matches NIST SP 800-63B-4; people choose passphrases | Needs a blocklist check to catch common choices |
| Rules | “One letter, one digit, one symbol” | Familiar | NIST says verifiers shall not impose composition rules; users pick predictable patterns |
| Hash | Argon2id | OWASP first choice; memory-hard | Needs memory budget; library availability |
| Hash | bcrypt | Widely available | 72-byte input limit; OWASP lists it for legacy systems |
| Blocklist | Local list of common passwords | No third-party call | Smaller coverage |
| Blocklist | k-anonymity range query to a breach service | Very large coverage; only a hash prefix leaves your server | Third-party dependency and availability |
| Attempts | Account lockout | Stops guessing on one account | An attacker can lock real users out (practitioner experience) |
| Attempts | Throttling plus layered limits | No lockout abuse | More tuning |
3. Recommended design
Password rules
- Minimum length: NIST SP 800-63B-4 requires at least 8 characters when passwords are used with another factor and 15 when the password is the only factor; the OWASP Authentication Cheat Sheet repeats both. Example: require 10 or more for password-only products, because shorter ones are weak against guessing.
- Allow a maximum of at least 64 characters, all characters including spaces and Unicode. No composition rules and no forced periodic change (NIST).
- Reject passwords longer than your hashing algorithm’s limit instead of silently truncating them (bcrypt keeps 72 bytes, per OWASP). Also cap length overall so very long inputs cannot be used to exhaust the processor.
- Blocklist: NIST requires comparing new passwords against commonly used, expected or compromised values. Start with a local list of the most common leaked passwords. Option: query a breach-corpus service by hash range (k-anonymity), which sends only a hash prefix; weigh the extra dependency. The OWASP sheet points to this approach.
Hashing
- Prefer Argon2id. OWASP’s minimum: 19 MiB of memory, 2 iterations, 1 degree of parallelism (RFC 9106 defines the algorithm). Increase as hardware allows.
- If you use bcrypt: work factor at least 10 (OWASP minimum), higher if latency allows (example: 12), and respect the 72-byte limit.
- Hash work is slow on purpose. Run it in a separate worker pool so it cannot block the main process, and answer “busy” when the queue is full instead of letting requests pile up (practitioner experience).
Login
- Accept the canonical email or the username.
- One uniform error: “email or password is wrong”. If the account does not exist, still compare against a fake hash so the response time does not differ. The OWASP Authentication Cheat Sheet explains why a quick exit for unknown users reveals them by timing.
- Attempt limits at three levels (example roles): a coarse limit per address; a strict limit per “typed identity + address”; a total limit per identity across all addresses (catches distributed guessing). Count only failures for identity limits. Do not show remaining attempts in response headers. See rate limits.
- Lockout versus throttling: OWASP names lockout as the common defense and says the counter belongs to the account, not the address. NIST allows up to 100 consecutive failures before disabling an authenticator. Lockout lets an attacker lock a victim out on purpose (practitioner experience), so many projects use slowing plus the layered limits above and add MFA. Choose deliberately and write the choice down.
- Suspension details (reason, duration) are shown only after the correct password. A deleted account gets the same answer as an unknown one.
- Keep a login record (address, time, device) for the legally required time. Store the email as a hash, not plain text, in the record.
Credential stuffing
- MFA is the strongest defense against it (OWASP Credential Stuffing Prevention). Client-side checks such as device fingerprints can be spoofed, so never rely on them alone.
4. Scenarios and edge cases
- Valid email, wrong password vs unknown email: same text, same status code, similar time.
- Distributed attack on one account from many addresses: the per-identity total limit slows it.
- One office shares one address: the per-address limit is high; the identity limits do the real work.
- User pastes a 5,000-character password: rejected quickly by the length cap before hashing.
- Hash parameters change later: re-hash at the next successful login.
5. Common mistakes
- Different messages for “no such user” and “wrong password”.
- Fast hashes (plain SHA-2) for passwords.
- Silent truncation at the hash’s input limit.
- Showing remaining attempts or lock reasons to anyone.
- Logging the typed password or token.
- Counting successful logins toward the identity limit.
6. How to audit
- What: uniform errors and timing.
- How to check: log in with a real email and wrong password; with an unknown email; compare body, status and timing over many tries.
- Evidence: both responses and timing numbers.
- What: the hash algorithm and parameters.
- How to check: read the code and configuration; check stored hash format.
- Evidence: the algorithm name and parameters from the stored value prefix.
- What: limits exist at three levels.
- How to check: script repeated failures per address, per identity and across addresses.
- Evidence: the point at which each limit triggers.
- What: long and breached passwords are rejected.
- How to check: try a very long password and a well-known leaked one.
- Evidence: the error responses.
Sources
- Password Storage Cheat Sheet — OWASP Cheat Sheet Series
- Authentication Cheat Sheet — OWASP Cheat Sheet Series
- Credential Stuffing Prevention Cheat Sheet — OWASP Cheat Sheet Series
- NIST SP 800-63B-4: Authentication and Authenticator Management — NIST
- RFC 9106: Argon2 Memory-Hard Function for Password Hashing and Proof-of-Work Applications — IETF / RFC Editor
- Have I Been Pwned: API Documentation (Pwned Passwords) — Have I Been Pwned
- Logging Cheat Sheet — OWASP Cheat Sheet Series
- Application Security Verification Standard (ASVS) — OWASP