Repository navigation
Commit 4aceae5
fix(plugin-auth): an unconfigured auth origin ends on the port the server bound, not the port it was asked for (#32194)
Fixes #31948
Clause-②: no
Metadata-protocol: none
## 维护者速读
-
改了什么:没有配置认证地址时,认证模块改在服务器真正开始监听之后(`kernel:listening`)再读取端口,并把认证地址改成服务器实际监听的端口。如果启动期间认证实例已经按旧地址建好,就丢弃它,下次使用时按正确地址重建。
- 为什么改:用 `objectstack dev -p 0`,或开发服务器因端口被占自动换端口时,认证地址停在
`http://localhost:0` 或
`http://localhost:3000`,邀请链接、登录和重置密码邮件都指向一个没人监听的端口。
- 风险与回滚:只影响「没有配置认证地址」的开发场景。已配置的地址(包括
`http://localhost`)照原样保留,不受影响。回滚即还原这一个提交。
- 先看:`packages/plugins/plugin-auth/src/auth-plugin.ts` 中
`kernel:listening` 那一段,风险点是用 `applyConfigPatch` 替换
`setRuntimeBaseUrl`,它会丢弃已建好的认证实例;其次看 `auth-plugin.test.ts` 末尾新增的真实启动测试。
- 两分钟验证:`pnpm --filter @objectstack/plugin-auth exec vitest run
src/auth-plugin.test.ts -t 'transport bound'`,预期 4 个用例全部通过;把修复还原后,其中 3
个会失败,失败信息是 `localhost:0` 或 `localhost:3000` 与实际端口不符。
- 需要你做的:无,席位落地。
## What changes
With no auth base URL configured, `AuthPlugin` corrected the port from
its `kernel:ready` hook by calling `httpServer.getPort()`. The transport
opens its socket later, on `kernel:listening`: `HonoServerPlugin`
registers that handler in its own `start()`, and the kernel fires
`kernel:listening` only after every `kernel:ready` and
`kernel:bootstrapped` handler (`packages/core/src/kernel.ts`, phases 3
to 4; the contract text is in
`packages/spec/src/contracts/plugin-lifecycle-events.ts`). So on
`kernel:ready` the adapter answers the REQUESTED port (`listeningPort ||
port`): `0` for `--port 0`, and the busy port for a dev server about to
shift off it. `if (actualPort)` then skipped the correction for `0`, and
the origin stayed `http://localhost:0` or `http://localhost:3000`.
The fix stays inside the port-correction region of `auth-plugin.ts`:
1. The existing `kernel:ready` handler now registers the correction as a
`kernel:listening` handler. A handler registered during `kernel:ready`
is appended after every `start()`-registered `kernel:listening` handler,
the transport's `listen` included, so it reads the bound port whichever
plugin was used first. No producer change and no new event was needed:
the ordering already exists in the kernel's phases.
2. The correction goes through `authManager.applyConfigPatch({ baseUrl
})` instead of `setRuntimeBaseUrl`. Measured: on every real boot,
something in `kernel:ready` has already built the better-auth instance
by `kernel:listening`. `setRuntimeBaseUrl` then logs `called after the
auth instance was already created — ignoring` and changes nothing.
`applyConfigPatch` discards the built or in-flight instance (its
documented contract), so the next caller rebuilds it on the bound
origin.
3. The `baseUrlDefaulted` doc comment now says `kernel:listening` where
it said `kernel:ready`. Its ruling text is otherwise unchanged: a
configured base URL, `http://localhost` included, is kept as written (PR
#31946).
The configured-versus-defaulted predicate (`baseUrlConfigured`) and the
localhost-only guard are unchanged.
## Tests
- New top-level `describe` in `auth-plugin.test.ts` (the component's
harness file), which boots a real `ObjectKernel` with in-memory SQLite,
`ObjectQLPlugin`, the real `HonoServerPlugin({ port: 0 })` (aliased to
source) and the real `AuthPlugin`:
- no base URL: the origin ends on the bound port;
- the `os dev -p 0` shape (`http://localhost:0`, `baseUrlDefaulted:
true`) with auth used BEFORE the server: the bound port;
- a better-auth instance built from a `kernel:ready` hook (the dev admin
seed's position): `instance.options.baseURL` is the bound origin;
- CONTROL: a configured `http://localhost` is kept as written, both the
console URL and the instance's `baseURL`.
- The existing mock-server cases now drive `kernel:listening` after
`kernel:ready` and spy on `applyConfigPatch`. Without that, their "not
called" controls would pass vacuously.
- RED on the unfixed source: 3 of 4 new cases fail (`expected
'http://localhost:3000/_console/login' to be
'http://localhost:44627/_console/login'`, `'http://localhost:0/…'`, and
`'http://localhost:0'` for the instance); the control passes.
## Acceptance notes
- The same timing defect also covered the other case named in the doc
comment, a dev server that shifted off a busy port: `getPort()` answered
the busy port on `kernel:ready`, too. This change covers that case on
the same path. No separate pin was added, because occupying a fixed port
in a test is flaky.
- `AuthManager.setRuntimeBaseUrl`'s JSDoc still says it is "typically
called from a `kernel:ready` hook where the actual server port is
known". Its one caller is the correction this change moves, and nothing
else in the workspace calls it now. `auth-manager.ts` is outside this
card's file surface, so it is left for its owner.
- The CLI's own trusted-origin entry for the base URL is computed from
the requested port (`resolveAuthBaseUrl(port, …)` in `serve.ts`). In
dev, `http://localhost:*` is trusted alongside it, so nothing is
refused, and it is not touched here.
- Not driven through the CLI door (`objectstack dev --fresh -p 0`): that
needs the whole CLI build closure. The pin boots the same kernel, the
same transport plugin and the same auth plugin that `os serve` composes.
## Verification
All local readings are on head `56d75889` (after one `git merge
origin/main`; the merge touched no `packages/` path).
<details><summary>Ablation (fix reverted, pin red, restored)</summary>
- Leg 1: `auth-plugin.ts` restored to base `0f7389cc` (on disk:
`kernel:listening` hook count 0, `setRuntimeBaseUrl(actualUrl)` count
1). Result: `Tests 3 failed | 1 passed`. Restore: `git checkout HEAD
--`, then hash `a1d5bb3e` equal to the HEAD blob, `git diff HEAD` empty.
- Leg 2: `node scripts/ablation-replace.mjs` replaced
`applyConfigPatch({ baseUrl: actualUrl })` with
`setRuntimeBaseUrl(actualUrl)` (anchor 1 to 0, blob `a1d5bb3e` to
`26c06211`). Result: `Tests 3 failed | 1 passed`, with the
`setRuntimeBaseUrl() called after the auth instance was already created
— ignoring` warning on all three legs. The tool then restored the file
(blob equal to HEAD).
- Neither leg needed a rebuild: the subject is imported from source
(relative), and `@objectstack/plugin-hono-server` is aliased to `src/`.
</details>
<details><summary>Package suite and typecheck</summary>
- `pnpm --filter @objectstack/plugin-auth test`: `Test Files 255 passed
(255)`, `Tests 4591 passed | 10 skipped (4601)`.
- `pnpm --filter @objectstack/plugin-auth typecheck` (after `build`):
exit 0, `check:test-typecheck: OK — 10 file(s) / 72 error(s) / 23 pinned
signature(s) held`.
- File weight: `auth-plugin.test.ts` import time is 16.0–17.7 s with the
new imports, against 16.5 s for the base file. The new cases add about
1.6 s of test time.
</details>
<details><summary>Derived gates</summary>
- `node scripts/pm/dispatch-gates.mjs --commands`: 75 commands.
Reconciled with `--ran`: `75 derived, 74 run, 1 NOT-MEASURED, 0 UNRUN`.
- NOT MEASURED: `pnpm check:dual-build-cjs-loads`, exit 3 `PREREQUISITE
NOT MET` (it needs every package's `dist/`). CI runs it.
- `node scripts/check-plugin-teardown-shape.mjs --self-test` first
exited 3 because the shallow checkout could not reach its pinned fixture
commit. After fetching that one commit it passed (`48 cases pass`).
- All other derived commands exited 0.
- `pnpm lint` (repo-wide, `eslint . --no-inline-config`): finished with
no diagnostics. Its exit code was not captured by the background
wrapper, so the same eslint on the two changed files was run again with
the code captured: exit 0, no output.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_012dn2Ae4fomvXPZ2AioCpY4)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>1 parent 5430b4a commit 4aceae5
3 files changed
Lines changed: 179 additions & 26 deletions
File tree
- .changeset
- 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
410 | 413 | | |
411 | 414 | | |
412 | 415 | | |
413 | | - | |
| 416 | + | |
414 | 417 | | |
415 | 418 | | |
416 | 419 | | |
| 420 | + | |
417 | 421 | | |
418 | | - | |
| 422 | + | |
419 | 423 | | |
420 | 424 | | |
421 | 425 | | |
| |||
445 | 449 | | |
446 | 450 | | |
447 | 451 | | |
448 | | - | |
| 452 | + | |
449 | 453 | | |
450 | 454 | | |
451 | 455 | | |
| 456 | + | |
452 | 457 | | |
453 | | - | |
| 458 | + | |
454 | 459 | | |
455 | 460 | | |
456 | 461 | | |
| |||
477 | 482 | | |
478 | 483 | | |
479 | 484 | | |
480 | | - | |
| 485 | + | |
481 | 486 | | |
482 | 487 | | |
483 | 488 | | |
| 489 | + | |
484 | 490 | | |
485 | | - | |
| 491 | + | |
486 | 492 | | |
487 | 493 | | |
488 | 494 | | |
| |||
2231 | 2237 | | |
2232 | 2238 | | |
2233 | 2239 | | |
2234 | | - | |
| 2240 | + | |
2235 | 2241 | | |
2236 | 2242 | | |
2237 | 2243 | | |
2238 | 2244 | | |
2239 | 2245 | | |
2240 | 2246 | | |
2241 | | - | |
| 2247 | + | |
2242 | 2248 | | |
2243 | 2249 | | |
2244 | 2250 | | |
| |||
2268 | 2274 | | |
2269 | 2275 | | |
2270 | 2276 | | |
2271 | | - | |
| 2277 | + | |
2272 | 2278 | | |
2273 | 2279 | | |
2274 | | - | |
| 2280 | + | |
| 2281 | + | |
| 2282 | + | |
2275 | 2283 | | |
2276 | 2284 | | |
2277 | 2285 | | |
2278 | | - | |
| 2286 | + | |
2279 | 2287 | | |
2280 | | - | |
| 2288 | + | |
2281 | 2289 | | |
2282 | 2290 | | |
2283 | 2291 | | |
2284 | 2292 | | |
2285 | 2293 | | |
2286 | 2294 | | |
2287 | | - | |
| 2295 | + | |
2288 | 2296 | | |
2289 | | - | |
| 2297 | + | |
2290 | 2298 | | |
2291 | 2299 | | |
2292 | 2300 | | |
2293 | 2301 | | |
2294 | | - | |
| 2302 | + | |
2295 | 2303 | | |
2296 | 2304 | | |
2297 | 2305 | | |
2298 | 2306 | | |
2299 | | - | |
| 2307 | + | |
2300 | 2308 | | |
2301 | 2309 | | |
2302 | 2310 | | |
2303 | 2311 | | |
2304 | | - | |
| 2312 | + | |
2305 | 2313 | | |
2306 | | - | |
| 2314 | + | |
2307 | 2315 | | |
2308 | 2316 | | |
2309 | 2317 | | |
| 2318 | + | |
| 2319 | + | |
| 2320 | + | |
| 2321 | + | |
| 2322 | + | |
| 2323 | + | |
| 2324 | + | |
| 2325 | + | |
| 2326 | + | |
| 2327 | + | |
| 2328 | + | |
| 2329 | + | |
| 2330 | + | |
| 2331 | + | |
| 2332 | + | |
| 2333 | + | |
| 2334 | + | |
| 2335 | + | |
| 2336 | + | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
| 2341 | + | |
| 2342 | + | |
| 2343 | + | |
| 2344 | + | |
| 2345 | + | |
| 2346 | + | |
| 2347 | + | |
| 2348 | + | |
| 2349 | + | |
| 2350 | + | |
| 2351 | + | |
| 2352 | + | |
| 2353 | + | |
| 2354 | + | |
| 2355 | + | |
| 2356 | + | |
| 2357 | + | |
| 2358 | + | |
| 2359 | + | |
| 2360 | + | |
| 2361 | + | |
| 2362 | + | |
| 2363 | + | |
| 2364 | + | |
| 2365 | + | |
| 2366 | + | |
| 2367 | + | |
| 2368 | + | |
| 2369 | + | |
| 2370 | + | |
| 2371 | + | |
| 2372 | + | |
| 2373 | + | |
| 2374 | + | |
| 2375 | + | |
| 2376 | + | |
| 2377 | + | |
| 2378 | + | |
| 2379 | + | |
| 2380 | + | |
| 2381 | + | |
| 2382 | + | |
| 2383 | + | |
| 2384 | + | |
| 2385 | + | |
| 2386 | + | |
| 2387 | + | |
| 2388 | + | |
| 2389 | + | |
| 2390 | + | |
| 2391 | + | |
| 2392 | + | |
| 2393 | + | |
| 2394 | + | |
| 2395 | + | |
| 2396 | + | |
| 2397 | + | |
| 2398 | + | |
| 2399 | + | |
| 2400 | + | |
| 2401 | + | |
| 2402 | + | |
| 2403 | + | |
| 2404 | + | |
| 2405 | + | |
| 2406 | + | |
| 2407 | + | |
| 2408 | + | |
| 2409 | + | |
| 2410 | + | |
| 2411 | + | |
| 2412 | + | |
| 2413 | + | |
| 2414 | + | |
| 2415 | + | |
| 2416 | + | |
| 2417 | + | |
| 2418 | + | |
| 2419 | + | |
| 2420 | + | |
| 2421 | + | |
| 2422 | + | |
| 2423 | + | |
| 2424 | + | |
| 2425 | + | |
| 2426 | + | |
| 2427 | + | |
| 2428 | + | |
| 2429 | + | |
| 2430 | + | |
| 2431 | + | |
| 2432 | + | |
| 2433 | + | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
412 | | - | |
| 412 | + | |
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| |||
1568 | 1568 | | |
1569 | 1569 | | |
1570 | 1570 | | |
1571 | | - | |
1572 | | - | |
1573 | | - | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
| 1583 | + | |
1574 | 1584 | | |
1575 | 1585 | | |
1576 | 1586 | | |
| |||
1581 | 1591 | | |
1582 | 1592 | | |
1583 | 1593 | | |
1584 | | - | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
1585 | 1600 | | |
1586 | 1601 | | |
1587 | 1602 | | |
1588 | 1603 | | |
1589 | | - | |
| 1604 | + | |
1590 | 1605 | | |
1591 | 1606 | | |
1592 | 1607 | | |
| |||
0 commit comments