Skip to content

fix(security): handle WorkOS user.deleted for primary-bound users (#3718) - #4617

Merged
bokelley merged 1 commit into
mainfrom
fix/3718-workos-user-deleted-primary-bound
May 17, 2026
Merged

bokelley merged 1 commit into
mainfrom
fix/3718-workos-user-deleted-primary-bound

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

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_id drops the binding. The identity is then left with zero primaries:

  • attachIdentityId resolves primary_workos_user_id to NULL and skips the id-swap.
  • The surviving non-primary user signs in successfully but to an empty workspace — all their app-state was keyed on the now-deleted primary's 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.deleted switch case in server/src/routes/workos-webhooks.ts:

  1. Promote before CASCADE. Call promoteSecondaryIfPrimaryDeleted (new helper in server/src/db/identity-db.ts) which, in a single transaction, picks the longest-bound surviving secondary on the identity and flips is_primary from the deleted user's binding onto it. Mirrors the findSuccessorForPromotion pattern already used by deleteMembership. attachIdentityId then routes both the dead binding and the surviving secondary to the new primary, keeping app-state reads intact.
  2. Invalidate sessions. Call 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.
  3. Return 200 on promotion failure. The helper swallows DB errors, emits logger.warn (auto-routes to #admin-errors via posthog.ts:201-205) plus an explicit notifySystemError ops 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 — the attachIdentityId indirection already handles routing, per scope guard.

Surface area

File Change
server/src/db/identity-db.ts New: promoteSecondaryIfPrimaryDeleted helper
server/src/routes/workos-webhooks.ts Wire helper + invalidateSessionsForUsers into user.deleted
server/tests/integration/workos-user-deleted-primary.test.ts Integration: real-DB exercise of helper
server/tests/unit/workos-webhook-user-deleted-wiring.test.ts Static-analysis: handler wiring + error-containment assertions
.changeset/3718-workos-user-deleted-primary.md Patch, security

~415 lines total (within scope guard).

Test plan

  • Integration: vitest run server/tests/integration/workos-user-deleted-primary.test.ts — 4/4 pass
    • Asserts longest-bound secondary becomes primary, partial unique index holds (exactly one primary)
    • Asserts null + no-op when identity has no secondaries (single-credential normal-deletion path)
    • Asserts null + no-op when deleted user is not primary
    • Asserts CASCADE removes the (now non-primary) binding after promotion
  • Unit (static analysis): vitest run server/tests/unit/workos-webhook-user-deleted-wiring.test.ts — 6/6 pass
    • promoteSecondaryIfPrimaryDeleted is called BEFORE deleteUser in the switch case
    • invalidateSessionsForUsers is called in the case body
    • Helper catches DB errors + returns null (no thrown error escapes to outer 500)
    • Helper emits logger.warn and notifySystemError on failure
  • npx tsc --noEmit -p server/tsconfig.json — no new errors in touched files
  • No regression on identity-layer.test.ts (7/7 pass) or workos-webhook-email-refresh.test.ts (2/2 pass)

Out of scope

  • Re-keying app-state FKs from the deleted user to the new primary — the attachIdentityId indirection already handles this routing.
  • Changing deleteMembership — just reused its findSuccessorForPromotion pattern.

🤖 Generated with Claude Code

)

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]>
@bokelley
bokelley merged commit e22f19a into main May 17, 2026
13 checks passed
@bokelley
bokelley deleted the fix/3718-workos-user-deleted-primary-bound branch May 17, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Identity layer M2: handle WorkOS deletion of a primary-bound user

1 participant