|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #15417 — WHICH 404 the auth catch-all is allowed to yield. |
| 5 | + * |
| 6 | + * `auth-catchall-fallthrough.test.ts` (#4088) pins that the catch-all yields at |
| 7 | + * all. This file pins the other edge: it may only yield a 404 that DISCLAIMS |
| 8 | + * ownership. A 404 from a path better-auth's own router serves is its ANSWER — |
| 9 | + * a switched-off capability — and handing that to the rest of the chain is how |
| 10 | + * it comes back as somebody else's `200 {}`. |
| 11 | + * |
| 12 | + * ── The measurement this file exists for ──────────────────────────────────── |
| 13 | + * |
| 14 | + * #15417 reported `POST /api/v1/auth/admin/<nonexistent>` answering `200 {}` on |
| 15 | + * a cloud composition, with a nonexistent path as the control. Reproduced on a |
| 16 | + * framework-side boot (`@objectstack/verify` + the showcase stack, both with |
| 17 | + * better-auth's admin plugin off and on), that path answers **404** — bodyless, |
| 18 | + * no content-type — so the framework does not produce the reported status on |
| 19 | + * its own. What does produce it is the yield: register ONE broad downstream |
| 20 | + * mount after the catch-all — `app.all('/api/v1/*', c => c.json({}))`, the |
| 21 | + * shape a composition adds — and the same request comes back `200 {}`, because |
| 22 | + * the catch-all handed it on and the wildcard answered. |
| 23 | + * |
| 24 | + * That is a framework-side defect regardless of who mounts the wildcard, |
| 25 | + * because the request handed on need not be an unknown path at all: |
| 26 | + * `delete-user` answers 404 by the ledger's `disabled` disposition, and so does |
| 27 | + * every routed endpoint that 404s on a bad token or an unknown id. Those |
| 28 | + * answers were all up for grabs. Confirmed end-to-end on the framework-side |
| 29 | + * boot with the wildcard installed: `POST /api/v1/auth/delete-user` now |
| 30 | + * answers 404 where the wildcard's `200 {}` used to stand. |
| 31 | + * |
| 32 | + * ⚠️ Ownership is "does better-call ROUTE this", not "is it in `auth.api`". |
| 33 | + * Measured on the stock boot: the nine `/admin/oauth2/*` endpoints are in |
| 34 | + * `auth.api` and every one carries `SERVER_ONLY: true`, so `createRouter` skips |
| 35 | + * them and their 404 is an unrouted one — they stay yieldable, and the pin |
| 36 | + * below says so. |
| 37 | + * |
| 38 | + * ── Why the fixture stubs better-auth, and what it does NOT stub ──────────── |
| 39 | + * |
| 40 | + * Same seam as the #4088 file: `handleRequest` is a path table so the test |
| 41 | + * controls exactly which paths the vendor claims, and the real |
| 42 | + * `registerAuthRoutes` runs on a real Hono app so the assertions are about the |
| 43 | + * shipped handler. The ownership decision is NOT stubbed — `ownsRoute` here |
| 44 | + * runs the real `buildBetterAuthRouteOwnership` over a fake `auth.api`, so the |
| 45 | + * matcher under test is the shipped one. |
| 46 | + * |
| 47 | + * The stub's 404 is `new Response(null, { status: 404 })` — bodyless, no |
| 48 | + * content-type — because that is what better-call 1.4.0 really returns for an |
| 49 | + * unrouted path (`dist/router.mjs`), and what the framework-side boot measured |
| 50 | + * on the wire. The #4088 fixture's JSON 404 is a convenience of that file. |
| 51 | + */ |
| 52 | + |
| 53 | +import { describe, it, expect, vi } from 'vitest'; |
| 54 | +import { Hono } from 'hono'; |
| 55 | +import { AuthPlugin } from './auth-plugin'; |
| 56 | +import { buildBetterAuthRouteOwnership } from './better-auth-route-ownership'; |
| 57 | +import type { PluginContext } from '@objectstack/core'; |
| 58 | + |
| 59 | +const BASE = '/api/v1/auth'; |
| 60 | + |
| 61 | +/** What better-call returns for a path it does not route: bodyless, no content-type. */ |
| 62 | +const unrouted404 = () => new Response(null, { status: 404, statusText: 'Not Found' }); |
| 63 | + |
| 64 | +/** |
| 65 | + * Mount the real route registration on a real Hono app. |
| 66 | + * |
| 67 | + * @param api the fake `auth.api` the REAL ownership matcher reads |
| 68 | + * @param answers path -> Response for the paths better-auth answers |
| 69 | + */ |
| 70 | +async function mountCatchAll( |
| 71 | + api: Record<string, { path: string; options: { method: string | string[] } }>, |
| 72 | + answers: Record<string, () => Response>, |
| 73 | +) { |
| 74 | + const app = new Hono(); |
| 75 | + const ctx: PluginContext = { |
| 76 | + registerService: vi.fn(), |
| 77 | + getService: vi.fn((name: string) => (name === 'manifest' ? { register: vi.fn() } : undefined)), |
| 78 | + getServices: vi.fn(() => new Map()), |
| 79 | + hook: vi.fn(), |
| 80 | + trigger: vi.fn(), |
| 81 | + logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn(), debug: vi.fn() }, |
| 82 | + getKernel: vi.fn(), |
| 83 | + } as any; |
| 84 | + |
| 85 | + const plugin = new AuthPlugin({ secret: 'test-secret-at-least-32-chars-long!!' }); |
| 86 | + await plugin.init(ctx); |
| 87 | + |
| 88 | + const ownership = buildBetterAuthRouteOwnership(api as any); |
| 89 | + const handleRequest = vi.fn(async (req: Request) => { |
| 90 | + const make = answers[new URL(req.url).pathname]; |
| 91 | + return make ? make() : unrouted404(); |
| 92 | + }); |
| 93 | + (plugin as any).authManager = { |
| 94 | + handleRequest, |
| 95 | + // The shipped matcher, over the fake table — only the endpoint-path |
| 96 | + // derivation is inlined here (AuthManager's own is private). |
| 97 | + ownsRoute: async (req: Request) => |
| 98 | + ownership.owns(req.method, new URL(req.url).pathname.slice(BASE.length)), |
| 99 | + }; |
| 100 | + |
| 101 | + (plugin as any).registerAuthRoutes({ getRawApp: () => app, getPort: () => 0 }, ctx); |
| 102 | + return { app, handleRequest }; |
| 103 | +} |
| 104 | + |
| 105 | +/** The shape a composition adds: one wildcard over the whole API prefix. */ |
| 106 | +const addDownstreamWildcard = (app: Hono) => app.all('/api/v1/*', (c) => c.json({})); |
| 107 | + |
| 108 | +describe('#15417: the catch-all yields only a 404 that disclaims ownership', () => { |
| 109 | + it('does NOT yield a 404 from a path better-auth OWNS — even with a wildcard downstream', async () => { |
| 110 | + // `delete-user` is published and answers 404 because `user.deleteUser` is |
| 111 | + // deliberately unconfigured — `auth-route-ledger.ts`'s `disabled` |
| 112 | + // disposition. That 404 is an ANSWER and must reach the caller. |
| 113 | + const { app } = await mountCatchAll( |
| 114 | + { deleteUser: { path: '/delete-user', options: { method: 'POST' } } }, |
| 115 | + { [`${BASE}/delete-user`]: () => new Response(null, { status: 404 }) }, |
| 116 | + ); |
| 117 | + addDownstreamWildcard(app); |
| 118 | + |
| 119 | + const res = await app.request(`http://localhost${BASE}/delete-user`, { method: 'POST' }); |
| 120 | + |
| 121 | + expect(res.status).toBe(404); |
| 122 | + expect(await res.text()).toBe(''); |
| 123 | + }); |
| 124 | + |
| 125 | + it('does not yield an owned 404 on a PARAMETERISED path either', async () => { |
| 126 | + // `/callback/:id` is routed and parameterised; a 404 from it is an answer. |
| 127 | + const { app } = await mountCatchAll( |
| 128 | + { callback: { path: '/callback/:id', options: { method: 'GET' } } }, |
| 129 | + { [`${BASE}/callback/github`]: () => new Response(null, { status: 404 }) }, |
| 130 | + ); |
| 131 | + addDownstreamWildcard(app); |
| 132 | + |
| 133 | + const res = await app.request(`http://localhost${BASE}/callback/github`); |
| 134 | + |
| 135 | + expect(res.status).toBe(404); |
| 136 | + }); |
| 137 | + |
| 138 | + it('DOES yield a SERVER_ONLY endpoint\'s 404 — better-call never routed it', async () => { |
| 139 | + // Measured on the stock boot: all nine `/admin/oauth2/*` endpoints are in |
| 140 | + // `auth.api` carrying `SERVER_ONLY: true`. `createRouter` skips them, so the |
| 141 | + // 404 the wire sees is an unrouted one and yielding it is correct. Were the |
| 142 | + // table to trust `auth.api` wholesale instead of mirroring that skip, this |
| 143 | + // route would stop being yieldable and a composition serving it downstream |
| 144 | + // would break. |
| 145 | + const { app } = await mountCatchAll( |
| 146 | + { |
| 147 | + adminListOAuthResources: { |
| 148 | + path: '/admin/oauth2/resources', |
| 149 | + options: { method: 'GET', metadata: { SERVER_ONLY: true } }, |
| 150 | + }, |
| 151 | + } as any, |
| 152 | + {}, |
| 153 | + ); |
| 154 | + app.get(`${BASE}/admin/oauth2/resources`, (c) => c.json({ from: 'sibling' })); |
| 155 | + |
| 156 | + const res = await app.request(`http://localhost${BASE}/admin/oauth2/resources`); |
| 157 | + |
| 158 | + expect(res.status).toBe(200); |
| 159 | + expect(await res.json()).toEqual({ from: 'sibling' }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('STILL yields a 404 from a path better-auth does not own — #4088 intact', async () => { |
| 163 | + // The route plugin-hono-server mounts from its own kernel:ready hook, in |
| 164 | + // the registration order that used to 404. Nothing may make this red. |
| 165 | + const { app } = await mountCatchAll({ getSession: { path: '/get-session', options: { method: 'GET' } } }, {}); |
| 166 | + app.get(`${BASE}/me/permissions`, (c) => c.json({ authenticated: true, from: 'hono-plugin' })); |
| 167 | + |
| 168 | + const res = await app.request(`http://localhost${BASE}/me/permissions`); |
| 169 | + |
| 170 | + expect(res.status).toBe(200); |
| 171 | + expect(await res.json()).toEqual({ authenticated: true, from: 'hono-plugin' }); |
| 172 | + }); |
| 173 | + |
| 174 | + it('an unknown tail with nothing downstream still answers better-auth\'s 404', async () => { |
| 175 | + // The control the card could not run from outside. Unchanged by #15417: |
| 176 | + // the framework already answered 404 here, and still does. |
| 177 | + const { app } = await mountCatchAll({ getSession: { path: '/get-session', options: { method: 'GET' } } }, {}); |
| 178 | + |
| 179 | + const res = await app.request(`http://localhost${BASE}/admin/definitely-not-a-route-1989`, { method: 'POST' }); |
| 180 | + |
| 181 | + expect(res.status).toBe(404); |
| 182 | + }); |
| 183 | + |
| 184 | + it('a path better-auth owns is not yielded even when the DOWNSTREAM route is specific', async () => { |
| 185 | + // Precedence still favours the namespace owner (the #4088 file pins this |
| 186 | + // for 2xx; here it is pinned for the vendor's own 404 answer). |
| 187 | + const { app } = await mountCatchAll( |
| 188 | + { deleteUser: { path: '/delete-user', options: { method: 'POST' } } }, |
| 189 | + { [`${BASE}/delete-user`]: () => new Response(null, { status: 404 }) }, |
| 190 | + ); |
| 191 | + app.post(`${BASE}/delete-user`, (c) => c.json({ hijacked: true })); |
| 192 | + |
| 193 | + const res = await app.request(`http://localhost${BASE}/delete-user`, { method: 'POST' }); |
| 194 | + |
| 195 | + expect(res.status).toBe(404); |
| 196 | + expect(await res.text()).toBe(''); |
| 197 | + }); |
| 198 | + |
| 199 | + it('ownership is per METHOD: the same path on a verb better-auth does not serve still yields', async () => { |
| 200 | + const { app } = await mountCatchAll( |
| 201 | + { listUsers: { path: '/admin/list-users', options: { method: 'GET' } } }, |
| 202 | + {}, |
| 203 | + ); |
| 204 | + app.post(`${BASE}/admin/list-users`, (c) => c.json({ from: 'sibling' })); |
| 205 | + |
| 206 | + const res = await app.request(`http://localhost${BASE}/admin/list-users`, { method: 'POST' }); |
| 207 | + |
| 208 | + expect(res.status).toBe(200); |
| 209 | + expect(await res.json()).toEqual({ from: 'sibling' }); |
| 210 | + }); |
| 211 | + |
| 212 | + it('still forwards to better-auth exactly once per request', async () => { |
| 213 | + const { app, handleRequest } = await mountCatchAll( |
| 214 | + { deleteUser: { path: '/delete-user', options: { method: 'POST' } } }, |
| 215 | + { [`${BASE}/delete-user`]: () => new Response(null, { status: 404 }) }, |
| 216 | + ); |
| 217 | + addDownstreamWildcard(app); |
| 218 | + handleRequest.mockClear(); |
| 219 | + |
| 220 | + await app.request(`http://localhost${BASE}/delete-user`, { method: 'POST' }); |
| 221 | + |
| 222 | + expect(handleRequest).toHaveBeenCalledTimes(1); |
| 223 | + }); |
| 224 | +}); |
0 commit comments