Skip to content

fix(den-api): refuse SCIM-deprovisioned SSO sign-ins before Better Auth creates the user - #4775

Merged
yomgui merged 1 commit into
devfrom
fix/scim-deactivated-sso-ghost
Sep 10, 2026
Merged

yomgui merged 1 commit into
devfrom
fix/scim-deactivated-sso-ghost

Conversation

@yomgui

@yomgui yomgui commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Summary

  • A member deactivated over SCIM (active=false) who still followed the SSO link was refused at the callback with the intended 403 (This user was deprovisioned by SCIM…), but only inside provisionUser, after Better Auth had already committed a fresh user, account, and session row. In single-org mode the session.create hook (ensureSingletonOrganizationForUser) also re-created their membership.
  • The ghost user then made the IdP's re-provision POST /Users collide with 409 uniqueness, contradicting packages/docs/cloud/scim-okta.mdx §5.4 and breaking Okta reactivation for anyone who tried to sign in while deactivated.
  • Fix: refuse the deprovisioned email in Den's databaseHooks.user.create.before when the endpoint carries an SSO :providerId (OIDC callback and SAML ACS share this), so no rows are written and the 403 status/message are unchanged; and make ensureSingletonOrganizationForUser skip SCIM-deprovisioned identities.

Why

  • Reproduced identically on the published 0.18.43 and 0.18.45 images (self-hosted, single_org): not a regression, but a long-standing SSO/SCIM lifecycle gap.

Issue

  • Internal self-hosted SSO/SCIM validation finding F1 (no public issue).

Scope

  • ee/apps/den-api/src/scim-deprovisioning.ts: shared SCIM_DEPROVISIONED_SIGN_IN_MESSAGE and isScimDeprovisionedEmailForSsoProvider (tombstone lookup via the SSO provider's organization).
  • ee/apps/den-api/src/auth.ts: user.create.before refuses SSO-created users for tombstoned emails; provisionUser reuses the shared message (unchanged text).
  • ee/apps/den-api/src/orgs.ts: ensureSingletonOrganizationForUser returns null for SCIM-deprovisioned identities.
  • evals/specs/scim-deprovisioned-sso-reprovision.test.ts: new PR-lane journey (multi-org and single-org Den) that provisions over SCIM, deactivates, drives the real OIDC callback via the mock IdP, and re-provisions.

Out of scope

  • Multi-org Dens where the deprovisioned user keeps another organization's membership: POST /Users still returns 409 because the SCIM plugin's linkExistingUsers is off (pre-existing, also on the clean path).
  • Identity semantics on re-provision (new user id, as the existing scim-okta-lifecycle e2e asserts) are unchanged.
  • Docs: unchanged; the fix makes §5.3/§5.4 hold without a caveat.

Testing

Ran

  • pnpm evals:pr specs/scim-deprovisioned-sso-reprovision.test.ts (local lane; red before the fix, green after)
  • pnpm evals:e2e scim-okta-lifecycle --local
  • pnpm --filter @openwork-ee/den-api test
  • DATABASE_URL=… pnpm --filter @openwork-ee/den-api test:db
  • bun test --conditions development test/<file>.test.ts for scim-auth-sync, scim-groups, scim-token-storage, sso-jit, sso-saml-policy, sso-saml-response-policy, sso-resolve-helpers, sso-resolve-route, sso-account-linking-policy, sso-entra-domain, sso-readiness, member-removal-rejoin
  • pnpm --filter @openwork-ee/den-api exec tsc --noEmit -p tsconfig.json

Result

  • pass/fail: pass. Red-first: before the fix the new spec failed with ghost user rows after refused SSO (multi_org|single_org): expected 1 to have length 0; with the user-row assertions removed it went on to fail with re-provision after refused SSO: HTTP 409 … scimType uniqueness (multi-org) and ghost membership after refused SSO (single_org): expected true to be false. After the fix: 2/2 passed; scim-okta-lifecycle 1/1 passed; den-api test 24/24; test:db 2/2; SSO/SCIM unit files 39/39; tsc exit 0.
  • Pre-existing, reproduced on a clean origin/dev (7d5097d) worktree with the same command: member-removal-rejoin.test.ts › "a new explicit invitation activates its placeholder as a fresh member lifecycle" (Expected: "admin" Received: "member").

CI status

  • pass: pending
  • code-related failures: none known
  • external/env/auth blockers: none known

Manual verification

  1. In a Den with SSO enabled and SCIM connected, provision a user over SCIM, then PATCH /Users/{id} with active=false.
  2. Complete an SSO sign-in for that email: the callback returns 403 This user was deprovisioned by SCIM…, no session cookie, and the user table has no row for that email; in single_org mode /v1/org lists no joined member for it.
  3. POST /Users for the same userName returns 201 and the member re-joins.

Evidence

  • Sticky test-evidence comment: new spec, single-org Den run (PR head 29a8a704d).
  • "Additional test evidence" comment: new spec, multi-org Den run, and scim-okta-lifecycle e2e run, both on the same head. The legacy publisher shows one test per sticky comment and the review-app multi-run publication needs the CI-only private review Blob token, so those two are rendered from their stored runs.
  • Lane: local for every run (these specs read the isolated local testkit database, like the existing SCIM journeys); no screenshots exist for server-boundary specs.
  • Local Warden final review on 29a8a704d vs origin/dev 7d5097d42: pnpm warden:check, all 4 skills completed (diff-security-review, confidentiality-review, spec-provenance-review, desktop-den-sync-review), no findings.

Risk

  • Low. The new check only runs on Better Auth user creation for endpoints with a :providerId param (SSO callbacks) and is a single indexed lookup; the single-org guard runs one tombstone/identity lookup per session creation.

Rollback

  • Revert this commit.

…th creates the user

A member deactivated over SCIM who still followed the SSO link was correctly
refused at the callback, but only in provisionUser, after Better Auth had
already committed a fresh user, account, and session; in single-org mode the
session hook also re-created their membership. The ghost user then made the
IdP's re-provision POST /Users collide with 409 uniqueness.

Refuse the deprovisioned email in the user.create.before hook for SSO
callbacks (both the OIDC callback and the SAML ACS carry :providerId), so no
rows are written and the 403 response and message are unchanged, and make
ensureSingletonOrganizationForUser skip SCIM-deprovisioned identities.

Journey: evals/specs/scim-deprovisioned-sso-reprovision.test.ts drives the
real OIDC callback through the mock IdP in multi-org and single-org Dens.
@vercel

vercel Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
openwork-app Ready Ready Preview Sep 10, 2026 5:02am UTC
openwork-den Ready Ready Preview Sep 10, 2026 5:02am UTC
openwork-diagnostics Ready Ready Preview Sep 10, 2026 5:02am UTC
openwork-landing Ready Ready Preview, v0 Sep 10, 2026 5:02am UTC

@github-actions

Copy link
Copy Markdown
Contributor

Warden review summary — Clear

Native findings: 1. Blocking policy matches before consolidation: 0. Full finding text remains in Warden's native checks and review threads. This comment retains only native IDs, severity, skill attribution, and safe locations. Analysis run

Reported trigger coverage (not proof of complete repository or context coverage): diff-security-review (121645 ms), confidentiality-review (75507 ms), spec-provenance-review (160022 ms), desktop-den-sync-review (109602 ms).

Blockers

  • None.

Advisories

  • R22-GNY · medium · spec-provenance-review · evals/specs/scim-deprovisioned-sso-reprovision.test.ts:303

Needs recheck

  • None.

Observed review-thread metrics

  • Unique observed run attempts in retained window: 1
  • Distinct heads in retained window: 1
  • Current unresolved Warden threads: 0 of 0 observed
  • Observed resolved/unresolved transitions in retained snapshots: 0 / 0
  • First observed resolved/unresolved (state at first collection, not event time): 0 / 0
  • Precision: unavailable (no adjudications; no TP/FP values are inferred)
  • Observation window: 1/20 run attempts retained; truncated: false
  • Transition snapshot: 0/0 current attributed threads retained (limit 500); truncated: false

Thread states are read-only collection snapshots. These metrics are neither lifetime totals nor exact resolution times, and this summary is not clearance authority.

@diff-warden diff-warden Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warden security clearance: clear. No blocking findings (security or desktop↔den sync) in this diff (29a8a704d7c22b2df3924805b534f268eeff09a4). Automated clearance satisfies the required-review gate only — a human still reviews and merges. Analysis run

@yomgui

yomgui commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Additional test evidence (same PR head 29a8a704d)

The sticky test-evidence comment below holds the single-org run of the new spec. The legacy publisher shows one test per sticky comment and the multi-run review-app publication needs a private review Blob token that is only configured in CI, so the other two head-matching runs are rendered here verbatim (sticky marker removed so they do not compete with the sticky comment). No screenshots exist for these server-boundary specs.

Test evidence — a SCIM-deactivated member who tries SSO is refused without a ghost identity, so the IdP can re-provision them (multi-org Den) — ✅ passed

SHA 29a8a70 · engine v1

✅ passed · 2 expectations passed · 0 failed · 0 pending

Rendered offline from the stored test run; this spec records no screenshots.

verdict passed · 0 user observations · 0 probes · steps 0/0

ℹ️ ASSERTION — 1. A SCIM-deprovisioned person is refused at the SSO callback without leaving a ghost identity (multi_org)

The OIDC callback returned 403 "This user was deprovisioned by SCIM. Reactivate them in the identity provider before signing in." with no session cookie; afterwards the database holds 0 user row(s) and 0 session row(s) for [email protected], /v1/org lists no joined member for that email, and [email protected] remains member.

  • ✅ PASS A SCIM-deprovisioned person is refused at the SSO callback without leaving a ghost identity (multi_org) — The OIDC callback returned 403 "This user was deprovisioned by SCIM. Reactivate them in the identity provider before signing in." with no session cookie; afterwards the database holds 0 user row(s) and 0 session row(s) for [email protected], /v1/org lists no joined member for that email, and [email protected] remains member.

ℹ️ ASSERTION — 2. The IdP's re-provision restores access after the refused SSO attempt (multi_org)

POST /Users for [email protected] returned 201 with id usr_01m24v47rse5x99xw9718k3stb; GET reports active=true, /v1/org lists the member as member, exactly 1 user row exists, password sign-in returned 403, and [email protected] is still member.

  • ✅ PASS The IdP's re-provision restores access after the refused SSO attempt (multi_org) — POST /Users for [email protected] returned 201 with id usr_01m24v47rse5x99xw9718k3stb; GET reports active=true, /v1/org lists the member as member, exactly 1 user row exists, password sign-in returned 403, and [email protected] is still member.

Test run created 2026-09-10T05:01:54.224Z · Source: evals/results/test-runs/2026-09-10T05-01-54-223Z-a-scim-deactivated-member-who-tries-sso-is-refused-without-a-ghost-identity-so-t/test-run.json · Repro: pnpm --dir evals artifacts:publish -- --pr 4775 --test-run 2026-09-10T05-01-54-223Z-a-scim-deactivated-member-who-tries-sso-is-refused-without-a-ghost-identity-so-t


Test evidence — an Okta-shaped SCIM client provisions, renames, deprovisions, and reactivates a member without ever granting password access — ✅ passed

SHA 29a8a70 · engine v1

✅ passed · 8 expectations passed · 0 failed · 0 pending

Rendered offline from the stored test run; this spec records no screenshots.

verdict passed · 0 user observations · 0 probes · steps 0/0

ℹ️ ASSERTION — 1. Okta SAML registration persists a usable ACS callback configuration

POSTing a deliberately malformed assertion to the advertised ACS URL reached response-policy validation and returned invalid_encoding, not invalid_saml_configuration.

  • ✅ PASS Okta SAML registration persists a usable ACS callback configuration — POSTing a deliberately malformed assertion to the advertised ACS URL reached response-policy validation and returned invalid_encoding, not invalid_saml_configuration.

ℹ️ ASSERTION — 2. Den issues an org-scoped SCIM bearer and rejects the garbage bearer without mutation

POST /v1/scim/token advertised http://127.0.0.1:62024/api/auth/scim/v2; Bearer nonsense returned 401, and the new real bearer then listed totalResults=0.

ℹ️ ASSERTION — 3. Okta's pre-create lookup finds no existing assignment

GET Users?filter=userName eq [email protected] returned 200 with totalResults=0 and no Resources, so the later create cannot be mistaken for an update.

  • ✅ PASS Okta's pre-create lookup finds no existing assignment — GET Users?filter=userName eq [email protected] returned 200 with totalResults=0 and no Resources, so the later create cannot be mistaken for an update.

ℹ️ ASSERTION — 4. Okta creates only federated access

After the zero-user baseline, the real token lists exactly one SCIM resource ([email protected]), /v1/org includes that member within the 60s org-context cache window, and password sign-in returned 403; the rejected garbage call therefore added no extra resource.

  • ✅ PASS Okta creates only federated access — After the zero-user baseline, the real token lists exactly one SCIM resource ([email protected]), /v1/org includes that member within the 60s org-context cache window, and password sign-in returned 403; the rejected garbage call therefore added no extra resource.

ℹ️ ASSERTION — 5. Okta's full replace updates the family name without retaining the old profile

PUT and GET returned 200/200; GET formatted the name as Avery Nguyen, not Avery Morgan.

  • ✅ PASS Okta's full replace updates the family name without retaining the old profile — PUT and GET returned 200/200; GET formatted the name as Avery Nguyen, not Avery Morgan.

ℹ️ ASSERTION — 6. Okta deprovisioning cannot be bypassed and does not touch the control member

PATCH active=false returned 204; [email protected] has no joined membership after invite-backed sign-up returned 200 and explicit acceptance was refused with scim_deprovisioned. Invitations cannot resurrect an IdP-deprovisioned member: the refusal is org-scoped, the invitation remains unaccepted with userId null, and [email protected]'s existing session and membership remain intact.

  • ✅ PASS Okta deprovisioning cannot be bypassed and does not touch the control member — PATCH active=false returned 204; [email protected] has no joined membership after invite-backed sign-up returned 200 and explicit acceptance was refused with scim_deprovisioned. Invitations cannot resurrect an IdP-deprovisioned member: the refusal is org-scoped, the invitation remains unaccepted with userId null, and [email protected]'s existing session and membership remain intact.

ℹ️ ASSERTION — 7. The IdP cannot silently absorb a pre-existing personal account

Re-provisioning [email protected], now owned by a self-created account, returned SCIM 409 with scimType=uniqueness.

  • ✅ PASS The IdP cannot silently absorb a pre-existing personal account — Re-provisioning [email protected], now owned by a self-created account, returned SCIM 409 with scimType=uniqueness.

ℹ️ ASSERTION — 8. Okta re-provisioning is the only way back in and still grants no password access

Okta deprovisioned [email protected], leaving old id usr_01m24v6v1ge66rp0r4mxz229xs at 404, then POSTed the same userName/externalId into new id usr_01m24v6v7ke938x2gaqhjf58sx; the new identity joined active with totalResults=1, while password sign-in returned 403.

  • ✅ PASS Okta re-provisioning is the only way back in and still grants no password access — Okta deprovisioned [email protected], leaving old id usr_01m24v6v1ge66rp0r4mxz229xs at 404, then POSTed the same userName/externalId into new id usr_01m24v6v7ke938x2gaqhjf58sx; the new identity joined active with totalResults=1, while password sign-in returned 403.

Test run created 2026-09-10T05:03:15.081Z · Source: evals/results/test-runs/2026-09-10T05-03-15-080Z-an-okta-shaped-scim-client-provisions-renames-deprovisions-and-reactivates-a-mem/test-run.json · Repro: pnpm --dir evals artifacts:publish -- --pr 4775 --test-run 2026-09-10T05-03-15-080Z-an-okta-shaped-scim-client-provisions-renames-deprovisions-and-reactivates-a-mem


@yomgui

yomgui commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Test evidence — a SCIM-deactivated member who tries SSO is refused without a ghost identity, so the IdP can re-provision them (single-org Den) — ✅ passed

SHA 29a8a70 · engine v1

✅ passed · 2 expectations passed · 0 failed · 0 pending

verdict passed · 0 user observations · 0 probes · steps 0/0

ℹ️ ASSERTION — 1. A SCIM-deprovisioned person is refused at the SSO callback without leaving a ghost identity (single_org)

The OIDC callback returned 403 "This user was deprovisioned by SCIM. Reactivate them in the identity provider before signing in." with no session cookie; afterwards the database holds 0 user row(s) and 0 session row(s) for [email protected], /v1/org lists no joined member for that email, and [email protected] remains owner.

  • ✅ PASS A SCIM-deprovisioned person is refused at the SSO callback without leaving a ghost identity (single_org) — The OIDC callback returned 403 "This user was deprovisioned by SCIM. Reactivate them in the identity provider before signing in." with no session cookie; afterwards the database holds 0 user row(s) and 0 session row(s) for [email protected], /v1/org lists no joined member for that email, and [email protected] remains owner.

ℹ️ ASSERTION — 2. The IdP's re-provision restores access after the refused SSO attempt (single_org)

POST /Users for [email protected] returned 201 with id usr_01m24v59h8eqv8wca2n1v3dfc6; GET reports active=true, /v1/org lists the member as member, exactly 1 user row exists, password sign-in returned 403, and [email protected] is still owner.

  • ✅ PASS The IdP's re-provision restores access after the refused SSO attempt (single_org) — POST /Users for [email protected] returned 201 with id usr_01m24v59h8eqv8wca2n1v3dfc6; GET reports active=true, /v1/org lists the member as member, exactly 1 user row exists, password sign-in returned 403, and [email protected] is still owner.

Test run created 2026-09-10T05:02:27.852Z · Source: evals/results/test-runs/2026-09-10T05-02-27-851Z-a-scim-deactivated-member-who-tries-sso-is-refused-without-a-ghost-identity-so-t/test-run.json · Repro: pnpm --dir evals artifacts:publish -- --pr 4775 --test-run 2026-09-10T05-02-27-851Z-a-scim-deactivated-member-who-tries-sso-is-refused-without-a-ghost-identity-so-t

@yomgui
yomgui merged commit 82d98b1 into dev Sep 10, 2026
36 checks passed

This branch was successfully deployed

4 active deployments
Preview – openwork-landing — 29a8a704 Deployed Sep 10, 2026 by vercel[bot]
Preview – openwork-app — 29a8a704 Deployed Sep 10, 2026 by vercel[bot]
Preview – openwork-den — 29a8a704 Deployed Sep 10, 2026 by vercel[bot]
Preview – openwork-diagnostics — 29a8a704 Deployed Sep 10, 2026 by vercel[bot]
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.

1 participant