| index 1fa768316..974227908 100644 |
| --- a/internal/console/emailsignin_store.go |
| +++ b/internal/console/emailsignin_store.go |
| @@ -72,11 +72,31 @@ func (s *Store) migrateEmailSignin() error { |
| next TEXT NOT NULL DEFAULT '', |
| attempts INTEGER NOT NULL DEFAULT 0, |
| created_at INTEGER NOT NULL, |
| - expires_at INTEGER NOT NULL |
| + expires_at INTEGER NOT NULL, |
| + alias_hashes TEXT NOT NULL DEFAULT '' |
| );`) |
| - return err |
| + if err != nil { |
| + return err |
| + } |
| + // Additive: the re-keyed ids (RekeyEmailChallenge). Forgiving "duplicate |
| + // column", so re-running is always safe. |
| + if _, err := s.db.Exec(`ALTER TABLE email_challenges ADD COLUMN alias_hashes TEXT NOT NULL DEFAULT ''`); err != nil && |
| + !strings.Contains(err.Error(), "duplicate column") { |
| + return err |
| + } |
| + return nil |
| } |
| |
| +// A challenge answers to its first browser-held id (id_hash) and to every id |
| +// RekeyEmailChallenge has since given it (alias_hashes, space-separated, at |
| +// most maxChallengeAliases, newest last), all until it expires. Hashes are |
| +// fixed-length hex, so a substring match is a whole-entry match. |
| +// challengeByID is the WHERE clause for that; it takes the id's hash twice. |
| +const ( |
| + challengeByID = `(id_hash = ? OR instr(alias_hashes, ?) > 0)` |
| + maxChallengeAliases = 8 |
| +) |
| + |
| // codeMAC is epoch ":" hex(HMAC-SHA256(process key, link_hash ":" code)). The |
| // row's link hash binds the code to its challenge and, unlike the |
| // browser-held id, never changes, so RekeyEmailChallenge can rotate the id |
| @@ -115,7 +135,8 @@ func (s *Store) CreateEmailChallenge(c EmailChallenge, replaceID string, now tim |
| } |
| defer tx.Rollback() |
| if replaceID != "" { |
| - if _, err := tx.Exec(`DELETE FROM email_challenges WHERE id_hash = ?`, hashToken(replaceID)); err != nil { |
| + h := hashToken(replaceID) |
| + if _, err := tx.Exec(`DELETE FROM email_challenges WHERE `+challengeByID, h, h); err != nil { |
| return "", "", "", err |
| } |
| } |
| @@ -151,8 +172,9 @@ func (s *Store) PeekEmailChallenge(id string, now time.Time) (*EmailChallenge, b |
| if id == "" { |
| return nil, false |
| } |
| + h := hashToken(id) |
| c, exp, err := scanEmailChallenge(s.db.QueryRow(`SELECT purpose, address, session_hash, pending, next, expires_at |
| -FROM email_challenges WHERE id_hash = ?`, hashToken(id))) |
| +FROM email_challenges WHERE `+challengeByID, h, h)) |
| if err != nil || now.Unix() > exp { |
| return nil, false |
| } |
| @@ -203,7 +225,7 @@ func (s *Store) TakeEmailCode(id, code, purpose string, now time.Time) (*EmailCh |
| var mac, linkHash string |
| var attempts int |
| row := tx.QueryRow(`SELECT purpose, address, session_hash, pending, next, expires_at, code_mac, link_hash, attempts |
| -FROM email_challenges WHERE id_hash = ?`, idHash) |
| +FROM email_challenges WHERE `+challengeByID, idHash, idHash) |
| var c EmailChallenge |
| var exp int64 |
| if err := row.Scan(&c.Purpose, &c.Address, &c.SessionHash, &c.PendingHash, &c.Next, &exp, &mac, &linkHash, &attempts); err != nil { |
| @@ -211,7 +233,7 @@ FROM email_challenges WHERE id_hash = ?`, idHash) |
| } |
| c.ExpiresAt = time.Unix(exp, 0) |
| if now.Unix() > exp || c.Purpose != purpose { |
| - if _, err := tx.Exec(`DELETE FROM email_challenges WHERE id_hash = ?`, idHash); err != nil { |
| + if _, err := tx.Exec(`DELETE FROM email_challenges WHERE link_hash = ?`, linkHash); err != nil { |
| return nil, CodeGone |
| } |
| if err := tx.Commit(); err != nil { |
| @@ -229,9 +251,9 @@ FROM email_challenges WHERE id_hash = ?`, idHash) |
| res := CodeWrong |
| if attempts >= emailCodeAttempts { |
| res = CodeBurned |
| - _, err = tx.Exec(`DELETE FROM email_challenges WHERE id_hash = ?`, idHash) |
| + _, err = tx.Exec(`DELETE FROM email_challenges WHERE link_hash = ?`, linkHash) |
| } else { |
| - _, err = tx.Exec(`UPDATE email_challenges SET attempts = ? WHERE id_hash = ?`, attempts, idHash) |
| + _, err = tx.Exec(`UPDATE email_challenges SET attempts = ? WHERE link_hash = ?`, attempts, linkHash) |
| } |
| if err != nil { |
| return nil, CodeGone |
| @@ -241,7 +263,7 @@ FROM email_challenges WHERE id_hash = ?`, idHash) |
| } |
| return nil, res |
| } |
| - if _, err := tx.Exec(`DELETE FROM email_challenges WHERE id_hash = ?`, idHash); err != nil { |
| + if _, err := tx.Exec(`DELETE FROM email_challenges WHERE link_hash = ?`, linkHash); err != nil { |
| return nil, CodeGone |
| } |
| if err := tx.Commit(); err != nil { |
| @@ -251,10 +273,13 @@ FROM email_challenges WHERE id_hash = ?`, idHash) |
| } |
| |
| // RekeyEmailChallenge gives a live sign-in challenge for address a new |
| -// browser-held id, in one transaction, and returns it. The emailed link and |
| -// code are unchanged and keep working. It reports false, changing nothing, |
| -// when oldID names no such challenge (unknown, expired, another purpose or |
| -// another address). |
| +// browser-held id, in one transaction, and returns it. The id it was asked |
| +// with, and every earlier one, keep working until the challenge expires, so |
| +// two overlapping retries from one browser each get an id that works, |
| +// whichever response lands last; only the newest maxChallengeAliases re-keyed |
| +// ids are kept. The emailed link and code are unchanged. It reports false, |
| +// changing nothing, when oldID names no such challenge (unknown, expired, |
| +// another purpose or another address). |
| func (s *Store) RekeyEmailChallenge(oldID, address string, now time.Time) (string, bool) { |
| if oldID == "" { |
| return "", false |
| @@ -268,13 +293,20 @@ func (s *Store) RekeyEmailChallenge(oldID, address string, now time.Time) (strin |
| return "", false |
| } |
| defer tx.Rollback() |
| - res, err := tx.Exec(`UPDATE email_challenges SET id_hash = ? |
| -WHERE id_hash = ? AND purpose = ? AND address = ? AND expires_at >= ?`, |
| - hashToken(newID), hashToken(oldID), ChallengeSignIn, address, now.Unix()) |
| + h := hashToken(oldID) |
| + var linkHash, aliases string |
| + err = tx.QueryRow(`SELECT link_hash, alias_hashes FROM email_challenges |
| +WHERE `+challengeByID+` AND purpose = ? AND address = ? AND expires_at >= ?`, |
| + h, h, ChallengeSignIn, address, now.Unix()).Scan(&linkHash, &aliases) |
| if err != nil { |
| return "", false |
| } |
| - if n, err := res.RowsAffected(); err != nil || n != 1 { |
| + list := append(strings.Fields(aliases), hashToken(newID)) |
| + if len(list) > maxChallengeAliases { |
| + list = list[len(list)-maxChallengeAliases:] |
| + } |
| + if _, err := tx.Exec(`UPDATE email_challenges SET alias_hashes = ? WHERE link_hash = ?`, |
| + strings.Join(list, " "), linkHash); err != nil { |
| return "", false |
| } |
| if err := tx.Commit(); err != nil { |
| @@ -286,7 +318,8 @@ WHERE id_hash = ? AND purpose = ? AND address = ? AND expires_at >= ?`, |
| // DeleteEmailChallenge removes a challenge by its browser-held id (for |
| // example when the email it belongs to could not be sent). |
| func (s *Store) DeleteEmailChallenge(id string) error { |
| - _, err := s.db.Exec(`DELETE FROM email_challenges WHERE id_hash = ?`, hashToken(id)) |
| + h := hashToken(id) |
| + _, err := s.db.Exec(`DELETE FROM email_challenges WHERE `+challengeByID, h, h) |
| return err |
| } |
| |