Skip to content

Commit c0665c0

Browse files
fix(plugin-auth): SSO-user doors admit the stamped organizationId when it is the session's, so console Import Users and Add User work (#34966)
<!-- claude-projects-attribution: {"github_login":"os-support-ai"} --> _Requested by **Yibo Wang** · [project thread](https://claude.ai/code/project/chan_01BVUyyPie8gHwoPM18iABwB?thread=cmsg_01BVUyyPie8gHwoPM18iABwBXim82sEGBUBXqH4hkWyBJX)_ Fixes #34912 repo:objectstack#3 · os-support-ai · session_01WGufzi3ezPzTysQPDEjmVz Clause-②: yes (widening) Metadata-protocol: none ## 维护者速读 - **改了什么**:三个 SSO 用户入口(`admin/import-sso-users`、`admin/add-sso-user`、`admin/remove-sso-user`)现在接受请求体里的 `organizationId`,但只在它等于调用者会话当前组织时接受;不等、类型不对、或会话没有当前组织都会被 400 拒绝,且什么都不写。 - **为什么改**:控制台的「导入用户」「添加用户」经服务端动作转发到 `/api/v1/auth/`,转发统一附带调用者的 `organizationId`,而这三个入口拒收任何未知键,所以托管环境里每个管理员点这两个按钮都必然 400。 - **风险与回滚**:组织范围仍只取自会话,请求体里的值只做相等校验、从不参与范围;风险面是「请求体能否改写组织」,已有拒绝用例与消融钉住。回滚即还原 `pending-sso-users.ts` 一个文件。 - **先看**:`packages/plugins/plugin-auth/src/pending-sso-users.ts` 的 `refuseForeignOrganization`(三个入口共用的校验);同文件三处 `unknownBodyKeys` 调用;`pending-sso-users.test.ts` 末尾的 `[#34912]` 用例组。 - **两分钟验证**:`pnpm --filter @objectstack/plugin-auth exec vitest run --maxWorkers=2 src/pending-sso-users.test.ts -t 34912`,预期 14 个用例全绿。 - **需要你做的**:无,席位落地。 ## What changes - `pending-sso-users.ts`: one shared check, `refuseForeignOrganization`, runs in all three doors right after their closed-key check. `organizationId` is the only key admitted beyond each door's own set (`unknownBodyKeys`), and only as a statement the session already makes: - absent: unchanged; - equal to `deps.getCallerActiveOrganizationId()` (the caller's own session, read with the request's headers through `authApi.getSession`; the route wiring already passes it to these doors, so no wiring change): admitted, then never read again. Scope stays the session's: single-org binds the sole organization as before, multi-org joins the session organization in `settleRequest` as before; - a different string: `400 INVALID_REQUEST`, the sentence names the organization sent and the one the session holds; - not a non-empty string (`null`, `''`, a number): `400 INVALID_REQUEST`; - any value while the session works in no organization: `400 NO_ACTIVE_ORGANIZATION`. Every refusal happens before any read, write or host call. Both codes are already in the error-code ledger; no new code. - The generic identity-route stamp in `buildApiActionRequest` (`packages/runtime/src/action-execution.ts`) is unchanged (seat ruling A). - Changeset: `@objectstack/plugin-auth` patch, with both declaration lines. ## Tests - `packages/plugins/plugin-auth/src/pending-sso-users.test.ts`, new top-level describe in the shared harness (real `AuthManager` and real route mount, real bearer sessions): - for each of add, import and remove: absent key accepted; equal key accepted (membership in the session organization); a different organization refused 400 naming both, with nothing written and no host told; no active organization in the session plus a body organization refused 400 `NO_ACTIVE_ORGANIZATION` (and, on add, a CONTROL: the same request without the key still answers 201); - add: `null`, `''` and `42` refused 400; - over fakes with multi-org tenancy on: a foreign body organization is refused and the join never runs; an equal one is admitted and the join is handed the session's value. - `packages/runtime/src/domains/api-action-forward.test.ts` (the `buildApiActionRequest` harness), CONTROL describe: the declared `sys_user.import_sso_users` and `add_sso_user` actions are still stamped with the caller organization, `ban_user` (another identity route) keeps the stamp, and a caller with no organization gets no stamp. - Ablation of the equality check (`if (sent !== current) {` to `if (false) {`, via `scripts/ablation-replace.mjs`, anchor 1 to 0, blob `ed0b3c405d81` to `2cd498db4bb9`): exactly the four foreign-organization legs go red (add, import, remove, multi-org fake), 10 others stay green; restored, blob back to `ed0b3c405d81` = HEAD, `git diff HEAD` empty. <details><summary>Test and type-check readings</summary> - `pnpm --filter @objectstack/plugin-auth test` (tree at c8d32b0; head ce3609c adds only the changeset): Test Files 285 passed (285), Tests 5983 passed, 10 skipped. Exit 0. - `pnpm --filter @objectstack/plugin-auth typecheck`: exit 0. `pnpm --filter @objectstack/runtime typecheck` (includes `check:test-typecheck` over the test file): exit 0. - `vitest run src/domains/api-action-forward.test.ts` (runtime): 89 passed (89). - `vitest run src/pending-sso-users.test.ts -t 34912`: 14 passed. - Ablation run: `Tests 4 failed | 10 passed`; the failed names are the three "a different organization is refused 400 naming both" legs and "the body organization never feeds scope". </details> ## Acceptance notes - `admin/remove-sso-user` has no declared console action: no `sys_user` action targets it, although the auth route ledger note says Setup → Users posts it. Its live reading posts the body an identity-route forward would send. Noted, not filed. - The Import Finished result dialog draws its fields and per-row table past the dialog's right edge (screenshot `after-2-import-result.png` in the live readings). Reported to the seat as a separate UX finding; not touched here. - **Claim amendment (seat order, 2026-11-17 19:52Z): docs fold.** The doc and the ledger now describe the bodies this PR changes. Commit 7f0c346 changes prose and ledger `note` strings only, with no code path changed: - `content/docs/permissions/authentication.mdx`, in the cloud-managed two-doors section: one paragraph says that add-sso-user, import-sso-users and remove-sso-user take an optional `organizationId`. It is admitted only when it equals the session's active organization. A different or non-string value gets `400 INVALID_REQUEST`, and a body org with no session org gets `400 NO_ACTIVE_ORGANIZATION`. Nothing is written before either refusal, and the value never widens scope. The remove-sso-user line points to that paragraph. - `packages/plugins/plugin-auth/src/auth-route-ledger.ts`: the three `note` strings for those routes state the same rule. - At 7f0c346, dispatch-gates derives 104 families, and all 104 ran with 0 UNRUN. Seven waited on builds and passed on rerun after the builds. One is NOT MEASURED: the teardown self-test, whose fixture commit is not in the shallow clone. check:auth-mount-ledger, check:route-ledger-census and check:error-code-casing exit 0. overlap-check is clean, including against #34969 and #34738, which also edit authentication.mdx. - L1 ACCEPT 6959151659 and seat L2 PASS were read at ce3609c. This fold answers the doc nit both of them named. ## Verification Live readings on a cloud-managed showcase (`OS_CLOUD_MANAGED=1`), driven through the real console, before (origin/main's door) and after (this head): posted as a PR comment. <details><summary>Local gates at ce3609c</summary> - `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack`: 71 commands. All 71 run: 69 exit 0; 2 exit 3, read as NOT MEASURED: `node scripts/check-plugin-teardown-shape.mjs --self-test` (pinned fixture commit not reachable from this shallow clone; script untouched) and `pnpm check:dual-build-cjs-loads` (PREREQUISITE NOT MET: whole-tree gate, unbuilt packages outside this diff). CI runs both. - `--ran` reconciliation: 71 derived, 71 run, 0 UNRUN. - Run beyond the derivation, roster gates whose roster sits under a changed directory: `check:auth-mount-ledger`, `check:error-code-casing`, `check:route-ledger-census`, all exit 0. - `node scripts/pm/overlap-check.mjs --branch=HEAD`: `VERDICT clean exit 0` against origin/main 8e49847 and 0 PR heads. </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01WGufzi3ezPzTysQPDEjmVz <!-- signed: repo:objectstack#3 · os-support-ai · session_01WGufzi3ezPzTysQPDEjmVz --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4443014 commit c0665c0

6 files changed

Lines changed: 375 additions & 9 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'@objectstack/plugin-auth': patch
3+
---
4+
5+
fix(auth): the console's Import Users and Add User work on a cloud-managed environment again
6+
7+
Clause-②: yes (widening) — added: `organizationId` in the bodies of `POST /api/v1/auth/admin/add-sso-user`, `/admin/import-sso-users` and `/admin/remove-sso-user`, admitted only when it equals the organization the caller's session is working in. Nothing that was accepted is refused.
8+
Metadata-protocol: none
9+
10+
A `type: 'api'` action forwarded to the auth mount carries the caller's active organization as `body.organizationId`. The three SSO-user doors refused every key outside their own set, so the console's Import Users (`sys_user.import_sso_users`) and Add User (`sys_user.add_sso_user`) always answered `400 INVALID_REQUEST` "Unknown field(s): organizationId".
11+
12+
Each door now admits that one key under a constraint:
13+
14+
- absent: unchanged;
15+
- equal to the session's active organization: admitted, and ignored. The organization a person joins is still read from the session, never from the body;
16+
- a different organization: `400 INVALID_REQUEST`, naming the organization sent and the one the session holds;
17+
- a value that is not a non-empty string: `400 INVALID_REQUEST`, saying the key must be the id of the organization you are working in. This is checked before the session is read, so it is the answer whether or not the session has an organization;
18+
- a string organization sent by a caller whose session works in no organization: `400 NO_ACTIVE_ORGANIZATION`, naming the organization sent.
19+
20+
On every refusal nothing is read or written and the host is not told.
21+
22+
The stamp itself (`buildApiActionRequest` in `@objectstack/runtime`) is unchanged: every identity-route forward still carries it.

‎content/docs/permissions/authentication.mdx‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,20 @@ admin plugin, and both are platform-admin gated:
13461346
with its own message and the good rows land. The answer carries a
13471347
`summary` and one result per row.
13481348

1349+
Both doors, and `remove-sso-user` below, also take an optional
1350+
`organizationId`. A console action forwarded to the auth mount carries the
1351+
caller's active organization there, so the key is admitted only when it
1352+
equals the organization the caller's session is working in. A different
1353+
organization gets `400 INVALID_REQUEST` naming both organizations; a
1354+
non-string or empty value gets `400 INVALID_REQUEST` saying the key must be
1355+
the id of the organization you are working in (a non-string value is checked
1356+
before the session, so this is the answer even when the session has no
1357+
organization); a string organization from a caller whose session works in no
1358+
organization gets `400 NO_ACTIVE_ORGANIZATION` naming the organization sent. Every refusal
1359+
comes before anything is written or any host is told. The value never
1360+
widens or chooses scope: the organization a person joins is always read from
1361+
the session.
1362+
13491363
The person is stored **pending** (`sys_user.source` reads `idp_pending`): no
13501364
password, and no session until they sign in through the hosting
13511365
organisation's identity provider. Every password door refuses a pending
@@ -1414,9 +1428,10 @@ it are listed on the answer as `notApplied`; set them on the person's record.
14141428

14151429
A pending person who never signs in, for example one added with a mistyped
14161430
address, can be removed: `POST /api/v1/auth/admin/remove-sso-user` takes
1417-
`{ userId }`, removes the person with their positions, permission sets and
1418-
membership, and cancels their invitation at the host. The host is asked
1419-
first, through its `userRemoved` member; a host without that member is asked
1431+
`{ userId }` (and the optional `organizationId` described above), removes
1432+
the person with their positions, permission sets and membership, and cancels
1433+
their invitation at the host. The host is asked first, through its
1434+
`userRemoved` member; a host without that member is asked
14201435
through `userDeactivated` for the pending person instead, which means the same
14211436
thing, but a host should implement `userRemoved`. A host's refusal leaves the
14221437
person as they are. A host that cannot be reached refuses the removal too,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ export const AUTH_ROUTE_LEDGER: readonly AuthRouteLedgerEntry[] = [
260260
{ route: 'POST /api/v1/auth/admin/import-users', family: 'objectstack-mount', source: 'objectstack', disposition: 'server-only', note: 'no SDK method builds this URL — objectui app-shell\'s identity-import wizard (views/identityImport.ts) posts it directly from the Users list; platform-admin gated (ADR-0068). A dedicated identity-import door rather than the generic sys_user import, because a direct ObjectQL write would bypass better-auth\'s password hashing and mint no sys_account credential' },
261261
// #33181 — the tracker id stays HERE and never in the `note` string, per the
262262
// #16678 note below.
263-
{ 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)' },
264-
{ 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)' },
265-
{ 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)' },
263+
{ 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 (email, name, positions, permissionSets, manager) plus an optional organizationId, admitted only when it equals the caller session active organization: a different organization is 400 INVALID_REQUEST naming both organizations, a non-string or empty value is 400 INVALID_REQUEST (a non-string value is checked before the session), a string organization with no session organization is 400 NO_ACTIVE_ORGANIZATION, and every refusal writes nothing and tells no host; it never widens scope (the joined organization is the session one); the environment-user host is told after the write commits, and its answer rides host; platform-admin gated (ADR-0068)' },
264+
{ 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; body { csv } or { rows } plus the same optional organizationId as admin/add-sso-user, admitted only when it equals the caller session active organization: a different organization is 400 INVALID_REQUEST naming both organizations, a non-string or empty value is 400 INVALID_REQUEST (a non-string value is checked before the session), a string organization with no session organization is 400 NO_ACTIVE_ORGANIZATION, and every refusal writes nothing and tells no host; it never widens scope; 409 RESOURCE_CONFLICT off a managed environment; platform-admin gated (ADR-0068)' },
265+
{ 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 } plus the same optional organizationId as admin/add-sso-user, admitted only when it equals the caller session active organization: a different organization is 400 INVALID_REQUEST naming both organizations, a non-string or empty value is 400 INVALID_REQUEST (a non-string value is checked before the session), a string organization with no session organization is 400 NO_ACTIVE_ORGANIZATION, and every refusal writes nothing and tells no host; it never widens scope; 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)' },
266266
{ 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' },
267267
{ 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)' },
268268
// #30958 — the tracker id stays HERE and never in the `note` string, per the

0 commit comments

Comments
 (0)