|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 4 | +import { request as httpRequest } from 'node:http'; |
| 5 | +import { LiteKernel, Plugin, PluginContext } from '@objectstack/core'; |
| 6 | +import { HonoServerPlugin } from '@objectstack/plugin-hono-server'; |
| 7 | +import type { IHttpServer } from '@objectstack/spec/contracts'; |
| 8 | + |
| 9 | +import { createDispatcherPlugin } from './dispatcher-plugin.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Every verb on `/api/v1/actions/...` reaches the `actions` domain on the |
| 13 | + * standalone server, so its anonymous baseline answers first (#24228) — over |
| 14 | + * a real Hono socket. |
| 15 | + * |
| 16 | + * `handleActionsRequest` decides an anonymous caller as its FIRST statement, |
| 17 | + * ahead of its `405` (ADR-0138 D2: "Everything else answers 401 |
| 18 | + * (UNAUTHENTICATED). The decision is taken once per domain, as the domain's |
| 19 | + * first statement"). The standalone dispatcher mounts the three `/actions` |
| 20 | + * paths for `POST` only, so every other verb matched no route and the |
| 21 | + * transport answered it itself: `405` + `Allow: POST`, to an anonymous caller |
| 22 | + * too. A `createHonoApp` host answers the same request `401`, because its |
| 23 | + * catch-all hands the domain every verb. |
| 24 | + * |
| 25 | + * The dispatcher now declares these paths as ones whose domain decides every |
| 26 | + * verb, and its fallback seam hands the domain each request the router could |
| 27 | + * not route there. Deliberately NOT refusal-only `GET`/`PUT`/`PATCH`/`DELETE` |
| 28 | + * mounts (the `/keys` shape): the transport builds `Allow` from the verbs |
| 29 | + * REGISTERED on a path, so each such mount would have been listed as supported |
| 30 | + * in the `405` the transport still answers for the verbs left over (`OPTIONS` |
| 31 | + * with CORS off, `TRACE`, …). The signed-in rows therefore pin `Allow` too. |
| 32 | + * |
| 33 | + * Which DOOR answered is read off the body, as in |
| 34 | + * `keys-every-verb.hono.integration.test.ts`: the dispatcher's envelope fills |
| 35 | + * `error.httpStatus` and carries no `error.details`; the transport's fills |
| 36 | + * `error.details.{method,path,allowed}` and no `httpStatus`. The signed-in |
| 37 | + * rows answered `405` + `Allow: POST` before the fix as well, so the door is |
| 38 | + * what tells them apart. |
| 39 | + * |
| 40 | + * Both prefixes: `enableProjectScoping` mounts the three paths under |
| 41 | + * `/api/v1` and under `/api/v1/environments/:environmentId`, through the same |
| 42 | + * `registerActionRoutes`, and the scoped request reaches `dispatch()` with its |
| 43 | + * environment read off the path rather than off route params (an unrouted |
| 44 | + * request has none) — so the scoped rows are not a copy of the unscoped ones. |
| 45 | + * |
| 46 | + * Raw `node:http`, not `fetch`: `fetch` refuses `TRACE`, and `TRACE` is the |
| 47 | + * verb the transport answers even with CORS on (CORS answers `OPTIONS`). |
| 48 | + * `LiteKernel`, no `objectql`: every answer under test comes before the domain |
| 49 | + * touches storage, so the signed-in `POST` (a real invocation) is not driven. |
| 50 | + */ |
| 51 | + |
| 52 | +const SESSION_HEADER = 'x-test-session'; |
| 53 | +const PREFIXES = ['/api/v1', '/api/v1/environments/env_actions_verb'] as const; |
| 54 | +const SHAPES = ['/actions//close_all', '/actions/task/close', '/actions/task/close/rec_1'] as const; |
| 55 | +/** Verbs that carry a JSON body on the wire; the rest are sent bodiless. */ |
| 56 | +const BODY_VERBS = new Set(['PUT', 'PATCH']); |
| 57 | +/** Every verb but `POST` and the two whose answer differs in shape (below). */ |
| 58 | +const REFUSED_VERBS = ['GET', 'PUT', 'PATCH', 'DELETE', 'TRACE'] as const; |
| 59 | + |
| 60 | +function fakeAuthPlugin(): Plugin { |
| 61 | + return { |
| 62 | + name: 'com.objectstack.test.fake-auth-actions-every-verb', |
| 63 | + version: '1.0.0', |
| 64 | + init: async (ctx: PluginContext) => { |
| 65 | + ctx.registerService('auth', { |
| 66 | + api: { |
| 67 | + getSession: async ({ headers }: any) => |
| 68 | + (headers?.get?.(SESSION_HEADER) ? { user: { id: 'u_actions_verb' } } : undefined), |
| 69 | + }, |
| 70 | + }); |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +interface Answer { |
| 76 | + status: number; |
| 77 | + allow: string | null; |
| 78 | + /** Parsed JSON body; `undefined` for a bodiless answer (`HEAD`). */ |
| 79 | + body: Record<string, any> | undefined; |
| 80 | +} |
| 81 | + |
| 82 | +describe('every /api/v1/actions verb reaches the actions domain (integration, #24228)', () => { |
| 83 | + let kernel: LiteKernel; |
| 84 | + let port: number; |
| 85 | + |
| 86 | + beforeAll(async () => { |
| 87 | + kernel = new LiteKernel(); |
| 88 | + kernel.use(fakeAuthPlugin()); |
| 89 | + // port 0 → an OS-assigned free port, read back through getPort(). |
| 90 | + // CORS off: with it on, CORS answers every `OPTIONS` before routing. |
| 91 | + kernel.use(new HonoServerPlugin({ port: 0, cors: false })); |
| 92 | + kernel.use(createDispatcherPlugin({ |
| 93 | + prefix: '/api/v1', |
| 94 | + securityHeaders: false, |
| 95 | + scoping: { enableProjectScoping: true, projectResolution: 'auto' }, |
| 96 | + })); |
| 97 | + await kernel.bootstrap(); |
| 98 | + port = kernel.getService<IHttpServer>('http.server').getPort!(); |
| 99 | + }, 30_000); |
| 100 | + |
| 101 | + afterAll(async () => { |
| 102 | + if (kernel) { |
| 103 | + await Promise.race([ |
| 104 | + kernel.shutdown(), |
| 105 | + new Promise<void>((resolve) => setTimeout(resolve, 10_000)), |
| 106 | + ]); |
| 107 | + } |
| 108 | + }, 30_000); |
| 109 | + |
| 110 | + function call(method: string, path: string, signedIn: boolean): Promise<Answer> { |
| 111 | + return new Promise((resolve, reject) => { |
| 112 | + const payload = BODY_VERBS.has(method) ? JSON.stringify({ params: {} }) : undefined; |
| 113 | + const req = httpRequest({ |
| 114 | + host: '127.0.0.1', |
| 115 | + port, |
| 116 | + method, |
| 117 | + path, |
| 118 | + headers: { |
| 119 | + ...(payload ? { 'content-type': 'application/json' } : {}), |
| 120 | + ...(signedIn ? { [SESSION_HEADER]: '1' } : {}), |
| 121 | + }, |
| 122 | + }, (res) => { |
| 123 | + let text = ''; |
| 124 | + res.setEncoding('utf8'); |
| 125 | + res.on('data', (chunk) => { text += chunk; }); |
| 126 | + res.on('end', () => { |
| 127 | + const allow = res.headers.allow; |
| 128 | + resolve({ |
| 129 | + status: res.statusCode ?? 0, |
| 130 | + allow: typeof allow === 'string' ? allow : null, |
| 131 | + body: text.length > 0 ? JSON.parse(text) as Record<string, any> : undefined, |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + req.on('error', reject); |
| 136 | + req.end(payload); |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + for (const prefix of PREFIXES) { |
| 141 | + for (const shape of SHAPES) { |
| 142 | + const path = `${prefix}${shape}`; |
| 143 | + |
| 144 | + // ── the defect: an anonymous non-POST verb is refused 401, by the domain ── |
| 145 | + for (const method of REFUSED_VERBS) { |
| 146 | + it(`anonymous ${method} ${path} → 401 UNAUTHENTICATED, no Allow, in the dispatcher's envelope`, async () => { |
| 147 | + const { status, allow, body } = await call(method, path, false); |
| 148 | + |
| 149 | + expect(status).toBe(401); |
| 150 | + expect(allow).toBeNull(); |
| 151 | + expect(body?.success).toBe(false); |
| 152 | + expect(body?.error?.code).toBe('UNAUTHENTICATED'); |
| 153 | + expect(body?.error?.httpStatus).toBe(401); |
| 154 | + expect(body?.error).not.toHaveProperty('details'); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + // ── signed in: the DOMAIN's 405, naming only the verb it serves ── |
| 159 | + for (const method of REFUSED_VERBS) { |
| 160 | + it(`signed-in ${method} ${path} → the actions domain's 405 with \`Allow: POST\`, not the transport's`, async () => { |
| 161 | + const { status, allow, body } = await call(method, path, true); |
| 162 | + |
| 163 | + expect(status).toBe(405); |
| 164 | + expect(allow).toBe('POST'); |
| 165 | + expect(body?.error?.code).toBe('METHOD_NOT_ALLOWED'); |
| 166 | + // The door: the domain fills `httpStatus`; the transport fills `details`. |
| 167 | + expect(body?.error?.httpStatus).toBe(405); |
| 168 | + expect(body?.error).not.toHaveProperty('details'); |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + // HEAD has no body to read the door from; status and header say it. |
| 173 | + it(`HEAD ${path} → 401 with no Allow anonymously, 405 with \`Allow: POST\` signed in`, async () => { |
| 174 | + const anonymous = await call('HEAD', path, false); |
| 175 | + expect(anonymous.status).toBe(401); |
| 176 | + expect(anonymous.allow).toBeNull(); |
| 177 | + |
| 178 | + const signedIn = await call('HEAD', path, true); |
| 179 | + expect(signedIn.status).toBe(405); |
| 180 | + expect(signedIn.allow).toBe('POST'); |
| 181 | + }); |
| 182 | + |
| 183 | + // `shouldDenyAnonymous` lets every `OPTIONS` through (a CORS preflight |
| 184 | + // carries no credentials), so the domain's 405 answers it — on a |
| 185 | + // `createHonoApp` host with CORS off as well. Pinned as the boundary: |
| 186 | + // the domain answers it, and `Allow` names `POST` alone. |
| 187 | + it(`anonymous OPTIONS ${path} (CORS off) → the domain's 405 with \`Allow: POST\` — the preflight exemption`, async () => { |
| 188 | + const { status, allow, body } = await call('OPTIONS', path, false); |
| 189 | + |
| 190 | + expect(status).toBe(405); |
| 191 | + expect(allow).toBe('POST'); |
| 192 | + expect(body?.error?.httpStatus).toBe(405); |
| 193 | + expect(body?.error).not.toHaveProperty('details'); |
| 194 | + }); |
| 195 | + |
| 196 | + // ── control: the verb the route always served is answered as before ── |
| 197 | + it(`anonymous POST ${path} → 401 UNAUTHENTICATED (unchanged)`, async () => { |
| 198 | + const { status, body } = await call('POST', path, false); |
| 199 | + |
| 200 | + expect(status).toBe(401); |
| 201 | + expect(body?.error?.code).toBe('UNAUTHENTICATED'); |
| 202 | + expect(body?.error?.httpStatus).toBe(401); |
| 203 | + }); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + // ── the claim is narrow: a path no `/actions` route serves stays the transport's 404 ── |
| 208 | + it('a four-segment /actions path no route serves → the transport\'s 404, not the domain', async () => { |
| 209 | + const { status, body } = await call('GET', '/api/v1/actions/task/close/rec_1/extra', false); |
| 210 | + |
| 211 | + expect(status).toBe(404); |
| 212 | + expect(body?.error?.code).toBe('ENDPOINT_NOT_FOUND'); |
| 213 | + }); |
| 214 | +}); |
0 commit comments