Merge pull request #1326 from warmbly/fix/signin-required-address-errors

fix(connectlink): refuse a sign-in when the link's required address cannot be read
This commit is contained in:
Matthew Meszaros
2026-11-27 10:47:59 +01:00
committed by GitHub
6 changed files with 63 additions and 24 deletions
+6 -1
View File
@@ -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)
+2 -1
View File
@@ -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.
+19 -3
View File
@@ -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)
}
}
+1 -1
View File
@@ -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) {
+17 -8
View File
@@ -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.
+18 -10
View File
@@ -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")
}
}