From d6682027086e050b33afb8637a8b1cabb3461b97 Mon Sep 17 00:00:00 2001 From: Matthew Meszaros Date: Fri, 27 Nov 2026 10:24:30 +0100 Subject: [PATCH] fix(store): refuse a sign-in link signed in as another account A store sign-in link is for one purchased mailbox, but any account that signed in through it was connected: Inboxlogy signed jordan@stagovahq.com's link in as the tenant's admin@stagovahqcom.onmicrosoft.com, which connected that account into the customer's workspace, used up the link ("This link is complete") and never reached GraphKit, so the purchase stayed unconnected until the link neared expiry. - connectlink.PartnerLinks gains RequiredAddress: store links require their mailbox; partner API links keep a pre-fill-only hint (""). - email.OAuthConnectWithCodeAs checks the signed-in address right after the owner lookup and refuses another account (signed_in_as_other) before storing anything, so the slot is released and the link stays usable. The connect page shows "You signed in to Microsoft as X, but this link is for Y ...". --- internal/app/connectlink/oauth.go | 6 ++- internal/app/connectlink/partner.go | 3 ++ internal/app/connectlink/partner_test.go | 62 ++++++++++++++++++++++ internal/app/connectlink/service.go | 3 +- internal/app/connectlink/service_test.go | 15 ++++-- internal/app/email/broker.go | 31 ++++++++++- internal/app/email/require_address_test.go | 26 +++++++++ internal/app/email/service.go | 3 ++ internal/app/partner/connect.go | 3 ++ internal/app/store/signin.go | 18 +++++++ internal/app/store/signin_test.go | 11 +++- 11 files changed, 174 insertions(+), 7 deletions(-) create mode 100644 internal/app/email/require_address_test.go diff --git a/internal/app/connectlink/oauth.go b/internal/app/connectlink/oauth.go index df2d33bd2..5d85f5045 100644 --- a/internal/app/connectlink/oauth.go +++ b/internal/app/connectlink/oauth.go @@ -138,8 +138,12 @@ func (s *Service) finishOAuth(ctx context.Context, sess *models.ClientConnectLin return nil, ErrProviderUnavailable } orgID := link.OrganizationID + address := "" + if s.partner != nil { + address = s.partner.RequiredAddress(ctx, link.ID) + } return s.connect(ctx, link, provider, sess.RemoteIPHash, func(userID string) (*models.Email, *errx.Error) { - return s.d.Mailboxes.OAuthConnectWithCode(ctx, userID, &orgID, provider, code, sess.CodeVerifier) + return s.d.Mailboxes.OAuthConnectWithCodeAs(ctx, userID, &orgID, provider, code, sess.CodeVerifier, address) }) } diff --git a/internal/app/connectlink/partner.go b/internal/app/connectlink/partner.go index c5c6458e0..cdd1409b7 100644 --- a/internal/app/connectlink/partner.go +++ b/internal/app/connectlink/partner.go @@ -17,6 +17,9 @@ import ( type PartnerLinks interface { // LoginHint pre-fills the provider's sign-in for a partner's link, "" for any other. LoginHint(ctx context.Context, linkID uuid.UUID) string + // RequiredAddress is the one mailbox a link may connect, "" when any account may sign in (a + // hint only pre-fills); a sign-in as another account is refused and leaves the link usable. + RequiredAddress(ctx context.Context, linkID uuid.UUID) string // Connected hands the partner the mailbox its link connected. Connected(ctx context.Context, linkID uuid.UUID, acc *models.Email) // ReturnURL is where a partner's link sends the person after the provider; ok is false for any other link. diff --git a/internal/app/connectlink/partner_test.go b/internal/app/connectlink/partner_test.go index 734591891..60a9ca9e5 100644 --- a/internal/app/connectlink/partner_test.go +++ b/internal/app/connectlink/partner_test.go @@ -16,9 +16,17 @@ import ( type fakePartnerLinks struct { link uuid.UUID hint string + required string connected []uuid.UUID } +func (p *fakePartnerLinks) RequiredAddress(_ context.Context, id uuid.UUID) string { + if id != p.link { + return "" + } + return p.required +} + func (p *fakePartnerLinks) LoginHint(_ context.Context, id uuid.UUID) string { if id != p.link { return "" @@ -117,3 +125,57 @@ func TestPartnerLinkDeclinedReturnsCancelled(t *testing.T) { t.Fatalf("declined = %q %v %v", back, xerr, p.connected) } } + +// A store sign-in link is for one mailbox: signing in as another account (a tenant's own +// admin@….onmicrosoft.com) connects nothing, tells the person who to sign in as, and leaves the +// link usable for the right account. +func TestRequiredAddressRefusesAnotherAccountAndKeepsTheLink(t *testing.T) { + s, repo, mb, _, token := newFixture(t) + repo.link.MaxMailboxes = 1 + p := &fakePartnerLinks{link: repo.link.ID, hint: "jordan@client.test", required: "jordan@client.test"} + s.WirePartnerLinks(p) + + mb.owner = "admin@client.onmicrosoft.com" + state := startHinted(t, s, token) + if _, _, xerr := s.FinishOAuth(context.Background(), "outlook", "code", state, ""); xerr == nil || xerr.ResponseCode() != "signed_in_as_other" { + t.Fatalf("wrong account = %v", xerr) + } + if mb.gotAddress != "jordan@client.test" || len(p.connected) != 0 || len(repo.uses) != 0 { + t.Fatalf("address %q, partner told of %v, uses %v", mb.gotAddress, p.connected, repo.uses) + } + if _, xerr := s.Resolve(context.Background(), token); xerr != nil { + t.Fatalf("link after a refused sign-in = %v", xerr) + } + + mb.owner = "Jordan@Client.test" + res, _, xerr := s.FinishOAuth(context.Background(), "outlook", "code", startHinted(t, s, token), "") + if xerr != nil || len(p.connected) != 1 || p.connected[0] != res.Account.ID { + t.Fatalf("right account = %v, partner told of %v", xerr, p.connected) + } +} + +// A partner API link's hint only pre-fills: any account may still sign in. +func TestPartnerHintAloneAcceptsAnyAccount(t *testing.T) { + s, repo, mb, _, token := newFixture(t) + p := &fakePartnerLinks{link: repo.link.ID, hint: "bo@client.test"} + s.WirePartnerLinks(p) + mb.owner = "someone@else.test" + if _, _, xerr := s.FinishOAuth(context.Background(), "outlook", "code", startHinted(t, s, token), ""); xerr != nil || mb.gotAddress != "" { + t.Fatalf("hint-only link = %v (address %q)", xerr, mb.gotAddress) + } +} + +// startHinted starts an Outlook sign-in on a link whose sign-in URL carries a login hint. +func startHinted(t *testing.T, s *Service, token string) string { + t.Helper() + link, xerr := s.Resolve(context.Background(), token) + if xerr != nil { + t.Fatal(xerr) + } + started, xerr := s.StartOAuth(context.Background(), link, models.InboxProviderOutlook, returnURL, "203.0.113.9") + if xerr != nil { + t.Fatal(xerr) + } + u, _ := url.Parse(started.URL) + return u.Query().Get("state") +} diff --git a/internal/app/connectlink/service.go b/internal/app/connectlink/service.go index 3385e9dcc..7879dea3a 100644 --- a/internal/app/connectlink/service.go +++ b/internal/app/connectlink/service.go @@ -41,7 +41,8 @@ const ( type Mailboxes interface { OAuthAuthorizeURL(provider models.InboxProvider, state, verifier string) (string, *errx.Error) OAuthCallbackOrigin(provider models.InboxProvider) (string, *errx.Error) - OAuthConnectWithCode(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string) (*models.Email, *errx.Error) + // OAuthConnectWithCodeAs refuses any account but address ("" accepts any). + OAuthConnectWithCodeAs(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier, address string) (*models.Email, *errx.Error) OnboardSMTPIMAP(ctx context.Context, userID string, orgID *uuid.UUID, data *models.NewSMTPIMAPAccount) (*models.Email, *errx.Error) BulkUpdateTags(ctx context.Context, orgID string, emailIDs, addTags, removeTags []uuid.UUID) (int, *errx.Error) SetWarmupLifecycle(ctx context.Context, orgID, emailAccountID, action string) (*models.Email, *errx.Error) diff --git a/internal/app/connectlink/service_test.go b/internal/app/connectlink/service_test.go index 63f0cdc0d..20f2e334d 100644 --- a/internal/app/connectlink/service_test.go +++ b/internal/app/connectlink/service_test.go @@ -185,6 +185,8 @@ type fakeMailboxes struct { gotOrg uuid.UUID gotCode string gotVerif string + gotAddress string + owner string // the account the provider signs in, client@example.test when "" calls int } @@ -196,13 +198,20 @@ func (m *fakeMailboxes) OAuthAuthorizeURL(_ models.InboxProvider, state, verifie return "https://login.example.test/?state=" + state, nil } -func (m *fakeMailboxes) OAuthConnectWithCode(_ context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string) (*models.Email, *errx.Error) { +func (m *fakeMailboxes) OAuthConnectWithCodeAs(_ context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier, address string) (*models.Email, *errx.Error) { m.calls++ - m.gotUser, m.gotOrg, m.gotCode, m.gotVerif = userID, *orgID, code, verifier + m.gotUser, m.gotOrg, m.gotCode, m.gotVerif, m.gotAddress = userID, *orgID, code, verifier, address if m.connectErr != nil { return nil, m.connectErr } - return &models.Email{ID: uuid.New(), OrganizationID: orgID, Email: "client@example.test", Provider: string(provider)}, nil + owner := m.owner + if owner == "" { + owner = "client@example.test" + } + if address != "" && !strings.EqualFold(owner, address) { + return nil, errx.NewWithIdentifier(errx.Conflict, "signed_in_as_other", "signed in as "+owner) + } + return &models.Email{ID: uuid.New(), OrganizationID: orgID, Email: owner, Provider: string(provider)}, nil } type fakeWorkspaces struct { diff --git a/internal/app/email/broker.go b/internal/app/email/broker.go index 8c8f8a5c2..87b92ab04 100644 --- a/internal/app/email/broker.go +++ b/internal/app/email/broker.go @@ -42,10 +42,36 @@ func (s *emailService) OAuthCallbackOrigin(provider models.InboxProvider) (strin } func (s *emailService) OAuthConnectWithCode(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string) (*models.Email, *errx.Error) { - return s.OAuthConnectWithCodeForAccount(ctx, userID, orgID, provider, code, verifier, uuid.Nil) + return s.oauthConnectWithCode(ctx, userID, orgID, provider, code, verifier, uuid.Nil, "") } func (s *emailService) OAuthConnectWithCodeForAccount(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string, accountID uuid.UUID) (*models.Email, *errx.Error) { + return s.oauthConnectWithCode(ctx, userID, orgID, provider, code, verifier, accountID, "") +} + +func (s *emailService) OAuthConnectWithCodeAs(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier, address string) (*models.Email, *errx.Error) { + return s.oauthConnectWithCode(ctx, userID, orgID, provider, code, verifier, uuid.Nil, address) +} + +// ErrSignedInAsOther: a link for one mailbox was signed in with another account, so nothing was stored. +const ErrSignedInAsOther = "signed_in_as_other" + +// requireAddress refuses a sign-in whose account is not the one mailbox it was for ("" accepts any). +func requireAddress(provider models.InboxProvider, got, want string) *errx.Error { + want = strings.TrimSpace(want) + if want == "" || strings.EqualFold(strings.TrimSpace(got), want) { + return nil + } + who := "Google" + if provider == models.InboxProviderOutlook { + who = "Microsoft" + } + return errx.NewWithIdentifier(errx.Conflict, ErrSignedInAsOther, + "You signed in to "+who+" as "+got+", but this link is for "+want+", so nothing was connected. "+ + "Sign out of "+who+" (or use a private window), open the link again and sign in as "+want+".") +} + +func (s *emailService) oauthConnectWithCode(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string, accountID uuid.UUID, address string) (*models.Email, *errx.Error) { ctx, cancel := detach(ctx, connectBudget) defer cancel() if code = strings.TrimSpace(code); code == "" { @@ -84,6 +110,9 @@ func (s *emailService) OAuthConnectWithCodeForAccount(ctx context.Context, userI if xerr != nil { return nil, xerr } + if xerr := requireAddress(provider, owner.Email, address); xerr != nil { + return nil, xerr + } if existing, xerr := s.findExisting(ctx, userID, orgID, owner.Email); xerr != nil { return nil, xerr } else if existing != nil { diff --git a/internal/app/email/require_address_test.go b/internal/app/email/require_address_test.go new file mode 100644 index 000000000..282e9ff9e --- /dev/null +++ b/internal/app/email/require_address_test.go @@ -0,0 +1,26 @@ +package email + +import ( + "strings" + "testing" + + "github.com/warmbly/warmbly/internal/models" +) + +func TestRequireAddress(t *testing.T) { + if xerr := requireAddress(models.InboxProviderOutlook, "Jordan@Client.test ", "jordan@client.test"); xerr != nil { + t.Fatalf("same mailbox, other case = %v", xerr) + } + if xerr := requireAddress(models.InboxProviderOutlook, "anyone@else.test", ""); xerr != nil { + t.Fatalf("no required address = %v", xerr) + } + xerr := requireAddress(models.InboxProviderOutlook, "admin@client.onmicrosoft.com", "jordan@client.test") + if xerr == nil || xerr.ResponseCode() != ErrSignedInAsOther { + t.Fatalf("another account = %v", xerr) + } + for _, want := range []string{"Microsoft", "admin@client.onmicrosoft.com", "jordan@client.test"} { + if !strings.Contains(xerr.Message, want) { + t.Errorf("message %q lacks %q", xerr.Message, want) + } + } +} diff --git a/internal/app/email/service.go b/internal/app/email/service.go index 2c8681aeb..04f06ab9e 100644 --- a/internal/app/email/service.go +++ b/internal/app/email/service.go @@ -186,6 +186,9 @@ type EmailService interface { OAuthCallbackOrigin(provider models.InboxProvider) (string, *errx.Error) OAuthConnectWithCode(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string) (*models.Email, *errx.Error) OAuthConnectWithCodeForAccount(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier string, accountID uuid.UUID) (*models.Email, *errx.Error) + // OAuthConnectWithCodeAs is OAuthConnectWithCode for one mailbox only: a sign-in as any other + // account is refused before anything is stored ("" accepts any account). + OAuthConnectWithCodeAs(ctx context.Context, userID string, orgID *uuid.UUID, provider models.InboxProvider, code, verifier, address string) (*models.Email, *errx.Error) // OAuthAccessToken is a live access token for an OAuth mailbox, refreshed when near expiry. OAuthAccessToken(ctx context.Context, accountID uuid.UUID) (*oauth2.Token, *errx.Error) // LoadAccountOntoWorker assigns a worker if needed and ships the mailbox diff --git a/internal/app/partner/connect.go b/internal/app/partner/connect.go index f67737006..f185f7202 100644 --- a/internal/app/partner/connect.go +++ b/internal/app/partner/connect.go @@ -122,6 +122,9 @@ func (s *Service) LoginHint(ctx context.Context, linkID uuid.UUID) string { return l.LoginHint } +// RequiredAddress implements connectlink.PartnerLinks: a partner's hint only pre-fills, so any account may sign in. +func (s *Service) RequiredAddress(context.Context, uuid.UUID) string { return "" } + // ReturnURL implements connectlink.PartnerLinks: the partner's URL with the mailbox and the outcome. func (s *Service) ReturnURL(ctx context.Context, linkID uuid.UUID, status string) (string, bool) { l, err := s.mb.Repo.GetPartnerConnectLink(ctx, linkID) diff --git a/internal/app/store/signin.go b/internal/app/store/signin.go index bdb108cff..df154aed6 100644 --- a/internal/app/store/signin.go +++ b/internal/app/store/signin.go @@ -139,6 +139,13 @@ func (l SignInLinks) LoginHint(ctx context.Context, linkID uuid.UUID) string { return "" } +// RequiredAddress: a store link connects only the purchased mailbox. Anyone else's sign-in (a +// tenant's own admin@….onmicrosoft.com, say) is refused instead of connecting a stray mailbox +// and using up the link. +func (l SignInLinks) RequiredAddress(ctx context.Context, linkID uuid.UUID) string { + return l.LoginHint(ctx, linkID) +} + // ReturnURL: a store link ends on the hosted connect page's own result. func (SignInLinks) ReturnURL(context.Context, uuid.UUID, string) (string, bool) { return "", false } @@ -170,6 +177,7 @@ func (l SignInLinks) Connected(ctx context.Context, linkID uuid.UUID, acc *model // PartnerLinksChain asks each in turn: the store's links first, then the partner API's. type PartnerLinksChain []interface { LoginHint(ctx context.Context, linkID uuid.UUID) string + RequiredAddress(ctx context.Context, linkID uuid.UUID) string Connected(ctx context.Context, linkID uuid.UUID, acc *models.Email) ReturnURL(ctx context.Context, linkID uuid.UUID, status string) (string, bool) } @@ -184,6 +192,16 @@ func (c PartnerLinksChain) LoginHint(ctx context.Context, linkID uuid.UUID) stri return "" } +// RequiredAddress is the first non-empty one. +func (c PartnerLinksChain) RequiredAddress(ctx context.Context, linkID uuid.UUID) string { + for _, p := range c { + if a := p.RequiredAddress(ctx, linkID); a != "" { + return a + } + } + return "" +} + // Connected tells each one; each ignores links it did not mint. func (c PartnerLinksChain) Connected(ctx context.Context, linkID uuid.UUID, acc *models.Email) { for _, p := range c { diff --git a/internal/app/store/signin_test.go b/internal/app/store/signin_test.go index c5eadf209..a7c65b095 100644 --- a/internal/app/store/signin_test.go +++ b/internal/app/store/signin_test.go @@ -12,11 +12,13 @@ import ( type stubLinks struct { hint, back string + required string ok bool connected int } -func (s *stubLinks) LoginHint(context.Context, uuid.UUID) string { return s.hint } +func (s *stubLinks) LoginHint(context.Context, uuid.UUID) string { return s.hint } +func (s *stubLinks) RequiredAddress(context.Context, uuid.UUID) string { return s.required } func (s *stubLinks) Connected(context.Context, uuid.UUID, *models.Email) { s.connected++ } @@ -40,6 +42,13 @@ func TestPartnerLinksChainAsksEachInTurn(t *testing.T) { if got := c.LoginHint(ctx, id); got != "store@b.com" { t.Fatalf("store hint first: %q", got) } + if got := c.RequiredAddress(ctx, id); got != "" { + t.Fatalf("a hint alone requires no address: %q", got) + } + storeLinks.required = "store@b.com" + if got := c.RequiredAddress(ctx, id); got != "store@b.com" { + t.Fatalf("required %q", got) + } } func TestOAuthMailboxesConnectOnlyThroughTheirSignIn(t *testing.T) {