mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-07 16:37:52 +08:00
对 #4814 的审计跟进修复: - 域名尾随点绕过:user@gmail.com. 的域名不在 gmail 家族名单内,点号折叠与 googlemail 归一被整体跳过,别名刷号原样可复现。归一化入口统一去掉 FQDN 根点。 - 误拒合法用户:剥 "+后缀" 缺空串守卫,+alice@ 与 +bob@ 都折叠成 @domain, 该域后续 "+x@" 注册会永久 EMAIL_EXISTS 且无自助恢复。改为仅当 "+" 不在首位时剥离。 - 无界不可索引全表扫描:原实现按 LOWER(email) LIKE '%@domain' 把整域邮箱读进内存, 且挂在公开未鉴权的 send-verify-code 上。改为按去点邮箱 REPLACE(LOWER(TRIM(email)), '.', '') 做等值 + "local+%@domain" 前缀探针并带 LIMIT, 新增同表达式的部分索引(migrations/190)。TRIM 口径与既有精确匹配一致, 历史带首尾空白的行同样命中;LIKE 元字符转义,% 与 _ 不会扩大匹配面。 - 并发竞态:注册改走 CreateWithEmailAliasGuard,在邮箱唯一性锁上追加收件箱身份锁并在 锁内复查,避免同一收件箱的多个别名变体同时通过服务层前置查重。管理员建号仍走 Create,不受别名限制。 - 能力断言静默 fail-open:别名查重方法上提到 UserRepository 端口(编译期强制), 移除可选接口类型断言与静默降级分支。 - OAuth 邮箱注册的两条建号路径(同样发放注册赠额)纳入同一查重口径;邮箱换绑/绑定 不纳入,否则用户把邮箱改成自己收件箱的别名会被误拒。
143 lines
5.2 KiB
Go
143 lines
5.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
)
|
|
|
|
// Email alias normalization for registration dedup.
|
|
//
|
|
// Problem: registration duplicate checks compare the email verbatim (lowercase
|
|
// + trim only), so a single real inbox can spawn unlimited accounts using
|
|
// provider alias features:
|
|
// - Plus addressing: user+tag@gmail.com is delivered to user@gmail.com
|
|
// (supported by Gmail, Outlook/Hotmail, Yahoo, iCloud, Fastmail, and more).
|
|
// - Gmail dot trick: u.s.e.r@gmail.com is delivered to user@gmail.com.
|
|
// - FQDN root dot: user@gmail.com. is the absolute form of user@gmail.com,
|
|
// passes the registration validator, and reaches the same mailbox.
|
|
//
|
|
// This lets abusers bulk-register accounts (e.g. to farm signup grants) while
|
|
// the domain whitelist and email verification see each variant as a distinct,
|
|
// deliverable address. NormalizeEmailForAliasDedup collapses these variants to
|
|
// a single "inbox identity" so the registration path can reject duplicates.
|
|
//
|
|
// Normalization rules:
|
|
// - All domains: lowercase, trim, drop the FQDN root dot, and strip the
|
|
// local-part "+suffix". Stripping the plus suffix on domains that do not
|
|
// support plus addressing is harmless — those exact addresses are virtually
|
|
// never registered.
|
|
// - Gmail family (gmail.com / googlemail.com): additionally remove dots from
|
|
// the local part and fold the domain to gmail.com.
|
|
//
|
|
// This only affects registration duplicate detection. It intentionally does not
|
|
// change how emails are stored, displayed, or used for login/delivery.
|
|
|
|
var gmailFamilyDomains = map[string]struct{}{
|
|
"gmail.com": {},
|
|
"googlemail.com": {},
|
|
}
|
|
|
|
// NormalizeEmailForAliasDedup returns the canonical "inbox identity" of an
|
|
// email. Malformed input is returned lowercased/trimmed unchanged; format
|
|
// validation is the caller's responsibility.
|
|
func NormalizeEmailForAliasDedup(email string) string {
|
|
local, domain, ok := splitEmailForAliasDedup(email)
|
|
if !ok {
|
|
return strings.ToLower(strings.TrimSpace(email))
|
|
}
|
|
local = stripEmailPlusSuffix(local)
|
|
if isGmailFamilyDomain(domain) {
|
|
local = stripEmailLocalDots(local)
|
|
domain = "gmail.com"
|
|
}
|
|
return local + "@" + domain
|
|
}
|
|
|
|
// EmailAliasProbe describes a shape a stored duplicate can have, expressed on the
|
|
// dot-stripped email form used by UserRepository.ExistsByEmailAlias: Local is the
|
|
// plus-stripped and dot-stripped local part, Domain the dot-stripped candidate
|
|
// domain. Dots are removed on both sides of that comparison so one probe per
|
|
// domain also covers the Gmail dot trick and the FQDN root dot. The over-matching
|
|
// this introduces (dots stay significant outside the Gmail family) is filtered by
|
|
// re-checking every candidate with NormalizeEmailForAliasDedup.
|
|
type EmailAliasProbe struct {
|
|
Local string
|
|
Domain string
|
|
}
|
|
|
|
// EmailAliasDedupProbes returns the probes covering every stored address that
|
|
// could resolve to the same inbox as email: gmail-family domains are mutual
|
|
// aliases, every other domain only collides with itself. It returns nil when
|
|
// there is nothing to probe (malformed address, or a local part made of dots
|
|
// only, which no provider delivers).
|
|
func EmailAliasDedupProbes(email string) []EmailAliasProbe {
|
|
local, domain, ok := splitEmailForAliasDedup(email)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
probeLocal := strings.ReplaceAll(stripEmailPlusSuffix(local), ".", "")
|
|
if probeLocal == "" {
|
|
return nil
|
|
}
|
|
domains := []string{domain}
|
|
if isGmailFamilyDomain(domain) {
|
|
domains = []string{"gmail.com", "googlemail.com"}
|
|
}
|
|
probes := make([]EmailAliasProbe, 0, len(domains))
|
|
for _, candidate := range domains {
|
|
probes = append(probes, EmailAliasProbe{
|
|
Local: probeLocal,
|
|
Domain: strings.ReplaceAll(candidate, ".", ""),
|
|
})
|
|
}
|
|
return probes
|
|
}
|
|
|
|
func splitEmailForAliasDedup(email string) (local string, domain string, ok bool) {
|
|
local, domain, ok = splitEmailForPolicy(email)
|
|
if !ok {
|
|
return "", "", false
|
|
}
|
|
domain = strings.TrimRight(domain, ".")
|
|
if domain == "" {
|
|
return "", "", false
|
|
}
|
|
return local, domain, true
|
|
}
|
|
|
|
func stripEmailPlusSuffix(local string) string {
|
|
// idx > 0 only: "+tag@host" has no local part left to keep, and folding every
|
|
// "+x@host" into "@host" would lock unrelated senders out of that domain.
|
|
if idx := strings.IndexByte(local, '+'); idx > 0 {
|
|
return local[:idx]
|
|
}
|
|
return local
|
|
}
|
|
|
|
func stripEmailLocalDots(local string) string {
|
|
if stripped := strings.ReplaceAll(local, ".", ""); stripped != "" {
|
|
return stripped
|
|
}
|
|
return local
|
|
}
|
|
|
|
func isGmailFamilyDomain(domain string) bool {
|
|
_, ok := gmailFamilyDomains[domain]
|
|
return ok
|
|
}
|
|
|
|
// existsByEmailOrAlias reports whether an email — or any alias variant that
|
|
// resolves to the same inbox — is already registered.
|
|
//
|
|
// It first performs the exact ExistsByEmail check, then, only on a miss, probes
|
|
// for an alias collision. Consistent with ExistsByEmail, lookup errors are
|
|
// surfaced (fail-closed) so the registration path returns a service error instead
|
|
// of letting an attacker bypass the check by inducing errors.
|
|
func (s *AuthService) existsByEmailOrAlias(ctx context.Context, email string) (bool, error) {
|
|
exists, err := s.userRepo.ExistsByEmail(ctx, email)
|
|
if err != nil || exists {
|
|
return exists, err
|
|
}
|
|
return s.userRepo.ExistsByEmailAlias(ctx, email)
|
|
}
|