mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-05 08:03:01 +00:00
* fix(auth): enforce exact email matching and stored address on password reset Enforce exact normalized email comparison in Go in GetUniqueUserByEmail (and FillUserByEmail) without premature SQL LIMIT 2, preventing accent-insensitive database collations from matching look-alike email addresses. Send password reset emails, key reset codes, and build query-escaped reset URLs strictly using the stored normalized user email, revoking generated codes if delivery fails. Atomically verify and consume reset tokens in constant time via common.ConsumeCodeWithKey to prevent replay, and decode the reset payload via common.DecodeJson. Add regression tests covering look-alike email rejection, stored-email delivery, single-use token enforcement, failed delivery cleanup, and token expiry (OWASP ASVS v4.0.3 2.5.1/2.5.2/2.5.3/2.5.6). * fix(auth): preserve reset recipients and enforce exact email lookup at login
101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
package common
|
|
|
|
import (
|
|
"crypto/subtle"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type verificationValue struct {
|
|
code string
|
|
time time.Time
|
|
}
|
|
|
|
const (
|
|
EmailVerificationPurpose = "v"
|
|
PasswordResetPurpose = "r"
|
|
)
|
|
|
|
var verificationMutex sync.Mutex
|
|
var verificationMap map[string]verificationValue
|
|
var verificationMapMaxSize = 10
|
|
var VerificationValidMinutes = 10
|
|
|
|
func GenerateVerificationCode(length int) string {
|
|
code := uuid.New().String()
|
|
code = strings.Replace(code, "-", "", -1)
|
|
if length == 0 {
|
|
return code
|
|
}
|
|
return code[:length]
|
|
}
|
|
|
|
func RegisterVerificationCodeWithKey(key string, code string, purpose string) {
|
|
verificationMutex.Lock()
|
|
defer verificationMutex.Unlock()
|
|
verificationMap[purpose+key] = verificationValue{
|
|
code: code,
|
|
time: time.Now(),
|
|
}
|
|
if len(verificationMap) > verificationMapMaxSize {
|
|
removeExpiredPairs()
|
|
}
|
|
}
|
|
|
|
func VerifyCodeWithKey(key string, code string, purpose string) bool {
|
|
verificationMutex.Lock()
|
|
defer verificationMutex.Unlock()
|
|
value, okay := verificationMap[purpose+key]
|
|
now := time.Now()
|
|
if !okay || int(now.Sub(value.time).Seconds()) >= VerificationValidMinutes*60 {
|
|
return false
|
|
}
|
|
return subtle.ConstantTimeCompare([]byte(code), []byte(value.code)) == 1
|
|
}
|
|
|
|
// ConsumeCodeWithKey atomically verifies and removes a single-use code so
|
|
// concurrent requests cannot replay the same token.
|
|
func ConsumeCodeWithKey(key string, code string, purpose string) bool {
|
|
verificationMutex.Lock()
|
|
defer verificationMutex.Unlock()
|
|
mapKey := purpose + key
|
|
value, okay := verificationMap[mapKey]
|
|
now := time.Now()
|
|
if !okay || int(now.Sub(value.time).Seconds()) >= VerificationValidMinutes*60 {
|
|
if okay {
|
|
delete(verificationMap, mapKey)
|
|
}
|
|
return false
|
|
}
|
|
if subtle.ConstantTimeCompare([]byte(code), []byte(value.code)) != 1 {
|
|
return false
|
|
}
|
|
delete(verificationMap, mapKey)
|
|
return true
|
|
}
|
|
|
|
func DeleteKey(key string, purpose string) {
|
|
verificationMutex.Lock()
|
|
defer verificationMutex.Unlock()
|
|
delete(verificationMap, purpose+key)
|
|
}
|
|
|
|
// no lock inside, so the caller must lock the verificationMap before calling!
|
|
func removeExpiredPairs() {
|
|
now := time.Now()
|
|
for key := range verificationMap {
|
|
if int(now.Sub(verificationMap[key].time).Seconds()) >= VerificationValidMinutes*60 {
|
|
delete(verificationMap, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
verificationMutex.Lock()
|
|
defer verificationMutex.Unlock()
|
|
verificationMap = make(map[string]verificationValue)
|
|
}
|