fix: mitigate timing-based user enumeration in password grant - #2693
Open
Mahaveer1013 wants to merge 2 commits into
Open
fix: mitigate timing-based user enumeration in password grant#2693Mahaveer1013 wants to merge 2 commits into
Mahaveer1013 wants to merge 2 commits into
Conversation
ResourceOwnerPasswordGrant only ran a bcrypt comparison when a user was found and had a password set. The not-found and no-password branches returned immediately, making response time distinguishable from a real failed-password attempt (~75-80ms gap at default bcrypt cost), which discloses account existence. Generate a fixed decoy hash at startup (same cost as real password hashes) and compare against it on both short-circuit branches before returning the same invalid_credentials error, so timing no longer depends on whether the account exists. Closes supabase#2674 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Summary
ResourceOwnerPasswordGrantonly performed a bcrypt comparison when thelooked-up user existed and had a password set. The not-found and
no-password branches returned the
invalid_credentialserror immediately,with no comparable work — making response time distinguishable from a real
failed-password attempt (~75-80ms gap at default bcrypt cost measured
against a hosted project) and disclosing whether an account exists.
bcrypt cost used for real user passwords (
crypto.GenerateFromPassword,same call site pattern as
internal/models/user.go).compareDummyPasswordHash, which runs a bcrypt compare against thatdecoy hash and swallows the expected mismatch error.
ResourceOwnerPasswordGrant(user not found, and user found but has no password) before returning
the same
invalid_credentialserror, so timing no longer depends onwhich branch was taken.
No change to response status codes, error codes, or response bodies —
this only equalizes latency.
Closes #2674
Test plan
go build ./...gofmt -lclean on changed filesgo vet ./internal/api/...go test ./internal/api/... -run TestToken -v -count=1— all password grant tests pass, including newTestTokenPasswordGrantMissingUserChecksDummyPasswordHash, which corrupts the decoy hash and asserts the not-found branch now surfaces the comparison failure (500), proving the helper is actually wired into that branch.