feat: refuse workspace imports that would pair an imported OAuth app id with a different client in either conflict mode, with live coverage of each collision

This commit is contained in:
Matthew Meszaros
2026-11-09 17:25:41 +01:00
parent 1211340960
commit 03469b799b
3 changed files with 109 additions and 0 deletions
+29
View File
@@ -362,6 +362,11 @@ func (s *service) importTable(
if foreign > 0 {
return fmt.Errorf("%s: %w", t.Name, ErrForeignRows)
}
if t.Name == oauthAppsTable {
if err := s.refuseChangedOAuthApps(ctx, tx, orgID, batch); err != nil {
return err
}
}
n, err := s.repo.InsertBatch(ctx, tx, t.Name, insertCols, batch, ic.conflict, pk, t.OwnerScope(), orgID, resetOnOverwrite, invalidations)
if err != nil {
if t.Name == oauthAppsTable && repository.IsUniqueViolation(err) {
@@ -1019,3 +1024,27 @@ func (s *service) requireImportedOAuthApps(ctx context.Context, tx pgx.Tx, orgID
}
return nil
}
// refuseChangedOAuthApps refuses an app id the destination holds under another client, in either conflict mode.
func (s *service) refuseChangedOAuthApps(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, batch []json.RawMessage) error {
ids := make([]uuid.UUID, 0, len(batch))
clients := make([]string, 0, len(batch))
for _, raw := range batch {
var row struct {
ID uuid.UUID `json:"id"`
ClientID string `json:"client_id"`
}
if err := json.Unmarshal(raw, &row); err != nil {
return err
}
ids, clients = append(ids, row.ID), append(clients, row.ClientID)
}
changed, err := s.repo.ChangedOAuthAppClients(ctx, tx, orgID, ids, clients)
if err != nil {
return err
}
if changed > 0 {
return ErrOAuthAppConflict
}
return nil
}
@@ -0,0 +1,70 @@
package orgtransfer
import (
"context"
"errors"
"testing"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/models"
)
// A grant can only be refreshed by the client that issued it, so an import never leaves an imported app's id
// pointing at a different client, in either conflict mode, and never rewrites the client of an app the
// destination already holds.
func TestLiveImportRefusesAnOAuthAppThatWouldChangeClient(t *testing.T) {
svc, pool := liveImportService(t)
ctx := context.Background()
appRow := func(id, org uuid.UUID, client string) map[string]any {
return map[string]any{
"id": id, "organization_id": org, "provider": "gmail", "client_id": client,
"client_secret": "", "app_name": "Acme Mail", "redirect_url": "",
}
}
cases := []struct {
name string
conflict models.OrgImportConflict
sameID bool
client string
refused bool
}{
{"same id, other client, overwrite", models.OrgImportConflictOverwrite, true, "other.apps.googleusercontent.com", true},
{"same id, other client, skip", models.OrgImportConflictSkip, true, "other.apps.googleusercontent.com", true},
{"other id, same provider, overwrite", models.OrgImportConflictOverwrite, false, "dest.apps.googleusercontent.com", true},
{"other id, same provider, skip", models.OrgImportConflictSkip, false, "dest.apps.googleusercontent.com", true},
{"same id, same client, skip", models.OrgImportConflictSkip, true, "dest.apps.googleusercontent.com", false},
{"same id, same client, overwrite", models.OrgImportConflictOverwrite, true, "dest.apps.googleusercontent.com", false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
dest := newTenantFixture(t, pool)
existing := uuid.New()
if _, err := pool.Exec(ctx, `INSERT INTO organization_oauth_apps (id, organization_id, provider, client_id, client_secret, app_name)
VALUES ($1, $2, 'gmail', 'dest.apps.googleusercontent.com', 'sealed', 'Dest Mail')`, existing, dest.org); err != nil {
t.Fatalf("fixture app: %v", err)
}
archived := uuid.New()
if c.sameID {
archived = existing
}
archive := tenantArchive(t, map[string][]map[string]any{
"organization_oauth_apps": {appRow(archived, uuid.New(), c.client)},
})
_, err := svc.ImportFrom(ctx, dest.org, archive, ImportOptions{Conflict: c.conflict, ActorUserID: dest.owner}, nil)
if c.refused != errors.Is(err, ErrOAuthAppConflict) {
t.Fatalf("import err = %v, want refused=%v", err, c.refused)
}
if !c.refused && err != nil {
t.Fatalf("import: %v", err)
}
var client string
if err := pool.QueryRow(ctx, `SELECT client_id FROM organization_oauth_apps WHERE id = $1`, existing).Scan(&client); err != nil {
t.Fatal(err)
}
if client != "dest.apps.googleusercontent.com" {
t.Fatalf("the destination app's client changed to %s", client)
}
})
}
}
+10
View File
@@ -91,6 +91,8 @@ type OrgTransferRepository interface {
MarkMailboxesNeedingReconnect(ctx context.Context, tx pgx.Tx, orgID uuid.UUID) (int64, error)
// MissingOAuthApps counts the archived OAuth app ids the destination workspace does not hold after their insert.
MissingOAuthApps(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, ids []uuid.UUID) (int64, error)
// ChangedOAuthAppClients counts archived OAuth apps whose id the destination already holds under a different client.
ChangedOAuthAppClients(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, ids []uuid.UUID, clientIDs []string) (int64, error)
// Begin starts the import transaction.
Begin(ctx context.Context) (pgx.Tx, error)
@@ -1263,3 +1265,11 @@ func (r *orgTransferRepository) MissingOAuthApps(ctx context.Context, tx pgx.Tx,
WHERE NOT EXISTS (SELECT 1 FROM organization_oauth_apps a WHERE a.id = u.id AND a.organization_id = $1)`, orgID, ids).Scan(&n)
return n, err
}
func (r *orgTransferRepository) ChangedOAuthAppClients(ctx context.Context, tx pgx.Tx, orgID uuid.UUID, ids []uuid.UUID, clientIDs []string) (int64, error) {
var n int64
err := tx.QueryRow(ctx, `SELECT count(*) FROM unnest($2::uuid[], $3::text[]) AS u(id, client_id)
JOIN organization_oauth_apps a ON a.id = u.id
WHERE a.organization_id <> $1 OR a.client_id <> u.client_id`, orgID, ids, clientIDs).Scan(&n)
return n, err
}