|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #6334 — position-bound capabilities must reach `/auth/me/permissions`. |
| 4 | +// |
| 5 | +// The standalone resolver behind these endpoints used to read `sys_member` + |
| 6 | +// `sys_user_permission_set` and nothing else. `sys_user_position` / |
| 7 | +// `sys_position_permission_set` — the ADR-0090 D3 DISTRIBUTION mechanism, which |
| 8 | +// is how showcase grants every persona — were invisible to it, so a permission |
| 9 | +// set bound to a position never reached the response: `positions: []`, |
| 10 | +// `permissionSets` without the set, `systemPermissions` without its |
| 11 | +// capabilities. objectui's four `useCapabilityGate` surfaces (ADR-0066 D4) read |
| 12 | +// this endpoint, so the button was hidden from a user who genuinely HELD the |
| 13 | +// capability, while the data plane (SecurityPlugin middleware, canonical chain) |
| 14 | +// granted the same action. Hiding from an entitled user is the failure |
| 15 | +// direction the fail-open design names as the worse one. |
| 16 | +// |
| 17 | +// The fix delegates all grant aggregation to `resolveUserAuthzGrants` — the |
| 18 | +// canonical resolver's userId-driven core. These cases therefore assert BOTH |
| 19 | +// halves: the issue's own repro (a position-bound set surfaces), and the |
| 20 | +// semantics that come free with the delegation and would have to be re-written |
| 21 | +// by hand otherwise — the implicit `everyone` anchor (ADR-0090 D5), null-org = |
| 22 | +// global, active-org matching, and the ADR-0091 validity window. |
| 23 | +// |
| 24 | +// Every negative case here carries a co-present VALID grant and asserts it |
| 25 | +// surfaced. A negative asserted on its own would pass in the pre-fix world for |
| 26 | +// the wrong reason — because the resolver produced nothing at all, not because |
| 27 | +// it judged the invalid row correctly. |
| 28 | + |
| 29 | +import { describe, it, expect } from 'vitest'; |
| 30 | +import { Hono } from 'hono'; |
| 31 | +import { registerCurrentUserEndpoints } from './current-user-endpoints'; |
| 32 | + |
| 33 | +const ME_PERMISSIONS = '/api/v1/auth/me/permissions'; |
| 34 | +const ME_APPS = '/api/v1/me/apps'; |
| 35 | + |
| 36 | +const USER = 'usr_ops'; |
| 37 | +const ACTIVE_ORG = 'org_active'; |
| 38 | +const OTHER_ORG = 'org_other'; |
| 39 | + |
| 40 | +/** Far past / far future bounds for the ADR-0091 window cases (real clock). */ |
| 41 | +const PAST = '2020-01-01T00:00:00.000Z'; |
| 42 | +const FUTURE = '2999-01-01T00:00:00.000Z'; |
| 43 | + |
| 44 | +type Row = Record<string, any>; |
| 45 | + |
| 46 | +/** `where` matcher: scalar equality plus the `$in` form both resolvers use. */ |
| 47 | +function matches(row: Row, where: Row | undefined): boolean { |
| 48 | + return Object.entries(where ?? {}).every(([key, cond]) => { |
| 49 | + const value = row[key] ?? null; |
| 50 | + if (cond && typeof cond === 'object' && Array.isArray((cond as any).$in)) { |
| 51 | + return (cond as any).$in.includes(value); |
| 52 | + } |
| 53 | + return value === (cond ?? null); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * A seeded fake data engine — READ ONLY (`find`), which is every verb this |
| 59 | + * surface uses. `where`/`limit` are honoured so a resolver that queries the |
| 60 | + * wrong table or the wrong scope gets nothing, exactly as it would in the |
| 61 | + * engine. |
| 62 | + */ |
| 63 | +function makeQl(tables: Record<string, Row[]>) { |
| 64 | + return { |
| 65 | + find: async (object: string, opts: any, _ctx?: any) => { |
| 66 | + const rows = (tables[object] ?? []).filter((r) => matches(r, opts?.where)); |
| 67 | + return typeof opts?.limit === 'number' ? rows.slice(0, opts.limit) : rows; |
| 68 | + }, |
| 69 | + registry: { getAllApps: () => tables.__apps ?? [], getAllObjects: () => [] }, |
| 70 | + getSchema: () => undefined, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +/** A permission set as `sys_permission_set` stores it (JSON columns as text). */ |
| 75 | +function permissionSet(id: string, name: string, systemPermissions: string[]): Row { |
| 76 | + return { |
| 77 | + id, |
| 78 | + name, |
| 79 | + object_permissions: '{}', |
| 80 | + field_permissions: '{}', |
| 81 | + system_permissions: JSON.stringify(systemPermissions), |
| 82 | + tab_permissions: '{}', |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * A stand-in for plugin-security's `PermissionEvaluator` on its DB-backed |
| 88 | + * branch: resolve the requested identifiers through the loader the endpoint |
| 89 | + * supplies. plugin-hono-server must not depend on plugin-security (that |
| 90 | + * package is OPTIONAL in the stacks these endpoints serve), so the double |
| 91 | + * covers the one method both handlers call — and it is the DB branch that |
| 92 | + * matters here, since the identifiers under test are exactly what the endpoint |
| 93 | + * feeds it. |
| 94 | + */ |
| 95 | +const evaluator = { |
| 96 | + resolvePermissionSets: async ( |
| 97 | + identifiers: string[], |
| 98 | + _metadata: unknown, |
| 99 | + _bootstrap: unknown[] | undefined, |
| 100 | + dbLoader?: (names: string[]) => Promise<unknown[]>, |
| 101 | + ) => (dbLoader ? dbLoader(identifiers) : []), |
| 102 | +}; |
| 103 | + |
| 104 | +/** Minimal `metadata` — present so the endpoint takes its full (non-degraded) branch. */ |
| 105 | +const metadata = { list: async () => [] as unknown[] }; |
| 106 | + |
| 107 | +interface MountOptions { |
| 108 | + tables: Record<string, Row[]>; |
| 109 | + /** Active organization on the session (`session.activeOrganizationId`). */ |
| 110 | + activeOrg?: string | null; |
| 111 | +} |
| 112 | + |
| 113 | +function mount({ tables, activeOrg = ACTIVE_ORG }: MountOptions) { |
| 114 | + const services: Record<string, unknown> = { |
| 115 | + auth: { |
| 116 | + api: { |
| 117 | + getSession: async () => ({ |
| 118 | + user: { id: USER, email: 'ops@example.com' }, |
| 119 | + session: activeOrg ? { activeOrganizationId: activeOrg } : {}, |
| 120 | + }), |
| 121 | + }, |
| 122 | + }, |
| 123 | + objectql: makeQl(tables), |
| 124 | + metadata, |
| 125 | + 'security.permissions': evaluator, |
| 126 | + }; |
| 127 | + const app = new Hono(); |
| 128 | + registerCurrentUserEndpoints({ |
| 129 | + rawApp: app, |
| 130 | + ctx: { |
| 131 | + logger: { debug() {}, warn() {} }, |
| 132 | + // Throws for an unclaimed slot, like the real kernel locator. |
| 133 | + getService: <T,>(name: string): T => { |
| 134 | + if (!(name in services)) throw new Error(`[Kernel] Service '${name}' not found`); |
| 135 | + return services[name] as T; |
| 136 | + }, |
| 137 | + }, |
| 138 | + }); |
| 139 | + return app; |
| 140 | +} |
| 141 | + |
| 142 | +const permissionsOf = async (app: any) => |
| 143 | + (await app.request(`http://localhost${ME_PERMISSIONS}`)).json() as Promise<any>; |
| 144 | + |
| 145 | +/** The showcase-shaped seed: `ops` position ↔ `showcase_ops` permission set. */ |
| 146 | +function baseTables(overrides: Record<string, Row[]> = {}): Record<string, Row[]> { |
| 147 | + return { |
| 148 | + sys_user: [{ id: USER, email: 'ops@example.com' }], |
| 149 | + sys_member: [{ user_id: USER, organization_id: ACTIVE_ORG, role: 'member' }], |
| 150 | + sys_user_position: [], |
| 151 | + sys_user_permission_set: [], |
| 152 | + sys_position: [ |
| 153 | + { id: 'pos_ops', name: 'ops' }, |
| 154 | + { id: 'pos_auditor', name: 'auditor' }, |
| 155 | + { id: 'pos_everyone', name: 'everyone' }, |
| 156 | + ], |
| 157 | + sys_position_permission_set: [ |
| 158 | + { position_id: 'pos_ops', permission_set_id: 'ps_ops' }, |
| 159 | + { position_id: 'pos_auditor', permission_set_id: 'ps_auditor' }, |
| 160 | + { position_id: 'pos_everyone', permission_set_id: 'ps_default' }, |
| 161 | + ], |
| 162 | + sys_permission_set: [ |
| 163 | + permissionSet('ps_ops', 'showcase_ops', ['setup.access', 'showcase.export_data']), |
| 164 | + permissionSet('ps_auditor', 'showcase_auditor', ['showcase.audit_read']), |
| 165 | + permissionSet('ps_default', 'showcase_member_default', []), |
| 166 | + permissionSet('ps_direct', 'showcase_direct', ['showcase.direct_only']), |
| 167 | + ], |
| 168 | + ...overrides, |
| 169 | + }; |
| 170 | +} |
| 171 | + |
| 172 | +describe('position-bound permission sets reach /auth/me/permissions (#6334)', () => { |
| 173 | + it('surfaces a sys_user_position → sys_position_permission_set grant', async () => { |
| 174 | + // The issue's repro, verbatim: one `sys_user_position` row for the |
| 175 | + // active org, `valid_from`/`valid_until` both empty. |
| 176 | + const app = mount({ |
| 177 | + tables: baseTables({ |
| 178 | + sys_user_position: [{ id: 'up1', user_id: USER, position: 'ops', organization_id: ACTIVE_ORG }], |
| 179 | + }), |
| 180 | + }); |
| 181 | + |
| 182 | + const body = await permissionsOf(app); |
| 183 | + |
| 184 | + expect(body.authenticated).toBe(true); |
| 185 | + expect(body.positions).toContain('ops'); |
| 186 | + expect(body.permissionSets).toContain('showcase_ops'); |
| 187 | + expect(body.systemPermissions).toContain('showcase.export_data'); |
| 188 | + expect(body.systemPermissions).toContain('setup.access'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('still resolves a direct sys_user_permission_set binding (positive control)', async () => { |
| 192 | + // The table the pre-fix resolver DID read — the issue's own control, |
| 193 | + // where switching to a direct binding made the capability appear. It |
| 194 | + // must keep working after the delegation. |
| 195 | + const app = mount({ |
| 196 | + tables: baseTables({ |
| 197 | + sys_user_permission_set: [ |
| 198 | + { id: 'ups1', user_id: USER, permission_set_id: 'ps_direct', organization_id: null }, |
| 199 | + ], |
| 200 | + }), |
| 201 | + }); |
| 202 | + |
| 203 | + const body = await permissionsOf(app); |
| 204 | + |
| 205 | + expect(body.permissionSets).toContain('showcase_direct'); |
| 206 | + expect(body.systemPermissions).toContain('showcase.direct_only'); |
| 207 | + }); |
| 208 | + |
| 209 | + it('carries the implicit `everyone` position and its default set (ADR-0090 D5)', async () => { |
| 210 | + // No position row at all: every AUTHENTICATED member implicitly holds |
| 211 | + // `everyone`, so sets bound to it resolve like any other position-bound |
| 212 | + // grant. The issue saw this missing too (`everyone → showcase_member_default`). |
| 213 | + const app = mount({ tables: baseTables() }); |
| 214 | + |
| 215 | + const body = await permissionsOf(app); |
| 216 | + |
| 217 | + expect(body.positions).toContain('everyone'); |
| 218 | + expect(body.permissionSets).toContain('showcase_member_default'); |
| 219 | + }); |
| 220 | + |
| 221 | + it('projects the normalized org-membership position (sys_member.role)', async () => { |
| 222 | + // `member` → `org_member` (mapMembershipRole). The pre-fix envelope put |
| 223 | + // these under `roles` while every reader here — and ExecutionContext |
| 224 | + // itself — calls the field `positions`, so they were dropped on the |
| 225 | + // floor independently of the position tables. |
| 226 | + const body = await permissionsOf(mount({ tables: baseTables() })); |
| 227 | + |
| 228 | + expect(body.positions).toContain('org_member'); |
| 229 | + }); |
| 230 | + |
| 231 | + it('treats a null-org position row as global (resolves under any active org)', async () => { |
| 232 | + const app = mount({ |
| 233 | + tables: baseTables({ |
| 234 | + sys_user_position: [{ id: 'up1', user_id: USER, position: 'ops', organization_id: null }], |
| 235 | + }), |
| 236 | + }); |
| 237 | + |
| 238 | + const body = await permissionsOf(app); |
| 239 | + |
| 240 | + expect(body.positions).toContain('ops'); |
| 241 | + expect(body.systemPermissions).toContain('showcase.export_data'); |
| 242 | + }); |
| 243 | + |
| 244 | + it('drops a position row scoped to another organization, keeping the active-org one', async () => { |
| 245 | + const app = mount({ |
| 246 | + tables: baseTables({ |
| 247 | + sys_user_position: [ |
| 248 | + { id: 'up1', user_id: USER, position: 'ops', organization_id: ACTIVE_ORG }, |
| 249 | + { id: 'up2', user_id: USER, position: 'auditor', organization_id: OTHER_ORG }, |
| 250 | + ], |
| 251 | + }), |
| 252 | + }); |
| 253 | + |
| 254 | + const body = await permissionsOf(app); |
| 255 | + |
| 256 | + // The co-present valid grant is asserted so the negative below cannot |
| 257 | + // pass merely because nothing resolved. |
| 258 | + expect(body.positions).toContain('ops'); |
| 259 | + expect(body.positions).not.toContain('auditor'); |
| 260 | + expect(body.permissionSets).not.toContain('showcase_auditor'); |
| 261 | + expect(body.systemPermissions).not.toContain('showcase.audit_read'); |
| 262 | + }); |
| 263 | + |
| 264 | + it('drops position grants outside their ADR-0091 validity window', async () => { |
| 265 | + const app = mount({ |
| 266 | + tables: baseTables({ |
| 267 | + sys_user_position: [ |
| 268 | + { id: 'up1', user_id: USER, position: 'ops', organization_id: ACTIVE_ORG }, |
| 269 | + // Expired, and not-yet-active — both spellings of "outside |
| 270 | + // the half-open [from, until) window". |
| 271 | + { |
| 272 | + id: 'up2', user_id: USER, position: 'auditor', |
| 273 | + organization_id: ACTIVE_ORG, valid_until: PAST, |
| 274 | + }, |
| 275 | + { |
| 276 | + id: 'up3', user_id: USER, position: 'auditor', |
| 277 | + organization_id: ACTIVE_ORG, valid_from: FUTURE, |
| 278 | + }, |
| 279 | + ], |
| 280 | + }), |
| 281 | + }); |
| 282 | + |
| 283 | + const body = await permissionsOf(app); |
| 284 | + |
| 285 | + expect(body.positions).toContain('ops'); |
| 286 | + expect(body.positions).not.toContain('auditor'); |
| 287 | + expect(body.systemPermissions).not.toContain('showcase.audit_read'); |
| 288 | + }); |
| 289 | +}); |
| 290 | + |
| 291 | +describe('/me/apps sees the same position-bound capabilities (#6334)', () => { |
| 292 | + it('lists an app whose requiredPermissions come from a position-bound set', async () => { |
| 293 | + const app = mount({ |
| 294 | + tables: baseTables({ |
| 295 | + sys_user_position: [{ id: 'up1', user_id: USER, position: 'ops', organization_id: ACTIVE_ORG }], |
| 296 | + __apps: [ |
| 297 | + { name: 'exports', requiredPermissions: ['showcase.export_data'] }, |
| 298 | + { name: 'billing', requiredPermissions: ['billing.manage'] }, |
| 299 | + ], |
| 300 | + }), |
| 301 | + }); |
| 302 | + |
| 303 | + const body = await (await app.request(`http://localhost${ME_APPS}`)).json() as any; |
| 304 | + |
| 305 | + // `exports` is entered through a capability the user holds ONLY via the |
| 306 | + // position chain; `billing` is the control that the filter still filters. |
| 307 | + expect(body.apps.map((a: any) => a.name)).toEqual(['exports']); |
| 308 | + }); |
| 309 | +}); |
0 commit comments