Skip to content

Commit 2223c25

Browse files
committed
fix(sso): roll back the SSO provider by row id, not logical keys
The compensating rollback deleted by (providerId, orgId). providerId is unique, so if this request's row were deleted and recreated by a concurrent registration in the narrow window before the rollback, the logical-key delete would remove that other request's provider. Delete by the primary-key id registerSSOProvider returns instead, so only the exact row this request created is ever removed.
1 parent 8cea6b9 commit 2223c25

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

  • apps/sim/app/api/auth/sso/register

apps/sim/app/api/auth/sso/register/route.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -574,18 +574,19 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
574574

575575
// Close the residual TOCTOU between the re-check above and Better Auth
576576
// persisting the provider: the verified sso_domain row could be removed in
577-
// that window. Only roll back a provider this request just created (guarded
578-
// by providerExistedBefore), scoped to (providerId, orgId). Personal SSO is
577+
// that window. Roll back only the exact row this request created — delete by
578+
// its primary-key `id`, not the logical (providerId, orgId): providerId is
579+
// unique, so if our row were deleted and recreated by a concurrent
580+
// registration in this window, a logical-key delete would remove that other
581+
// request's row. Guarded by providerExistedBefore so a hypothetical future
582+
// update of a pre-existing provider is never rolled back. Personal SSO is
579583
// not gated, so this only runs for org-scoped registration.
580584
if (orgId && !providerExistedBefore && !(await isOrgDomainVerified())) {
585+
// double-cast-allowed: registerSSOProvider spreads the created row's `id` at runtime but Better Auth's return type omits it
586+
const createdRowId = (registration as unknown as { id: string }).id
581587
await db
582588
.delete(ssoProvider)
583-
.where(
584-
and(
585-
eq(ssoProvider.providerId, registration.providerId),
586-
eq(ssoProvider.organizationId, orgId)
587-
)
588-
)
589+
.where(and(eq(ssoProvider.id, createdRowId), eq(ssoProvider.organizationId, orgId)))
589590
logger.warn('Rolled back SSO provider: domain verification revoked mid-registration', {
590591
domain,
591592
orgId,

0 commit comments

Comments
 (0)