Skip to content

Commit 2e868fa

Browse files
committed
fix(auth): state the remove door's pending check as the engine delivers it, and prove the in-unit rollback
The row delete carries `source: 'idp_pending'`, but on an object with cascading lookups the engine re-deletes by id after the cascade walk, so a first sign-in committing in that window is not yet refused (#34373, an engine defect). The docblocks, ledger note, docs and changeset now say so instead of claiming the window is closed. The in-unit sign-in test now runs the removal through the plugin's transactional seam (a snapshot transaction on the memory engine) and asserts the positions, permission sets and membership survive. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q4EjjPP1zuWBNq3nBXVei9
1 parent 1f9b39d commit 2e868fa

5 files changed

Lines changed: 43 additions & 15 deletions

File tree

‎.changeset/33228-remove-pending-sso-user.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ On a cloud-managed environment, a person added to sign in through the hosting or
1515
- A host refusal (403/409) answers with the host's sentence, and no one is removed.
1616
- A host that cannot be reached refuses the removal with `503 SERVICE_UNAVAILABLE`, and no one is removed: the door calls the host once and keeps no record to ask again from, so try again. Durable retry is the host's job.
1717
- With no host wired, the person is removed and the answer says `host: { notified: false, reason: 'no_host' }`.
18-
- A person who is not pending answers `409`: deactivate them instead. This holds at the removal itself (the row's delete carries the pending marker), so a person whose first sign-in lands while the host is asked or while the removal is written is not removed.
18+
- A person who is not pending answers `409`: deactivate them instead. This holds at the removal itself: the person is re-checked inside the unit of work and the delete carries `source: 'idp_pending'`, so a person whose first sign-in lands while the host is asked is not removed. On an object with cascading lookups (such as `sys_user`) the engine re-deletes by id after the cascade, so a first sign-in committing in that narrow window is not yet refused.
1919
- A person that other records still point at (`deleteBehavior: 'restrict'`) answers the engine's refusal, and nothing is removed. If the host had already cancelled the invitation of a person who is still pending and not deactivated, the door sends it again through `userAdded`, once, and the answer says whether that worked.
2020
- An unknown id answers `404`; a body with any key but `userId` answers `400`. Off a cloud-managed environment the door answers `409` and removes no one.
2121

‎content/docs/permissions/authentication.mdx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,9 +1349,12 @@ with `503` and nothing removed, as it refuses a deactivation: the door calls
13491349
the host once and keeps no record to ask again from, so try again once the
13501350
host is back. A person who is not pending (they have signed in, or were not
13511351
added this way) is refused with `409`: deactivate them instead. That holds at
1352-
the removal itself, so a person whose first sign-in lands while the host is
1353-
asked, or while the removal is being written, is not removed either, and the
1354-
answer says the host was already told. A record that
1352+
the removal itself: the person is checked again inside the removal's unit of
1353+
work and the delete carries `source: 'idp_pending'`, so a person whose first
1354+
sign-in lands while the host is asked is not removed either, and the answer
1355+
says the host was already told. Because `sys_user` has cascading lookups, the
1356+
engine deletes the row by id after walking them, so a first sign-in that
1357+
commits in that narrow window is not yet refused. A record that
13551358
still points at the person through a lookup that restricts deletes refuses the
13561359
removal with that lookup's own answer, and nothing is removed; when the host
13571360
had already cancelled the invitation of a person who is still pending and not

‎packages/plugins/plugin-auth/src/auth-route-ledger.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export const AUTH_ROUTE_LEDGER: readonly AuthRouteLedgerEntry[] = [
260260
// #16678 note below.
261261
{ route: 'POST /api/v1/auth/admin/add-sso-user', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — Setup → Users on a cloud-managed environment posts it; adds one person who signs in only through the hosting organisation identity provider: a sys_user row marked idp_pending (no password, refused a session) with optional positions, permission sets and manager, joined to the organization; 201, or 200 alreadyPending for a person already pending (the host is told again, unless the person is deactivated: then host reads deactivated and nothing is sent); 409 RESOURCE_CONFLICT off a managed environment or for an address already in use; closed body key set; the environment-user host is told after the write commits, and its answer rides host; platform-admin gated (ADR-0068)' },
262262
{ route: 'POST /api/v1/auth/admin/import-sso-users', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — Setup → Users on a cloud-managed environment posts it; the CSV or JSON twin of admin/add-sso-user (columns email, name, positions, permission_sets, manager_id), at most 500 rows, each judged and written on its own so a bad row fails alone; answers a summary and one result per row; 409 RESOURCE_CONFLICT off a managed environment; platform-admin gated (ADR-0068)' },
263-
{ route: 'POST /api/v1/auth/admin/remove-sso-user', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — Setup → Users on a cloud-managed environment posts it; removes a person who is still pending (sys_user.source idp_pending: the email-typo case) with their positions, permission sets and membership, and cancels their invitation at the environment-user host (userRemoved, else userDeactivated), which is asked first so its typed refusal leaves the person as they are; a host that cannot be reached refuses it with 503 SERVICE_UNAVAILABLE and nothing is removed (the door calls the host once and keeps no record to retry from); the row delete carries the pending marker, so a person who signs in meanwhile is kept with 409; a removal that fails after the host took the cancellation sends the invitation again once through userAdded and says so; body { userId }; 404 RESOURCE_NOT_FOUND for an unknown id; 409 RESOURCE_CONFLICT off a managed environment or for a person who is not pending; a lookup that restricts the delete refuses it with the engine answer; platform-admin gated (ADR-0068)' },
263+
{ route: 'POST /api/v1/auth/admin/remove-sso-user', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — Setup → Users on a cloud-managed environment posts it; removes a person who is still pending (sys_user.source idp_pending: the email-typo case) with their positions, permission sets and membership, and cancels their invitation at the environment-user host (userRemoved, else userDeactivated), which is asked first so its typed refusal leaves the person as they are; a host that cannot be reached refuses it with 503 SERVICE_UNAVAILABLE and nothing is removed (the door calls the host once and keeps no record to retry from); the person is re-checked inside the unit of work and the row delete carries source idp_pending, so a person who signs in while the host is asked is kept with 409 (on an object with cascading lookups the engine re-deletes by id, so a first sign-in committing in that narrow window is not yet refused); a removal that fails after the host took the cancellation sends the invitation again once through userAdded and says so; body { userId }; 404 RESOURCE_NOT_FOUND for an unknown id; 409 RESOURCE_CONFLICT off a managed environment or for a person who is not pending; a lookup that restricts the delete refuses it with the engine answer; platform-admin gated (ADR-0068)' },
264264
{ route: 'POST /api/v1/auth/admin/list-platform-admins', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — the sys_user list_platform_admins action posts it directly from the Users list; answers who holds platform-admin standing (the last-admin guard\'s enumerator, each id confirmed by hasPlatformAdminStanding), served only to a caller who holds that standing; reads no body. ADR-0095 D3' },
265265
{ route: 'POST /api/v1/auth/admin/oauth2/toggle-disabled', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — the sys_oauth_application disable/enable actions post it directly; ObjectStack mount closing a vendor gap (better-auth\'s /admin/oauth2/update-client strips `disabled` from its body schema), platform-admin gated (ADR-0068)' },
266266
// #30958 — the tracker id stays HERE and never in the `note` string, per the

‎packages/plugins/plugin-auth/src/pending-sso-users.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,11 +1442,29 @@ describe('[#33228] a deactivated person is never sent an invitation, and a remov
14421442
expect(s.rowsOf('sys_member', id).length).toBeGreaterThan(0);
14431443
});
14441444

1445-
it('a person whose first sign-in commits inside the unit of work, after the pending read, is not removed: 409', async () => {
1445+
it('a person whose first sign-in commits inside the unit of work, after the pending read, is not removed: 409, and the child deletes roll back', async () => {
14461446
const email = 'signs.in.late@example.com';
14471447
const { host } = removingHost();
14481448
const s = await managedStage({ host });
14491449
const id = await addPending(s, email);
1450+
const CHILDREN = ['sys_user_position', 'sys_user_permission_set', 'sys_member'] as const;
1451+
for (const table of CHILDREN) expect(s.rowsOf(table, id).length, table).toBeGreaterThan(0);
1452+
// The memory engine has no transaction, so the plugin's `runAtomically`
1453+
// would run the removal unatomically. Give it one: the seam calls
1454+
// `engine.transaction`, and a throw out of it restores every table this
1455+
// unit of work wrote. `sys_user` is left as it is, because its change is
1456+
// the concurrent sign-in's commit, not this unit of work's write.
1457+
let units = 0;
1458+
(s.engine as any).transaction = async (work: (trx: unknown, info: unknown) => Promise<unknown>) => {
1459+
units += 1;
1460+
const before = new Map([...s.engine.tables].filter(([name]) => name !== 'sys_user').map(([name, rows]) => [name, structuredClone(rows)]));
1461+
try {
1462+
return await work({}, {});
1463+
} catch (error) {
1464+
for (const [name, rows] of before) s.engine.tables.set(name, rows);
1465+
throw error;
1466+
}
1467+
};
14501468
// The first sign-in lands between the in-unit pending read and the row's
14511469
// delete: the first child delete is the moment after that read.
14521470
const realDelete = s.engine.delete.bind(s.engine);
@@ -1461,6 +1479,8 @@ describe('[#33228] a deactivated person is never sent an invitation, and a remov
14611479
expect(v.code).toBe('RESOURCE_CONFLICT');
14621480
expect(v.message).toBe(`${PENDING_SSO_MESSAGES.removeNotPending} ${PENDING_SSO_MESSAGES.removeHostCancelled}`);
14631481
expect(s.row(email)).toMatchObject({ id, source: 'idp_provisioned' });
1482+
expect(units, 'the removal ran inside the transactional seam').toBe(1);
1483+
for (const table of CHILDREN) expect(s.rowsOf(table, id).length, `${table} rolled back`).toBeGreaterThan(0);
14641484
});
14651485
});
14661486

‎packages/plugins/plugin-auth/src/pending-sso-users.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,16 @@ const TAKE_BACK_OBJECTS = ['sys_user_position', 'sys_user_permission_set', 'sys_
481481
* record that points at the person follows its lookup's `deleteBehavior`.
482482
* Two callers: an add the host refused (the person was just written and
483483
* never told anything), and `admin/remove-sso-user`, which passes
484-
* `pendingOnly`. Then the person is read inside the unit of work before
485-
* anything is deleted, and the row's delete carries the pending marker in
486-
* its `where`, so a person whose first sign-in commits at any point before
487-
* that delete is not removed: {@link NoLongerPending} is thrown and the unit
488-
* of work rolls the child deletes back. A failure here is thrown: the caller
489-
* must hear it rather than a clean answer.
484+
* `pendingOnly`. Then the person is re-checked inside the unit of work
485+
* before anything is deleted, and the row's delete carries
486+
* `source: 'idp_pending'` in its `where`: when it removes no row,
487+
* {@link NoLongerPending} is thrown and the unit of work rolls the child
488+
* deletes back. One window stays open: `sys_user` has cascading lookups, so
489+
* the engine reads the matching ids, walks the cascade and then deletes by
490+
* id alone, with no row lock, and a first sign-in committing between that
491+
* read and that delete is not yet refused (#34373, an engine defect). A
492+
* failure here is thrown: the caller must hear it rather than a clean
493+
* answer.
490494
*/
491495
async function takeBackPerson(
492496
deps: PendingSsoUserDeps,
@@ -734,9 +738,10 @@ const REMOVE_KEYS = new Set(['userId']);
734738
* of work, through the engine's delete, so a record that points at the person
735739
* follows its lookup's `deleteBehavior`: a `restrict` refuses the removal with
736740
* the engine's own answer and nothing is removed. "Pending only" is held at
737-
* the removal too: the row's delete carries the pending marker, so a person
738-
* who signs in while the host is asked, or inside the unit of work, is
739-
* answered `409` and kept. A host that could not be reached refuses the
741+
* the removal too: the person is re-checked inside the unit of work and the
742+
* row's delete carries `source: 'idp_pending'`, so a person who signs in
743+
* while the host is asked is answered `409` and kept (the narrow window the
744+
* engine's delete leaves is in {@link takeBackPerson}). A host that could not be reached refuses the
740745
* removal (`503`, nothing removed), as it refuses a deactivation: the door
741746
* calls the host once and keeps no record to ask again from, so removing the
742747
* person would leave an invitation nothing can cancel. With no host wired

0 commit comments

Comments
 (0)