registration_email_alias.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package service
  2. import (
  3. "context"
  4. "strings"
  5. )
  6. // Email alias normalization for registration dedup.
  7. //
  8. // Problem: registration duplicate checks compare the email verbatim (lowercase
  9. // + trim only), so a single real inbox can spawn unlimited accounts using
  10. // provider alias features:
  11. // - Plus addressing: user+tag@gmail.com is delivered to user@gmail.com
  12. // (supported by Gmail, Outlook/Hotmail, Yahoo, iCloud, Fastmail, and more).
  13. // - Gmail dot trick: u.s.e.r@gmail.com is delivered to user@gmail.com.
  14. // - FQDN root dot: user@gmail.com. is the absolute form of user@gmail.com,
  15. // passes the registration validator, and reaches the same mailbox.
  16. //
  17. // This lets abusers bulk-register accounts (e.g. to farm signup grants) while
  18. // the domain whitelist and email verification see each variant as a distinct,
  19. // deliverable address. NormalizeEmailForAliasDedup collapses these variants to
  20. // a single "inbox identity" so the registration path can reject duplicates.
  21. //
  22. // Normalization rules:
  23. // - All domains: lowercase, trim, drop the FQDN root dot, and strip the
  24. // local-part "+suffix". Stripping the plus suffix on domains that do not
  25. // support plus addressing is harmless — those exact addresses are virtually
  26. // never registered.
  27. // - Gmail family (gmail.com / googlemail.com): additionally remove dots from
  28. // the local part and fold the domain to gmail.com.
  29. //
  30. // This only affects registration duplicate detection. It intentionally does not
  31. // change how emails are stored, displayed, or used for login/delivery.
  32. var gmailFamilyDomains = map[string]struct{}{
  33. "gmail.com": {},
  34. "googlemail.com": {},
  35. }
  36. // NormalizeEmailForAliasDedup returns the canonical "inbox identity" of an
  37. // email. Malformed input is returned lowercased/trimmed unchanged; format
  38. // validation is the caller's responsibility.
  39. func NormalizeEmailForAliasDedup(email string) string {
  40. local, domain, ok := splitEmailForAliasDedup(email)
  41. if !ok {
  42. return strings.ToLower(strings.TrimSpace(email))
  43. }
  44. local = stripEmailPlusSuffix(local)
  45. if isGmailFamilyDomain(domain) {
  46. local = stripEmailLocalDots(local)
  47. domain = "gmail.com"
  48. }
  49. return local + "@" + domain
  50. }
  51. // EmailAliasProbe describes a shape a stored duplicate can have, expressed on the
  52. // dot-stripped email form used by UserRepository.ExistsByEmailAlias: Local is the
  53. // plus-stripped and dot-stripped local part, Domain the dot-stripped candidate
  54. // domain. Dots are removed on both sides of that comparison so one probe per
  55. // domain also covers the Gmail dot trick and the FQDN root dot. The over-matching
  56. // this introduces (dots stay significant outside the Gmail family) is filtered by
  57. // re-checking every candidate with NormalizeEmailForAliasDedup.
  58. type EmailAliasProbe struct {
  59. Local string
  60. Domain string
  61. }
  62. // EmailAliasDedupProbes returns the probes covering every stored address that
  63. // could resolve to the same inbox as email: gmail-family domains are mutual
  64. // aliases, every other domain only collides with itself. It returns nil when
  65. // there is nothing to probe (malformed address, or a local part made of dots
  66. // only, which no provider delivers).
  67. func EmailAliasDedupProbes(email string) []EmailAliasProbe {
  68. local, domain, ok := splitEmailForAliasDedup(email)
  69. if !ok {
  70. return nil
  71. }
  72. probeLocal := strings.ReplaceAll(stripEmailPlusSuffix(local), ".", "")
  73. if probeLocal == "" {
  74. return nil
  75. }
  76. domains := []string{domain}
  77. if isGmailFamilyDomain(domain) {
  78. domains = []string{"gmail.com", "googlemail.com"}
  79. }
  80. probes := make([]EmailAliasProbe, 0, len(domains))
  81. for _, candidate := range domains {
  82. probes = append(probes, EmailAliasProbe{
  83. Local: probeLocal,
  84. Domain: strings.ReplaceAll(candidate, ".", ""),
  85. })
  86. }
  87. return probes
  88. }
  89. func splitEmailForAliasDedup(email string) (local string, domain string, ok bool) {
  90. local, domain, ok = splitEmailForPolicy(email)
  91. if !ok {
  92. return "", "", false
  93. }
  94. domain = strings.TrimRight(domain, ".")
  95. if domain == "" {
  96. return "", "", false
  97. }
  98. return local, domain, true
  99. }
  100. func stripEmailPlusSuffix(local string) string {
  101. // idx > 0 only: "+tag@host" has no local part left to keep, and folding every
  102. // "+x@host" into "@host" would lock unrelated senders out of that domain.
  103. if idx := strings.IndexByte(local, '+'); idx > 0 {
  104. return local[:idx]
  105. }
  106. return local
  107. }
  108. func stripEmailLocalDots(local string) string {
  109. if stripped := strings.ReplaceAll(local, ".", ""); stripped != "" {
  110. return stripped
  111. }
  112. return local
  113. }
  114. func isGmailFamilyDomain(domain string) bool {
  115. _, ok := gmailFamilyDomains[domain]
  116. return ok
  117. }
  118. // existsByEmailOrAlias reports whether an email — or any alias variant that
  119. // resolves to the same inbox — is already registered.
  120. //
  121. // It first performs the exact ExistsByEmail check, then, only on a miss, probes
  122. // for an alias collision. Consistent with ExistsByEmail, lookup errors are
  123. // surfaced (fail-closed) so the registration path returns a service error instead
  124. // of letting an attacker bypass the check by inducing errors.
  125. func (s *AuthService) existsByEmailOrAlias(ctx context.Context, email string) (bool, error) {
  126. exists, err := s.userRepo.ExistsByEmail(ctx, email)
  127. if err != nil || exists {
  128. return exists, err
  129. }
  130. return s.userRepo.ExistsByEmailAlias(ctx, email)
  131. }