Repository navigation
fix(security): handle WorkOS user.deleted for primary-bound users (#3718) - #4617
Merged
Merged
Conversation
) When a WorkOS user that is the primary credential on a multi-credential identity was deleted (operator action, account closure, GDPR/CCPA erasure webhook), the CASCADE on identity_workos_users.workos_user_id dropped the binding and left the identity with zero primaries. attachIdentityId then resolved primary_workos_user_id to NULL, skipped the id-swap, and the surviving secondary signed in to an empty workspace — a denial-of-service against any non-primary user, reachable end-user-initiated via GDPR/CCPA. The user.deleted handler now promotes the longest-bound surviving secondary to primary in a single transaction before the CASCADE fires, mirroring the findSuccessorForPromotion pattern already used by deleteMembership. The handler also invalidates the session/JWT cache for both the deleted user and the promoted successor to close the 60-second window where a cached id-swap could still route reads to the dead binding. Promotion failures emit logger.warn (auto-routed to #admin-errors) plus an explicit notifySystemError ops alert, then return 200 so WorkOS does not retry-storm the webhook; the identity is left in a recoverable state for admin repair. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This was referenced Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3718.
When a WorkOS user that is the primary credential on a multi-credential identity is deleted (operator action, account closure, GDPR/CCPA erasure webhook), the CASCADE on
identity_workos_users.workos_user_iddrops the binding. The identity is then left with zero primaries:attachIdentityIdresolvesprimary_workos_user_idto NULL and skips the id-swap.workos_user_id.That's a denial-of-service against any non-primary user, reachable end-user-initiated via GDPR/CCPA flows in WorkOS — not just operator action.
Fix (Option A — auto-promote successor)
In the
user.deletedswitch case inserver/src/routes/workos-webhooks.ts:promoteSecondaryIfPrimaryDeleted(new helper inserver/src/db/identity-db.ts) which, in a single transaction, picks the longest-bound surviving secondary on the identity and flipsis_primaryfrom the deleted user's binding onto it. Mirrors thefindSuccessorForPromotionpattern already used bydeleteMembership.attachIdentityIdthen routes both the dead binding and the surviving secondary to the new primary, keeping app-state reads intact.invalidateSessionsForUsers([deletedUserId, promotedUserId])to close the 60-second session/JWT cache window where a cached pre-deletion id-swap could still route reads to the dead binding.logger.warn(auto-routes to#admin-errorsviaposthog.ts:201-205) plus an explicitnotifySystemErrorops alert, and returns null. The handler awaits the helper directly without a try/catch, so the outer 500 path is never reached; WorkOS doesn't retry-storm.The CASCADE still drops the (now non-primary) binding when
deleteUser()runs. App-state FKs aren't rewritten — theattachIdentityIdindirection already handles routing, per scope guard.Surface area
server/src/db/identity-db.tspromoteSecondaryIfPrimaryDeletedhelperserver/src/routes/workos-webhooks.tsinvalidateSessionsForUsersintouser.deletedserver/tests/integration/workos-user-deleted-primary.test.tsserver/tests/unit/workos-webhook-user-deleted-wiring.test.ts.changeset/3718-workos-user-deleted-primary.md~415 lines total (within scope guard).
Test plan
vitest run server/tests/integration/workos-user-deleted-primary.test.ts— 4/4 passvitest run server/tests/unit/workos-webhook-user-deleted-wiring.test.ts— 6/6 passpromoteSecondaryIfPrimaryDeletedis called BEFOREdeleteUserin the switch caseinvalidateSessionsForUsersis called in the case bodylogger.warnandnotifySystemErroron failurenpx tsc --noEmit -p server/tsconfig.json— no new errors in touched filesidentity-layer.test.ts(7/7 pass) orworkos-webhook-email-refresh.test.ts(2/2 pass)Out of scope
attachIdentityIdindirection already handles this routing.deleteMembership— just reused itsfindSuccessorForPromotionpattern.🤖 Generated with Claude Code