fix: prevent reuse of team password-reset links
Consume the specific reset OTP atomically before updating the password, and reject already-used codes. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
4f6c356b6c
commit
89c91484d1
|
|
@ -16,7 +16,26 @@ WHERE team_member_id = $1
|
|||
ORDER BY id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: MarkTeamOtpAsUsed :exec
|
||||
-- name: GetTeamResetOtpByCode :one
|
||||
SELECT id, team_member_id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at
|
||||
FROM team_otps
|
||||
WHERE team_member_id = $1
|
||||
AND otp = $2
|
||||
AND otp_for = 'reset'
|
||||
ORDER BY id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- Consume a reset OTP exactly once. Returns 0 rows if already used or missing.
|
||||
-- name: MarkTeamOtpAsUsed :execrows
|
||||
UPDATE team_otps
|
||||
SET used = TRUE, used_at = $2
|
||||
WHERE id = $1;
|
||||
WHERE id = $1
|
||||
AND used = FALSE;
|
||||
|
||||
-- Invalidate any other unused reset OTPs for the member after a successful reset.
|
||||
-- name: InvalidateUnusedTeamResetOtps :exec
|
||||
UPDATE team_otps
|
||||
SET used = TRUE, used_at = CURRENT_TIMESTAMP
|
||||
WHERE team_member_id = $1
|
||||
AND otp_for = 'reset'
|
||||
AND used = FALSE;
|
||||
|
|
|
|||
|
|
@ -83,10 +83,71 @@ func (q *Queries) GetLatestTeamOtp(ctx context.Context, teamMemberID int64) (Get
|
|||
return i, err
|
||||
}
|
||||
|
||||
const MarkTeamOtpAsUsed = `-- name: MarkTeamOtpAsUsed :exec
|
||||
const GetTeamResetOtpByCode = `-- name: GetTeamResetOtpByCode :one
|
||||
SELECT id, team_member_id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at
|
||||
FROM team_otps
|
||||
WHERE team_member_id = $1
|
||||
AND otp = $2
|
||||
AND otp_for = 'reset'
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetTeamResetOtpByCodeParams struct {
|
||||
TeamMemberID int64 `json:"team_member_id"`
|
||||
Otp string `json:"otp"`
|
||||
}
|
||||
|
||||
type GetTeamResetOtpByCodeRow struct {
|
||||
ID int64 `json:"id"`
|
||||
TeamMemberID int64 `json:"team_member_id"`
|
||||
SentTo string `json:"sent_to"`
|
||||
Medium string `json:"medium"`
|
||||
OtpFor string `json:"otp_for"`
|
||||
Otp string `json:"otp"`
|
||||
Used bool `json:"used"`
|
||||
UsedAt pgtype.Timestamptz `json:"used_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetTeamResetOtpByCode(ctx context.Context, arg GetTeamResetOtpByCodeParams) (GetTeamResetOtpByCodeRow, error) {
|
||||
row := q.db.QueryRow(ctx, GetTeamResetOtpByCode, arg.TeamMemberID, arg.Otp)
|
||||
var i GetTeamResetOtpByCodeRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.TeamMemberID,
|
||||
&i.SentTo,
|
||||
&i.Medium,
|
||||
&i.OtpFor,
|
||||
&i.Otp,
|
||||
&i.Used,
|
||||
&i.UsedAt,
|
||||
&i.CreatedAt,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const InvalidateUnusedTeamResetOtps = `-- name: InvalidateUnusedTeamResetOtps :exec
|
||||
UPDATE team_otps
|
||||
SET used = TRUE, used_at = CURRENT_TIMESTAMP
|
||||
WHERE team_member_id = $1
|
||||
AND otp_for = 'reset'
|
||||
AND used = FALSE
|
||||
`
|
||||
|
||||
// Invalidate any other unused reset OTPs for the member after a successful reset.
|
||||
func (q *Queries) InvalidateUnusedTeamResetOtps(ctx context.Context, teamMemberID int64) error {
|
||||
_, err := q.db.Exec(ctx, InvalidateUnusedTeamResetOtps, teamMemberID)
|
||||
return err
|
||||
}
|
||||
|
||||
const MarkTeamOtpAsUsed = `-- name: MarkTeamOtpAsUsed :execrows
|
||||
UPDATE team_otps
|
||||
SET used = TRUE, used_at = $2
|
||||
WHERE id = $1
|
||||
AND used = FALSE
|
||||
`
|
||||
|
||||
type MarkTeamOtpAsUsedParams struct {
|
||||
|
|
@ -94,7 +155,11 @@ type MarkTeamOtpAsUsedParams struct {
|
|||
UsedAt pgtype.Timestamptz `json:"used_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) MarkTeamOtpAsUsed(ctx context.Context, arg MarkTeamOtpAsUsedParams) error {
|
||||
_, err := q.db.Exec(ctx, MarkTeamOtpAsUsed, arg.ID, arg.UsedAt)
|
||||
return err
|
||||
// Consume a reset OTP exactly once. Returns 0 rows if already used or missing.
|
||||
func (q *Queries) MarkTeamOtpAsUsed(ctx context.Context, arg MarkTeamOtpAsUsedParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, MarkTeamOtpAsUsed, arg.ID, arg.UsedAt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,5 +51,9 @@ type TeamStore interface {
|
|||
|
||||
CreateTeamOtp(ctx context.Context, otp domain.TeamOtp) error
|
||||
GetLatestTeamOtp(ctx context.Context, teamMemberID int64) (domain.TeamOtp, error)
|
||||
GetTeamResetOtpByCode(ctx context.Context, teamMemberID int64, otpCode string) (domain.TeamOtp, error)
|
||||
// MarkTeamOtpAsUsed marks the OTP used only if it is still unused.
|
||||
// Returns domain.ErrOtpAlreadyUsed when the row was already consumed.
|
||||
MarkTeamOtpAsUsed(ctx context.Context, otp domain.TeamOtp) error
|
||||
InvalidateUnusedTeamResetOtps(ctx context.Context, teamMemberID int64) error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -542,28 +542,87 @@ func (s *Store) GetLatestTeamOtp(ctx context.Context, teamMemberID int64) (domai
|
|||
if !row.ExpiresAt.Valid {
|
||||
return domain.TeamOtp{}, domain.ErrOtpNotFound
|
||||
}
|
||||
return mapTeamOtpRow(
|
||||
row.ID,
|
||||
row.TeamMemberID,
|
||||
row.SentTo,
|
||||
row.Medium,
|
||||
row.OtpFor,
|
||||
row.Otp,
|
||||
row.Used,
|
||||
row.UsedAt,
|
||||
row.CreatedAt,
|
||||
row.ExpiresAt,
|
||||
), nil
|
||||
}
|
||||
|
||||
func (s *Store) GetTeamResetOtpByCode(ctx context.Context, teamMemberID int64, otpCode string) (domain.TeamOtp, error) {
|
||||
row, err := s.queries.GetTeamResetOtpByCode(ctx, dbgen.GetTeamResetOtpByCodeParams{
|
||||
TeamMemberID: teamMemberID,
|
||||
Otp: otpCode,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.TeamOtp{}, domain.ErrOtpNotFound
|
||||
}
|
||||
return domain.TeamOtp{}, err
|
||||
}
|
||||
if !row.ExpiresAt.Valid {
|
||||
return domain.TeamOtp{}, domain.ErrOtpNotFound
|
||||
}
|
||||
return mapTeamOtpRow(
|
||||
row.ID,
|
||||
row.TeamMemberID,
|
||||
row.SentTo,
|
||||
row.Medium,
|
||||
row.OtpFor,
|
||||
row.Otp,
|
||||
row.Used,
|
||||
row.UsedAt,
|
||||
row.CreatedAt,
|
||||
row.ExpiresAt,
|
||||
), nil
|
||||
}
|
||||
|
||||
func mapTeamOtpRow(
|
||||
id, teamMemberID int64,
|
||||
sentTo, medium, otpFor, otp string,
|
||||
used bool,
|
||||
usedAt, createdAt, expiresAt pgtype.Timestamptz,
|
||||
) domain.TeamOtp {
|
||||
return domain.TeamOtp{
|
||||
ID: row.ID,
|
||||
TeamMemberID: row.TeamMemberID,
|
||||
SentTo: row.SentTo,
|
||||
Medium: domain.OtpMedium(row.Medium),
|
||||
For: domain.OtpFor(row.OtpFor),
|
||||
Otp: row.Otp,
|
||||
Used: row.Used,
|
||||
UsedAt: row.UsedAt.Time,
|
||||
CreatedAt: row.CreatedAt.Time,
|
||||
ExpiresAt: row.ExpiresAt.Time,
|
||||
}, nil
|
||||
ID: id,
|
||||
TeamMemberID: teamMemberID,
|
||||
SentTo: sentTo,
|
||||
Medium: domain.OtpMedium(medium),
|
||||
For: domain.OtpFor(otpFor),
|
||||
Otp: otp,
|
||||
Used: used,
|
||||
UsedAt: usedAt.Time,
|
||||
CreatedAt: createdAt.Time,
|
||||
ExpiresAt: expiresAt.Time,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) MarkTeamOtpAsUsed(ctx context.Context, otp domain.TeamOtp) error {
|
||||
return s.queries.MarkTeamOtpAsUsed(ctx, dbgen.MarkTeamOtpAsUsedParams{
|
||||
rows, err := s.queries.MarkTeamOtpAsUsed(ctx, dbgen.MarkTeamOtpAsUsedParams{
|
||||
ID: otp.ID,
|
||||
UsedAt: pgtype.Timestamptz{
|
||||
Time: otp.UsedAt,
|
||||
Valid: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rows == 0 {
|
||||
return domain.ErrOtpAlreadyUsed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) InvalidateUnusedTeamResetOtps(ctx context.Context, teamMemberID int64) error {
|
||||
return s.queries.InvalidateUnusedTeamResetOtps(ctx, teamMemberID)
|
||||
}
|
||||
|
||||
func optionalPgText(value string) pgtype.Text {
|
||||
|
|
|
|||
|
|
@ -65,12 +65,13 @@ func (s *Service) SendPasswordReset(ctx context.Context, email string) error {
|
|||
|
||||
func (s *Service) ResetPasswordWithOtp(ctx context.Context, req domain.TeamResetPasswordReq) error {
|
||||
email := strings.TrimSpace(strings.ToLower(req.Email))
|
||||
otpCode := strings.TrimSpace(req.Otp)
|
||||
member, err := s.teamStore.GetTeamMemberByEmail(ctx, email)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
otp, err := s.teamStore.GetLatestTeamOtp(ctx, member.ID)
|
||||
otp, err := s.teamStore.GetTeamResetOtpByCode(ctx, member.ID, otpCode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -84,10 +85,18 @@ func (s *Service) ResetPasswordWithOtp(ctx context.Context, req domain.TeamReset
|
|||
if time.Now().After(otp.ExpiresAt) {
|
||||
return domain.ErrOtpExpired
|
||||
}
|
||||
if subtle.ConstantTimeCompare([]byte(otp.Otp), []byte(strings.TrimSpace(req.Otp))) != 1 {
|
||||
if subtle.ConstantTimeCompare([]byte(otp.Otp), []byte(otpCode)) != 1 {
|
||||
return domain.ErrInvalidOtp
|
||||
}
|
||||
|
||||
// Consume the OTP before updating the password so the same link cannot be replayed,
|
||||
// even if a later step fails.
|
||||
otp.UsedAt = time.Now()
|
||||
if err := s.teamStore.MarkTeamOtpAsUsed(ctx, otp); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = s.teamStore.InvalidateUnusedTeamResetOtps(ctx, member.ID)
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcryptCost)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -97,11 +106,6 @@ func (s *Service) ResetPasswordWithOtp(ctx context.Context, req domain.TeamReset
|
|||
return err
|
||||
}
|
||||
|
||||
otp.UsedAt = time.Now()
|
||||
if err := s.teamStore.MarkTeamOtpAsUsed(ctx, otp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = s.teamStore.RevokeAllActiveTeamRefreshTokensForMember(ctx, member.ID)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user