Skip to content

Commit 1f9b39d

Browse files
committed
fix(auth): hold the remove door to pending people at the row delete, and refuse it when the host is unreachable
Delta review round on the reshaped door: - The sys_user delete carries `source: idp_pending` (a predicate delete, which honours deleteBehavior like the by-id one); a zero-row delete throws NoLongerPending, so a first sign-in committing inside the unit of work is answered 409 and the child deletes roll back. - A host that cannot be reached refuses the removal with 503 and nothing removed, as it refuses a deactivation: with no framework record nothing could ask the host again. The "not confirmed" sentence is gone with the path that produced it. - takeBackPerson's doc and throw text now hold for both callers. - A banned column read back as '1' or 'true' counts as deactivated. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q4EjjPP1zuWBNq3nBXVei9
1 parent e10b14b commit 1f9b39d

6 files changed

Lines changed: 98 additions & 58 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ On a cloud-managed environment, a person added to sign in through the hosting or
1313

1414
- The host is told through `userRemoved`. A host without it is told through `userDeactivated` with `source: 'idp_pending'`, which already means "cancel the invitation". A host should implement `userRemoved`.
1515
- A host refusal (403/409) answers with the host's sentence, and no one is removed.
16-
- A host that cannot be reached does not block the removal; the answer says `host: { notified: false, reason: 'host_failed' }`. The door calls the host once: durable retry is the host's job.
16+
- 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, so a person who signs in while the host is asked is not removed.
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.
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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,12 +1344,14 @@ membership, and cancels their invitation at the host. The host is asked
13441344
first, through its `userRemoved` member; a host without that member is asked
13451345
through `userDeactivated` for the pending person instead, which means the same
13461346
thing, but a host should implement `userRemoved`. A host's refusal leaves the
1347-
person as they are. A host that cannot be reached does not block the removal:
1348-
the person is removed and the answer says `host: { notified: false, reason:
1349-
'host_failed' }`. A person who is not pending (they have signed in, or were
1350-
not added this way) is refused with `409`: deactivate them instead. That holds
1351-
at the removal itself, so a person who signs in while the host is asked is not
1352-
removed either, and the answer says the host was already told. A record that
1347+
person as they are. A host that cannot be reached refuses the removal too,
1348+
with `503` and nothing removed, as it refuses a deactivation: the door calls
1349+
the host once and keeps no record to ask again from, so try again once the
1350+
host is back. A person who is not pending (they have signed in, or were not
1351+
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
13531355
still points at the person through a lookup that restricts deletes refuses the
13541356
removal with that lookup's own answer, and nothing is removed; when the host
13551357
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 does not block it and the answer says host_failed (the door calls the host once; retrying belongs to the host); 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 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)' },
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-marker.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export const PENDING_SSO_MESSAGES = {
5050
+ 'succeed, so they stay pending with no invitation: add them again to send one.',
5151
removeHostCancelled:
5252
'The hosting organisation had already been told to cancel their invitation, and it is not sent again.',
53-
removeHostUnconfirmed:
54-
'The hosting organisation was asked to cancel their invitation but did not confirm it, and the request is not '
55-
+ 'retried.',
53+
removeHostUnreachable:
54+
"The hosting organisation could not be reached to cancel this person's invitation, so they were not removed. "
55+
+ 'Nothing was changed; try again.',
5656
alreadyExists:
5757
'A user with this email address already exists in this environment, so no one was added. Change the existing '
5858
+ "user's positions, permission sets or manager on their record instead.",

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

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ describe('[#33228] admin/remove-sso-user removes a pending person and cancels th
12831283
expect(s.rowsOf('sys_member', id).length).toBeGreaterThan(0);
12841284
});
12851285

1286-
it('a host that cannot be reached does not block the removal, and the answer says host_failed', async () => {
1286+
it('a host that cannot be reached refuses the removal: 503, the person is kept and nothing is re-sent', async () => {
12871287
const { host, told } = removingHost({
12881288
removed: () => {
12891289
throw new Error('host down');
@@ -1292,11 +1292,12 @@ describe('[#33228] admin/remove-sso-user removes a pending person and cancels th
12921292
const s = await managedStage({ host });
12931293
const id = await addPending(s, 'unreached.remove@example.com');
12941294

1295-
const body: any = await (await s.fire('/admin/remove-sso-user', { userId: id }, s.bearers.admin)).json();
1295+
const v = await verdict(await s.fire('/admin/remove-sso-user', { userId: id }, s.bearers.admin));
12961296

1297-
expect(body.data).toEqual({ userId: id, email: 'unreached.remove@example.com', removed: true, host: { notified: false, reason: 'host_failed' } });
1298-
expect(s.row('unreached.remove@example.com')).toBeUndefined();
1299-
// Asked once: the door retries nothing.
1297+
expect([v.status, v.code, v.message]).toEqual([503, 'SERVICE_UNAVAILABLE', PENDING_SSO_MESSAGES.removeHostUnreachable]);
1298+
expect(s.row('unreached.remove@example.com')).toMatchObject({ id, source: PENDING_SSO_SOURCE });
1299+
expect(s.rowsOf('sys_member', id).length).toBeGreaterThan(0);
1300+
// Asked once, and no invitation is sent again.
13001301
expect(told.map((t) => t.kind)).toEqual(['added', 'removed']);
13011302
});
13021303

@@ -1408,21 +1409,17 @@ describe('[#33228] a deactivated person is never sent an invitation, and a remov
14081409
expect(told.map((t) => t.kind)).toEqual(['added', 'deactivated', 'removed']);
14091410
});
14101411

1411-
it('a refused removal the host never took sends no new invitation, and says the cancellation was not confirmed', async () => {
1412-
const { host, told } = removingHost({
1413-
removed: () => {
1414-
throw new Error('host down');
1415-
},
1416-
});
1412+
it('a deactivated pending person whose banned column reads back as a string is not invited on a re-add', async () => {
1413+
const { host, told } = removingHost();
14171414
const s = await managedStage({ host });
1418-
const id = await addPending(s, 'held.unreached@example.com');
1419-
restrictDelete(s);
1420-
1421-
const v = await verdict(await s.fire('/admin/remove-sso-user', { userId: id }, s.bearers.admin));
1422-
1423-
expect(v.status, v.text).toBe(409);
1424-
expect(v.message).toBe(`${PENDING_SSO_MESSAGES.removeRefused} ${RESTRICTED} ${PENDING_SSO_MESSAGES.removeHostUnconfirmed}`);
1425-
expect(told.map((t) => t.kind)).toEqual(['added', 'removed']);
1415+
const email = 'readd.banned.string@example.com';
1416+
const id = await addPending(s, email);
1417+
for (const stored of ['1', 'true']) {
1418+
s.row(email)!.banned = stored;
1419+
const body: any = await (await s.fire('/admin/add-sso-user', { email, name: 'Typo' }, s.bearers.admin)).json();
1420+
expect(body.data, stored).toMatchObject({ userId: id, alreadyPending: true, host: { notified: false, reason: 'deactivated' } });
1421+
}
1422+
expect(told.map((t) => t.kind)).toEqual(['added']);
14261423
});
14271424

14281425
it('a person who signs in while the host is asked is not removed: 409, and the answer says the host was told', async () => {
@@ -1444,6 +1441,27 @@ describe('[#33228] a deactivated person is never sent an invitation, and a remov
14441441
expect(s.row(email)).toMatchObject({ id, source: 'idp_provisioned' });
14451442
expect(s.rowsOf('sys_member', id).length).toBeGreaterThan(0);
14461443
});
1444+
1445+
it('a person whose first sign-in commits inside the unit of work, after the pending read, is not removed: 409', async () => {
1446+
const email = 'signs.in.late@example.com';
1447+
const { host } = removingHost();
1448+
const s = await managedStage({ host });
1449+
const id = await addPending(s, email);
1450+
// The first sign-in lands between the in-unit pending read and the row's
1451+
// delete: the first child delete is the moment after that read.
1452+
const realDelete = s.engine.delete.bind(s.engine);
1453+
s.engine.delete = async (name: string, q: any = {}) => {
1454+
if (name === 'sys_user_position') s.row(email)!.source = 'idp_provisioned';
1455+
return realDelete(name, q);
1456+
};
1457+
1458+
const v = await verdict(await s.fire('/admin/remove-sso-user', { userId: id }, s.bearers.admin));
1459+
1460+
expect(v.status, v.text).toBe(409);
1461+
expect(v.code).toBe('RESOURCE_CONFLICT');
1462+
expect(v.message).toBe(`${PENDING_SSO_MESSAGES.removeNotPending} ${PENDING_SSO_MESSAGES.removeHostCancelled}`);
1463+
expect(s.row(email)).toMatchObject({ id, source: 'idp_provisioned' });
1464+
});
14471465
});
14481466

14491467
// ─────────────────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)