|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #2567 — the raw-hono standard `/data` endpoints must honour the same |
| 4 | +// secure-by-default (`requireAuth`) anonymous-deny posture as the REST `/data` |
| 5 | +// routes. Before the gate, these routes delegated straight to ObjectQL and were |
| 6 | +// only *shadowed* when the REST plugin registered the same paths first — so the |
| 7 | +// anonymous posture depended on plugin registration order. These tests drive the |
| 8 | +// real routes on a real Hono app (no REST plugin in the picture) to prove the |
| 9 | +// gate stands on its own. |
| 10 | + |
| 11 | +import { describe, it, expect } from 'vitest'; |
| 12 | +import { HonoServerPlugin } from './hono-plugin'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Boot a plugin and register ONLY the standard CRUD routes on its real Hono |
| 16 | + * app, then hand back the app so tests can drive HTTP requests directly. No |
| 17 | + * listening socket, no CORS/static — we exercise the data routes in isolation. |
| 18 | + */ |
| 19 | +function bootStandardEndpoints(opts: { |
| 20 | + restConfig?: { api?: { requireAuth?: boolean } }; |
| 21 | + services: Record<string, unknown>; |
| 22 | +}) { |
| 23 | + const plugin = new HonoServerPlugin({ port: 0, restConfig: opts.restConfig as any }); |
| 24 | + const ctx: any = { |
| 25 | + logger: { info() {}, debug() {}, warn() {}, error() {} }, |
| 26 | + getKernel: () => ({ getService: (n: string) => opts.services[n] }), |
| 27 | + registerService: () => {}, |
| 28 | + hook: () => {}, |
| 29 | + getService: (n: string) => { |
| 30 | + const s = opts.services[n]; |
| 31 | + if (s === undefined) throw new Error(`no service: ${n}`); |
| 32 | + return s; |
| 33 | + }, |
| 34 | + }; |
| 35 | + (plugin as any).registerDiscoveryAndCrudEndpoints(ctx); |
| 36 | + return (plugin as any).server.getRawApp(); |
| 37 | +} |
| 38 | + |
| 39 | +// ObjectQL stub — every read returns empty, every write echoes an id. Enough |
| 40 | +// for the routes to reach a 200 once the gate has been cleared. |
| 41 | +const objectql = { |
| 42 | + find: async () => [], |
| 43 | + insert: async () => ({ id: 'new-id' }), |
| 44 | +}; |
| 45 | + |
| 46 | +// Auth stub — resolves a session ONLY when the request carries `x-test-user`, |
| 47 | +// so the same boot serves both anonymous and authenticated requests. |
| 48 | +const auth = { |
| 49 | + api: { |
| 50 | + getSession: async ({ headers }: { headers: Headers }) => { |
| 51 | + const uid = headers?.get?.('x-test-user'); |
| 52 | + return uid ? { user: { id: uid }, session: {} } : null; |
| 53 | + }, |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +const REQ = 'http://localhost/api/v1/data/thing'; |
| 58 | + |
| 59 | +describe('raw-hono /data — anonymous-deny gate (#2567)', () => { |
| 60 | + it('secure-by-default (no restConfig): anonymous LIST is 401', async () => { |
| 61 | + const app = bootStandardEndpoints({ services: { objectql, auth } }); |
| 62 | + const res = await app.request(REQ, { method: 'GET' }); |
| 63 | + expect(res.status).toBe(401); |
| 64 | + }); |
| 65 | + |
| 66 | + it('secure-by-default: anonymous GET-by-id is 401', async () => { |
| 67 | + const app = bootStandardEndpoints({ services: { objectql, auth } }); |
| 68 | + const res = await app.request(`${REQ}/abc`, { method: 'GET' }); |
| 69 | + expect(res.status).toBe(401); |
| 70 | + }); |
| 71 | + |
| 72 | + it('secure-by-default: anonymous CREATE is 401', async () => { |
| 73 | + const app = bootStandardEndpoints({ services: { objectql, auth } }); |
| 74 | + const res = await app.request(REQ, { |
| 75 | + method: 'POST', |
| 76 | + headers: { 'Content-Type': 'application/json' }, |
| 77 | + body: JSON.stringify({ title: 'x' }), |
| 78 | + }); |
| 79 | + expect(res.status).toBe(401); |
| 80 | + }); |
| 81 | + |
| 82 | + it('an AUTHENTICATED caller is served (deny targets anonymity, not the route)', async () => { |
| 83 | + const app = bootStandardEndpoints({ services: { objectql, auth } }); |
| 84 | + const res = await app.request(REQ, { method: 'GET', headers: { 'x-test-user': 'u1' } }); |
| 85 | + expect(res.status).toBe(200); |
| 86 | + }); |
| 87 | + |
| 88 | + it('explicit opt-out (requireAuth:false) keeps the surface anonymously reachable', async () => { |
| 89 | + const app = bootStandardEndpoints({ |
| 90 | + restConfig: { api: { requireAuth: false } }, |
| 91 | + services: { objectql, auth }, |
| 92 | + }); |
| 93 | + const res = await app.request(REQ, { method: 'GET' }); |
| 94 | + expect(res.status).toBe(200); |
| 95 | + }); |
| 96 | +}); |
0 commit comments