Repository navigation
Commit 374d9d3
fix(plugin-auth)!: an anonymous get-session is refused with the declared 401 envelope, not answered 200 null (#17881)
Fixes #17238
Ruled B by the director seat (decision batch #117 item 4, comment
5642584682). Maintainer, verbatim: 「17238 B」. The seat had recommended A
and was corrected, verbatim: 「我改推荐的都是从平台长远合理性角度考虑的」, under the charter
rule 「spec 与代码不一致默认改代码,改协议单独立卡非选项」.
`GET /api/v1/auth/get-session` now answers an anonymous caller the
platform's standard ADR-0112 failure envelope at HTTP 401, instead of
`200` + the literal JSON `null`. `SessionResponseSchema` is untouched.
## The defect, and why the code is what moves
`ObjectStackClient.auth.me()` declares `Promise<SessionResponse>`, and
`SessionResponseSchema` requires `data.session` and `data.user` — so no
value of that type means "nobody is signed in", and the most ordinary
call a logged-out caller can make resolved to something *outside* the
method's own declared type.
⇒ Every value `auth.me()` **returns** is now inside `SessionResponse`.
The anonymous case is delivered as a rejection instead: the SDK's
`fetch` wrapper already throws on every non-2xx, so the caller gets an
error carrying `code: 'UNAUTHENTICATED'` and `httpStatus: 401`. **No
runtime edit in `packages/client` was needed** — that fell out of the
measurement rather than being assumed.
## Measured, on the merge base and after
Driven through a real `AuthManager` (better-auth, default plugin set,
in-memory engine) via `handleRequest`:
```
BEFORE AFTER
GET /get-session (no credential) 200 application/json null 401 {"success":false,"error":{"code":"UNAUTHENTICATED","message":"Sign in first"}}
GET /get-session (unknown cookie) 200 application/json null 401 (same)
GET /get-session (signed in) 200 {user,session} 200 {user,session} ← unchanged, asserted by identity
POST /get-session (no credential) 404 (no content-type) '' 404 (unchanged)
```
The `POST` row is why the rule is keyed on *the 200-null answer* and not
on the path alone: a path-only rule would have converted that 404 into a
401 and claimed a route better-auth does not serve.
## Where it lives
`packages/plugins/plugin-auth/src/anonymous-session-refusal.ts`, wired
into `AuthManager.handleRequest` beside the existing `/admin/` envelope
normalization — the one seam every vendor route passes through. 1 parent c744c0a commit 374d9d3
13 files changed
Lines changed: 667 additions & 43 deletions
File tree
- .changeset
- packages
- client/src
- plugins/plugin-auth/src
- scripts
| 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 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | | - | |
34 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
35 | 39 | | |
36 | 40 | | |
37 | 41 | | |
| |||
240 | 244 | | |
241 | 245 | | |
242 | 246 | | |
243 | | - | |
244 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
245 | 262 | | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
253 | 284 | | |
254 | 285 | | |
255 | 286 | | |
| |||
264 | 295 | | |
265 | 296 | | |
266 | 297 | | |
267 | | - | |
268 | | - | |
| 298 | + | |
| 299 | + | |
269 | 300 | | |
270 | 301 | | |
271 | 302 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
877 | 877 | | |
878 | 878 | | |
879 | 879 | | |
880 | | - | |
881 | | - | |
882 | | - | |
883 | | - | |
884 | | - | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
885 | 900 | | |
886 | 901 | | |
887 | 902 | | |
| |||
Lines changed: 8 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
170 | | - | |
171 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
172 | 177 | | |
173 | 178 | | |
174 | 179 | | |
| |||
0 commit comments