|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #13866 — Director ruling 决裁批 #24 (2026-09-01), clause 2: give |
| 4 | +// `ObjectRepository.execute()` the same elevated `ScopedContext` REST |
| 5 | +// `/actions` and MCP `run_action` already supply an action body (#13832), |
| 6 | +// so all three `executeAction` dispatch paths behave identically under the |
| 7 | +// platform's documented trusted posture and #3914's identity-less shape is |
| 8 | +// gone from the third one. |
| 9 | +// |
| 10 | +// Before this fix, `ObjectRepository.execute()` handed the handler |
| 11 | +// `{ ...params, userId, tenantId, roles }` — no `api`, no `executionContext`. |
| 12 | +// A handler reaching a sibling write via `ctx.api.object(x).update(y)` (the |
| 13 | +// in-process action-composition shape `action-execution.ts` and |
| 14 | +// `body-runner.ts` document as this method's own reason to exist) got |
| 15 | +// `ctx.api === undefined` and threw, or — for the sandbox's own last-resort |
| 16 | +// facade — a context-less repo whose writes ran as a non-system caller, so |
| 17 | +// the engine's static `readonly` strip (`!opCtx.context?.isSystem`, |
| 18 | +// `validation/rule-validator.ts`) applied to it and NOT to the same write |
| 19 | +// made through REST `/actions` or MCP `run_action`. This suite pins the |
| 20 | +// fixed shape: `ctx.api` is a real `ScopedContext` bound to |
| 21 | +// `{ ...callerContext, isSystem: true }` — the same `sudo()`-shaped |
| 22 | +// elevation `buildActionExecutionContext` uses — so a `readonly: true` |
| 23 | +// column a handler writes through `ctx.api` now LANDS on this path exactly |
| 24 | +// as it already does on the other two. |
| 25 | +// |
| 26 | +// The census this ruling required (repo + `examples/` + `apps/`, production |
| 27 | +// and test) found ZERO existing callers of `ObjectRepository.execute()` — |
| 28 | +// every hit was prose describing the shape (`action-execution.ts`, |
| 29 | +// `body-runner.ts`, `validate-readonly-action-writes.ts`, this method's own |
| 30 | +// call site), never an invocation — so nothing shipped today depends on the |
| 31 | +// old, context-less behaviour this suite retires. |
| 32 | + |
| 33 | +import { describe, it, expect } from 'vitest'; |
| 34 | +import type { ExecutionContext } from '@objectstack/spec/kernel'; |
| 35 | +import { ObjectQL, ScopedContext } from './engine.js'; |
| 36 | + |
| 37 | +function makeDriver() { |
| 38 | + const stores = new Map<string, Map<string, any>>(); |
| 39 | + const storeFor = (o: string) => { |
| 40 | + let s = stores.get(o); |
| 41 | + if (!s) { s = new Map(); stores.set(o, s); } |
| 42 | + return s; |
| 43 | + }; |
| 44 | + const matches = (row: any, where: any): boolean => { |
| 45 | + if (!where || typeof where !== 'object') return true; |
| 46 | + return Object.entries(where).every(([k, v]: [string, any]) => { |
| 47 | + // [check:where-matcher] REFUSE a combinator this fixture does not |
| 48 | + // implement, rather than silently reading it as a field name — the |
| 49 | + // exact fake-driver idiom `engine-readonly-strip-caller-values.test.ts` |
| 50 | + // already carries. |
| 51 | + if (k.startsWith('$')) throw new Error(`fake driver: unsupported operator ${k}`); |
| 52 | + return row?.[k] === v; |
| 53 | + }); |
| 54 | + }; |
| 55 | + let n = 0; |
| 56 | + const driver: any = { |
| 57 | + name: 'memory', version: '0.0.0', supports: {}, |
| 58 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; }, |
| 59 | + async find(object: string, ast: any) { |
| 60 | + const rows = Array.from(storeFor(object).values()).filter((r) => matches(r, ast?.where)); |
| 61 | + // [check:objectql-double-limit] Apply the caller's bound AFTER the |
| 62 | + // filter, by PRESENCE — this fixture is not under test for pagination, |
| 63 | + // but a `find` double that silently ignores `limit` is exactly the |
| 64 | + // shape that gate exists to catch. |
| 65 | + return typeof ast?.limit === 'number' ? rows.slice(0, ast.limit) : rows; |
| 66 | + }, |
| 67 | + async findOne(object: string, ast: any) { |
| 68 | + for (const r of storeFor(object).values()) if (matches(r, ast?.where)) return r; |
| 69 | + return null; |
| 70 | + }, |
| 71 | + async create(object: string, data: Record<string, unknown>) { |
| 72 | + n += 1; |
| 73 | + const id = (data.id as string) ?? `r_${n}`; |
| 74 | + const row = { ...data, id }; |
| 75 | + storeFor(object).set(id, row); |
| 76 | + return row; |
| 77 | + }, |
| 78 | + async update(object: string, id: string, data: Record<string, unknown>) { |
| 79 | + const s = storeFor(object); |
| 80 | + const row = { ...s.get(id), ...data, id }; |
| 81 | + s.set(id, row); |
| 82 | + return row; |
| 83 | + }, |
| 84 | + async updateMany() { return 0; }, |
| 85 | + async delete(object: string, id: string) { return storeFor(object).delete(id); }, |
| 86 | + async count() { return 0; }, |
| 87 | + async bulkCreate(object: string, rows: Record<string, unknown>[]) { |
| 88 | + return Promise.all(rows.map((r) => this.create(object, r, undefined))); |
| 89 | + }, |
| 90 | + async bulkUpdate() { return []; }, async bulkDelete() {}, |
| 91 | + async beginTransaction() { return { __trx: true, commit: async () => {}, rollback: async () => {} }; }, |
| 92 | + async commit() {}, async rollback() {}, |
| 93 | + }; |
| 94 | + return { driver, storeFor }; |
| 95 | +} |
| 96 | + |
| 97 | +function makeRig() { |
| 98 | + const engine = new ObjectQL({}); |
| 99 | + const d = makeDriver(); |
| 100 | + engine.registerDriver(d.driver, true); |
| 101 | + engine.registry.registerObject({ |
| 102 | + name: 'os_repo_execute_probe', |
| 103 | + fields: { |
| 104 | + title: { type: 'text' }, |
| 105 | + // Author-declared lock — the exact gate `!opCtx.context?.isSystem` |
| 106 | + // guards (`validation/rule-validator.ts`'s `stripReadonlyFields`). |
| 107 | + stamped_by: { type: 'text', readonly: true }, |
| 108 | + }, |
| 109 | + } as any); |
| 110 | + return { engine, storeFor: d.storeFor }; |
| 111 | +} |
| 112 | + |
| 113 | +describe('ObjectRepository.execute() elevation (#13866, 决裁批 #24)', () => { |
| 114 | + it('THE FIX: a readonly-field write through ctx.api LANDS, matching REST/MCP', async () => { |
| 115 | + const { engine, storeFor } = makeRig(); |
| 116 | + await engine.init(); |
| 117 | + storeFor('os_repo_execute_probe').set('p_1', { id: 'p_1', title: 'A', stamped_by: null }); |
| 118 | + |
| 119 | + engine.registerAction('os_repo_execute_probe', 'stamp', async (ctx: any) => { |
| 120 | + // The in-process composition shape this method exists for: a handler |
| 121 | + // reaching a sibling write via `ctx.api.object(x).update(y)`. |
| 122 | + await ctx.api.object('os_repo_execute_probe').update({ id: ctx.id, stamped_by: 'action-body' }); |
| 123 | + return { ok: true }; |
| 124 | + }); |
| 125 | + |
| 126 | + const callerCtx: ExecutionContext = { userId: 'u_1', tenantId: 't_1' } as any; |
| 127 | + const repo = new ScopedContext(callerCtx, engine as any).object('os_repo_execute_probe'); |
| 128 | + const result = await repo.execute('stamp', { id: 'p_1' }); |
| 129 | + |
| 130 | + expect(result).toEqual({ ok: true }); |
| 131 | + // THE REGRESSION, stated as the value it must NOT be: before the fix |
| 132 | + // `ctx.api` was `undefined` (throwing) or a context-less facade whose |
| 133 | + // write the static strip silently discarded, leaving `stamped_by: null`. |
| 134 | + expect(storeFor('os_repo_execute_probe').get('p_1').stamped_by).toBe('action-body'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('ctx.executionContext carries isSystem: true — the same envelope buildActionExecutionContext builds', async () => { |
| 138 | + const { engine } = makeRig(); |
| 139 | + await engine.init(); |
| 140 | + let seenExecutionContext: any; |
| 141 | + let seenApi: any; |
| 142 | + engine.registerAction('os_repo_execute_probe', 'inspect', async (ctx: any) => { |
| 143 | + seenExecutionContext = ctx.executionContext; |
| 144 | + seenApi = ctx.api; |
| 145 | + return { ok: true }; |
| 146 | + }); |
| 147 | + |
| 148 | + const callerCtx: ExecutionContext = { userId: 'u_2', tenantId: 't_2' } as any; |
| 149 | + await new ScopedContext(callerCtx, engine as any).object('os_repo_execute_probe').execute('inspect', {}); |
| 150 | + |
| 151 | + expect(seenExecutionContext).toMatchObject({ userId: 'u_2', tenantId: 't_2', isSystem: true }); |
| 152 | + expect(seenApi).toBeInstanceOf(ScopedContext); |
| 153 | + }); |
| 154 | + |
| 155 | + it('the elevation is ATTRIBUTABLE, not anonymous: userId/tenantId still ride the elevated context', async () => { |
| 156 | + // The reason `buildActionExecutionContext` spreads the caller's envelope |
| 157 | + // FIRST rather than handing over a bare `{ isSystem: true }` — pinned |
| 158 | + // here on the third path exactly as `recomputeSummaries`' `systemCtx` |
| 159 | + // pins it on the second. |
| 160 | + const { engine } = makeRig(); |
| 161 | + await engine.init(); |
| 162 | + let capturedUserId: unknown; |
| 163 | + let capturedTenantId: unknown; |
| 164 | + engine.registerAction('os_repo_execute_probe', 'capture_identity', async (ctx: any) => { |
| 165 | + capturedUserId = ctx.executionContext.userId; |
| 166 | + capturedTenantId = ctx.executionContext.tenantId; |
| 167 | + return { ok: true }; |
| 168 | + }); |
| 169 | + |
| 170 | + const callerCtx: ExecutionContext = { userId: 'u_3', tenantId: 't_3' } as any; |
| 171 | + await new ScopedContext(callerCtx, engine as any).object('os_repo_execute_probe').execute('capture_identity', {}); |
| 172 | + |
| 173 | + expect(capturedUserId).toBe('u_3'); |
| 174 | + expect(capturedTenantId).toBe('t_3'); |
| 175 | + }); |
| 176 | + |
| 177 | + it('an already-elevated caller composing through repo.execute() stays elevated (no regression)', async () => { |
| 178 | + const { engine, storeFor } = makeRig(); |
| 179 | + await engine.init(); |
| 180 | + storeFor('os_repo_execute_probe').set('p_2', { id: 'p_2', title: 'B', stamped_by: null }); |
| 181 | + |
| 182 | + engine.registerAction('os_repo_execute_probe', 'stamp2', async (ctx: any) => { |
| 183 | + await ctx.api.object('os_repo_execute_probe').update({ id: ctx.id, stamped_by: 'still-elevated' }); |
| 184 | + return { ok: true }; |
| 185 | + }); |
| 186 | + |
| 187 | + const systemCtx: ExecutionContext = { isSystem: true } as any; |
| 188 | + await new ScopedContext(systemCtx, engine as any).object('os_repo_execute_probe').execute('stamp2', { id: 'p_2' }); |
| 189 | + |
| 190 | + expect(storeFor('os_repo_execute_probe').get('p_2').stamped_by).toBe('still-elevated'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('PARITY: the same landed value a non-system caller updating directly with { context: { isSystem: true } } produces', async () => { |
| 194 | + // Not a REST/MCP integration test (those live in `packages/runtime`, |
| 195 | + // which cannot be imported from here without a circular dependency) — |
| 196 | + // this asserts the same OUTCOME that posture produces on the write path |
| 197 | + // both those doors and this one now share: a readonly write elevated by |
| 198 | + // `isSystem: true` lands, non-elevated does not. |
| 199 | + const { engine, storeFor } = makeRig(); |
| 200 | + await engine.init(); |
| 201 | + storeFor('os_repo_execute_probe').set('p_3', { id: 'p_3', title: 'C', stamped_by: null }); |
| 202 | + storeFor('os_repo_execute_probe').set('p_4', { id: 'p_4', title: 'D', stamped_by: null }); |
| 203 | + |
| 204 | + // The REST/MCP-equivalent direct write. |
| 205 | + await engine.update( |
| 206 | + 'os_repo_execute_probe', |
| 207 | + { id: 'p_3', stamped_by: 'direct-elevated' }, |
| 208 | + { context: { isSystem: true } } as any, |
| 209 | + ); |
| 210 | + |
| 211 | + // The repo.execute()-mediated write, now under the same posture. |
| 212 | + engine.registerAction('os_repo_execute_probe', 'stamp3', async (ctx: any) => { |
| 213 | + await ctx.api.object('os_repo_execute_probe').update({ id: ctx.id, stamped_by: 'via-repo-execute' }); |
| 214 | + }); |
| 215 | + const callerCtx: ExecutionContext = { userId: 'u_4' } as any; |
| 216 | + await new ScopedContext(callerCtx, engine as any).object('os_repo_execute_probe').execute('stamp3', { id: 'p_4' }); |
| 217 | + |
| 218 | + expect(storeFor('os_repo_execute_probe').get('p_3').stamped_by).toBe('direct-elevated'); |
| 219 | + expect(storeFor('os_repo_execute_probe').get('p_4').stamped_by).toBe('via-repo-execute'); |
| 220 | + }); |
| 221 | +}); |
0 commit comments