Repository navigation
Commit ecd4b2b
fix(plugin-auth)!: SSO discovery connects only to addresses that are public at connect time (#35781)
<!-- 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
or sign-in; the rule is written into the hand-written SSO guide in this
diff
## 维护者速读
-
平台管理员登记或更新企业单点登录身份提供方、员工经单点登录登录时,服务器每次读取身份提供方配置,都只连接到连接那一刻判定为公网的地址;部署受信来源未列出的内网地址一律不连、什么都不发送,其余照旧。
-
延续上一轮“登记前先检查发现文档主机”的同一条规则:把判定放到连接发生的那一刻,并补上登出开关与登录回调的读取,替部署运维把这条规则收成一处实现。
-
碰到的是身份提供方放在内网、又没列入受信来源的部署:登记、打开“同时登出公司登录”或单点登录会被拒并说明怎么办;运维把它的来源加入受信来源即可恢复;不改任何已存数据,可直接回滚。
- 无。
## What changes
**The rule: SSO discovery connects only to addresses that are public at
connect time.** Every read plugin-auth has the server make of an
identity provider's OpenID Connect configuration is sent through the
core outbound-request guard (`@objectstack/core/egress`, the same guard
webhook and flow `http` requests use): the connection resolves the host,
judges every address with the platform's address classes
(`classifyAddress`), and connects to an address it judged, never
resolving again. So the address judged is the address connected. A host
that is not public is refused and nothing is sent to it; so is a name
that does not resolve when the read connects. `OS_TRUSTED_ORIGINS` (the
deployment's configured trusted origins) stays the one way to an
identity provider on an internal network; `OS_EGRESS_ALLOWED_TARGETS`
does not open SSO discovery.
The reads it covers: the registration's discovery document
(`/sso/register`, and the Register SSO Provider bridge that
re-dispatches there), the sign-out switch's read of that document on
register and on update-provider, a sign-in's discovery (`/sign-in/sso`),
and the callback's token, key-set and userinfo reads
(`/sso/callback/*`). The vendor's own update-provider endpoint reads
nothing.
- **How the reads reach the guard** (`sso-discovery-egress.ts`, new).
`@better-auth/sso` 1.7.12 takes no fetch and no dispatcher: its reads go
through `betterFetch`, which reads `globalThis.fetch` when called, and a
bare `fetch` for the key set. So the request routing opens an SSO
discovery scope (AsyncLocalStorage, opened with `run` around the whole
vendor request, as the SCIM scope is) for the three endpoint paths that
read, and a wrapper over the global fetch sends a read inside that scope
through the guard. The wrapper is inert outside a scope, and inside one
it judges only a read at an origin the request's provider configuration
names; a scope starts naming none. The origins are named where the
configuration is read, before anything reads from them: a registration's
from its body (issuer, every `oidcConfig` endpoint, the discovery
document, companion origins) by the global before-hook, which reads
nothing; a stored provider's as the vendor reads its row, through the
auth context's adapter, which a plugin mounted with `sso`
(`ssoProviderReadsObserverPlugin`) hands the context as an observed
view; the sign-out read's by its caller. Every other request in the same
request (a datastore or a rate-limit store reached over HTTP, a host's
own call), before or after the origins are named, is sent as it was. A
context whose adapter is not observed (a later view that does not hand
its reads on through the observed one) is answered `503
SERVICE_UNAVAILABLE` before the endpoint runs, so nothing is read or
sent. The scope store and the wrapper are keyed process-wide
(`Symbol.for`), so a second copy of the module finds both and never
wraps the global fetch again; the wrapper keeps the wrapped fetch's own
properties. A refused read rejects with the vendor's own
`DiscoveryError`, so every vendor path answers it as its own refusal of
a non-public host; the refusal names the host and the rule, never the
resolved address (that goes to the server log through the guard's
logger).
- **The sign-out read** (`AuthManager.assertSsoSignOutOfIdpSupported`)
runs in its own scope, which names the origins of the body's and the
stored provider configuration (the one document it reads) and judges a
read at them unless the configured trust lists it.
- **The registration's early check** (`refuseSsoDiscoveryHostNotPublic`)
now asks the guard's own judgement (`judgeUrl`, same resolver, same
policy) instead of resolving and classifying on its own, so the early
check and the connection apply one rule with one implementation. That
brings the platform's address classes to it, an address literal
included: `168.63.129.16` (the Azure host-platform endpoint, in
`METADATA_ADDRESSES`) is refused there, where the vendor's own predicate
admits it. A name that does not resolve at that check is left to the
read, as before; the connection then refuses it and sends nothing.
- **One answer for a registration.** A registration whose discovery read
is refused when it connects is answered `400 VALIDATION_FAILED` on
`issuer` / `oidcConfig.discoveryEndpoint`, the same answer the early
check gives (after-hook `explainSsoDiscoveryConnectRefusal`, and the
sign-out path on register). An update with the switch on is refused on
`signOutOfIdp`; a callback ends on the error page with
`invalid_provider`.
- **`@objectstack/core`**: the guarded fetch's init takes fetch's
`redirect` mode (it already reached undici; the type now says so). The
vendor reads with `redirect: 'manual'` and refuses redirects itself.
- **`AuthManagerOptions.ssoDiscoveryEgress`** (optional): the guard to
send through; absent, the real one is built once per manager with the
manager's logger. A suite that fakes the identity provider over a
stubbed global fetch hands the stand-in in
`sso-discovery-egress.testkit.ts` (it judges with the real policy over
`node:dns/promises`, then hands an admitted read to the stub).
- Docs: the SSO guide's paragraph on discovery hosts now states the
connect-time rule.
**Read first (by risk):**
1. `packages/plugins/plugin-auth/src/sso-discovery-egress.ts`:
`installFetchWrapper` / `sendThroughGuard` / `judges` (only named
origins are judged), `observingSsoProviderReads` +
`ssoProviderReadsObserverPlugin` (a stored provider's origins, named
from the row the vendor reads), and `declareSsoDiscoveryReadsOf` (a
registration's origins; no read).
2. `packages/plugins/plugin-auth/src/auth-manager-request-routing.ts`
(the `runHandler` choice: which endpoint paths open the scope) and
`auth-manager.ts` `runSsoDiscoveryEgress` (answers a context whose
adapter is not observed `503 SERVICE_UNAVAILABLE` before the endpoint
runs); the observer is mounted in `auth-manager-plugin-list.ts`.
3. `packages/plugins/plugin-auth/src/auth-manager.ts`:
`assertSsoSignOutOfIdpSupported` (the sign-out read's own scope and its
register answer).
## Tests
- `sso-discovery-connect-time.test.ts` (new, 25 cases): a real
`AuthManager` with the real `sso()` plugin and the real guard (its
policy and transport), name resolution as a double, and the ordinary
transport (the global fetch) as a double that resolves again at connect
and records the address it connects to. The host answers a public
address to every check before a read and `127.0.0.1` / `169.254.169.254`
/ `10.0.0.5` / `168.63.129.16` when the read connects:
- register: refused `400 VALIDATION_FAILED` on `issuer`, the answer
never names the address, nothing connects (the ordinary transport
records nothing, the loopback receiver gets nothing), the guard logs one
refusal with its class (4 cases);
- register with the sign-out switch on: the same, on its first read (4
cases);
- update-provider turning the switch on: refused on `signOutOfIdp`, row
unchanged (1 case);
- sign-in callback: the token read is refused, the callback redirects
with `invalid_provider`, nothing connects (4 cases);
- CONTROL: a host whose address is public at connect registers, its
document read over the guard's own connection (127.0.0.1 standing in for
the public host, as `guarded-fetch.test.ts` does);
- CONTROL: an origin `OS_TRUSTED_ORIGINS` lists is read as before (both
reads, nothing judged);
- CONTROL: a registration whose datastore and rate-limit counter store
are reached over the global fetch, both on internal addresses the guard
would refuse: their requests are sent through the ordinary transport
before the guard's first judgement (the counter store, ahead of the
global before-hook), between, and after its discovery read (the provider
row's insert); the guard is never asked for either host;
- CONTROL: the same for a sign-in by email and its callback: the
provider lookup's datastore read and the counter store are sent as they
were, while the callback's token read is the one read judged, and it is
refused;
- the scope: CONTROL: a read at an origin no provider configuration
names is sent as it was, before and after naming, while a named one is
judged unless configured-trusted; a provider row the observed adapter
answers names its origins (another model's row names none; the view is
idempotent and writes nothing onto the adapter); outside a scope the
global fetch is the fetch it was, keeping its own properties; a second
copy of the module (registry reset) shares the wrapper and the scope:
the global fetch is wrapped once across 3 alternating rounds.
- a context whose adapter is not the observed view (a later plugin
rebuilt it as a plain object): `/sso/register`, `/sign-in/sso` and
`/sso/callback/:providerId` are each answered `503 SERVICE_UNAVAILABLE`
before the endpoint runs: no rate-limit consume, no datastore read, no
judgement, no connection, the stored row unchanged (3 cases); CONTROL: a
later view that hands its reads on through the observed adapter keeps
it: the sign-in runs and its callback's token read is judged and refused
(1 case);
- `sso-discovery-companion-origins.test.ts`: the early-check unit cases
take the shared judgement; new case: an address literal the vendor
admits (`168.63.129.16`) is refused by the platform's classes. Its
relying party hands the stubbed-fetch stand-in.
- `guarded-fetch.test.ts` (core): a manual redirect mode answers the
redirect and follows nothing.
<details><summary>Before (base wiring) / after / ablation</summary>
```text
BEFORE plugin-auth wiring at merge base 92ab056 (after-hook, before-hook, routing, AuthManager, plugin list, early check), new module + test in place
sso-discovery-connect-time.test.ts: Tests 16 failed | 5 passed (21)
failures: registrations answered 200 and stored the provider; the ordinary transport recorded the internal
address (register, register + switch, update, callback); the public CONTROL and the registration datastore CONTROL
answered 502 (no guarded transport); the sign-in datastore CONTROL saw no judgement of the token read
passing: the configured-trust CONTROL and the four scope cases; restored: 6 blobs == HEAD, git diff HEAD empty
AFTER HEAD 608a01f: Tests 25 passed (25)
ABLATION 1 (HEAD 8b4ca8c, 21 cases) wrapper hands every read on (installFetchWrapper; anchor 1 -> 0, blob 0e5c2dd -> cc78b532de98)
Tests 19 failed | 2 passed (21); restored: blob == HEAD (0e5c2dd), git diff HEAD empty
ABLATION 2 (HEAD 8b4ca8c, 21 cases) the scope judges every read, no origin filter (judges(); anchor 1 -> 0, blob 0e5c2dd -> 48bdf224a512)
Tests 4 failed | 17 passed (21): both datastore CONTROLs, the scope CONTROL, the adapter-observation case;
restored: blob == HEAD, git diff HEAD empty
ABLATION 3 HEAD 608a01f: runSsoDiscoveryEgress hands an unobserved context on (auth-manager.ts; anchor 1 -> 0; blob 5d28c23 -> 4796447f4aab)
Tests 3 failed | 1 passed | 21 skipped (25): the three doors answered 409 / 200 / 302 where 503 is pinned;
the CONTROL held; restored: blob == HEAD (5d28c23), git diff HEAD empty
```
</details>
<details><summary>Package suites and typecheck</summary>
```text
pnpm --filter @objectstack/plugin-auth exec vitest run VERDICT command-exit 0 Test Files 289 passed (289) · Tests 6110 passed | 10 skipped (6120) HEAD 608a01f
pnpm --filter @objectstack/plugin-auth typecheck VERDICT command-exit 0 (tsc, examples, check:test-typecheck OK, debt held)
pnpm --filter @objectstack/core test && typecheck VERDICT command-exit 0 Test Files 154 passed (154) · Tests 3671 passed (3671) HEAD 8b4ca8c; packages/core unchanged since
```
</details>
<details><summary>Derived gate families (local, HEAD
608a01f)</summary>
```text
node scripts/pm/dispatch-gates.mjs --commands 109 commands derived from the change set (14 paths vs merge base 92ab056)
109 exit 0 (3 of them re-run on the same head after a first-pass exit 3: check:type-check-debt under the verify lock,
then spec check:skill-examples and check:dual-build-cjs-loads once the re-measure build produced the dists they read)
dispatch-gates --ran (with exit codes): 109 derived, 109 run, 0 NOT-MEASURED, 0 UNRUN
node scripts/check-platform-checklist.mjs --changed-from origin/main exit 0 (1 anchored file, 18 anchors judged, 0 lost)
```
</details>
<details><summary>Full dogfood battery (HEAD 608a01f)</summary>
```text
pnpm --filter '@objectstack/dogfood^...' build VERDICT command-exit 0 (7m26s)
pnpm --filter @objectstack/dogfood test VERDICT command-exit 0 · held the lock 38m11s
Test Files 482 passed | 3 skipped (485) · Tests 4094 passed | 68 skipped (4162)
```
</details>
## Acceptance notes
- The sign-in's discovery (`/sign-in/sso` for a provider stored without
endpoints) reads only origins the deployment trusts: the vendor
validates that discovery URL and every endpoint it names against the
configured trusted origins at sign-in. Those reads stay exempt, as the
rule says; the scope still sits under them.
- The registration's early check stays open on a lookup error (a name
that does not resolve there is left to the read); the read's connection
is where an unresolved name is refused, and nothing is sent. The
register answer for that case is the vendor's `502
discovery_unexpected_error`.
- The guard connects with its own transport, as it does for webhook
deliveries and flow `http` requests: a deployment that sends outbound
HTTP through a global dispatcher or an outbound proxy no longer has
these reads go through it. An identity provider reachable only that way
is listed in `OS_TRUSTED_ORIGINS`, whose reads are sent through the
global fetch as before.
- The wrapper over the global fetch is installed on the first SSO
request, once per process (a process-wide mark every copy of the module
reads), and again over a fetch someone set after it; a runtime whose
global fetch cannot be replaced refuses the read rather than sending it
unjudged.
- Every adapter read of the auth context passes through the observed
view (one extra await and a model check per findOne / findMany); it
names origins only for `ssoProvider` rows inside an SSO scope. The
before-hook no longer reads the provider table: the rows that matter are
the ones the vendor reads anyway.
## Verification
Two minutes: `pnpm --filter @objectstack/plugin-auth exec vitest run
--maxWorkers=2 src/sso-discovery-connect-time.test.ts` (after building
`@objectstack/plugin-auth`'s dependencies) prints `Tests 25 passed
(25)`.
Session: https://claude.ai/code/session_016WV4czt69hUUaG5XcgFNYL
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- signed: security#2 · os-elon-musk ·
session_016WV4czt69hUUaG5XcgFNYL -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent aaa11a2 commit ecd4b2b
14 files changed
Lines changed: 1735 additions & 102 deletions
File tree
- .changeset
- content/docs/permissions
- packages
- core/src/egress
- 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 | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
194 | 211 | | |
195 | 212 | | |
196 | 213 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
187 | 199 | | |
188 | 200 | | |
189 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
| 111 | + | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
117 | 123 | | |
118 | 124 | | |
119 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
534 | 535 | | |
535 | 536 | | |
536 | 537 | | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
537 | 544 | | |
538 | 545 | | |
539 | 546 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| 97 | + | |
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| |||
241 | 242 | | |
242 | 243 | | |
243 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
244 | 259 | | |
245 | 260 | | |
246 | 261 | | |
| |||
633 | 648 | | |
634 | 649 | | |
635 | 650 | | |
636 | | - | |
637 | | - | |
638 | | - | |
639 | | - | |
640 | | - | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
641 | 656 | | |
642 | 657 | | |
643 | 658 | | |
644 | | - | |
| 659 | + | |
| 660 | + | |
645 | 661 | | |
646 | 662 | | |
647 | 663 | | |
648 | 664 | | |
| 665 | + | |
649 | 666 | | |
650 | 667 | | |
651 | 668 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
786 | 786 | | |
787 | 787 | | |
788 | 788 | | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
789 | 794 | | |
790 | 795 | | |
791 | 796 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
| |||
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
| 95 | + | |
94 | 96 | | |
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
98 | | - | |
| 100 | + | |
99 | 101 | | |
100 | 102 | | |
101 | 103 | | |
| |||
214 | 216 | | |
215 | 217 | | |
216 | 218 | | |
217 | | - | |
| 219 | + | |
218 | 220 | | |
219 | 221 | | |
220 | 222 | | |
221 | 223 | | |
222 | 224 | | |
223 | 225 | | |
224 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
225 | 241 | | |
226 | 242 | | |
227 | 243 | | |
| |||
0 commit comments