Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/sso-discovery-public-hosts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/plugin-auth': patch
---

fix(plugin-auth)!: SSO discovery fetches only hosts that resolve to public addresses

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- 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. -->

**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`.

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.

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.
14 changes: 14 additions & 0 deletions content/docs/permissions/sso.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ identity provider on an internal or private network is refused **400
`VALIDATION_FAILED`** on `signOutOfIdp` until the provider's origin is listed in
`OS_TRUSTED_ORIGINS`.

**A registration checks that its discovery host resolves to public addresses
before reading it.** Before a registration reads its discovery document, the
server resolves that document's host (the `discoveryEndpoint` the registration
names, else the issuer's) and every address it resolves to must be public. A
host that resolves to a private, loopback, link-local or otherwise non-public
address is refused **400 `VALIDATION_FAILED`** on `issuer` or
`oidcConfig.discoveryEndpoint`, and nothing is read from it; the Register SSO
Provider bridge answers the same refusal as `SSO_REGISTER_FAILED`. The check
comes before the **Sign Out of the Company Sign-In Too** check above, so a
registration that also turns that switch on is refused on the same member.
Trust an origin gains because a registration names it never stands in for this
check. An identity provider on an internal network registers once its origin
is listed in `OS_TRUSTED_ORIGINS`.

Google is one such provider: its discovery document names the token endpoint,
the key set and the userinfo endpoint each on a host of its own, so a Google
registration names all three, either in the **Token URL**, **JWKS URL** and
Expand Down
31 changes: 29 additions & 2 deletions packages/plugins/plugin-auth/src/auth-manager-before-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import { isPendingSsoSource, PENDING_SSO_MESSAGES } from './pending-sso-marker.j
import { ORGANIZATION_DELETE_PATH } from './organization-delete.js';
import { carriesPrincipalSession } from './principal-session-mint.js';
import { SSO_PROVIDER_DELETE_PATH } from './sign-in-method-floor.js';
import { ssoDiscoveryCompanionContext } from './sso-discovery-companion-origins.js';
import { refuseSsoDiscoveryHostNotPublic, ssoDiscoveryCompanionContext } from './sso-discovery-companion-origins.js';
import { snapshotRoleChangeGrants, UPDATE_MEMBER_ROLE_PATH } from './role-change-granted-set.js';
import { refuseVisitorResendWithoutLiveTransport } from './visitor-verification-resend.js';

Expand Down Expand Up @@ -592,6 +592,10 @@ export async function globalBeforeHook(
// answered `200` and the provider row landed, in every held state.
if (ctx?.path === '/sso/register') {
const actor = await this.resolveActor(ctx);
// The origins the body names reach the vendor's register endpoint only
// for an actor these gates admitted, and only after the host its
// discovery reads was resolved (below).
let bodyOrigins: string[] = [];
if (actor?.userId) {
await this.assertActorNotHeld(actor);
const ok = await this.isPlatformAdminUserId(actor.userId);
Expand All @@ -605,16 +609,39 @@ export async function globalBeforeHook(
// [#32085] One email domain, one provider: a domain another
// provider already serves is refused 409, naming that provider.
await this.assertSsoProviderDomainFree(ctx);
// SSO discovery fetches only hosts that resolve to public addresses:
// the host this registration's discovery reads is resolved before the
// vendor's endpoint runs, and every address must be public, unless the
// deployment's configured trusted origins (read here, before any
// body-named origin joins them) list it
// (`refuseSsoDiscoveryHostNotPublic`). Asked before the sign-out
// switch below, whose check reads the same discovery document: a host
// that is not public is refused on the member that names it, whatever
// the switch says, and nothing is read from it.
const notPublic = await refuseSsoDiscoveryHostNotPublic({
body: ctx?.body,
isConfiguredTrusted: (url) =>
typeof ctx?.context?.isTrustedOrigin === 'function' && ctx.context.isTrustedOrigin(url) === true,
});
if (notPublic) {
const { APIError } = await import('better-auth/api');
throw new APIError(notPublic.status, {
code: notPublic.code,
message: notPublic.message,
field: notPublic.field,
});
}
// Ruling record 6866334319: the sign-out switch, as on update-provider.
await this.assertSsoSignOutOfIdpSupported(ctx);
bodyOrigins = await this.ssoDiscoveryTrustedOrigins(ctx?.body);
}
// The origins the body names (its issuer and any explicit `oidcConfig`
// endpoint) and a known IdP's companion origins (Microsoft Entra ID's
// userinfo on graph.microsoft.com), trusted for this endpoint's discovery
// fetch and validation only: the router's origin check has already run
// against the configured trusted origins alone, and never sees them
// (`sso-discovery-companion-origins.ts`).
return ssoDiscoveryCompanionContext(ctx, await this.ssoDiscoveryTrustedOrigins(ctx?.body));
return ssoDiscoveryCompanionContext(ctx, bodyOrigins);
}

// ── D5.1: cloud-as-IdP authorization gate ───────────────────
Expand Down
7 changes: 5 additions & 2 deletions packages/plugins/plugin-auth/src/auth-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6188,8 +6188,11 @@ export class AuthManager {
* `/sso/update-provider` body; the answer is the PUBLIC-ROUTABLE origins of its `issuer` and every explicit
* `oidcConfig` endpoint. Private / internal / loopback hosts are never
* trusted this way (better-auth's `isPublicRoutableHost`, the same predicate
* its own sub-endpoint check uses). Best-effort: anything unreadable yields
* `[]`.
* its own sub-endpoint check uses). A name that looks public is judged by
* its name here; the host a registration's discovery reads is resolved, and
* each address judged, before the register door hands this list on
* (`refuseSsoDiscoveryHostNotPublic`). Best-effort: anything unreadable
* yields `[]`.
*
* ⛔ Never an accepted `Origin`: the request's own origin check reads the
* deployment's configured trusted origins alone. This list reaches two
Expand Down
Loading
Loading