Repository navigation
Commit 3c44a97
fix(auth)!: an unmapped SAML sign-in reads the standard email and name claims before the NameID; SSO creates accounts only for email addresses (#33418)
Fixes #32146
Clause-②: yes (narrowing) — narrowed: a new account a SAML or OIDC
sign-in would create through an `@better-auth/sso` provider is refused
`403 INVALID_EMAIL` (`error=INVALID_EMAIL` on the sign-in's error
redirect) when the email the sign-in resolved to is not an email
address, with SSO domain verification off as well as on. Widened: for
each SAML attribute-mapping member a provider leaves unset, the sign-in
first reads the standard claim the assertion carries
(`.../identity/claims/emailaddress`, `givenname`, `surname`, `name`). No
first-party composition in this repository registers a SAML or OIDC
provider.
Metadata-protocol: none
Skills-docs: none — no skill under `skills/**` teaches the SAML
attribute mapping or SSO account creation;
`content/docs/permissions/sso.mdx` gains both rules in this change.
## 维护者速读
- **改了什么**:SAML 登录在管理员没配属性映射时,先读 Entra ID / AD FS
默认发送的标准声明(emailaddress、givenname、surname、name),再退回 `email` 属性与
NameID;另外,无论是否开启域名验证,SSO 只为真实邮箱地址建账号,否则以 `INVALID_EMAIL` 拒绝。
- **为什么改**:Entra 不发 `email` 属性,未配映射时账号邮箱取自 NameID:从登录页进入是 UPN,从
Entra「我的应用」进入是不透明的持久 ID,同一个人会多出第二个账号(而且带着映射的权限集)。
- **风险与回滚**:SAML 或 OIDC 提供方若把用户解析成非邮箱值(只发不透明
NameID、或映射到用户名声明),新用户将不再被创建(已有账号不受影响);回滚即 revert 本 PR。读取标准声明依赖供应商内部行为(见
Acceptance notes),供应商升级若改变它,端到端测试会变红。
-
**先看**:`packages/plugins/plugin-auth/src/sso-saml-standard-claims.ts`(整个机制与其对供应商的依赖)·
`packages/plugins/plugin-auth/src/sso-account-at-proven-domain.ts` 末尾的
`refuseSsoAccountWithoutAddress` ·
`packages/plugins/plugin-auth/src/auth-manager.ts` 中 `runSsoCallback` 的
SAML 分支。
- **两分钟验证**:`pnpm --filter @objectstack/plugin-auth exec vitest run
--maxWorkers=2 src/sso-saml-refusal-landing.test.ts -t "standard
claims"`,预期 10 个用例全绿;其中「SP-initiated then
IdP-initiated」与「portal-first」两例显示两次进入后只有一个账号,第二次进入被拒(account_not_linked)。
- **需要你做的**:决定同一人从 IdP 门户第二次进入时,是否应自动登录到同一账号(今天按平台关联规则以
`account_not_linked` 拒绝,不再建第二个账号)—— 选项见报告的 needs_user_decision。
## What changes
- **An unmapped SAML sign-in reads the standard claims before the
NameID** (`sso-saml-standard-claims.ts`). For each of `email`,
`firstName`, `lastName`, `name` the provider's mapping leaves unset, a
sign-in whose assertion carries the standard claim
(`http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress`,
`givenname`, `surname`, `name`) reads it before the vendor's own default
and before the NameID. A mapped member is read as mapped; a provider
that sends `email` and none of these claims signs in exactly as before;
when the mapping is unset and the assertion carries both a plain `email`
attribute and the `emailaddress` claim, the standard claim takes
precedence (where the two differ, a new account is created at the claim
address; an existing account still signs in by its account key). The
stored mapping is never written.
- Mechanism: `@better-auth/sso` 1.7.12 reads one attribute name per
member off `samlConfig.mapping` and offers no seam between verifying the
assertion and resolving the user. So the ACS request's posted response
is read once, before the vendor runs, for the attribute NAMES of its one
unencrypted assertion (values always come from the vendor's verified
extract), and the adapter's read of that provider's row in that request
hands the vendor a mapping that also names those claims, as
non-enumerable members so the vendor's provider fingerprint (taken at
the sign-in's start and again at the ACS) does not change.
- **Verification on or off, an SSO provider creates an account only for
an email address** (`refuseSsoAccountWithoutAddress`, wired into
`validateUserInfo` after the proven-domain rule). A SAML or OIDC
creation whose resolved email is not an address is refused
`INVALID_EMAIL` (an existing ledger code for plugin-auth) with a
description naming the remedy and not the value. With verification on,
the proven-domain rule still answers first (`EMAIL_DOMAIN_NOT_ALLOWED`),
unchanged. Existing accounts, linking and non-SSO creations are
unchanged.
- Docs: `content/docs/permissions/sso.mdx` (the register table, the
mapping section, the verification section). Changeset: `patch`, declared
BREAKING with its ADR-0087 disposition.
## Tests
All end-to-end cases run the real `AuthManager` with the real
`better-auth` + `@better-auth/sso` over the in-memory engine; the IdP is
played with the vendor's own `samlify`, signing with a key generated in
the test (`sso-saml-refusal-landing.test.ts`, two new top-level
`describe` blocks in the existing harness):
- SP-initiated (email-format NameID = UPN) then IdP-initiated
(persistent opaque NameID), Entra default claims: one account
`mia.chen@acme.com (Mia Chen)`; the second entry reaches it and is
refused `account_not_linked` by the platform's linking rule,
verification off and on. IdP-initiated first: the account is made for
the address, never the opaque id.
- Claim before an `email` attribute; the `name` claim when no
given/surname; an admin-named member wins; a stored mapping naming only
the groups attribute still completes an SP-initiated sign-in
(fingerprint); CONTROL: an `email`-only IdP is unchanged; CONTROL: the
stored mapping is unchanged.
- No email attribute/claim and an opaque NameID: refused
`INVALID_EMAIL`, nothing written (IdP- and SP-initiated); CONTROL: an
address is admitted.
- Pure halves: `sso-saml-standard-claims.test.ts` (name reading:
encrypted, two or nested assertions, oversize, non-XML read nothing;
member choice; the adapter read half incl. non-enumerability and no
mutation of the engine object), and a new `describe` in
`sso-account-at-proven-domain.test.ts` (SAML and OIDC, refusal names no
value, only SSO creations judged).
Ablations (each through `scripts/ablation-replace.mjs` on a committed
head, restore verified as blob == HEAD and `git diff HEAD` empty):
1. Drop the adapter read half (`applyStandardSamlClaimsOnRead` in
`findOne`): 8 of the 9 standard-claim cases red; the `email`-only
CONTROL stays green.
2. Make the added mapping members enumerable: exactly the 3 SP-initiated
cases red with
`error=invalid_state&error_description=sso_provider_changed_during_authentication`
(the fingerprint).
3. Unwire `refuseSsoAccountWithoutAddress`: both `INVALID_EMAIL` cases
red; the CONTROL stays green.
## Acceptance notes
- **Vendor reliance.** The standard-claim read depends on
`@better-auth/sso` reading `parsedSamlConfig.mapping` off the object the
adapter returns, and fingerprinting only enumerable members. A vendor
upgrade that copies the config first returns the old defaults (ablation
1 shape: the end-to-end pins go red); one that fingerprints
non-enumerable members refuses SP-initiated sign-ins at the ACS
(ablation 2 shape). The durable fix is upstream: a default-candidates
option for the SAML mapping.
- **Encrypted assertions** carry no readable names before the vendor
decrypts them, so such a provider reads only what it maps (documented).
- **The second, IdP-initiated entry is refused `account_not_linked`, not
signed in**: the account the first entry made is not email-verified, and
an unverified provider is not trusted to link itself to an existing
account. Whether that entry should sign in to the same account (key on a
stable attribute, or a verified link) is a product choice, reported to
the dispatching seat, not decided here.
- **Form text, not changed here**:
`packages/platform-objects/src/identity/sys-sso-provider.object.ts`
(held by another card) still says the SAML email member "Defaults to
"email", then the NameID", and the mapping summary says "Empty means the
default: email from "email" (SAML: then the NameID)". Both now read the
standard claim first; the help text wants that sentence.
- **Console copy**:
`ui/apps/console/src/pages/auth/ssoCallbackRefusal.ts` maps no sentence
for `INVALID_EMAIL` (nor, already, for `EMAIL_DOMAIN_NOT_ALLOWED`) on an
SSO callback, so the end user reads the generic "the sign-in link
expired or was already used" with the code behind the details toggle.
Read from the code, not measured in a browser.
- An Entra user whose `mail` differs from the UPN and who already has an
account keyed on the UPN (made before this change) still has that
account found by its account key on SP-initiated sign-in; an
IdP-initiated entry now resolves to `mail`, finds no user by that
address, and creates one (as an opaque-id account was created before).
## Verification
All readings below at head `e90385832` (the branch merged with
`origin/main` `e0ce1b523`), on a shared container.
<details><summary>plugin-auth: full suite, typecheck, lint</summary>
- `pnpm --filter @objectstack/plugin-auth test` (after building
`@objectstack/plugin-auth...`): `Test Files 273 passed (273)`, `Tests
5283 passed | 10 skipped (5293)`, lock `VERDICT command-exit 0`.
- `pnpm --filter @objectstack/plugin-auth typecheck` (tsc, examples,
`check:test-typecheck`): `VERDICT command-exit 0`;
`check:test-typecheck: OK`. Taken before the merge with `origin/main` at
`946912a7a`; the merge touched `auth-manager.ts` outside these hunks,
and the post-merge suite above compiles every file.
- `eslint --no-inline-config` over the changed `.ts` files: exit 0, no
output.
- Public surface: no export of the package entry changes (`src/index.ts`
untouched by this diff), so no dependent owes a test;
`packages/qa/dogfood` is the one dependent with an SSO test, and it ran
in the battery below.
</details>
<details><summary>Dogfood battery (title carries a breaking
marker)</summary>
- `pnpm --filter '@objectstack/dogfood^...' build && pnpm --filter
@objectstack/dogfood test` at `e90385832`: `Test Files 465 passed | 3
skipped (468)`, `Tests 3932 passed | 42 skipped (3974)`, lock `VERDICT
command-exit 0`.
</details>
<details><summary>Derived gate families (dispatch-gates --commands,
106)</summary>
- 103 exit 0. `check:type-check-debt` first answered PREREQUISITE NOT
MET (its closure build queued behind the dogfood holder), re-run under
the lock: `--re-measure: OK`, `VERDICT command-exit 0`.
- NOT MEASURED locally, exit 3 PREREQUISITE NOT MET (built output of
packages outside this diff absent in this container): `@objectstack/spec
check:skill-examples` (client SDK dist), `check:dual-build-cjs-loads`
(several plugin dists). Both are CI's.
- `dispatch-gates --ran`: 106 derived, 106 run, 0 unrun.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_016WV4d1NJYFGVi2FRztyBNo)_
<!-- signed: domain:server#5 · os-elon-musk ·
session_016WV4d1NJYFGVi2FRztyBNo -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 3c231cd commit 3c44a97
13 files changed
Lines changed: 821 additions & 48 deletions
File tree
- .changeset
- content/docs/permissions
- packages/plugins/plugin-auth/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
386 | 386 | | |
387 | 387 | | |
388 | 388 | | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
389 | 398 | | |
390 | 399 | | |
391 | 400 | | |
| |||
442 | 451 | | |
443 | 452 | | |
444 | 453 | | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
449 | 458 | | |
450 | 459 | | |
451 | 460 | | |
| |||
485 | 494 | | |
486 | 495 | | |
487 | 496 | | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
493 | 528 | | |
494 | 529 | | |
495 | 530 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
352 | 352 | | |
353 | 353 | | |
354 | 354 | | |
355 | | - | |
| 355 | + | |
| 356 | + | |
356 | 357 | | |
357 | 358 | | |
358 | 359 | | |
| |||
361 | 362 | | |
362 | 363 | | |
363 | 364 | | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
364 | 369 | | |
365 | 370 | | |
366 | 371 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
287 | 287 | | |
288 | 288 | | |
289 | 289 | | |
| 290 | + | |
290 | 291 | | |
291 | 292 | | |
292 | 293 | | |
| |||
4308 | 4309 | | |
4309 | 4310 | | |
4310 | 4311 | | |
4311 | | - | |
| 4312 | + | |
| 4313 | + | |
| 4314 | + | |
| 4315 | + | |
| 4316 | + | |
| 4317 | + | |
| 4318 | + | |
| 4319 | + | |
| 4320 | + | |
| 4321 | + | |
4312 | 4322 | | |
4313 | 4323 | | |
4314 | 4324 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
1014 | 1015 | | |
1015 | 1016 | | |
1016 | 1017 | | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
1017 | 1022 | | |
1018 | 1023 | | |
1019 | 1024 | | |
| |||
1065 | 1070 | | |
1066 | 1071 | | |
1067 | 1072 | | |
| 1073 | + | |
1068 | 1074 | | |
1069 | 1075 | | |
1070 | 1076 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1265 | 1265 | | |
1266 | 1266 | | |
1267 | 1267 | | |
1268 | | - | |
1269 | | - | |
1270 | | - | |
1271 | | - | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
1272 | 1275 | | |
1273 | 1276 | | |
1274 | 1277 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| |||
323 | 326 | | |
324 | 327 | | |
325 | 328 | | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
0 commit comments