Skip to content

feat(auth): add password hash compute policy - #2947

Draft
hf wants to merge 2 commits into
masterfrom
stojan/password-hash-compute-policy
Draft

hf wants to merge 2 commits into
masterfrom
stojan/password-hash-compute-policy

Conversation

@hf

@hf hf commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What

Adds GOTRUE_PASSWORD_HASH_COMPUTE_POLICY, a single JSON value that limits which password hashing algorithms the server will verify and the maximum cost parameters they may use. A stored hash outside the policy fails the request with a new error code, password_reset_required (HTTP 400), telling the client to start a password reset.

The goal is to keep sign-in latency predictable on all projects while keeping an industry-acceptable security level.

{
  "bcrypt":   { "max_cost": 10 },
  "argon2":   { "variants": ["argon2i", "argon2id"], "max_memory_kib": 2048, "max_time": 4, "max_threads": 1 },
  "fbscrypt": { "max_memory_cost_log2": 14, "max_rounds": 8, "max_threads": 1 }
}

Behaviour

  • Unset: everything is permitted, as today.
  • Set: it is an allowlist. An algorithm that is not mentioned is rejected. "bcrypt": {} allows it without limits, "disabled": true rejects it, and {} rejects every hash.
  • Validation: the value is checked against a JSON schema at startup (types and ranges). Unknown keys are ignored on purpose so policies written for newer versions still load. A misspelled key therefore has no effect.
  • Enforcement: User.Authenticate checks the hash before any hashing work. It applies to password sign-in and the password checks in PUT /user. In a recovery session the same-password check skips a blocked hash so the reset can replace it. Outside a recovery session the user gets password_reset_required.
  • Logging: a warning with the user ID and hash parameters is logged whenever a password is compared against a non-default hash (anything other than bcrypt cost 10). Salts, hashes and keys are never logged.
  • Config example and docs are in example.env.

Notes for review

  • User.Authenticate gains a policy argument. Callers and tests are updated.
  • fix(models): remove in-memory bcrypt rehash from User.Authenticate #2946 removes an unrelated in-memory bcrypt rehash block in the same function. The two should merge cleanly but touch nearby lines.
  • The compute policy check runs before the password is verified, so password_reset_required tells a caller who knows an email that the account exists and has a hash outside the policy. Verifying first would defeat the purpose of avoiding the work.

Testing

  • go build, go vet and the internal/conf and internal/crypto tests pass (policy table, schema accept and reject cases, env decoding, hash parameter extraction).
  • Not run: the internal/api and internal/models suites need a database and the local Postgres credentials were rejected. That includes the new token test for password_reset_required.
  • Not covered: there is no test yet for the recovery-session same-password skip or for the warning log.

🤖 Generated with Claude Code

hf and others added 2 commits November 20, 2026 17:00
Add GOTRUE_PASSWORD_HASH_COMPUTE_POLICY, a JSON object validated against a
JSON schema, that limits which password hashing algorithms the server will
verify and the maximum cost parameters they may use (bcrypt cost, Argon2
memory/time/threads and variants, Firebase scrypt n/r/p). Once set it is an
allowlist: an algorithm that is not mentioned is rejected. Unknown keys are
ignored so policies written for newer versions still load.

A stored hash outside the policy is rejected before any hashing is done and
the request fails with the new password_reset_required error code (HTTP 400).
Password sign-in and the password checks in PUT /user apply the policy. In a
recovery session the same-password check skips a blocked hash so the reset
can replace it.

Also log a warning, with the user ID and hash parameters (never salts, hashes
or keys), whenever a password is compared against a non-default hash.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant