From 5c06299e8ffcc0b4ecad1c211c301c8dfd2eeaa9 Mon Sep 17 00:00:00 2001 From: iamxvbaba <28732408+iamxvbaba@users.noreply.github.com> Date: Fri, 21 Aug 2026 23:26:38 +0800 Subject: [PATCH] fix(auth): sync canonicalize phone identities as E.164 --- ...2_phone_identity_canonicalization.down.sql | 3 + ...182_phone_identity_canonicalization.up.sql | 5 + go.mod | 2 + go.sum | 4 + internal/app/account/phone_change_test.go | 20 +++ internal/app/auth/otp_delivery_test.go | 26 +++ internal/app/auth/service_test.go | 49 ++++++ internal/app/contacts/service.go | 19 ++- internal/app/contacts/service_test.go | 30 ++++ internal/app/users/service.go | 34 ++-- internal/app/users/service_test.go | 51 ++++++ internal/domain/account.go | 77 ++++++--- internal/domain/phone_identity_test.go | 42 +++++ internal/domain/system.go | 5 +- internal/rpc/auth_code_rate_limit_test.go | 20 +++ internal/store/code.go | 7 +- .../postgres/phone_identity_migration.go | 150 ++++++++++++++++++ ...one_identity_migration_integration_test.go | 97 +++++++++++ internal/store/postgres/postgres.go | 49 ++++++ ...ft_lifecycle_migration_integration_test.go | 4 +- 20 files changed, 644 insertions(+), 50 deletions(-) create mode 100644 deploy/migrations/0182_phone_identity_canonicalization.down.sql create mode 100644 deploy/migrations/0182_phone_identity_canonicalization.up.sql create mode 100644 internal/domain/phone_identity_test.go create mode 100644 internal/store/postgres/phone_identity_migration.go create mode 100644 internal/store/postgres/phone_identity_migration_integration_test.go diff --git a/deploy/migrations/0182_phone_identity_canonicalization.down.sql b/deploy/migrations/0182_phone_identity_canonicalization.down.sql new file mode 100644 index 00000000..0380d647 --- /dev/null +++ b/deploy/migrations/0182_phone_identity_canonicalization.down.sql @@ -0,0 +1,3 @@ +-- E.164 canonicalization is intentionally irreversible: the removed national +-- trunk presentation is not part of the account identity. +SELECT 1; diff --git a/deploy/migrations/0182_phone_identity_canonicalization.up.sql b/deploy/migrations/0182_phone_identity_canonicalization.up.sql new file mode 100644 index 00000000..9bb280ea --- /dev/null +++ b/deploy/migrations/0182_phone_identity_canonicalization.up.sql @@ -0,0 +1,5 @@ +-- The country-aware data rewrite is executed transactionally by +-- postgres.canonicalizeStoredPhoneIdentities before this durable version marker. +-- Keeping it in Go avoids incorrect SQL-only stripping of significant leading +-- zeroes (for example Italian fixed-line numbers). +SELECT 1; diff --git a/go.mod b/go.mod index ebfec292..52fb0342 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/jackc/pgx/v5 v5.9.2 github.com/lestrrat-go/jwx/v3 v3.1.1 github.com/minio/minio-go/v7 v7.2.1 + github.com/nyaruka/phonenumbers v1.8.1 github.com/pion/datachannel v1.6.2 github.com/pion/dtls/v3 v3.1.5 github.com/pion/ice/v4 v4.3.0 @@ -98,6 +99,7 @@ require ( golang.org/x/mod v0.38.0 // indirect golang.org/x/time v0.14.0 // indirect golang.org/x/tools v0.48.0 // indirect + google.golang.org/protobuf v1.36.11 // indirect gopkg.in/ini.v1 v1.67.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect rsc.io/qr v0.2.0 // indirect diff --git a/go.sum b/go.sum index 54be9510..a8b4d875 100644 --- a/go.sum +++ b/go.sum @@ -141,6 +141,8 @@ github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/nyaruka/phonenumbers v1.8.1 h1:2K9YMQuv1dCGqjjzB1DwmdCe89khT4KPBQb2CxAMMlU= +github.com/nyaruka/phonenumbers v1.8.1/go.mod h1:fsKPJ70O9JetEA4ggnJadYTFWwtGPvu/lETTXNXq6Cs= github.com/ogen-go/ogen v1.23.0 h1:QaWeKm2KZ2zy7NkqqO1Vdl5idNqlG+svxdgwVAX+zbo= github.com/ogen-go/ogen v1.23.0/go.mod h1:bwwvC3AmCV+LrL5lazyQwwof90402mdcSyI0FOzzpfM= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= @@ -274,6 +276,8 @@ golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI= golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE= golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk= +google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= +google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/internal/app/account/phone_change_test.go b/internal/app/account/phone_change_test.go index 243fbd7d..bbd7ce4c 100644 --- a/internal/app/account/phone_change_test.go +++ b/internal/app/account/phone_change_test.go @@ -194,6 +194,26 @@ func TestPhoneChangeRejectsOccupiedAndCrossAuthCode(t *testing.T) { } } +func TestPhoneChangeRejectsOccupiedNationalTrunkVariant(t *testing.T) { + f := newPhoneChangeFixture(t) + occupied, err := f.users.Create(f.ctx, domain.User{AccessHash: 103, Phone: "989981679461", FirstName: "Iran"}) + if err != nil { + t.Fatalf("create occupied user: %v", err) + } + if _, _, err := f.service.SendChangePhoneCode( + f.ctx, + f.user.ID, + f.authKeyID, + 77, + "+98 0998 167 9461", + ); !errors.Is(err, domain.ErrPhoneNumberOccupied) { + t.Fatalf("occupied trunk variant err = %v", err) + } + if got, found, err := f.users.ByPhone(f.ctx, "989981679461"); err != nil || !found || got.ID != occupied.ID { + t.Fatalf("canonical owner user=%+v found=%v err=%v", got, found, err) + } +} + func TestPhoneChangeWrongCodeExhaustsAttempts(t *testing.T) { f := newPhoneChangeFixture(t) hash, _, err := f.service.SendChangePhoneCode(f.ctx, f.user.ID, f.authKeyID, 77, "15550012005") diff --git a/internal/app/auth/otp_delivery_test.go b/internal/app/auth/otp_delivery_test.go index 360d5eb8..49ad3e26 100644 --- a/internal/app/auth/otp_delivery_test.go +++ b/internal/app/auth/otp_delivery_test.go @@ -73,6 +73,32 @@ func TestWebhookPhoneLoginUsesRandomSMSCode(t *testing.T) { } } +func TestWebhookPhoneLoginCanonicalizesNationalTrunkBeforeOTP(t *testing.T) { + ctx := context.Background() + sender := &captureOTPSender{} + svc := NewService( + memory.NewUserStore(), + memory.NewAuthorizationStore(), + memory.NewCodeStore(), + nil, + nil, + "fixed-code-must-not-leak", + WithPhoneCodeDelivery(sender, 6), + ) + + hash, err := svc.SendCode(ctx, "+98 0998 167 9461") + if err != nil { + t.Fatalf("SendCode: %v", err) + } + if len(sender.requests) != 1 || sender.requests[0].Recipient != "989981679461" { + t.Fatalf("OTP requests = %+v, want canonical Iran recipient", sender.requests) + } + _, _, needSignUp, err := svc.SignIn(ctx, domain.Authorization{}, "989981679461", hash, sender.requests[0].Code) + if err != nil || !needSignUp { + t.Fatalf("SignIn canonical variant needSignUp=%v err=%v", needSignUp, err) + } +} + func TestWebhookExistingAccountRejectionKeepsDurableAppCode(t *testing.T) { ctx := context.Background() users := memory.NewUserStore() diff --git a/internal/app/auth/service_test.go b/internal/app/auth/service_test.go index be9c8dd9..28ee6a6d 100644 --- a/internal/app/auth/service_test.go +++ b/internal/app/auth/service_test.go @@ -430,6 +430,55 @@ func TestPhoneCodeAcceptsTDesktopDigitsOnlySignIn(t *testing.T) { } } +func TestIranNationalTrunkVariantsShareOneAccountIdentity(t *testing.T) { + ctx := context.Background() + users := memory.NewUserStore() + authz := memory.NewAuthorizationStore() + delivery := &captureLoginCodeDelivery{} + svc := NewService( + users, + authz, + memory.NewCodeStore(), + nil, + nil, + "12345", + WithLoginCodeDelivery(delivery), + ) + const ( + withNationalTrunk = "+98 0998 167 9461" + international = "989981679461" + canonical = "989981679461" + ) + + firstHash, err := svc.SendCode(ctx, withNationalTrunk) + if err != nil { + t.Fatalf("SendCode trunk variant: %v", err) + } + verifyCodeForSignUp(t, svc, international, firstHash, "12345") + created, _, err := svc.SignUp(ctx, domain.Authorization{AuthKeyID: [8]byte{1}}, international, firstHash, "Iran", "User") + if err != nil { + t.Fatalf("SignUp international variant: %v", err) + } + if created.Phone != canonical { + t.Fatalf("created phone = %q, want %q", created.Phone, canonical) + } + + secondHash, err := svc.SendCode(ctx, international) + if err != nil { + t.Fatalf("SendCode existing international variant: %v", err) + } + signedIn, _, needSignUp, err := svc.SignIn(ctx, domain.Authorization{AuthKeyID: [8]byte{2}}, withNationalTrunk, secondHash, "12345") + if err != nil { + t.Fatalf("SignIn trunk variant: %v", err) + } + if needSignUp || signedIn.ID != created.ID { + t.Fatalf("SignIn user=%d needSignUp=%v, want existing user %d", signedIn.ID, needSignUp, created.ID) + } + if got, found, err := users.ByPhone(ctx, canonical); err != nil || !found || got.ID != created.ID { + t.Fatalf("canonical lookup user=%+v found=%v err=%v", got, found, err) + } +} + func verifyCodeForSignUp(t *testing.T, svc *Service, phone, hash, code string) { t.Helper() got, msg, needSignUp, err := svc.SignIn(context.Background(), domain.Authorization{}, phone, hash, code) diff --git a/internal/app/contacts/service.go b/internal/app/contacts/service.go index 04670755..5f5eff44 100644 --- a/internal/app/contacts/service.go +++ b/internal/app/contacts/service.go @@ -129,10 +129,17 @@ func (s *Service) AddContact(ctx context.Context, userID int64, input domain.Con if input.FirstName == "" && input.LastName == "" { return domain.Contact{}, ErrContactNameEmpty } - // Android 的 contacts.addContact 会提交带 "+" 前缀的号码(TDesktop 传纯数字或空), - // 归一成纯数字。空串表示客户端只按 user id 添加联系人,必须原样保留; + // Android 的 contacts.addContact 会提交带 "+" 前缀的号码(TDesktop 传纯数字或空)。 + // 可解析的完整号码写成与账号相同的 E.164 identity;无法解析的本地名片号码只 + // 保留展示 digits,绝不能拿它做账号选择。空串表示客户端只按 user id 添加联系人,必须原样保留; // TL 明确允许省略号码,服务端不得从 target 全局资料反向补出隐私号码。 - input.Phone = digitsOnly(input.Phone) + if input.Phone != "" { + if canonical := domain.NormalizePhone(input.Phone); canonical != "" { + input.Phone = canonical + } else { + input.Phone = digitsOnly(input.Phone) + } + } if s.users != nil { _, found, err := s.users.ByID(ctx, input.ContactUserID) if err != nil { @@ -218,7 +225,7 @@ func (s *Service) ImportContacts(ctx context.Context, userID int64, inputs []dom phones := make([]string, 0, len(inputs)) seenPhones := make(map[string]struct{}, len(inputs)) for _, input := range inputs { - phone := normalizePhone(input.Phone) + phone := domain.NormalizePhone(input.Phone) if phone == "" { continue } @@ -338,7 +345,7 @@ func (s *Service) Search(ctx context.Context, userID int64, query string, limit } phoneQuery := "" if isPhoneSearchQuery(query) { - phoneQuery = normalizePhone(query) + phoneQuery = normalizePhoneQuery(query) } res, err := s.users.Search(ctx, userID, query, phoneQuery, limit) if err != nil { @@ -666,7 +673,7 @@ func digitsOnly(phone string) string { return b.String() } -func normalizePhone(phone string) string { +func normalizePhoneQuery(phone string) string { if !utf8.ValidString(phone) { return "" } diff --git a/internal/app/contacts/service_test.go b/internal/app/contacts/service_test.go index c168edbc..623806d7 100644 --- a/internal/app/contacts/service_test.go +++ b/internal/app/contacts/service_test.go @@ -168,6 +168,36 @@ func TestImportContactsBatchesPhonesAndDedupesUpserts(t *testing.T) { } } +func TestImportContactsResolvesNationalTrunkVariantToCanonicalUser(t *testing.T) { + ctx := context.Background() + users := memory.NewUserStore() + contactsStore := memory.NewContactStore() + owner, err := users.Create(ctx, domain.User{Phone: "15551234567", FirstName: "Owner"}) + if err != nil { + t.Fatalf("create owner: %v", err) + } + target, err := users.Create(ctx, domain.User{Phone: "989981679461", FirstName: "Iran"}) + if err != nil { + t.Fatalf("create target: %v", err) + } + svc := NewService(contactsStore, users) + + res, err := svc.ImportContacts(ctx, owner.ID, []domain.ContactInput{{ + ClientID: 98, + Phone: "+98 0998 167 9461", + FirstName: "Saved", + }}) + if err != nil { + t.Fatalf("ImportContacts: %v", err) + } + if len(res.Imported) != 1 || res.Imported[0].UserID != target.ID { + t.Fatalf("imported = %+v, want target %d", res.Imported, target.ID) + } + if len(res.Contacts) != 1 || res.Contacts[0].Phone != "989981679461" { + t.Fatalf("contacts = %+v, want one canonical contact", res.Contacts) + } +} + func TestAddContactWithoutPhoneDoesNotBackfillTargetPhone(t *testing.T) { ctx := context.Background() users := memory.NewUserStore() diff --git a/internal/app/users/service.go b/internal/app/users/service.go index 5e3f6b26..2a083204 100644 --- a/internal/app/users/service.go +++ b/internal/app/users/service.go @@ -702,17 +702,26 @@ func (s *Service) ResolvePhone(ctx context.Context, currentUserID int64, phone s if _, err := s.loadSelf(ctx, currentUserID); err != nil { return domain.User{}, false, err } - phone = normalizePhone(phone) - if phone == "" { + canonicalPhone := domain.NormalizePhone(phone) + collectiblePhone := domain.NormalizeCollectiblePhone(phone) + validCollectible := domain.ValidCollectiblePhone(collectiblePhone) + if canonicalPhone == "" && !validCollectible { return domain.User{}, false, domain.ErrPhoneNotOccupied } - u, found, err := s.users.ByPhone(ctx, phone) + var ( + u domain.User + found bool + err error + ) + if canonicalPhone != "" { + u, found, err = s.users.ByPhone(ctx, canonicalPhone) + } collectibleExclusive := false if err != nil { return u, false, err } - if !found && s.phones != nil && domain.ValidCollectiblePhone(phone) { - asset, assetErr := s.phones.CollectiblePhone(ctx, phone) + if !found && s.phones != nil && validCollectible { + asset, assetErr := s.phones.CollectiblePhone(ctx, collectiblePhone) if assetErr == nil && asset.Owned() { u, found, err = s.loadBaseUserByID(ctx, asset.OwnerUserID) collectibleExclusive = asset.AlwaysVisible() @@ -920,18 +929,3 @@ func validUsername(username string) bool { } return true } - -func normalizePhone(phone string) string { - phone = strings.TrimSpace(phone) - if phone == "" { - return "" - } - var b strings.Builder - b.Grow(len(phone)) - for _, r := range phone { - if r >= '0' && r <= '9' { - b.WriteRune(r) - } - } - return b.String() -} diff --git a/internal/app/users/service_test.go b/internal/app/users/service_test.go index b34a22de..72da7a2d 100644 --- a/internal/app/users/service_test.go +++ b/internal/app/users/service_test.go @@ -8,9 +8,32 @@ import ( privacyapp "telesrv/internal/app/privacy" "telesrv/internal/domain" + "telesrv/internal/store" "telesrv/internal/store/memory" ) +type resolvePhoneCollectibleStore struct { + store.CollectiblePhoneStore + asset domain.CollectiblePhone +} + +func (s resolvePhoneCollectibleStore) CollectiblePhone(_ context.Context, phone string) (domain.CollectiblePhone, error) { + if phone == s.asset.Phone { + return s.asset, nil + } + return domain.CollectiblePhone{}, domain.ErrCollectiblePhoneNotFound +} + +func (s resolvePhoneCollectibleStore) OwnedCollectiblePhones(_ context.Context, userIDs []int64) (map[int64]domain.CollectiblePhone, error) { + out := make(map[int64]domain.CollectiblePhone) + for _, userID := range userIDs { + if userID == s.asset.OwnerUserID { + out[userID] = s.asset + } + } + return out, nil +} + func TestServiceUsernameLifecycle(t *testing.T) { ctx := context.Background() store := memory.NewUserStore() @@ -186,6 +209,34 @@ func TestResolvePhoneHonorsAddedByPhone(t *testing.T) { } } +func TestResolvePhoneKeepsCollectibleAliasSeparateFromE164Identity(t *testing.T) { + ctx := context.Background() + users := memory.NewUserStore() + viewer, err := users.Create(ctx, domain.User{AccessHash: 1, Phone: "15550001011", FirstName: "Viewer"}) + if err != nil { + t.Fatalf("create viewer: %v", err) + } + target, err := users.Create(ctx, domain.User{AccessHash: 2, Phone: "15550001012", FirstName: "Target"}) + if err != nil { + t.Fatalf("create target: %v", err) + } + asset := domain.CollectiblePhone{ + Phone: "8881111", + Tier: domain.CollectiblePhoneTierExclusive, + Status: domain.CollectibleUsernameStatusOwned, + OwnerUserID: target.ID, + } + svc := NewService(users, WithCollectiblePhoneStore(resolvePhoneCollectibleStore{asset: asset})) + + got, found, err := svc.ResolvePhone(ctx, viewer.ID, "+888 1111") + if err != nil || !found || got.ID != target.ID { + t.Fatalf("ResolvePhone collectible = %+v found=%v err=%v, want target", got, found, err) + } + if got.Phone != asset.Phone { + t.Fatalf("projected phone = %q, want collectible %q", got.Phone, asset.Phone) + } +} + func TestServiceUpdateProfile(t *testing.T) { ctx := context.Background() store := memory.NewUserStore() diff --git a/internal/domain/account.go b/internal/domain/account.go index 07489e28..94fc2587 100644 --- a/internal/domain/account.go +++ b/internal/domain/account.go @@ -4,6 +4,9 @@ import ( "errors" "strings" "time" + "unicode" + + "github.com/nyaruka/phonenumbers" ) var ( @@ -260,33 +263,71 @@ func MaskEmail(email string) string { return name[:1] + "***" + name[len(name)-1:] + email[at:] } -// NormalizePhone 仅保留手机号中的数字(与 users.phone 的存储形态一致)。全部被过滤 -// 掉时返回原串,便于上层做 validPhone 拒绝。auth/account 两域共用同一规则避免漂移。 -func NormalizePhone(phone string) string { +// PhoneDigits removes presentation punctuation from a phone number. It is +// intentionally not an identity canonicalizer: callers that select accounts, +// issue codes, or persist users must use NormalizePhone and ValidPhone. +func PhoneDigits(phone string) string { var b strings.Builder b.Grow(len(phone)) + seenDigit := false + seenPlus := false for _, r := range phone { - if r >= '0' && r <= '9' { + switch { + case r >= '0' && r <= '9': b.WriteRune(r) + seenDigit = true + case r == '+': + if seenPlus || seenDigit { + return "" + } + seenPlus = true + case unicode.IsSpace(r), r == '-', r == '(', r == ')', r == '.', r == '/': + // Presentation separators accepted by official clients and contact UIs. + default: + return "" } } - if b.Len() == 0 { - return phone - } return b.String() } -// ValidPhone 校验 NormalizePhone 后的持久化形态:5-32 位纯数字。 -// 上限与 users.phone 列宽一致;当前开发登录/改号链路不强制精确 E.164 长度, -// 但拒绝空串、非数字和会截断的超长输入。 -func ValidPhone(phone string) bool { - if len(phone) < 5 || len(phone) > 32 { - return false +// NormalizePhone returns the one persisted identity for an international +// phone number: E.164 digits without the leading '+'. Parsing is deliberately +// country-aware so a national trunk prefix is removed only where the numbering +// plan says it is a prefix. For example, both +98 0998 167 9461 and +// +98 998 167 9461 become 989981679461, while Italy's significant leading zero +// in +39 02 ... is retained. +// +// IsPossibleNumber is the structural gate rather than IsValidNumber. It keeps +// syntactically possible reserved/test ranges usable without accepting local +// numbers that omit their country calling code or numbers outside E.164's +// length/plan metadata. +func NormalizePhone(phone string) string { + digits := PhoneDigits(phone) + if digits == "" { + return "" } - for _, r := range phone { - if r < '0' || r > '9' { - return false - } + // 42777 is the reserved, non-login phone of the built-in service identity. + // It predates the ordinary E.164 user invariant and remains resolvable only + // so auth can reject it as a system account instead of treating it as free. + if digits == OfficialSystemPhone { + return digits } - return true + number, err := phonenumbers.Parse("+"+digits, phonenumbers.UNKNOWN_REGION) + if err != nil || !phonenumbers.IsPossibleNumber(number) { + return "" + } + canonical := strings.TrimPrefix(phonenumbers.Format(number, phonenumbers.E164), "+") + if canonical == "" || len(canonical) > 15 { + return "" + } + return canonical +} + +// ValidPhone reports whether phone is already in the persisted canonical form. +// Callers accepting user input normalize first, then validate, so equivalent +// international spellings converge before lookup, rate limiting, OTP delivery, +// and uniqueness checks. +func ValidPhone(phone string) bool { + canonical := NormalizePhone(phone) + return canonical != "" && canonical == phone } diff --git a/internal/domain/phone_identity_test.go b/internal/domain/phone_identity_test.go new file mode 100644 index 00000000..2337c3ad --- /dev/null +++ b/internal/domain/phone_identity_test.go @@ -0,0 +1,42 @@ +package domain + +import "testing" + +func TestNormalizePhoneUsesCountryAwareE164Identity(t *testing.T) { + tests := []struct { + name string + input string + want string + }{ + {name: "iran redundant national trunk", input: "+98 0998 167 9461", want: "989981679461"}, + {name: "iran canonical", input: "+98 998 167 9461", want: "989981679461"}, + {name: "iran wire digits redundant trunk", input: "9809981679461", want: "989981679461"}, + {name: "italy significant leading zero", input: "+39 02 1234 5678", want: "390212345678"}, + {name: "china presentation", input: "+86 (188) 0000-0000", want: "8618800000000"}, + {name: "possible reserved NANP range", input: "+1 555 000 0001", want: "15550000001"}, + {name: "local number without country", input: "09981679461", want: ""}, + {name: "letters are not separators", input: "+98abc9981679461", want: ""}, + {name: "international prefix is not country code", input: "00989981679461", want: ""}, + {name: "reserved system identity", input: OfficialSystemPhone, want: OfficialSystemPhone}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := NormalizePhone(test.input); got != test.want { + t.Fatalf("NormalizePhone(%q) = %q, want %q", test.input, got, test.want) + } + }) + } +} + +func TestValidPhoneRequiresCanonicalStorageShape(t *testing.T) { + for _, phone := range []string{"989981679461", "390212345678", "8618800000000", "15550000001", OfficialSystemPhone} { + if !ValidPhone(phone) { + t.Fatalf("ValidPhone(%q) = false", phone) + } + } + for _, phone := range []string{"+989981679461", "9809981679461", "09981679461", "", "+98abc9981679461"} { + if ValidPhone(phone) { + t.Fatalf("ValidPhone(%q) = true", phone) + } + } +} diff --git a/internal/domain/system.go b/internal/domain/system.go index a2df99f7..e0a643ab 100644 --- a/internal/domain/system.go +++ b/internal/domain/system.go @@ -10,6 +10,9 @@ import ( const ( // OfficialSystemUserID 是 Telegram 兼容客户端识别的官方系统账号。 OfficialSystemUserID int64 = 777000 + // OfficialSystemPhone is a reserved service identity, not an ordinary E.164 + // login number. Auth must recognize and reject it before account lookup. + OfficialSystemPhone = "42777" // BotFatherUserID 是内置 BotFather 账号,与官方 @BotFather 同 ID。 BotFatherUserID int64 = 93372553 @@ -113,7 +116,7 @@ func OfficialSystemUser() User { return User{ ID: OfficialSystemUserID, AccessHash: 6599886787491911851, - Phone: "42777", + Phone: OfficialSystemPhone, FirstName: branding.ProductName(), Username: branding.ProductUsername(), Verified: true, diff --git a/internal/rpc/auth_code_rate_limit_test.go b/internal/rpc/auth_code_rate_limit_test.go index f4ed92a6..55b4ee8e 100644 --- a/internal/rpc/auth_code_rate_limit_test.go +++ b/internal/rpc/auth_code_rate_limit_test.go @@ -101,6 +101,26 @@ func TestAuthSendCodeRateLimitUsesOpaquePhoneAndRawAuthKeyKeys(t *testing.T) { } } +func TestAuthCodeRateLimitSharesBudgetAcrossNationalTrunkVariants(t *testing.T) { + limiter := &captureRateLimiter{} + r := New(Config{ + AuthCodePhoneRateLimit: 5, + AuthCodeRateWindow: time.Minute, + }, Deps{Limiter: limiter}, zaptest.NewLogger(t), clock.System) + + for _, phone := range []string{"+98 0998 167 9461", "989981679461"} { + if err := r.checkAuthCodeRateLimit(context.Background(), phone); err != nil { + t.Fatalf("checkAuthCodeRateLimit(%q): %v", phone, err) + } + } + if len(limiter.calls) != 2 { + t.Fatalf("limiter calls = %d, want 2", len(limiter.calls)) + } + if limiter.calls[0].key != limiter.calls[1].key { + t.Fatalf("equivalent phone variants used different limiter keys: %q != %q", limiter.calls[0].key, limiter.calls[1].key) + } +} + func TestAuthSendCodePhoneRateLimitPrecedesBusinessLookupAndWrite(t *testing.T) { limiter := &captureRateLimiter{block: true, retryAfter: 17} authService := &authCodeRateTestService{captureAuthService: &captureAuthService{}} diff --git a/internal/store/code.go b/internal/store/code.go index a2363933..4a94d533 100644 --- a/internal/store/code.go +++ b/internal/store/code.go @@ -38,9 +38,10 @@ func LoginCodeChannelTakeable(channel string) bool { } // PhoneCodeVersionCurrent is the only version accepted by the atomic login -// state machine. Version zero is the pre-state-machine shape and deliberately -// fails closed instead of being normalized on read. -const PhoneCodeVersionCurrent = 1 +// state machine. Version 2 binds every scope to an E.164 canonical identity; +// version 1 records are invalidated across rollout instead of being normalized +// on read and accidentally authorizing a different phone owner. +const PhoneCodeVersionCurrent = 2 // PhoneCode 是一条验证码记录(与某次 sendCode 的 phone_code_hash 或邮箱验证键关联)。 // Purpose/UserID/AuthKeyID/SessionID 为已登录敏感操作提供作用域;登录验证码保持零值。 diff --git a/internal/store/postgres/phone_identity_migration.go b/internal/store/postgres/phone_identity_migration.go new file mode 100644 index 00000000..b5d42d1a --- /dev/null +++ b/internal/store/postgres/phone_identity_migration.go @@ -0,0 +1,150 @@ +package postgres + +import ( + "context" + "fmt" + "math" + + "github.com/jackc/pgx/v5" + + "telesrv/internal/domain" +) + +const phoneIdentityMigrationBatchSize = 1000 + +type phoneIdentityCandidate struct { + userID int64 + canonicalPhone string + needsUpdate bool +} + +// canonicalizeStoredPhoneIdentities is the data half of migration 0182. SQL +// alone cannot correctly distinguish a removable national trunk prefix from a +// significant leading zero, so the application numbering-plan metadata builds +// a complete plan first. No users row is changed until every ordinary account +// is parseable and no two rows converge on the same E.164 identity. +func canonicalizeStoredPhoneIdentities(ctx context.Context, conn *pgx.Conn) error { + tx, err := conn.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.Serializable}) + if err != nil { + return fmt.Errorf("begin phone identity audit: %w", err) + } + committed := false + defer func() { + if !committed { + _ = tx.Rollback(context.Background()) + } + }() + if err := canonicalizeStoredPhoneIdentitiesTx(ctx, tx); err != nil { + return err + } + if err := tx.Commit(ctx); err != nil { + return fmt.Errorf("commit phone identity migration: %w", err) + } + committed = true + return nil +} + +func canonicalizeStoredPhoneIdentitiesTx(ctx context.Context, tx pgx.Tx) error { + return canonicalizeStoredPhoneIdentitiesInTableTx(ctx, tx, pgx.Identifier{"public", "users"}) +} + +func canonicalizeStoredPhoneIdentitiesInTableTx(ctx context.Context, tx pgx.Tx, usersTable pgx.Identifier) error { + if _, err := tx.Exec(ctx, ` +CREATE TEMP TABLE phone_identity_0182 ( + user_id bigint PRIMARY KEY, + canonical_phone text NOT NULL, + needs_update boolean NOT NULL +) ON COMMIT DROP`); err != nil { + return fmt.Errorf("create phone identity plan: %w", err) + } + + lastID := int64(math.MinInt64) + for { + rows, err := tx.Query(ctx, fmt.Sprintf(` +SELECT id, phone +FROM %s +WHERE id > $1 AND phone <> '' +ORDER BY id +LIMIT $2`, usersTable.Sanitize()), lastID, phoneIdentityMigrationBatchSize) + if err != nil { + return fmt.Errorf("scan stored phone identities: %w", err) + } + candidates := make([]phoneIdentityCandidate, 0, phoneIdentityMigrationBatchSize) + for rows.Next() { + var userID int64 + var rawPhone string + if err := rows.Scan(&userID, &rawPhone); err != nil { + rows.Close() + return fmt.Errorf("read stored phone identity: %w", err) + } + lastID = userID + canonical := rawPhone + if !domain.IsSystemUserID(userID) { + canonical = domain.NormalizePhone(rawPhone) + if canonical == "" { + rows.Close() + return fmt.Errorf("user %d has a non-canonicalizable phone", userID) + } + } + candidates = append(candidates, phoneIdentityCandidate{ + userID: userID, + canonicalPhone: canonical, + needsUpdate: canonical != rawPhone, + }) + } + if err := rows.Err(); err != nil { + rows.Close() + return fmt.Errorf("iterate stored phone identities: %w", err) + } + rows.Close() + if len(candidates) == 0 { + break + } + copyRows := make([][]any, 0, len(candidates)) + for _, candidate := range candidates { + copyRows = append(copyRows, []any{candidate.userID, candidate.canonicalPhone, candidate.needsUpdate}) + } + if _, err := tx.CopyFrom(ctx, + pgx.Identifier{"phone_identity_0182"}, + []string{"user_id", "canonical_phone", "needs_update"}, + pgx.CopyFromRows(copyRows), + ); err != nil { + return fmt.Errorf("stage phone identity plan: %w", err) + } + if len(candidates) < phoneIdentityMigrationBatchSize { + break + } + } + + if _, err := tx.Exec(ctx, ` +CREATE INDEX phone_identity_0182_canonical_idx +ON phone_identity_0182(canonical_phone, user_id)`); err != nil { + return fmt.Errorf("index phone identity plan: %w", err) + } + + var firstUserID, secondUserID int64 + err := tx.QueryRow(ctx, ` +SELECT first.user_id, second.user_id +FROM phone_identity_0182 AS first +JOIN phone_identity_0182 AS second + ON second.canonical_phone = first.canonical_phone + AND second.user_id > first.user_id +ORDER BY first.user_id, second.user_id +LIMIT 1`).Scan(&firstUserID, &secondUserID) + switch { + case err == nil: + return fmt.Errorf("users %d and %d resolve to the same canonical phone", firstUserID, secondUserID) + case err != pgx.ErrNoRows: + return fmt.Errorf("audit canonical phone uniqueness: %w", err) + } + + if _, err := tx.Exec(ctx, fmt.Sprintf(` +UPDATE %s AS target +SET phone = planned.canonical_phone +FROM phone_identity_0182 AS planned +WHERE planned.user_id = target.id + AND planned.needs_update`, usersTable.Sanitize())); err != nil { + return fmt.Errorf("apply canonical phone identities: %w", err) + } + return nil +} diff --git a/internal/store/postgres/phone_identity_migration_integration_test.go b/internal/store/postgres/phone_identity_migration_integration_test.go new file mode 100644 index 00000000..4fcb5eb0 --- /dev/null +++ b/internal/store/postgres/phone_identity_migration_integration_test.go @@ -0,0 +1,97 @@ +package postgres + +import ( + "context" + "strings" + "testing" + + "github.com/jackc/pgx/v5" + + "telesrv/internal/domain" +) + +func TestPhoneIdentityMigrationCanonicalizesOnlyAfterCompleteAuditPostgres(t *testing.T) { + pool := testPool(t) + ctx := context.Background() + + t.Run("canonicalizes national trunk and preserves significant zero", func(t *testing.T) { + tx := beginPhoneIdentityFixtureTx(t, ctx, pool) + defer func() { _ = tx.Rollback(context.Background()) }() + if _, err := tx.Exec(ctx, ` +INSERT INTO users(id, phone) VALUES + (1001, '9809981679461'), + (1002, '390212345678'), + ($1, $2)`, domain.OfficialSystemUserID, domain.OfficialSystemPhone); err != nil { + t.Fatalf("insert phone fixtures: %v", err) + } + + if err := canonicalizeStoredPhoneIdentitiesInTableTx(ctx, tx, pgx.Identifier{"users"}); err != nil { + t.Fatalf("canonicalizeStoredPhoneIdentitiesInTableTx: %v", err) + } + assertFixturePhone(t, ctx, tx, 1001, "989981679461") + assertFixturePhone(t, ctx, tx, 1002, "390212345678") + assertFixturePhone(t, ctx, tx, domain.OfficialSystemUserID, domain.OfficialSystemPhone) + }) + + t.Run("duplicate canonical identity fails without changing rows", func(t *testing.T) { + tx := beginPhoneIdentityFixtureTx(t, ctx, pool) + defer func() { _ = tx.Rollback(context.Background()) }() + if _, err := tx.Exec(ctx, ` +INSERT INTO users(id, phone) VALUES + (2001, '9809981679461'), + (2002, '989981679461')`); err != nil { + t.Fatalf("insert duplicate fixtures: %v", err) + } + + err := canonicalizeStoredPhoneIdentitiesInTableTx(ctx, tx, pgx.Identifier{"users"}) + if err == nil || !strings.Contains(err.Error(), "users 2001 and 2002 resolve to the same canonical phone") { + t.Fatalf("canonicalize err = %v, want duplicate user IDs", err) + } + assertFixturePhone(t, ctx, tx, 2001, "9809981679461") + assertFixturePhone(t, ctx, tx, 2002, "989981679461") + }) + + t.Run("invalid ordinary identity fails without exposing phone", func(t *testing.T) { + tx := beginPhoneIdentityFixtureTx(t, ctx, pool) + defer func() { _ = tx.Rollback(context.Background()) }() + const invalidPhone = "legacy-not-a-phone" + if _, err := tx.Exec(ctx, `INSERT INTO users(id, phone) VALUES (3001, $1)`, invalidPhone); err != nil { + t.Fatalf("insert invalid fixture: %v", err) + } + + err := canonicalizeStoredPhoneIdentitiesInTableTx(ctx, tx, pgx.Identifier{"users"}) + if err == nil || !strings.Contains(err.Error(), "user 3001 has a non-canonicalizable phone") { + t.Fatalf("canonicalize err = %v, want invalid user ID", err) + } + if strings.Contains(err.Error(), invalidPhone) { + t.Fatalf("migration error leaked phone: %v", err) + } + assertFixturePhone(t, ctx, tx, 3001, invalidPhone) + }) +} + +func beginPhoneIdentityFixtureTx(t *testing.T, ctx context.Context, pool interface { + Begin(context.Context) (pgx.Tx, error) +}) pgx.Tx { + t.Helper() + tx, err := pool.Begin(ctx) + if err != nil { + t.Fatalf("begin phone identity fixture: %v", err) + } + if _, err := tx.Exec(ctx, `CREATE TEMP TABLE users (id bigint PRIMARY KEY, phone text NOT NULL) ON COMMIT DROP`); err != nil { + _ = tx.Rollback(context.Background()) + t.Fatalf("create isolated users fixture: %v", err) + } + return tx +} + +func assertFixturePhone(t *testing.T, ctx context.Context, tx pgx.Tx, userID int64, want string) { + t.Helper() + var got string + if err := tx.QueryRow(ctx, `SELECT phone FROM users WHERE id=$1`, userID).Scan(&got); err != nil { + t.Fatalf("read user %d phone: %v", userID, err) + } + if got != want { + t.Fatalf("user %d phone = %q, want %q", userID, got, want) + } +} diff --git a/internal/store/postgres/postgres.go b/internal/store/postgres/postgres.go index 04615bd5..0d980b08 100644 --- a/internal/store/postgres/postgres.go +++ b/internal/store/postgres/postgres.go @@ -21,6 +21,14 @@ import ( const defaultMinConns = 16 +const ( + phoneIdentityPredecessorVersion = uint(181) + phoneIdentityMigrationVersion = uint(182) + // "phone182" as a signed PostgreSQL advisory-lock key. The lock spans the + // two-stage 0181 -> identity audit -> 0182 transition across new binaries. + phoneIdentityMigrationLockKey = int64(0x70686f6e65313832) +) + // MigrationStatus 是启动迁移后的 schema 状态。 type MigrationStatus struct { Version uint @@ -142,6 +150,19 @@ func Migrate(dsn string) error { // MigrateAndStatus 用嵌入迁移脚本迁移数据库,并返回迁移后的 schema 版本。 func MigrateAndStatus(dsn string) (MigrationStatus, error) { + ctx := context.Background() + lockConn, err := pgx.Connect(ctx, dsn) + if err != nil { + return MigrationStatus{}, fmt.Errorf("connect migration lock: %w", err) + } + defer lockConn.Close(ctx) + if _, err := lockConn.Exec(ctx, `SELECT pg_advisory_lock($1)`, phoneIdentityMigrationLockKey); err != nil { + return MigrationStatus{}, fmt.Errorf("lock migrations: %w", err) + } + defer func() { + _, _ = lockConn.Exec(context.Background(), `SELECT pg_advisory_unlock($1)`, phoneIdentityMigrationLockKey) + }() + src, err := iofs.New(deploy.Migrations, "migrations") if err != nil { return MigrationStatus{}, fmt.Errorf("iofs source: %w", err) @@ -151,9 +172,37 @@ func MigrateAndStatus(dsn string) (MigrationStatus, error) { return MigrationStatus{}, fmt.Errorf("migrate new: %w", err) } defer m.Close() + status, err := migrationStatus(m) + if err != nil { + return MigrationStatus{}, err + } + if status.Dirty { + return MigrationStatus{}, fmt.Errorf("migrate version %d is dirty", status.Version) + } + if status.Empty || status.Version < phoneIdentityPredecessorVersion { + if err := m.Migrate(phoneIdentityPredecessorVersion); err != nil && !errors.Is(err, migrate.ErrNoChange) { + return MigrationStatus{}, fmt.Errorf("migrate to phone identity predecessor: %w", err) + } + status, err = migrationStatus(m) + if err != nil { + return MigrationStatus{}, err + } + if status.Dirty || status.Empty || status.Version != phoneIdentityPredecessorVersion { + return MigrationStatus{}, fmt.Errorf("phone identity predecessor status = %+v", status) + } + } + if status.Version < phoneIdentityMigrationVersion { + if err := canonicalizeStoredPhoneIdentities(ctx, lockConn); err != nil { + return MigrationStatus{}, fmt.Errorf("migrate phone identities: %w", err) + } + } if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { return MigrationStatus{}, fmt.Errorf("migrate up: %w", err) } + return migrationStatus(m) +} + +func migrationStatus(m *migrate.Migrate) (MigrationStatus, error) { version, dirty, err := m.Version() if errors.Is(err, migrate.ErrNilVersion) { return MigrationStatus{Empty: true}, nil diff --git a/internal/store/postgres/star_gift_lifecycle_migration_integration_test.go b/internal/store/postgres/star_gift_lifecycle_migration_integration_test.go index 6c1d0c26..438f1344 100644 --- a/internal/store/postgres/star_gift_lifecycle_migration_integration_test.go +++ b/internal/store/postgres/star_gift_lifecycle_migration_integration_test.go @@ -14,7 +14,7 @@ func TestStarGiftLifecycleMigrationsApply(t *testing.T) { if err != nil { t.Fatalf("migrate star gift lifecycle schema: %v", err) } - if status.Dirty || status.Empty || status.Version != 173 { - t.Fatalf("migration status = %+v, want clean version 173", status) + if status.Dirty || status.Empty || status.Version != 182 { + t.Fatalf("migration status = %+v, want clean version 182", status) } }