From d2165f2b8ea74359e9424175410a7f3ceae5916e Mon Sep 17 00:00:00 2001 From: Matthew Meszaros Date: Fri, 27 Nov 2026 10:40:31 +0100 Subject: [PATCH] fix(connectlink): refuse a sign-in when the link's required address cannot be read RequiredAddress went through the best-effort LoginHint, so a failed lookup of a store sign-in link read as "any account may sign in". It now returns an error, the chain passes it on, and FinishOAuth refuses the sign-in before connecting: nothing is stored and the link stays unused. Follow-up to #1325 (CodeRabbit). --- internal/app/connectlink/oauth.go | 7 +++++- internal/app/connectlink/partner.go | 3 ++- internal/app/connectlink/partner_test.go | 22 ++++++++++++++++--- internal/app/partner/connect.go | 2 +- internal/app/store/signin.go | 25 ++++++++++++++------- internal/app/store/signin_test.go | 28 +++++++++++++++--------- 6 files changed, 63 insertions(+), 24 deletions(-) diff --git a/internal/app/connectlink/oauth.go b/internal/app/connectlink/oauth.go index 5d85f5045..ed0b4f988 100644 --- a/internal/app/connectlink/oauth.go +++ b/internal/app/connectlink/oauth.go @@ -140,7 +140,12 @@ func (s *Service) finishOAuth(ctx context.Context, sess *models.ClientConnectLin orgID := link.OrganizationID address := "" if s.partner != nil { - address = s.partner.RequiredAddress(ctx, link.ID) + a, err := s.partner.RequiredAddress(ctx, link.ID) + if err != nil { + errs.CaptureException(err) + return nil, errx.InternalError() + } + address = a } return s.connect(ctx, link, provider, sess.RemoteIPHash, func(userID string) (*models.Email, *errx.Error) { 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 cdd1409b7..c805cd105 100644 --- a/internal/app/connectlink/partner.go +++ b/internal/app/connectlink/partner.go @@ -19,7 +19,8 @@ type PartnerLinks interface { 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 + // An error means it could not be told, and the sign-in is refused rather than let anyone in. + RequiredAddress(ctx context.Context, linkID uuid.UUID) (string, error) // 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 60a9ca9e5..799c37c53 100644 --- a/internal/app/connectlink/partner_test.go +++ b/internal/app/connectlink/partner_test.go @@ -2,6 +2,7 @@ package connectlink import ( "context" + "errors" "net/url" "strings" "testing" @@ -17,14 +18,15 @@ type fakePartnerLinks struct { link uuid.UUID hint string required string + lookupErr error connected []uuid.UUID } -func (p *fakePartnerLinks) RequiredAddress(_ context.Context, id uuid.UUID) string { +func (p *fakePartnerLinks) RequiredAddress(_ context.Context, id uuid.UUID) (string, error) { if id != p.link { - return "" + return "", nil } - return p.required + return p.required, p.lookupErr } func (p *fakePartnerLinks) LoginHint(_ context.Context, id uuid.UUID) string { @@ -179,3 +181,17 @@ func startHinted(t *testing.T, s *Service, token string) string { u, _ := url.Parse(started.URL) return u.Query().Get("state") } + +// When the link's mailbox cannot be looked up, nobody is let in: nothing is stored and the link stays usable. +func TestRequiredAddressLookupFailureRefusesTheSignIn(t *testing.T) { + s, repo, mb, _, token := newFixture(t) + repo.link.MaxMailboxes = 1 + p := &fakePartnerLinks{link: repo.link.ID, hint: "jordan@client.test", lookupErr: errors.New("db down")} + s.WirePartnerLinks(p) + if _, _, xerr := s.FinishOAuth(context.Background(), "outlook", "code", startHinted(t, s, token), ""); xerr == nil { + t.Fatal("a failed lookup let the sign-in through") + } + if mb.calls != 0 || len(p.connected) != 0 || repo.link.MailboxesConnected != 0 { + t.Fatalf("calls %d, partner told of %v, used %d", mb.calls, p.connected, repo.link.MailboxesConnected) + } +} diff --git a/internal/app/partner/connect.go b/internal/app/partner/connect.go index f185f7202..21e24d8a7 100644 --- a/internal/app/partner/connect.go +++ b/internal/app/partner/connect.go @@ -123,7 +123,7 @@ func (s *Service) LoginHint(ctx context.Context, linkID uuid.UUID) string { } // 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 "" } +func (s *Service) RequiredAddress(context.Context, uuid.UUID) (string, error) { return "", nil } // 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) { diff --git a/internal/app/store/signin.go b/internal/app/store/signin.go index df154aed6..cbadaf9a0 100644 --- a/internal/app/store/signin.go +++ b/internal/app/store/signin.go @@ -142,8 +142,16 @@ func (l SignInLinks) LoginHint(ctx context.Context, linkID uuid.UUID) string { // 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) +// A failed lookup is an error, never "any account": LoginHint cannot tell it from another link's. +func (l SignInLinks) RequiredAddress(ctx context.Context, linkID uuid.UUID) (string, error) { + if l.s == nil || l.s.signIns == nil { + return "", nil + } + row, err := l.s.signIns.Get(ctx, linkID) + if err != nil || row == nil { + return "", err + } + return row.Email, nil } // ReturnURL: a store link ends on the hosted connect page's own result. @@ -177,7 +185,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 + RequiredAddress(ctx context.Context, linkID uuid.UUID) (string, error) Connected(ctx context.Context, linkID uuid.UUID, acc *models.Email) ReturnURL(ctx context.Context, linkID uuid.UUID, status string) (string, bool) } @@ -192,14 +200,15 @@ 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 { +// RequiredAddress is the first non-empty one; any lookup error refuses the sign-in. +func (c PartnerLinksChain) RequiredAddress(ctx context.Context, linkID uuid.UUID) (string, error) { for _, p := range c { - if a := p.RequiredAddress(ctx, linkID); a != "" { - return a + a, err := p.RequiredAddress(ctx, linkID) + if err != nil || a != "" { + return a, err } } - return "" + return "", nil } // Connected tells each one; each ignores links it did not mint. diff --git a/internal/app/store/signin_test.go b/internal/app/store/signin_test.go index a7c65b095..5a3969b9b 100644 --- a/internal/app/store/signin_test.go +++ b/internal/app/store/signin_test.go @@ -2,6 +2,7 @@ package store import ( "context" + "errors" "testing" "github.com/google/uuid" @@ -11,14 +12,17 @@ import ( ) type stubLinks struct { - hint, back string - required string - ok bool - connected int + hint, back string + required string + requiredErr error + ok bool + connected int } -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) LoginHint(context.Context, uuid.UUID) string { return s.hint } +func (s *stubLinks) RequiredAddress(context.Context, uuid.UUID) (string, error) { + return s.required, s.requiredErr +} func (s *stubLinks) Connected(context.Context, uuid.UUID, *models.Email) { s.connected++ } @@ -42,12 +46,16 @@ 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) + if got, err := c.RequiredAddress(ctx, id); got != "" || err != nil { + t.Fatalf("a hint alone requires no address: %q %v", got, err) } storeLinks.required = "store@b.com" - if got := c.RequiredAddress(ctx, id); got != "store@b.com" { - t.Fatalf("required %q", got) + if got, err := c.RequiredAddress(ctx, id); got != "store@b.com" || err != nil { + t.Fatalf("required %q %v", got, err) + } + storeLinks.required, storeLinks.requiredErr = "", errors.New("db down") + if _, err := c.RequiredAddress(ctx, id); err == nil { + t.Fatal("a failed lookup must not read as any account") } }