Merge pull request #1325 from warmbly/fix/signin-link-wrong-account

fix(store): refuse a sign-in link signed in as another account
This commit is contained in:
Matthew Meszaros
2026-11-27 10:34:51 +01:00
committed by GitHub
11 changed files with 174 additions and 7 deletions
+5 -1
View File
@@ -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)
})
}
+3
View File
@@ -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.
+62
View File
@@ -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")
}
+2 -1
View File
@@ -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)
+12 -3
View File
@@ -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 {
+30 -1
View File
@@ -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 {
@@ -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)
}
}
}
+3
View File
@@ -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
+3
View File
@@ -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)
+18
View File
@@ -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 {
+10 -1
View File
@@ -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) {