-
Notifications
You must be signed in to change notification settings - Fork 673
fix: mitigate user enumeration vulnerability in recover endpoint #2549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jualhosting
wants to merge
7
commits into
supabase:master
Choose a base branch
from
Jualhosting:fix-user-enumeration-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
93e5501
fix: mitigate user enumeration vulnerability in recover endpoint
Jualhosting e54366c
fix: remove user-controlled aud from fake rate-limit cache key
Jualhosting 741c724
fix: use database-backed fake rate limiter to fix TOCTOU and distribu…
Jualhosting 223bf2e
fix: secure fake rate limits with HMAC-SHA256 and insert-first pattern
Jualhosting c864709
fix: mitigate timing attack on user enumeration by enforcing minimum …
Jualhosting 8ff1b77
fix: global min response time and domain-separated HMAC secret
Jualhosting 9e15977
fix: implement probabilistic cleanup for fake rate limits table
Jualhosting File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package utilities | ||
|
|
||
| import ( | ||
| "crypto/hmac" | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "math/rand" | ||
| "time" | ||
|
|
||
| "github.com/supabase/auth/internal/storage" | ||
| ) | ||
|
|
||
| type FakeRateLimit struct { | ||
| EmailHash string `db:"email_hash"` | ||
| LastRequestAt time.Time `db:"last_request_at"` | ||
| } | ||
|
|
||
| // TableName returns the table name | ||
| func (FakeRateLimit) TableName() string { | ||
| return "fake_rate_limits" | ||
| } | ||
|
|
||
| // CheckFakeRateLimit simulates a rate limit check for a non-existent email. | ||
| // It returns the timestamp of the last request if it was rate limited, or nil if not. | ||
| func CheckFakeRateLimit(db *storage.Connection, email string, frequency time.Duration, secret []byte) *time.Time { | ||
| h := hmac.New(sha256.New, secret) | ||
| h.Write([]byte(email)) | ||
| hashStr := hex.EncodeToString(h.Sum(nil)) | ||
|
|
||
| var lastReq *time.Time | ||
| _ = db.Transaction(func(tx *storage.Connection) error { | ||
| // Pre-insert a sentinel row so the row always exists before we lock it. | ||
| // This prevents two concurrent first-requests from both racing past FOR UPDATE. | ||
| epoch := time.Unix(0, 0).UTC() | ||
| _ = tx.RawQuery(`INSERT INTO fake_rate_limits (email_hash, last_request_at) VALUES (?, ?) ON CONFLICT DO NOTHING`, hashStr, epoch).Exec() | ||
|
|
||
| // Lock the now-guaranteed-existing row | ||
| existing := &FakeRateLimit{} | ||
| if err := tx.RawQuery(`SELECT last_request_at FROM fake_rate_limits WHERE email_hash = ? FOR UPDATE`, hashStr).First(existing); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| now := time.Now() | ||
| if now.Sub(existing.LastRequestAt) < frequency { | ||
| // Rate limited! | ||
| last := existing.LastRequestAt | ||
| lastReq = &last | ||
| return nil | ||
| } | ||
| // Not rate limited, update the timestamp | ||
| _ = tx.RawQuery(`UPDATE fake_rate_limits SET last_request_at = ? WHERE email_hash = ?`, now, hashStr).Exec() | ||
| return nil | ||
| }) | ||
|
|
||
| // Probabilistic cleanup (10% chance) to prevent table unbounded growth | ||
| if rand.Intn(10) == 0 { | ||
| go CleanupFakeRateLimitCache(db, frequency) | ||
| } | ||
|
|
||
| return lastReq | ||
| } | ||
|
|
||
| // CleanupFakeRateLimitCache removes expired entries from the cache. | ||
| // Call this periodically or when necessary to prevent unbounded memory growth. | ||
| func CleanupFakeRateLimitCache(db *storage.Connection, frequency time.Duration) { | ||
| _ = db.RawQuery( | ||
| `DELETE FROM fake_rate_limits WHERE EXTRACT(EPOCH FROM (NOW() - last_request_at)) > ?`, | ||
| frequency.Seconds(), | ||
| ).Exec() | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| CREATE TABLE IF NOT EXISTS fake_rate_limits ( | ||
| email_hash VARCHAR(64) PRIMARY KEY, | ||
| last_request_at TIMESTAMP WITH TIME ZONE NOT NULL | ||
| ); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/go-jose/go-jose/v3" | ||
| ) | ||
|
|
||
| func main() { | ||
| jwksStr := `{"keys":[{"kty":"EC","crv":"secp256k1","x":"1","y":"2"}]}` | ||
| var jwks jose.JSONWebKeySet | ||
| err := jwks.UnmarshalJSON([]byte(jwksStr)) | ||
| if err != nil { | ||
| fmt.Println("Error:", err) | ||
| return | ||
| } | ||
| fmt.Println("Parsed:", len(jwks.Keys)) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.