Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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.Measured (median of 9 wrong-password logins, SQLite, M-series Mac):
Tests. The first commit adds the tests alone. They fail on main (
expected: 12, actual: 10for both decoys). The compatibility testTestLoginWorksForEveryStoredHashCostpasses 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.
UpdateUserrewrites 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:
Unchanged and still distinguishable:
Fix in this commit:
sync.Once.Operational note for the release notes:
Verification on the branch:
make test: 3495 passed, 25 skipped.make lint: 0 issues.-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
padPasswordCheckfromLogin, or a decoy fromSignUp, left every test passing. Login's compare now goes through the same seam.TestLoginAndSignupSpendEqualBcryptWorkrunsLoginandSignUpthemselves and checks that each path spends exactly one cost-12 check's worth of bcrypt work: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.
redirect_uriwere validated after the lookup, so an invalid value was reported only for a new email.redirect_uriwas even checked after the new user row was written, leaving it behind with no verification request.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) andmake lint(0 issues).