|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + HonoServerPlugin, |
| 6 | + isPerfDisclosurePrincipal, |
| 7 | + permissionSetGrantsSuperUser, |
| 8 | +} from './hono-plugin'; |
| 9 | +import type { PluginContext } from '@objectstack/core'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Regression for #3361 (route parity #3369): on the shipped hono server |
| 13 | + * (`os serve` / `os dev` / `os start`) the ADMIN-GATED per-request |
| 14 | + * `Server-Timing` header never emitted. The only production caller of |
| 15 | + * `allowPerfDisclosure()` lived on the runtime dispatcher path |
| 16 | + * (`http-dispatcher.ts` `timedResolveExecutionContext`), but `os serve` mounts |
| 17 | + * the hono server, which resolves identity via its OWN self-contained |
| 18 | + * `resolveCtx` and never opened the disclosure gate — so an admin sending |
| 19 | + * `X-OS-Debug-Timing: 1` got no header on any route. |
| 20 | + * |
| 21 | + * These tests drive the REAL hono request pipeline (perf middleware + the |
| 22 | + * standard CRUD/auth routes that call `resolveCtx`) with a stubbed auth + |
| 23 | + * objectql, asserting the gate now opens for a super-user principal and stays |
| 24 | + * closed for an ordinary member — the exact behaviour the docs promise and no |
| 25 | + * single-layer unit test caught. |
| 26 | + */ |
| 27 | + |
| 28 | +/** A permission-set row a driver would return (object_permissions as an object). */ |
| 29 | +const ADMIN_SET = { |
| 30 | + id: 'ps-admin', |
| 31 | + name: 'admin_full_access', |
| 32 | + object_permissions: { '*': { viewAllRecords: true, modifyAllRecords: true } }, |
| 33 | +}; |
| 34 | +/** Same, but object_permissions arrives as a JSON string (SQLite/Turso shape). */ |
| 35 | +const ORG_ADMIN_SET_STRING = { |
| 36 | + id: 'ps-org', |
| 37 | + name: 'organization_admin', |
| 38 | + object_permissions: JSON.stringify({ '*': { modifyAllRecords: true } }), |
| 39 | +}; |
| 40 | +const MEMBER_SET = { |
| 41 | + id: 'ps-mem', |
| 42 | + name: 'member_default', |
| 43 | + object_permissions: { crm_lead: { allowRead: true } }, |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * A minimal objectql stub that answers the exact `find()` calls `resolveCtx` |
| 48 | + * makes, keyed by the requesting user. `admin1` resolves `admin_full_access`; |
| 49 | + * `user1` resolves only `member_default`. |
| 50 | + */ |
| 51 | +function fakeObjectQL() { |
| 52 | + const assignments: Record<string, { permission_set_id: string }[]> = { |
| 53 | + admin1: [{ permission_set_id: 'ps-admin' }], |
| 54 | + user1: [{ permission_set_id: 'ps-mem' }], |
| 55 | + }; |
| 56 | + const setsById: Record<string, any> = { |
| 57 | + 'ps-admin': ADMIN_SET, |
| 58 | + 'ps-mem': MEMBER_SET, |
| 59 | + }; |
| 60 | + return { |
| 61 | + async find(object: string, opts: any) { |
| 62 | + const where = opts?.where ?? {}; |
| 63 | + if (object === 'sys_member') return []; |
| 64 | + if (object === 'sys_user') return []; |
| 65 | + if (object === 'sys_user_permission_set') { |
| 66 | + const uid = where.user_id; |
| 67 | + return (assignments[uid] ?? []).map((a) => ({ ...a, user_id: uid, organization_id: null })); |
| 68 | + } |
| 69 | + if (object === 'sys_permission_set') { |
| 70 | + const ids: string[] = where?.id?.$in ?? []; |
| 71 | + return ids.map((id) => setsById[id]).filter(Boolean); |
| 72 | + } |
| 73 | + // The data route's own list query. |
| 74 | + return []; |
| 75 | + }, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +function fakeAuth() { |
| 80 | + return { |
| 81 | + api: { |
| 82 | + // Resolve the session from a test header the request sets. |
| 83 | + async getSession({ headers }: { headers: Headers }) { |
| 84 | + const uid = headers.get('x-test-user'); |
| 85 | + return uid ? { user: { id: uid } } : null; |
| 86 | + }, |
| 87 | + }, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +async function bootHono() { |
| 92 | + const services = new Map<string, unknown>(); |
| 93 | + const ctx = { |
| 94 | + logger: { debug() {}, info() {}, warn() {}, error() {} }, |
| 95 | + registerService: (name: string, svc: unknown) => services.set(name, svc), |
| 96 | + getService: (name: string) => services.get(name), |
| 97 | + } as unknown as PluginContext; |
| 98 | + |
| 99 | + // serverTiming defaults to undefined → global OFF, admin-gated per-request ON. |
| 100 | + const plugin = new HonoServerPlugin({ cors: false, registerStandardEndpoints: true }); |
| 101 | + await (plugin as any).init(ctx); |
| 102 | + services.set('auth', fakeAuth()); |
| 103 | + services.set('objectql', fakeObjectQL()); |
| 104 | + // Register the standard CRUD/auth routes (normally wired on kernel:ready). |
| 105 | + (plugin as any).registerDiscoveryAndCrudEndpoints(ctx); |
| 106 | + const app = (plugin as any).server.getRawApp(); |
| 107 | + return { app }; |
| 108 | +} |
| 109 | + |
| 110 | +describe('isPerfDisclosurePrincipal / permissionSetGrantsSuperUser', () => { |
| 111 | + it('detects the wildcard super-user grant (object + JSON-string shapes)', () => { |
| 112 | + expect(permissionSetGrantsSuperUser(ADMIN_SET)).toBe(true); |
| 113 | + expect(permissionSetGrantsSuperUser(ORG_ADMIN_SET_STRING)).toBe(true); |
| 114 | + expect(permissionSetGrantsSuperUser(MEMBER_SET)).toBe(false); |
| 115 | + expect(permissionSetGrantsSuperUser(null)).toBe(false); |
| 116 | + expect(permissionSetGrantsSuperUser({ object_permissions: 'not json' })).toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it('treats system callers and platform-admin sets as privileged', () => { |
| 120 | + expect(isPerfDisclosurePrincipal({ isSystem: true })).toBe(true); |
| 121 | + expect(isPerfDisclosurePrincipal({ permissionSetNames: ['admin_full_access'] })).toBe(true); |
| 122 | + expect(isPerfDisclosurePrincipal({ permissionSetNames: ['organization_admin'] })).toBe(true); |
| 123 | + expect(isPerfDisclosurePrincipal({ permissionSetRows: [ADMIN_SET] })).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it('treats an ordinary member as NOT privileged', () => { |
| 127 | + expect(isPerfDisclosurePrincipal({ permissionSetNames: ['member_default'], permissionSetRows: [MEMBER_SET] })).toBe(false); |
| 128 | + expect(isPerfDisclosurePrincipal(undefined)).toBe(false); |
| 129 | + expect(isPerfDisclosurePrincipal({})).toBe(false); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('hono Server-Timing per-request admin gate (#3361, drives the real resolveCtx)', () => { |
| 134 | + it('emits Server-Timing for an admin sending X-OS-Debug-Timing on a real route', async () => { |
| 135 | + const { app } = await bootHono(); |
| 136 | + const res = await app.request('/api/v1/data/widget', { |
| 137 | + headers: { 'X-OS-Debug-Timing': '1', 'x-test-user': 'admin1' }, |
| 138 | + }); |
| 139 | + expect(res.status).toBe(200); |
| 140 | + const header = res.headers.get('Server-Timing'); |
| 141 | + expect(header, `expected a Server-Timing header for the admin`).toBeTruthy(); |
| 142 | + expect(header).toMatch(/(^|, )total;dur=/); |
| 143 | + }); |
| 144 | + |
| 145 | + it('withholds Server-Timing from an ordinary member (member_default)', async () => { |
| 146 | + const { app } = await bootHono(); |
| 147 | + const res = await app.request('/api/v1/data/widget', { |
| 148 | + headers: { 'X-OS-Debug-Timing': '1', 'x-test-user': 'user1' }, |
| 149 | + }); |
| 150 | + expect(res.status).toBe(200); |
| 151 | + expect(res.headers.get('Server-Timing')).toBeNull(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('withholds Server-Timing from an anonymous caller sending the header', async () => { |
| 155 | + const { app } = await bootHono(); |
| 156 | + // No x-test-user → resolveCtx returns undefined → anonymous. |
| 157 | + const res = await app.request('/api/v1/data/widget', { |
| 158 | + headers: { 'X-OS-Debug-Timing': '1' }, |
| 159 | + }); |
| 160 | + expect(res.headers.get('Server-Timing')).toBeNull(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('does not emit for an admin when no debug header is sent (opt-in only)', async () => { |
| 164 | + const { app } = await bootHono(); |
| 165 | + const res = await app.request('/api/v1/data/widget', { |
| 166 | + headers: { 'x-test-user': 'admin1' }, |
| 167 | + }); |
| 168 | + expect(res.headers.get('Server-Timing')).toBeNull(); |
| 169 | + }); |
| 170 | + |
| 171 | + it('also opens the gate on /auth/me/permissions for an admin', async () => { |
| 172 | + const { app } = await bootHono(); |
| 173 | + const res = await app.request('/api/v1/auth/me/permissions', { |
| 174 | + headers: { 'X-OS-Debug-Timing': '1', 'x-test-user': 'admin1' }, |
| 175 | + }); |
| 176 | + expect(res.status).toBe(200); |
| 177 | + expect(res.headers.get('Server-Timing')).toMatch(/total;dur=/); |
| 178 | + }); |
| 179 | +}); |
0 commit comments