Registry Mirror
Skip to content

Conversation

@lakhansamani

@lakhansamani lakhansamani commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Equalises password-check timing so unknown accounts can't be told apart from registered ones.

The bug. Since 2.4.0 (#751) password hashes are written at bcrypt cost 12 (crypto.PasswordHashCost), but the decoy hashes checked on the "no such account" paths stayed at cost 10:

  • login.go: user not found, no password, wrong auth method, and so on.
  • signup.go: email or phone already registered.

So an unknown account answered about 4x faster than a registered one.

The fix.

  • Decoys now cost crypto.PasswordHashCost. They are built lazily, one per cost, not at package load; a cost-12 hash at init would add ~200 ms to every binary and test start.
  • After checking a stored hash written at a lower cost c (cost 10 before 2.4.0), login runs decoy checks at costs c … 11. The total bcrypt work is 2^c + (2^c + … + 2^11) = 2^12, the same as one cost-12 check. Without this, the fix would only swap which accounts stand out: dormant pre-2.4.0 accounts would answer faster than the decoy.
  • The padding runs for right and wrong passwords alike and writes nothing. Stored hashes are never changed; an account keeps its cost-10 hash until its password next changes.

Measured (median of 9 wrong-password logins, SQLite, M-series Mac):

unknown account cost-10 account cost-12 account
main 45 ms 45 ms 181 ms
this PR 187 ms 190 ms 182 ms

Tests. The first commit adds the tests alone. They fail on main (expected: 12, actual: 10 for both decoys). The compatibility test TestLoginWorksForEveryStoredHashCost passes before and after: cost-10 and cost-12 accounts log in, failures stay generic, and the stored hash is unchanged. The second commit is the fix.

Verified: go build ./..., go vet ./..., make test (3,495 passed, 0 failed, 25 skipped), make lint (0 issues).

Not in this PR. Upgrading old hashes to cost 12 on login was considered and deferred. UpdateUser rewrites the whole user record on every backend, so a rehash racing with an admin revoke or a password reset could undo it. A safe version needs a compare-and-set write across all 7 storage backends.

Severity. This is an account-enumeration timing leak, moderate severity, so it is fixed in a public PR per AGENTS.md.

Which issue(s) does this PR fix?

None filed; found while reviewing signing and password-hash strength.

If this PR affects any API reference documentation, please share the updated endpoint references

No API change. The security page note is in the docs PR (Signing key strength / login timing).

Review follow-up (9926c3c)

An adversarial review measured every login outcome on main and on this branch: median of 9, fresh user each time, SQLite. Every outcome involving a hash Authorizer writes (cost 10 or 12) now takes about 199 ms:

Outcome main this PR
unknown email or phone 50 ms 198–200 ms
wrong / correct password, cost 10 50 / 50 ms 199 / 199 ms
wrong / correct password, cost 12 200 / 200 ms 199 / 201 ms
social-only, revoked, wrong method, unverified (email off) 50 ms 198–200 ms
signup, existing vs fresh email 50 vs 202 ms 199 vs 199 ms

Unchanged and still distinguishable:

  • Imported hashes above cost 12, and stored values that aren't bcrypt hashes. Authorizer never writes these.
  • Lockout, and an unverified email when the email service is on. These differ by their response, not by timing, and are out of scope here. The docs now name them instead of implying they're covered.

Fix in this commit:

  • One mutex guarded every decoy hash, so a burst of logins right after start-up waited 400–600 ms for about 0.6 s.
  • Each cost now has its own sync.Once.

Operational note for the release notes:

  • A failed login for an unknown account, and every login to a cost-10 account, now costs about 4x the bcrypt CPU of 2.5.0. This is inherent in equal timing.
  • Anonymous unknown-email attempts are bounded by the per-IP rate limit, not the per-account lockout.

Verification on the branch:

  • make test: 3495 passed, 25 skipped.
  • make lint: 0 issues.
  • The decoy tests pass under -race -count=2.

Second review (0abb975, 5a46a0c)

An independent re-review confirmed the timing fix: a probe measured 199 ms for an unknown account, 202 ms for a cost-10 account and 199 ms for a cost-12 account. It found two gaps, now fixed:

  • The tests did not pin the wiring. They called the helpers directly, so removing padPasswordCheck from Login, or a decoy from SignUp, left every test passing. Login's compare now goes through the same seam. TestLoginAndSignupSpendEqualBcryptWork runs Login and SignUp themselves and checks that each path spends exactly one cost-12 check's worth of bcrypt work:

    • login: unknown email or phone, and cost-10 and cost-12 accounts;
    • signup: a registered email or phone.

    Undoing any one call (padding, either signup decoy, or a cost-10 login decoy as in 2.5.0) makes it fail.

  • Signup told registered and new emails apart by its response.

    • With email verification on, signup gives a registered email the same "verification email sent" as a new one. But roles and redirect_uri were validated after the lookup, so an invalid value was reported only for a new email.
    • redirect_uri was even checked after the new user row was written, leaving it behind with no verification request.
    • Both checks now run before the lookup, under the same conditions as before, so a request that passed before still passes (TestSignupRejectsBadInputBeforeCheckingTheAddress, which fails on the previous head).

Still answering differently by response, and now documented: phone signup, and email signup without verification, return an error for a registered address, because a new one is signed in straight away.

Verification on the branch: go build, go vet, make test (3504 passed, 0 failed) and make lint (0 issues).

Fails on main: the login and signup decoys are bcrypt cost 10 while real
hashes are written at crypto.PasswordHashCost (12), so unknown accounts
answer about 4x faster than registered ones.

TestLoginWorksForEveryStoredHashCost pins that cost-10 and cost-12 accounts
keep logging in and that login never rewrites a stored hash.
The decoy bcrypt hashes checked for unknown accounts (login) and for an
address that is already registered (signup) were cost 10, while real
password hashes are written at crypto.PasswordHashCost (12). Unknown
accounts therefore answered about 4x faster than registered ones.

- Decoys now cost crypto.PasswordHashCost, built lazily (one per cost)
  instead of at package load, where a cost-12 hash would add ~200ms to
  every binary and test start.
- After checking a stored hash written at a lower cost (cost 10 before
  2.4.0), login runs decoy checks at costs c..PasswordHashCost-1, so the
  total bcrypt work is the same as one cost-12 check. Without this the fix
  would only swap which accounts stand out: dormant pre-2.4.0 accounts
  would answer faster than the decoy. The padding runs on right and wrong
  passwords alike and writes nothing; stored hashes are never changed.
One mutex guarded every decoy, so right after start-up a request needing
one decoy waited while another cost was being built (a burst of logins
saw 400-600ms for about 0.6s). Each cost now has its own sync.Once.
The decoy and padding tests called the helpers directly, so removing a
call from Login or SignUp left every test green. Login's password compare
now goes through the same seam, and a service-level test checks that each
path an attacker can tell apart (unknown or existing account at login,
cost-10 or cost-12 hash, registered email or phone at signup) spends one
cost-12 check's worth of bcrypt work.
With email verification on, signup answers a registered email with the
same "verification email sent" as a new one. But the requested roles and
the redirect URI were validated after the existence lookup, so an invalid
role or redirect URI was reported only for a new email: a registered one
still got "verification email sent", telling the two apart without any
timing. The redirect URI was even checked after the new user row was
written, leaving it behind with no verification request.

Both checks now run before the lookup, under the same conditions as
before, so a request that passed before still passes.

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