Skip to content

Passwords

In short

The threat

People reuse passwords, and a leaked table of fast hashes gives most of them back within hours, for every site they use.

The rule

Hash with Argon2id, salted, at a cost you have measured; check length and known breaches, never composition; refuse what fails instead of repairing it.

In the package
password/argon2.ts and password/policy.ts, configured under password.

People reuse passwords. A table of stored passwords, or of fast hashes of them, is a list of credentials for other sites too: a fast hash like SHA-256 can be tried billions of times a second on a GPU, and most real passwords fall to that within hours.

On the other side, the server pays for every login, so a hash that is slow for the attacker is slow for you as well.

Nothing is repaired, only refused: the password is checked as typed, and what the store keeps is a salted Argon2id hash that names its own cost.

A new password is first normalised to NFC and never trimmed or rewritten. Its length is then counted in code points and must be 12 to 256; outside that range it is refused with weak_password or password_too_long. The breach check sends only the first 5 characters of the password's SHA-1 to the Pwned Passwords range API and refuses a leaked password with breached_password; if the service is down the password is accepted, and the warning is logged and counted. Argon2id then hashes it with 19 MiB, 3 passes, 1 lane and its own random salt. The store keeps one string that starts with $argon2id$v=19$m=19456,t=3,p=1$ and holds the salt and the hash, so the parameters travel with the hash.

Text version

Make every guess expensive, check only what helps, and let old hashes upgrade themselves.

Memory is the cost. Argon2id needs a block of memory per hash, which is what makes GPU and custom hardware attacks expensive.

Every hash has its own salt. Each hash gets its own random salt, so equal passwords produce different hashes and precomputed tables are useless.

The defaults are measured, not guessed. The defaults are 19 MiB of memory, 3 passes and 1 lane: the memory figure is OWASP’s floor, the pass count is the auth guide’s. Raise the memory on a box that can afford it, not the passes or lanes.

Every login that succeeds checks whether the stored hash was made with the current parameters. If not, the password the user just typed is hashed again and stored, as a compare-and-set on the old hash.

Raising the cost needs no migration: each login upgrades its own hash, and a password change that lands first always wins.

After a login proves the password, needsRehash compares the parameters stored in the hash with the current memory, passes and lanes. If they differ, the password the user just typed is hashed again and written as a compare-and-set that expects the old hash. With no other write, the hash is upgraded, with no migration and no reset. If a password change or reset writes a new hash while the upgrade is being computed, the compare-and-set finds the old hash gone and the upgrade is dropped, so the new password stays and the old one never comes back.

Text version
  • A change that lands first wins. If a password change or reset lands while the new hash is being computed, the upgrade is dropped instead of bringing the old password back (A015).
  • No migration. Raising the cost later, or adopting hashes made by an older app, needs no migration and no reset.

A password must be 12 to 256 characters, counted as Unicode code points after NFC normalisation, so the same password typed on two keyboards hashes the same.

There are no “one digit, one symbol” rules: they push people towards predictable patterns without adding much strength. Instead, a new password is checked against known breaches (below).

Apart from NFC normalisation, input is never trimmed or rewritten. A rejected password gets a reason the user can act on: weak_password, password_too_long or breached_password.

The password never leaves the server. The breach check sends only the first five characters of the password’s SHA-1 to the Pwned Passwords range API and compares the rest locally. A malformed answer is never read as a verdict (A022).

An outage does not block sign-ups. If the service is down, the check fails open: the password is accepted, a warning is logged and the auth_breach_check{verdict="unavailable"} metric goes up, so the outage is visible.

pwnedPasswords() returns a checker with a second method, check(password), that checks exactly like calling it and returns the verdict with its cause. The optional onResult option gets the same outcome for every check: clean, breached and unavailable.

Use it to log or count, so a timeout shows apart from a changed response format. An unavailable outcome holds a reason:

reason When
timeout No answer within timeoutMs, while connecting or reading the body
network-error The request or the body read failed (DNS, TLS, a reset connection)
http-status An answer whose status is not a success; status holds the code
unparsable-body A success status with a body that is not range lines (an error page, a captive portal, a changed format)

An outcome holds nothing secret. It never carries the password, its hash or the five-character prefix (A088).

The hook cannot change the verdict. It is not awaited, and the outcome it gets is frozen. A hook that throws or rejects is reported to the checker’s logger option (silent by default) with the error message only.

Every door that hashes a password has a rate limit, and an input longer than the policy’s maximum is never handed to the hasher at all (A020).

The defaults written out, and a breach check that says why it could not answer.

passwords.ts
import type { PasswordPolicy } from '@noctcore/auth';
import type { Argon2idOptions } from '@noctcore/auth/argon2';
// The defaults, written out. Pass them to createAuth as
// `password: { hasher: argon2id(hashing), policy }`, or leave them out for
// the same values.
export const hashing = {
// KiB of memory for each hash: OWASP's floor. Raise this on a box that
// can afford it, not the passes or the lanes.
memoryCost: 19_456,
// Passes, the auth guide's floor, and lanes.
timeCost: 3,
parallelism: 1,
} satisfies Argon2idOptions;
// Counted in Unicode code points after NFC. No composition rules.
export const policy = { minLength: 12, maxLength: 256 } satisfies PasswordPolicy;
  • Minimum length. The guide suggests 8 or 10. The package uses 12, from the original production implementation, and refuses a configured minimum below 8.
  • Maximum length. The guide suggests 50 to 100. The package allows 256, so long passphrases and generated passwords fit; the per-door limits already bound the hashing cost.
  • Character set. The guide is comfortable limiting passwords to printable ASCII. The package accepts any Unicode and normalises it with NFC, which the guide names as the requirement when Unicode is allowed.
  • Passes. OWASP’s floor is 2 passes at 19 MiB, the guide’s is 3 passes at 16 MiB. The package takes the stricter value of each.
Test Attack Stopped by
A015 A login rehash bringing back an old password flows/rehash.ts
A020 Exhausting the server with oversized passwords password/policy.ts, password/verify.ts, flows/register.ts
A022 Turning a malformed breach-check response into a verdict through Unicode case folding password/breach.ts
A088 Reading a password, its hash or the hash prefix out of what the breach check reports password/breach.ts, ports/provider-outcome.ts