Skip to content

Commit de5eb08

Browse files
fix(plugin-auth)!: with SSO domain verification on, an SSO sign-in links to an existing account, or changes its email address, only at a domain the provider has proven (#33307)
Clause-②: no (narrowing) Metadata-protocol: none Skills-docs: none — no file under `skills/**` teaches SSO domain verification, account linking or an address change through an SSO provider; `content/docs/permissions/sso.mdx` ("Domain verification (opt-in)") states the rule. ## 维护者速读 - 改了什么:开启 SSO 域名验证后,"经由身份提供方到达的账号,其邮箱只能位于该提供方已验证的域名"这条规则(此前只管新建账号)现在也管另外两条路径:隐式关联到已有账号,以及会改写已关联账号已存邮箱的登录。会改写邮箱、而新邮箱不在已验证域名时拒绝;无法判定是否会改写时也拒绝;不改写任何东西的登录照常放行。拒绝码为 `EMAIL_DOMAIN_NOT_ALLOWED`,不写关联、不发会话、已存邮箱不变。依据为维护者裁决(review#2 记录 6884303063 转述,选项 B「只拦会改邮箱的登录」)。 - 为什么改:让域名验证的保证在账号被"到达"的每一条会落地变化的路径上都成立,而不只是在新建时;匹配方式与新建时完全相同(精确匹配,子域名须自身列出并验证)。 - 风险与回滚:仅在开启域名验证的环境生效;关闭验证、非 SSO 来源、用户自己发起的显式关联、不改写邮箱的登录(含所有 SAML 提供方,以及平台自身注册流程下从不开启更新的 OIDC 提供方)均不变。本地账号位于"已列出域名的未列出子域名"的人,首次经 SSO 登录会被拒,需把该子域名加到提供方并重新验证;完全未列出的域名此前就不关联,不受影响。回滚即还原本 PR。D6 的 ADR 说明在 #33356(Tier H,另行审批)。 - 先看:`packages/plugins/plugin-auth/src/implicit-account-linking.ts`(规则 4:`refuseImplicitAccountLink` 新增判定;`refuseSsoAddressChangeOutsideProvenDomain` 与 `ssoSignInWritesAddress`,后者读不到即按"会改写"处理);`packages/plugins/plugin-auth/src/auth-manager-auth-instance.ts`(`validateUserInfo` 串接顺序);`packages/plugins/plugin-auth/src/sso-account-at-proven-domain.ts`(仅抽出 `readSsoProviderProvenDomains`,新建规则行为不变)。 - 两分钟验证:`pnpm --filter @objectstack/plugin-auth exec vitest run src/implicit-account-linking.test.ts src/sso-oidc-refusal-cause.test.ts src/sso-saml-refusal-landing.test.ts src/sso-account-at-proven-domain.test.ts`,预期 `Tests 255 passed (255)`(合并 main 后)。 - 需要你做的:无,席位落地。 ## What changes The rule, as ruled (the maintainer's ruling relayed in review#2 record 6884303063): with SSO domain verification on, an account reached through a provider holds an email address only at a domain that provider has proven. Account creation already follows it. This PR applies the same rule, with the same module, to the two other ways an SSO sign-in (SAML or OIDC) reaches an account: - **An implicit link.** A sign-in that links to an account that already exists is admitted only when the asserted address is at a domain the provider has proven, compared exactly: a subdomain counts only when it is itself listed and verified (`refuseImplicitAccountLink`, rule 4 in the `implicit-account-linking.ts` header). The two cases are kept apart. An address at a domain the provider does not list at all was already not linked, and is still refused as before with the same answer (pinned as a CONTROL). An address at a subdomain of a listed domain that is not itself listed and verified was linked before and is now refused. - **A sign-in that would change the stored address.** A sign-in into an already-linked account is refused when it would change the stored address to one outside the proven domains, and refused where whether it would change the address cannot be determined. A sign-in that reports an address outside the proven domains but changes nothing is admitted, as before. This is `refuseSsoAddressChangeOutsideProvenDomain`, chained into `validateUserInfo` after the link gate and before the audience gate. Whether the sign-in writes the address is the vendor's own decision, read from the inputs it reads (`ssoSignInWritesAddress`): for OIDC, the provider's stored update setting, which the platform's own provider registration never turns on; for SAML, none; and a configured `defaultSSO` entry or `resolveUser` selection where one is composed. A decision that cannot be read is judged as a write. - **The refusal** is `EMAIL_DOMAIN_NOT_ALLOWED`, the code creation answers, with a rule-level description per case that names no domain. No new error code. A provider row that cannot be read refuses; an account that cannot be read throws, which the sign-in answers with the gate's general refusal. - **Reuse.** The provider read moves out of `refuseSsoAccountOutsideProvenDomain` into the exported `readSsoProviderProvenDomains`. The creation rule's behaviour is unchanged and its tests are untouched. - **Unchanged.** Verification off; sign-ins that would not change the stored address; non-SSO sources (the cloud identity provider and enterprise OAuth providers); a link the user starts from their own session (an explicit link); the domain-matching rule itself. - **Texts.** `content/docs/permissions/sso.mdx` states the rule for an implicit link (both cases) and for a sign-in that would change the address. The gate comments in `sso-account-at-proven-domain.ts` and `auth-manager-auth-instance.ts`, the SSO mount comment in `auth-manager-plugin-list.ts`, and the TSDoc of `isSsoDomainVerificationEnabled` all state the rule for creation, implicit link and address change. - **ADR.** The dated ADR-0135 D6 note recording this extension and its ruling is the separate governed PR #33356. ## Tests Named by the rule. - `sso-oidc-refusal-cause.test.ts`, section 6 (real `AuthManager`, real vendor callback, stubbed IdP), covering: - an implicit link at an unlisted subdomain: refused, with no `sys_account` row and no session; - an implicit link at a domain the provider does not list at all: refused as before (CONTROL); - an implicit link at a proven domain: links and signs in; - with a provider that updates the account, a change to an outside address or to an unlisted subdomain: refused, stored email unchanged, no new session; - a change to another proven domain: applies; - with a provider that does not update the account, an outside address asserted later: signs in, and nothing changes; - CONTROLS: verification off, and a later sign-in with the stored address. - `sso-saml-refusal-landing.test.ts`: - the implicit-link pair over SAML, plus the verification-off CONTROL; - a later SAML sign-in asserting an outside address: signs in, and the stored address is unchanged. - `implicit-account-linking.test.ts`, new top-level describe: the rule on its own inputs, OIDC and SAML, including: - the write decision read as the vendor reads it; - fail-closed cases; - CONTROLS for verification off, a non-SSO source, an explicit link (an OAuth source), and the creation rule unchanged. <details><summary>Readings</summary> - Before the fix, on this branch's base: the new end-to-end cases ran red, 3 failed of 8 (OIDC) and 1 failed of 3 (SAML). - At `c21b6b600c`, the four affected files: `Test Files 4 passed (4)`, `Tests 218 passed (218)`. `pnpm --filter @objectstack/plugin-auth typecheck` exit 0. - At `ba3fd25ea8`, the last code change (`c21b6b600c` adds comment and prose only), `pnpm --filter @objectstack/plugin-auth test`: `Test Files 270 passed (270)`, `Tests 5157 passed | 10 skipped (5167)`, and typecheck exit 0. - Ablation. Each leg ran through `scripts/ablation-replace.mjs`, with the anchor proven on disk and the restore proven by blob equality with `HEAD`. - The implicit-link check removed: `Tests 5 failed | 200 passed (205)`; restored. - The address-change judge removed (first rule): `Tests 4 failed | 201 passed (205)`; restored. - The write-decision call removed: `Tests 4 failed | 207 passed (211)`; restored. - The stored update setting forced on: `Tests 2 failed | 209 passed (211)`; restored. - The SAML branch of the write decision removed: `Tests 2 failed | 209 passed (211)`; restored. - Dogfood battery: - Last full run at `b0796f0239` (before the narrower address rule): `Tests 2 failed | 3925 passed | 42 skipped (3969)`. Both failures were 5 s timeouts on a contended box. Re-run alone: `Tests 36 passed (36)`. - At `ba3fd25ea8`: the local run was interrupted, so it is NOT MEASURED locally. CI's `Dogfood Regression Gate` on that head: success. - `c21b6b600c` is comment and prose only, so it owes no battery. </details> ## Acceptance notes - Two open drafts touch nearby text. One edits the header of `implicit-account-linking.ts` at the spot where rule 4 is inserted, which is a textual merge of two doc paragraphs. The other edits a different section of `sso.mdx`. Neither touches the gate code. - Local gates at `c21b6b600c`: the `dispatch-gates --commands` derivation for this diff, 105 families, reconciled with `--ran`: 105 run, 0 not measured. ## Verification - `pnpm --filter @objectstack/plugin-auth exec vitest run src/implicit-account-linking.test.ts src/sso-oidc-refusal-cause.test.ts src/sso-saml-refusal-landing.test.ts src/sso-account-at-proven-domain.test.ts` --- _Generated by [Claude Code](https://claude.ai/code/session_017xRBcT1w3zpekijraHJE4f)_ <details><summary>Merge of origin/main (head 4a7e194)</summary> - Two files conflicted, both from main's new SAML account-key and address-value checks in the same `validateUserInfo` chain: - `auth-manager-auth-instance.ts`: both kept, in the order identity locale, SAML account key, the proven-domain creation check, the address-value check, the implicit-link gate, the address-change gate, then audience admission. - `implicit-account-linking.test.ts`: both appended describes kept. - The rule text and code are unchanged from the reviewed head. `implicit-account-linking.ts`, `sso.mdx` and the changeset merged cleanly and are byte-identical in this PR's own lines. - At `4a7e194f97`: - the four touched files: `Tests 255 passed (255)`; - `pnpm --filter @objectstack/plugin-auth test`: `Test Files 275 passed (275)`, `Tests 5355 passed | 10 skipped (5365)`; typecheck exit 0; - dispatch-gates: 105 derived, 105 run, 0 NOT-MEASURED; - dogfood battery (owed: the merge brought non-text plugin-auth changes): `Test Files 467 passed | 3 skipped (470)`, `Tests 3953 passed | 42 skipped (3995)`, VERDICT command-exit 0. </details> <!-- signed: security#1 · sunhaolin · session_017xRBcT1w3zpekijraHJE4f --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent b95d915 commit de5eb08

10 files changed

Lines changed: 669 additions & 30 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
'@objectstack/plugin-auth': patch
3+
---
4+
5+
fix(plugin-auth)!: with SSO domain verification on, an SSO sign-in links to an existing account, or changes its email address, only at a domain the provider has proven
6+
7+
Clause-②: no (narrowing)
8+
Metadata-protocol: none
9+
Skills-docs: none — no file under `skills/**` teaches SSO domain verification, account linking or an address change through an SSO provider; `content/docs/permissions/sso.mdx` ("Domain verification (opt-in)") states the rule.
10+
11+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped: sys_sso_provider and sys_user keep their columns, and no stored row changes shape. The rule is judged when an SSO sign-in reaches an account, so there is nothing at rest for a metadata migration to rewrite and no tombstone. -->
12+
13+
**BREAKING** — with SSO domain verification on (`OS_SSO_DOMAIN_VERIFICATION=true` or `plugins.ssoDomainVerification`), the rule that already decides account creation (an account reached through a provider holds an email address only at a domain that provider has proven) now also decides the two other ways an SSO sign-in (SAML or OIDC) reaches an account:
14+
15+
- **An implicit link.** A sign-in that links to an account that already exists is admitted only when the asserted email address is at one of the provider's listed, verified domains, matched exactly. Two cases, kept apart: an address at a domain the provider does not list at all was already not linked and is still refused as before, with the same answer; an address at a subdomain of a listed domain that is not itself listed and verified was linked before and is now refused with `EMAIL_DOMAIN_NOT_ALLOWED`.
16+
- **An address change.** A later sign-in that would change the stored email address of an account the provider is already linked to (the provider is set to update the account from what the identity provider asserts) is admitted only when the new address is at one of those domains. A sign-in that would not change the stored address is not judged: one asserting the account's own address, and one through a provider that does not update the account (every SAML provider, and an OIDC provider without that update setting, which the platform's own provider registration never turns on).
17+
- A refusal under this rule is `EMAIL_DOMAIN_NOT_ALLOWED`, the code creation already answers: the sign-in redirects to the error page with `error=EMAIL_DOMAIN_NOT_ALLOWED`, no link is written, no session is issued, and the stored address is unchanged. A provider that cannot be read refuses.
18+
- Unchanged: environments with verification off; sign-ins that would not change the stored address; explicit links from account settings; sign-ins through the cloud identity provider and enterprise OAuth providers.
19+
20+
**Who moves.**
21+
22+
- A deployment with verification on, whose people already hold local accounts at a subdomain of a domain its provider lists (for example `eng.acme.com` under a provider listing `acme.com`), lists that subdomain on the provider and verifies it before those people first sign in through the provider. Accounts at a domain the provider does not list at all were not linked before either; nothing moves for them.
23+
- A deployment with verification on, whose provider updates accounts from what its identity provider asserts and whose people move to a new email domain, lists the new domain on the provider and verifies it before the move.

‎content/docs/permissions/sso.mdx‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,27 @@ What verification decides, with it on:
380380
letters) is refused. Any other new account is refused with
381381
`error=EMAIL_DOMAIN_NOT_ALLOWED`, and nothing is created. No audience
382382
posture, invitation, first-user carve-out or `allowJit` setting relaxes this.
383-
- **Accounts that already exist are not re-checked.** A person who already has
384-
an account signs in as before.
383+
- **An SSO sign-in links to an existing account only at a proven domain.** When
384+
a sign-in links to an account that already exists (an implicit link), the
385+
asserted address must be at one of the provider's listed, verified domains,
386+
matched the same way. Two cases, kept apart:
387+
- an address at a domain the provider does not list at all was already not
388+
linked, and is still refused as before, with the same answer;
389+
- an address at a subdomain of a listed domain that is not itself listed and
390+
verified is no longer linked: it is refused with
391+
`error=EMAIL_DOMAIN_NOT_ALLOWED`.
392+
393+
Either way no link is written and no session is issued.
394+
- **A sign-in changes a linked account's address only to a proven domain.** A
395+
provider set to update the account from what the identity provider asserts
396+
writes a new address at a later sign-in. That sign-in is refused with
397+
`error=EMAIL_DOMAIN_NOT_ALLOWED`, and the stored address stays, unless the
398+
new address is at one of the provider's listed, verified domains. A sign-in
399+
that would not change the stored address is not re-checked: one that asserts
400+
the account's own address, and one through a provider that does not update
401+
the account (every SAML provider, and an OIDC provider without that update
402+
setting, which the platform's own provider registration never turns on),
403+
signs in as before. An explicit link from account settings is unchanged.
385404

386405
So if your people first sign in with addresses at a subdomain or at another
387406
domain, list each such domain on the provider as a bare host name and verify it.

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ import {
6868
AUTH_VERIFICATION_CONFIG,
6969
} from './auth-schema-config.js';
7070
import { vendorClientAddressOptions } from './client-address-vendor.js';
71-
import { PLATFORM_IDP_PROVIDER_ID, refuseImplicitAccountLink } from './implicit-account-linking.js';
71+
import {
72+
PLATFORM_IDP_PROVIDER_ID,
73+
refuseImplicitAccountLink,
74+
refuseSsoAddressChangeOutsideProvenDomain,
75+
} from './implicit-account-linking.js';
7276
import { isPlaceholderEmail } from './placeholder-email.js';
7377
import { logAfterResponseSendFailure, sendAfterResponseOnPublicDoor } from './run-after-response.js';
7478
import { isVisitorResendInEndpointContext } from './visitor-verification-resend.js';
@@ -353,8 +357,11 @@ export async function createAuthInstance(this: AuthInstanceHost): Promise<Auth<a
353357
// for an email address at a domain it has proven
354358
// (`sso-account-at-proven-domain.ts`). Judged first, on its own: no
355359
// posture, invitation or JIT toggle relaxes it, and the audience gate
356-
// must admit as well. Then, verification on or off, an SSO provider
357-
// creates an account only for a value that is an email address.
360+
// must admit as well. The same rule holds when an SSO sign-in links
361+
// implicitly to an existing account, or would change a linked account's
362+
// stored address (`implicit-account-linking.ts`, rule 4). Then,
363+
// verification on or off, an SSO provider creates an account only for a
364+
// value that is an email address.
358365
//
359366
// Before all of them, on every action (create, link, sign-in): a SAML
360367
// sign-in keyed on a stable attribute read from the posted assertion
@@ -364,20 +371,25 @@ export async function createAuthInstance(this: AuthInstanceHost): Promise<Auth<a
364371
this.idpLocale.stage(data, ctx);
365372
const contradicted = refuseSamlAccountKeyContradiction(data, this.config.logger);
366373
if (contradicted) return contradicted;
367-
const outsideProvenDomain = await refuseSsoAccountOutsideProvenDomain(data, {
374+
const provenDomain = {
368375
verificationEnabled: () => this.isSsoDomainVerificationEnabled(),
369376
reader: this.config.dataEngine ? (withSystemReadContext(this.config.dataEngine) as never) : undefined,
370377
logger: this.config.logger,
371-
});
378+
};
379+
const outsideProvenDomain = await refuseSsoAccountOutsideProvenDomain(data, provenDomain);
372380
if (outsideProvenDomain) return outsideProvenDomain;
373381
// Verification on or off, an SSO provider creates an account only for
374382
// an email address (same module): an opaque NameID is no account key.
375383
const notAnAddress = refuseSsoAccountWithoutAddress(data, this.config.logger);
376384
if (notAnAddress) return notAnAddress;
377385
return (await refuseImplicitAccountLink(data, ctx, {
378386
requireLocalEmailVerified: this.implicitLinkRequiresLocalEmailVerified(),
387+
provenDomain,
379388
resolveContext: () => this.linkingContext(),
380389
logInfo: (m, meta) => this.config.logger?.info?.(m, meta),
390+
})) ?? (await refuseSsoAddressChangeOutsideProvenDomain(data, ctx, {
391+
provenDomain,
392+
resolveContext: () => this.linkingContext(),
381393
})) ?? this.validateAudienceAdmission(data, ctx);
382394
},
383395
},

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,10 +770,13 @@ export async function buildPluginList(this: PluginListHost): Promise<any[]> {
770770
// when on, @better-auth/sso mounts /sso/request-domain-verification +
771771
// /sso/verify-domain (DNS TXT proof-of-ownership of every domain the
772772
// provider lists) and signs nobody in through a provider until its domain
773-
// list is verified; editing the list clears it. A new account is limited
774-
// to an email address at a domain the provider has proven, by plugin-auth's
775-
// own rule at the `validateUserInfo` gate (`sso-account-at-proven-domain.ts`),
776-
// asked through the same decision (`AuthManager.isSsoDomainVerificationEnabled`).
773+
// list is verified; editing the list clears it. An account a provider
774+
// reaches holds an email address only at a domain the provider has proven,
775+
// by plugin-auth's own rule at the `validateUserInfo` gate: a new account
776+
// (`sso-account-at-proven-domain.ts`), an implicit link to an existing
777+
// account, and a sign-in that would change a linked account's stored
778+
// address (`implicit-account-linking.ts`, rule 4), each asked through the
779+
// same decision (`AuthManager.isSsoDomainVerificationEnabled`).
777780
// Off by default to preserve the register→login flow.
778781
//
779782
// Per-provider sign-in options (ruling record 6718355623, `sso-sign-in-options.ts`):

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5994,8 +5994,11 @@ export class AuthManager {
59945994
* Whether opt-in DNS domain-verification (ADR-0135 D6) is wired — i.e. the
59955995
* `/sso/request-domain-verification` + `/sso/verify-domain` endpoints are
59965996
* mounted, a provider signs nobody in until its listed domains are verified,
5997-
* and a provider creates an account only for an email address at a domain it
5998-
* has proven (`sso-account-at-proven-domain.ts`, which asks this method).
5997+
* and an account a provider reaches holds an email address only at a domain
5998+
* it has proven: a provider creates an account only for such an address
5999+
* (`sso-account-at-proven-domain.ts`), links to an existing account only
6000+
* with one, and changes a linked account's stored address only to one
6001+
* (`implicit-account-linking.ts`, rule 4). All three ask this method.
59996002
* Resolved with the EXACT logic `buildPluginList` uses for the `sso()`
60006003
* `domainVerification.enabled` option, so the bridge can return a clear
60016004
* "not enabled for this environment" instead of a bare 404 when off.

0 commit comments

Comments
 (0)