Skip to content

Commit c4c0794

Browse files
fix(plugin-auth,app-shell)!: while mail is live, accepting an invitation needs a verified address (#34666)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9R6fQuMSzSrRW2oowG4K2kQ)_ Fixes #34272 Clause-②: no (narrowing) Metadata-protocol: none Skills-docs: none — no page under `skills/` teaches accepting an invitation or `/organization/accept-invitation`; the rule is stated in the changeset and the self-hosting docs. > **Governed / Tier H.** A narrowing of an auth route (BREAKING). This PR stays **draft** until `GOVERNED_APPROVERS` approve; the seat lands it through Tier H. The area thread does not flip it ready. ## 维护者速读 - **改了什么**:邮件服务可投递(`isConfigured() === true`)时,`POST /organization/accept-invitation` 拒收邮箱未验证的会话:`403 EMAIL_VERIFICATION_REQUIRED_BEFORE_ACCEPTING_OR_REJECTING_INVITATION`(better-auth 自己的码与句子),什么都不写,邀请仍是 pending。控制台接受页把这个拒绝转到 `/verify-email-prompt`(发验证邮件),验证链接带回接受页。 - **为什么改**:规则是邮件可投递时,接受邀请需要已验证的邮箱,因为未验证的地址不能证明持有受邀邮箱。 - **范围(席位收窄后)**:只动 accept;`get-invitation`、`list-user-invitations`、`reject-invitation` 不变(不写成员);一步接受门不变(凭证模式先验证再接受,会话模式本来就答 `verify_email`);**没有可投递邮件的部署完全不变**(那里什么都无法验证地址,拒收等于死路,也是 `requireEmailVerificationOnInvitation: false` 的原因,该声明保留)。 - **风险与回滚**:BREAKING 只落在「邮件可投递 + 未验证地址的人去接受」这一种人身上,修法是先验证邮箱。回滚即还原本 PR:接线只有 `auth-manager-organization-plugin.ts` 里一个 `beforeAcceptInvitation` 钩子 + 两处宿主成员透传。 - **先看**:`packages/plugins/plugin-auth/src/invitation-accept-verified-email.ts`(规则与为何选 vendor 钩子);`auth-manager-organization-plugin.ts` 的 `beforeAcceptInvitation`;`ui/.../AcceptInvitationPage.tsx` 的 `handleAccept`。 - **两分钟验证**:`pnpm --filter @objectstack/plugin-auth exec vitest run src/invitation-accept-verified-email.test.ts`,预期 14 个用例全绿。 - **需要你做的**:作为受管(Tier H)收窄,按 GOVERNED_APPROVERS 批准;其余席位落地。 ## What changes **Rule.** While the deployment's mail transport is live, an invitation is accepted only by a session whose address is verified. Without a live transport nothing changes. - `packages/plugins/plugin-auth/src/invitation-accept-verified-email.ts` (new): `refuseUnverifiedAcceptWhileMailIsLive(user, { isEmailDeliveryLive, verificationRequired? })`, a no-op for a verified user (it does not even ask whether mail is live) and while mail is not live; otherwise it throws `APIError.from('FORBIDDEN', …)` with the vendor's own `$ERROR_CODES` entry (a local fallback is pinned equal to the vendor's). The verified test is the vendor's own (`!user.emailVerified`). - `auth-manager-organization-plugin.ts`: the rule is asked from better-auth's `organizationHooks.beforeAcceptInvitation`, which the vendor fires inside the accept endpoint after its not-found / expiry / status, recipient and membership-limit checks and before the invitation row or the membership is written, handing it the `session.user` its own session middleware resolved (cookie and bearer alike). The `requireEmailVerificationOnInvitation: false` comment now says why it stays false and where the live-mail rule lives. - `auth-manager.ts`, `auth-manager-plugin-list.ts`: `isEmailDeliveryLive` passed through the two typed hosts to the organization plugin. - `ui/packages/app-shell/.../AcceptInvitationPage.tsx`: on that code, navigate to `/verify-email-prompt?email=<account email>&redirect=<this page>` instead of a toast. `orgErrorMessage.ts` exports the code and `isAcceptNeedsVerifiedEmail(err)` (read by code only). - `content/docs/deployment/self-hosting.mdx`: one paragraph under the `invite_only` sign-up paragraph. - `packages/runtime/src/dispatcher-error-vocabulary.ts`: one `foreign-vocabulary` row classifying the vendor code, beside the sibling row for `EMAIL_VERIFICATION_REQUIRED_FOR_INVITATION` (`check:dispatcher-error-vocabulary`). - `.changeset/34272-accept-invitation-verified-email.md`: `@objectstack/plugin-auth`, `@object-ui/app-shell` and `@objectstack/runtime` patch (`Metadata-protocol: none` takes patch; the BREAKING banner stays); BREAKING, Who moves, `adr-0087: not-required (no-migration-prescription)`. No spec, schema, stored row, route or exported signature of a published entry changes. No new error code is minted (the vendor's), so nothing reaches the error-code ledger. ### Why the vendor hook and not the global before-hook The seat's correction named "a plugin-auth before-hook". `beforeAcceptInvitation` is that, at the vendor's own seam: the global `hooks.before` runs before `bearer()` turns a bearer credential into a session (so it would need a second session read and a user read), and it would answer ahead of the vendor's recipient check, telling a non-recipient about someone else's address. The vendor hook gets the exact user object the vendor's own verification check reads, after the recipient check, before any write. ### Why not turn the vendor option on where mail is live `requireEmailVerificationOnInvitation` governs accept, reject and get-invitation together (`crud-invites.mjs:270/382/505`, better-auth 1.7.12) and is read from options at boot. The narrowing the seat ruled is accept only, asked per request. ## Tests `src/invitation-accept-verified-email.test.ts` — real `AuthManager` + real better-auth pipeline over the memory engine (the `list-user-invitations-verification.test.ts` shape); the email service's `isConfigured()` is swapped between requests; every fixture account is asserted unverified. 1. the local fallback equals the vendor's `$ERROR_CODES` entry; 2. PREMISE: with the option false and no live mail, the vendor lets an unverified invitee accept; 3. mail live: unverified invitee → 403 with the vendor's code, no membership, invitation still pending; 4. mail live: a bearer-token session is refused alike; 5. mail live: once the address is verified, the same invitation is accepted; 6. ORDER PIN: a non-recipient still gets `YOU_ARE_NOT_THE_RECIPIENT_OF_THE_INVITATION`; 7. ONLY ACCEPT: the same unverified invitee still gets, lists and rejects (200); 8. mail live: the one-step door still accepts with the mail's proof (it verifies first); 9. no email service: unverified invitee accepts (unchanged); 10. log transport (`isConfigured()` false): unverified invitee accepts (unchanged); 11. read per request: live → 403, log → 200, live again → 403; 12–14. the function: verified passes without asking; unverified passes while not live; unverified / unknown user refused while live, with the entry the caller hands in. `ui/.../__tests__/acceptInvitationVerifiedEmail-34272.test.tsx` — the real `createAuthClient` over a stubbed `fetch` answering the server's 403 body: the page navigates to `/verify-email-prompt` with the account's email and `redirect=/accept-invitation/inv-1`, no toast, no org switch; CONTROL: `YOU_ARE_NOT_THE_RECIPIENT_OF_THE_INVITATION` still toasts and does not navigate there. **Ablation.** The hook body made a no-op on disk: tests 3, 4, 5 and 11 red (4 of 14), then restored. The page's routing branch disabled: the UI routing test red (1 of 2), then restored. ## Verification Final head `8e828209b5ce1d69a9c3513e319dce15eaa58101`. Measured at `b2d54a28` (final-head comment 6940567054); `8e828209` changes prose only (the changeset, the module's header comment and the self-hosting paragraph, after L1 6940626667), and the 41 derived gates over docs, changesets and ADR text plus `plugin-auth` typecheck and the module's 14 pins re-ran green on it. - **Derived gates.** `node scripts/pm/dispatch-gates.mjs --commands` derived 111 families; all 111 ran, each with its exit code captured, and all ended exit 0. `--ran` reconciliation: 111 accounted, 111 run, 0 NOT-MEASURED. Four first-run reds were fixed or were prerequisites: `check:adr-0087-registration` (the changeset's `FROM → TO` label read as a migration prescription; reworded to prose), `check:dispatcher-error-vocabulary` (the vendor code needed its classification row, added), and `spec check:docs` / `spec check:skill-examples` (stale `packages/spec` build; green after `pnpm --filter @objectstack/spec build`). - **Package suites.** `pnpm --filter @objectstack/plugin-auth test` exit 0; `pnpm --filter @objectstack/plugin-auth typecheck` exit 0; `ui`: `vitest run packages/app-shell/src/console/organizations/` exit 0; `pnpm --filter @object-ui/app-shell type-check` exit 0 (after building its workspace dependencies, whose stale `dist` predated #34333). - **Ablation.** As above: 4 of 14 server pins and 1 of 2 UI pins red with the change removed, restored after. - **Dogfood battery** (narrowing): `pnpm --filter @objectstack/dogfood test` under `scripts/pm/os-verify-lock.sh`, no `OS_TEST_SHARD`, at `b2d54a28` (the code at `8e828209` is the same; only prose moved): `Test Files 478 passed | 3 skipped (481)`, `Tests 4055 passed | 68 skipped (4123)`, `VERDICT command-exit 0`. - **merge-tree** at the head: clean against `origin/main` `fecd2e95d4`, which now holds #34617 (the other plugin-auth change in flight at opening). ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. (No spec change.) - [x] 2. Every name I added uses the casing for its kind. - [x] 3. No formatter ran; `pnpm lint` passes. - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. - [x] 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. - [x] 6. `packages/spec` touched: not touched. - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. - [x] 8. Title, body and comments are in English. (The 维护者速读 section is the maintainer's requested summary.) - [x] The *Before Submitting* list in CONTRIBUTING.md is done. --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01ADHkmSYe73LCZ6WxGFvcko <!-- signed: repo:objectstack#2 · os-steve · session_01ADHkmSYe73LCZ6WxGFvcko --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent c12fe66 commit c4c0794

11 files changed

Lines changed: 741 additions & 6 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@objectstack/plugin-auth': patch
3+
'@object-ui/app-shell': patch
4+
'@objectstack/runtime': patch
5+
---
6+
7+
fix(plugin-auth,app-shell)!: while mail is live, accepting an invitation needs a verified address
8+
9+
Clause-②: no (narrowing)
10+
Metadata-protocol: none
11+
Skills-docs: none — `git grep` of `skills/` finds no page that teaches accepting an invitation or `/organization/accept-invitation`; the rule is stated here and in the self-hosting docs.
12+
13+
<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value, metadata type or stored row is added, removed, renamed or rewritten: the moved answer is whether one auth route accepts a request from a session whose address is unverified, while the deployment's mail transport is live. Nothing exists for a migration to rewrite; the one-line fix for a refused person is in the body: verify the address, then accept. -->
14+
15+
**BREAKING** for a person who accepts an invitation from an account whose address was never verified, on a deployment whose mail transport is live. The rule: while mail is live, accepting an invitation needs a verified address, because an unverified address is not proof of the invited mailbox.
16+
17+
**What changes.**
18+
19+
- `POST /api/v1/auth/organization/accept-invitation` (`@objectstack/plugin-auth`), from a session whose address is unverified, while the email service answers `isConfigured() === true`, now answers `403 EMAIL_VERIFICATION_REQUIRED_BEFORE_ACCEPTING_OR_REJECTING_INVITATION` (better-auth's own code and sentence, the ones its accept route answers when its option is on) and writes nothing; the invitation stays pending. It used to answer `200` and write the membership. The vendor's not-found, expiry and recipient refusals still come first, so a session that is not the invitee learns nothing new. The check runs on the vendor's `beforeAcceptInvitation` hook, on the session user the vendor itself resolved (cookie or bearer token alike), and asks whether mail is live on every request.
20+
- The Console's accept page (`@object-ui/app-shell`) answers that refusal with the verification-mail screen (`/verify-email-prompt`) for the account's address, whose link returns to the accept page, instead of a toast.
21+
22+
**Who moves.** A person in that state verifies their address (the Console's accept page takes them to the screen that sends the mail), then accepts; an API client that accepts for its user does the same, through `POST /api/v1/auth/send-verification-email`. A visitor who is not signed in and opens the invitation mail's own link is not affected: the accept page offers the one-step form, which verifies the address as it accepts. A visitor already signed in to an account whose address is unverified who opens the same link accepts through `/organization/accept-invitation`, so is refused as above and sent to verify.
23+
24+
**Unchanged.** Every deployment without a live mail transport (no email service, or the log transport), where nothing could verify an address, so acceptance stays open to an unverified address; `get-invitation`, `list-user-invitations` and `reject-invitation`, which write no membership; the one-step door `POST /api/v1/auth/organization/accept-invitation-one-step`; and a verified session everywhere. better-auth's `requireEmailVerificationOnInvitation` stays declared `false`: it governs reading and declining too, and is read once at boot.
25+
26+
`@objectstack/runtime` carries only the dispatcher error-vocabulary row that classifies the vendor code above as foreign vocabulary; nothing it runs changes.

‎content/docs/deployment/self-hosting.mdx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,22 @@ reaches the invitee in the invitation mail; on a deployment with no mail
760760
transport, an administrator hands it over with the invitation's **Copy Invite
761761
Link** action. The first account of a new deployment needs no invitation.
762762

763+
**While the mail transport is live, accepting an invitation needs a verified
764+
address.** `POST /api/v1/auth/organization/accept-invitation` from a session
765+
whose address was never verified answers `403
766+
EMAIL_VERIFICATION_REQUIRED_BEFORE_ACCEPTING_OR_REJECTING_INVITATION` and
767+
writes nothing: an unverified address does not prove the person holds the
768+
invited mailbox. The Console's accept page sends the person to the
769+
verification-mail screen for their address, and the link in that mail brings
770+
them back to accept. A visitor who is not signed in and opens the invitation's
771+
link from the invitation mail itself needs no separate step: the accept page
772+
offers the one-step form, which verifies the address as it accepts. A visitor
773+
already signed in to an unverified account who opens that link is sent to
774+
verify first, like any other unverified session. Reading, listing and declining an invitation are not affected. On a
775+
deployment with no live transport (none configured, or the log transport)
776+
nothing can verify an address, so acceptance stays open to an unverified
777+
address, as before.
778+
763779
**What decides it on `open` and `email_domain`.** A sign-up for an address that
764780
already has an account gets the same answer as a successful new sign-up, and
765781
writes nothing, when all three of these hold:

‎packages/plugins/plugin-auth/src/auth-manager-organization-plugin.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import {
8181
import { buildOrganizationPluginSchema } from './auth-schema-config.js';
8282
import { CLOUD_MANAGED_REFUSAL_CODE, CLOUD_MANAGED_REFUSAL_MESSAGES } from './cloud-managed.js';
8383
import { resolveInvitationLimitOption } from './invitation-limit.js';
84+
import { refuseUnverifiedAcceptWhileMailIsLive } from './invitation-accept-verified-email.js';
8485
import { invitationAcceptPath } from './invitation-link-action.js';
8586
import { invitationRoleCapFailure, isPlainMemberInvitation } from './invitation-role-cap.js';
8687
import { blankInvitationRoleRefusal } from './invite-role-not-found-code.js';
@@ -108,6 +109,7 @@ export interface OrganizationPluginHost {
108109
getConsolePageUrl: AuthManager['getConsolePageUrl'];
109110
getDataEngine: AuthManager['getDataEngine'];
110111
getEmailService: AuthManager['getEmailService'];
112+
isEmailDeliveryLive: AuthManager['isEmailDeliveryLive'];
111113
multiOrgPostureEffective: AuthManager['multiOrgPostureEffective'];
112114
orgRolesMap: AuthManager['orgRolesMap'];
113115
resolveMembershipRole: AuthManager['resolveMembershipRole'];
@@ -192,11 +194,15 @@ export async function buildOrganizationPlugin(this: OrganizationPluginHost) {
192194
// portal exposes a Teams page; without this flag those endpoints
193195
// 404 and the section silently breaks.
194196
teams: { enabled: true },
195-
// Without a mailer wired in framework, requiring email verification
196-
// before accepting invitations dead-ends every invite flow with
197-
// FORBIDDEN EMAIL_VERIFICATION_REQUIRED…. Default-off here keeps
198-
// the built-in /accept-invitation route usable for pilots; operators
199-
// who wire a real mailer can re-enable downstream.
197+
// The vendor's switch governs accept, reject and get-invitation
198+
// together, and is read once, at boot. Declared off here: on a
199+
// deployment with no live mail nothing can ever verify an address, so
200+
// turning it on would dead-end every invite flow with FORBIDDEN
201+
// EMAIL_VERIFICATION_REQUIRED…. [#34272] Where mail IS live, the
202+
// accept door alone still refuses an unverified session, asked per
203+
// request by `beforeAcceptInvitation` below
204+
// (`invitation-accept-verified-email.ts`); reading and declining an
205+
// invitation stay open, as does every door while mail is not live.
200206
requireEmailVerificationOnInvitation: false,
201207
// Cap how many orgs a user can CREATE (OS_ORG_LIMIT). Counts only orgs
202208
// the user OWNS (role=owner) — never orgs they were merely invited into —
@@ -425,6 +431,20 @@ export async function buildOrganizationPlugin(this: OrganizationPluginHost) {
425431
});
426432
}
427433
},
434+
// [#34272] While mail is live, an unverified session may not accept:
435+
// the address it holds was never proven to be the invitee's mailbox.
436+
// Fired by the vendor inside the accept endpoint, after its recipient
437+
// and expiry checks and before anything is written, with the
438+
// `session.user` its own verification check reads. A no-op while mail
439+
// is not live. `invitation-accept-verified-email.ts` states the rule.
440+
beforeAcceptInvitation: async ({ user }: any) => {
441+
await refuseUnverifiedAcceptWhileMailIsLive(user, {
442+
isEmailDeliveryLive: () => this.isEmailDeliveryLive(),
443+
verificationRequired:
444+
organizationPlugin?.$ERROR_CODES
445+
?.EMAIL_VERIFICATION_REQUIRED_BEFORE_ACCEPTING_OR_REJECTING_INVITATION,
446+
});
447+
},
428448
// [ADR-0105 D8] Accept-time application + host seam. The placement
429449
// the issuance gate approved lands WITH the better-auth membership,
430450
// so the invitee arrives already in the right unit and role. The

‎packages/plugins/plugin-auth/src/auth-manager-plugin-list.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export interface PluginListHost {
123123
getEmailService: AuthManager['getEmailService'];
124124
getMcpResourceUrl: AuthManager['getMcpResourceUrl'];
125125
getApi: AuthManager['getApi'];
126+
isEmailDeliveryLive: AuthManager['isEmailDeliveryLive'];
126127
isPlatformAdminUserId: AuthManager['isPlatformAdminUserId'];
127128
isSsoOnlyMode: AuthManager['isSsoOnlyMode'];
128129
jwtSigningFailureReported: AuthManager['jwtSigningFailureReported'];
@@ -293,6 +294,7 @@ export async function buildPluginList(this: PluginListHost): Promise<any[]> {
293294
getConsolePageUrl: (...a) => this.getConsolePageUrl(...a),
294295
getDataEngine: (...a) => this.getDataEngine(...a),
295296
getEmailService: (...a) => this.getEmailService(...a),
297+
isEmailDeliveryLive: (...a) => this.isEmailDeliveryLive(...a),
296298
multiOrgPostureEffective: (...a) => this.multiOrgPostureEffective(...a),
297299
get orgRolesMap() { return readOrgRolesMap(); },
298300
set orgRolesMap(v) { writeOrgRolesMap(v); },

‎packages/plugins/plugin-auth/src/auth-manager.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,7 @@ export class AuthManager {
24442444
getEmailService: (...a) => this.getEmailService(...a),
24452445
getMcpResourceUrl: (...a) => this.getMcpResourceUrl(...a),
24462446
getApi: (...a) => this.getApi(...a),
2447+
isEmailDeliveryLive: (...a) => this.isEmailDeliveryLive(...a),
24472448
isPlatformAdminUserId: (...a) => this.isPlatformAdminUserId(...a),
24482449
isSsoOnlyMode: (...a) => this.isSsoOnlyMode(...a),
24492450
get jwtSigningFailureReported() { return readJwtSigningFailureReported(); },

0 commit comments

Comments
 (0)