Skip to content

Commit 1e692d1

Browse files
fix(plugin-auth)!: SSO discovery fetches only hosts that resolve to public addresses (#35697)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9D1WDVL9JpCQhTUF97fpymtZ)_ Clause-②: no (narrowing) Metadata-protocol: none Skills-docs: none — no published skill teaches SSO provider registration; the rule is written into the hand-written SSO guide in this diff ## 维护者速读 - 平台管理员登记 SSO 身份提供方时,服务器先确认其发现文档所在主机解析到公网地址;解析到内网、回环等非公网地址且未列入部署受信来源的,现在直接拒绝并说明原因,其余登记照旧。 - 替部署运维统一规则:登记时就按登录时已在执行的同一条规则检查发现文档的主机,两处说法一致。 - 碰到的是把身份提供方放在内网、又没把它列入受信来源的部署:登记会被拒,运维把它的来源加入受信来源即可;本改动不动已存数据,可直接回滚。 - 无。 ## What changes SSO discovery fetches only hosts that resolve to public addresses. - **The rule** (`refuseSsoDiscoveryHostNotPublic`, `sso-discovery-companion-origins.ts`): before the vendor's `POST /sso/register` endpoint runs, the host whose OpenID Connect discovery document the registration reads (the explicit `oidcConfig.discoveryEndpoint`, else the issuer's well-known URL, computed with the vendor's own `computeDiscoveryUrl`) is resolved, and every address must be public, judged by the vendor's own predicate (`isPublicRoutableHost`). Otherwise the registration is refused `400 VALIDATION_FAILED` on `issuer` / `oidcConfig.discoveryEndpoint` and nothing is read from the host. The Register SSO Provider bridge relays it as `400 SSO_REGISTER_FAILED`. - **Unchanged on purpose:** the origins a registration names stay trusted for that registration's discovery checks (the untrusted-origin verdicts); a discovery URL the deployment's configured trusted origins (`OS_TRUSTED_ORIGINS`) list is not resolved here, so an identity provider on an internal network still registers once listed; an address literal and a non-public name are left to the vendor's own synchronous refusals; a name that does not resolve, or a runtime with no `node:dns/promises`, is left to the read, as the vendor leaves it. - **Scope of the resolution is the one host the registration reads.** A SAML registration and an `oidcConfig.skipDiscovery` registration read nothing at registration and resolve nothing; endpoints such as the token, userinfo and key-set URLs are not read at registration either (the vendor resolves them when it reads them at sign-in). - **The register door hands the body-named origins on only for an admitted actor, and only after the check** (`auth-manager-before-hook.ts`). A request with no session still gets the vendor's `401`, and resolves no name. - The code is the ledger-registered `VALIDATION_FAILED` (plugin-auth's entry), so no error-code ledger change. - Docs: the SSO guide's discovery section states the rule (`content/docs/permissions/sso.mdx`). ## Read first 1. `packages/plugins/plugin-auth/src/sso-discovery-companion-origins.ts` — `ssoRegisterDiscoveryRead` / `refuseSsoDiscoveryHostNotPublic`: which host is resolved, and every early return (configured trust, literal, non-public name, no resolver, lookup failure). Risk: a host the vendor reads that this does not resolve. 2. `packages/plugins/plugin-auth/src/auth-manager-before-hook.ts` — the `/sso/register` branch: the check sits after the platform-admin, held-actor and domain gates and before the sign-out switch's check (so a registration with that switch on is refused on the same member before anything is read), and `bodyOrigins` is filled only inside the admitted-actor block. Risk: ordering against the sign-out switch's own discovery read. 3. `packages/plugins/plugin-auth/src/sso-discovery-companion-origins.test.ts` — the new top-level describes and the shared name-resolution double. ## Two-minute verification `pnpm --filter @objectstack/plugin-auth exec vitest run --maxWorkers=2 src/sso-discovery-companion-origins.test.ts` — expect every case green, including the describe "SSO discovery reads only hosts that resolve to public addresses". ## Tests All readings on the final head `ba221aaa508`, under the shared verify lock (lock outcome `command-exit`, rc 0). - `@objectstack/plugin-auth` test: Test Files 287 passed (287) · Tests 6066 passed, 10 skipped (6076). - `@objectstack/plugin-auth` typecheck: exit 0, including `check:test-typecheck: OK` for the test layer. - Full dogfood battery (`pnpm --filter @objectstack/dogfood test`, no shard, no path, after building its dependency closure): exit 0 · Test Files 482 passed, 3 skipped (485) · Tests 4093 passed, 68 skipped (4161) · 1766.26s. - New cases: - describe "SSO discovery reads only hosts that resolve to public addresses" drives the real register door with a `node:dns/promises` double and a fetch recorder. It covers an issuer whose host resolves to `10.0.0.5`, `127.0.0.1` or `169.254.169.254` (each refused `400 VALIDATION_FAILED` on `issuer`, nothing fetched, no row), a mixed public and private answer, an explicit `oidcConfig.discoveryEndpoint`, and the bridge (`400 SSO_REGISTER_FAILED`). With the sign-out switch on, a privately resolving issuer is refused on `issuer` before the sign-out check reads it. Controls: a non-platform-admin session answers `403 SSO_REGISTER_FORBIDDEN`, asks no name, fetches nothing and stores no row; a public answer registers, the name is asked and exactly the discovery URL is fetched; a configured trusted origin registers on a private answer; no session answers `401` and asks no name. - describe "the host a registration's discovery reads is the one host resolved" (unit, injected resolver): issuer well-known URL; explicit endpoint instead of the issuer; SAML, `skipDiscovery`, an empty body and no body resolve nothing; configured trust, address literals, `localhost` and a non-public name resolve nothing; a lookup error and an all-public answer are not refused. - Ablation of the ordering (one-shot, nothing kept): with `auth-manager-before-hook.ts` restored to `32d1f053b45` (check after the sign-out check), the test file went 2 failed, 42 passed (44): both switch-on cases, field `signOutOfIdp` instead of `issuer`. Restored to `HEAD` (blob `5ec97d7362`, `git diff HEAD` empty). - Ablation of the check (one-shot, nothing kept, on `32d1f053b45`): with `auth-manager-before-hook.ts` restored to the merge base (on-disk blob `990a149fe2` equal to the merge-base blob, 0 occurrences of the check), the test file went 7 failed, 35 passed (42): the six refusal cases and the public control's resolved-name assertion. Restored to `HEAD` (blob `62eb4d262d`, `git diff HEAD` empty). ## Acceptance notes - **Fixture triage.** The sign-out update case (its update's discovery read keeps the configured trust) used to register while the issuer's host resolved to a private address, which registration now refuses. Its setup now registers under a public answer and switches to the private answer before the update; what it pins is unchanged. - **Helper reuse.** `@objectstack/core/egress` is not reused: it is the connect-time guard of the outbound HTTP surfaces with its own allow-list (`OS_EGRESS_ALLOWED_TARGETS`). Registration judges with the vendor's own predicate so that registration and sign-in apply one rule with one way out (`OS_TRUSTED_ORIGINS`). - **Resolution failure** is left to the read, as the vendor's own check leaves it; the refusal names the host, the member, the rule and the remedy, not the resolved address. - **NOT MEASURED locally** (CI's Lint & Repo Gates owns each): - `check:type-check-debt`: it starts a whole-workspace closure build; stopped (exit 143) to free the shared lock. - `check:dual-build-cjs-loads` and `@objectstack/spec check:skill-examples`: exit 3, PREREQUISITE NOT MET (built output outside this closure). - `test:repo` (the vitest `repo` project) not run locally: the diff touches none of its tests and the derivation does not name it; CI's Test Core runs it. ## Verification <details><summary>Derived gate families on ba221aa (106)</summary> `node scripts/pm/dispatch-gates.mjs --commands` named 106 families for this diff. On the final head: 103 exit 0, 2 exit 3 (PREREQUISITE NOT MET), 1 NOT-MEASURED (`check:type-check-debt`; CI `Type Check · debt ledger` success). `--ran` reconciliation: `✓ dispatch-gates --ran: 106 derived famil(ies) accounted for — 103 run, 3 NOT-MEASURED (2 DERIVED from a recorded exit 3, 1 claimed).` Outside the derivation: `pnpm check:error-code-casing` exit 0 — `✓ no unlisted lowercase error codes in 12955 scanned file(s) (ADR-0112).` ADR-0087: `✓ check-adr-0087-registration: 1 declared-breaking changeset(s), each carrying an ADR-0087 disposition.` (`[BREAKING+bang+clause-②-narrowing] not-required (no-migration-prescription)`) </details> <details><summary>Package and dogfood runs on ba221aa</summary> ``` closure build exit 0 plugin-auth build exit 0 plugin-auth typecheck exit 0 check:test-typecheck: OK plugin-auth test exit 0 Test Files 287 passed (287) · Tests 6066 passed | 10 skipped (6076) dogfood closure build exit 0 dogfood test exit 0 Test Files 482 passed | 3 skipped (485) · Tests 4093 passed | 68 skipped (4161) tree clean after the run; HEAD at the end ba221aa ``` </details> <details><summary>Ablation legs</summary> ``` AFTER leg (HEAD 9377c64): Tests 1 failed | 41 passed (42) -- the sign-out update fixture, fixed in 9991011 ABLATION leg: before-hook from the merge base, blob 990a149 == merge-base blob, check occurrences 0 Tests 7 failed | 35 passed (42) RESTORED: blob 62eb4d2 == HEAD; git diff HEAD empty ORDER ABLATION (HEAD ba221aa): before-hook from 32d1f05, blob 62eb4d2, order signout@613 publichost@620 Tests 2 failed | 42 passed (44) RESTORED: blob 5ec97d7 == HEAD; order publichost@621 signout@635; git diff HEAD empty ``` </details> --- _Generated by [Claude Code](https://claude.ai/code/session_016WV4czt69hUUaG5XcgFNYL)_ <!-- signed: security#2 · os-elon-musk · session_016WV4czt69hUUaG5XcgFNYL --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5ed945b commit 1e692d1

6 files changed

Lines changed: 471 additions & 23 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@objectstack/plugin-auth': patch
3+
---
4+
5+
fix(plugin-auth)!: SSO discovery fetches only hosts that resolve to public addresses
6+
7+
Clause-②: no (narrowing)
8+
Metadata-protocol: none
9+
10+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. What moves is one runtime answer of the SSO provider register door: a registration whose discovery document sits on a host that resolves to an address that is not public, on an origin the deployment's trusted origins do not list, is refused before anything is read. No stored row, registry key or authored value changes, so there is nothing for a migration to rewrite and no tombstone. -->
11+
12+
**BREAKING** for an SSO provider registration (`POST /api/v1/auth/sso/register`, or the Register SSO Provider bridge `POST /api/v1/auth/admin/sso/register`) whose OpenID Connect discovery document sits on a host that resolves to a private, loopback, link-local or otherwise non-public address, on an origin `OS_TRUSTED_ORIGINS` does not list: it is refused `400 VALIDATION_FAILED` on `issuer` (or on `oidcConfig.discoveryEndpoint` when the registration names one), and the bridge answers `400 SSO_REGISTER_FAILED`. It ships `patch` because it is a non-protocol breaking change (AGENTS.md Post-Task Checklist step 3): no metadata author writes an SSO provider registration, so `Metadata-protocol` is `none`.
13+
14+
SSO discovery fetches only hosts that resolve to public addresses. Before a registration reads its discovery document, the server resolves that document's host and every address must be public (the same predicate `@better-auth/sso` applies to the endpoints it reads at sign-in). The origins a registration names stay trusted for that registration's discovery checks, as before; that trust never stands in for this one. A registration that reads no discovery document (a SAML provider, or `skipDiscovery`) resolves nothing, and a registration with no session is answered `401` before any name is resolved.
15+
16+
Fix for an operator whose identity provider runs on an internal network: list its origin in `OS_TRUSTED_ORIGINS`, and the registration reads its discovery document as before.

‎content/docs/permissions/sso.mdx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ identity provider on an internal or private network is refused **400
178178
`VALIDATION_FAILED`** on `signOutOfIdp` until the provider's origin is listed in
179179
`OS_TRUSTED_ORIGINS`.
180180

181+
**A registration checks that its discovery host resolves to public addresses
182+
before reading it.** Before a registration reads its discovery document, the
183+
server resolves that document's host (the `discoveryEndpoint` the registration
184+
names, else the issuer's) and every address it resolves to must be public. A
185+
host that resolves to a private, loopback, link-local or otherwise non-public
186+
address is refused **400 `VALIDATION_FAILED`** on `issuer` or
187+
`oidcConfig.discoveryEndpoint`, and nothing is read from it; the Register SSO
188+
Provider bridge answers the same refusal as `SSO_REGISTER_FAILED`. The check
189+
comes before the **Sign Out of the Company Sign-In Too** check above, so a
190+
registration that also turns that switch on is refused on the same member.
191+
Trust an origin gains because a registration names it never stands in for this
192+
check. An identity provider on an internal network registers once its origin
193+
is listed in `OS_TRUSTED_ORIGINS`.
194+
181195
Google is one such provider: its discovery document names the token endpoint,
182196
the key set and the userinfo endpoint each on a host of its own, so a Google
183197
registration names all three, either in the **Token URL**, **JWKS URL** and

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import { isPendingSsoSource, PENDING_SSO_MESSAGES } from './pending-sso-marker.j
8787
import { ORGANIZATION_DELETE_PATH } from './organization-delete.js';
8888
import { carriesPrincipalSession } from './principal-session-mint.js';
8989
import { SSO_PROVIDER_DELETE_PATH } from './sign-in-method-floor.js';
90-
import { ssoDiscoveryCompanionContext } from './sso-discovery-companion-origins.js';
90+
import { refuseSsoDiscoveryHostNotPublic, ssoDiscoveryCompanionContext } from './sso-discovery-companion-origins.js';
9191
import { snapshotRoleChangeGrants, UPDATE_MEMBER_ROLE_PATH } from './role-change-granted-set.js';
9292
import { refuseVisitorResendWithoutLiveTransport } from './visitor-verification-resend.js';
9393

@@ -592,6 +592,10 @@ export async function globalBeforeHook(
592592
// answered `200` and the provider row landed, in every held state.
593593
if (ctx?.path === '/sso/register') {
594594
const actor = await this.resolveActor(ctx);
595+
// The origins the body names reach the vendor's register endpoint only
596+
// for an actor these gates admitted, and only after the host its
597+
// discovery reads was resolved (below).
598+
let bodyOrigins: string[] = [];
595599
if (actor?.userId) {
596600
await this.assertActorNotHeld(actor);
597601
const ok = await this.isPlatformAdminUserId(actor.userId);
@@ -605,16 +609,39 @@ export async function globalBeforeHook(
605609
// [#32085] One email domain, one provider: a domain another
606610
// provider already serves is refused 409, naming that provider.
607611
await this.assertSsoProviderDomainFree(ctx);
612+
// SSO discovery fetches only hosts that resolve to public addresses:
613+
// the host this registration's discovery reads is resolved before the
614+
// vendor's endpoint runs, and every address must be public, unless the
615+
// deployment's configured trusted origins (read here, before any
616+
// body-named origin joins them) list it
617+
// (`refuseSsoDiscoveryHostNotPublic`). Asked before the sign-out
618+
// switch below, whose check reads the same discovery document: a host
619+
// that is not public is refused on the member that names it, whatever
620+
// the switch says, and nothing is read from it.
621+
const notPublic = await refuseSsoDiscoveryHostNotPublic({
622+
body: ctx?.body,
623+
isConfiguredTrusted: (url) =>
624+
typeof ctx?.context?.isTrustedOrigin === 'function' && ctx.context.isTrustedOrigin(url) === true,
625+
});
626+
if (notPublic) {
627+
const { APIError } = await import('better-auth/api');
628+
throw new APIError(notPublic.status, {
629+
code: notPublic.code,
630+
message: notPublic.message,
631+
field: notPublic.field,
632+
});
633+
}
608634
// Ruling record 6866334319: the sign-out switch, as on update-provider.
609635
await this.assertSsoSignOutOfIdpSupported(ctx);
636+
bodyOrigins = await this.ssoDiscoveryTrustedOrigins(ctx?.body);
610637
}
611638
// The origins the body names (its issuer and any explicit `oidcConfig`
612639
// endpoint) and a known IdP's companion origins (Microsoft Entra ID's
613640
// userinfo on graph.microsoft.com), trusted for this endpoint's discovery
614641
// fetch and validation only: the router's origin check has already run
615642
// against the configured trusted origins alone, and never sees them
616643
// (`sso-discovery-companion-origins.ts`).
617-
return ssoDiscoveryCompanionContext(ctx, await this.ssoDiscoveryTrustedOrigins(ctx?.body));
644+
return ssoDiscoveryCompanionContext(ctx, bodyOrigins);
618645
}
619646

620647
// ── D5.1: cloud-as-IdP authorization gate ───────────────────

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6188,8 +6188,11 @@ export class AuthManager {
61886188
* `/sso/update-provider` body; the answer is the PUBLIC-ROUTABLE origins of its `issuer` and every explicit
61896189
* `oidcConfig` endpoint. Private / internal / loopback hosts are never
61906190
* trusted this way (better-auth's `isPublicRoutableHost`, the same predicate
6191-
* its own sub-endpoint check uses). Best-effort: anything unreadable yields
6192-
* `[]`.
6191+
* its own sub-endpoint check uses). A name that looks public is judged by
6192+
* its name here; the host a registration's discovery reads is resolved, and
6193+
* each address judged, before the register door hands this list on
6194+
* (`refuseSsoDiscoveryHostNotPublic`). Best-effort: anything unreadable
6195+
* yields `[]`.
61936196
*
61946197
* ⛔ Never an accepted `Origin`: the request's own origin check reads the
61956198
* deployment's configured trusted origins alone. This list reaches two

0 commit comments

Comments
 (0)