@cosmicdrift/kumiko-framework 0.262.0 → 0.264.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -81,6 +81,19 @@ feature_disabled:
81
81
  handler. Enable the feature via feature toggle or routing config (see
82
82
  [Feature Toggles](/en/feature-reference/feature-toggles/)).
83
83
 
84
+ system_identity_switch_denied:
85
+ endUser: |
86
+ This action isn't allowed.
87
+ Please contact an administrator if you believe this is a mistake.
88
+ developer: |
89
+ `AccessDeniedError`: a handler or hook called `ctx.queryAs`/`ctx.writeAs`
90
+ with a SYSTEM identity without being an `r.systemScope()` handler, a job,
91
+ or declaring `escapeHatch: { reason }`.
92
+
93
+ Declare `escapeHatch: { reason: "..." }` on the write or query handler
94
+ (or on the `r.hook(...)` call, if the switch happens inside a hook), or
95
+ move the logic into an `r.systemScope()` feature or a job.
96
+
84
97
  agent.tool_not_allowed:
85
98
  endUser: |
86
99
  This tool is not available in the current mode.
@@ -38,6 +38,10 @@ export const FrameworkReasons = {
38
38
  // Distinct from access-denied — clients should surface "feature X is
39
39
  // currently unavailable" not "you don't have permission".
40
40
  featureDisabled: "feature_disabled",
41
+
42
+ // AccessDeniedError: a handler/hook called ctx.queryAs/ctx.writeAs with a
43
+ // SYSTEM identity without an r.systemScope() feature or a declared escapeHatch.
44
+ systemIdentitySwitchDenied: "system_identity_switch_denied",
41
45
  } as const;
42
46
 
43
47
  export type FrameworkReason = (typeof FrameworkReasons)[keyof typeof FrameworkReasons];
@@ -13,7 +13,13 @@ import { z } from "zod";
13
13
  import { ROLES } from "../../auth/roles";
14
14
  import { type BunTestDb, createTestDb } from "../../bun-db/__tests__/bun-test-db";
15
15
  import { asRawClient } from "../../db/query";
16
- import { createRegistry, defineFeature, type SessionUser, type WriteResult } from "../../engine";
16
+ import {
17
+ createRegistry,
18
+ createSystemUser,
19
+ defineFeature,
20
+ type SessionUser,
21
+ type WriteResult,
22
+ } from "../../engine";
17
23
  import { createWorkerEntrypoint } from "../../entrypoint";
18
24
  import { createArchivedStreamsTable, createEventsTable } from "../../event-store";
19
25
  import { createEventConsumerStateTable } from "../../pipeline";
@@ -28,6 +34,7 @@ type ProbeOutcome = { readonly isSuccess: boolean; readonly errorCode: string |
28
34
  const writeOutcomes: ProbeOutcome[] = [];
29
35
  const writeAsOutcomes: ProbeOutcome[] = [];
30
36
  const writeAsFailures: string[] = [];
37
+ const jobQueryAsSystemResults: Array<{ readonly roles: readonly string[] }> = [];
31
38
 
32
39
  function recordOutcome(sink: ProbeOutcome[], result: WriteResult): void {
33
40
  sink.push({
@@ -57,6 +64,10 @@ const writeAsProbeFeature = defineFeature("writeAsProbe", (r) => {
57
64
  { access: { roles: [ROLES.TenantAdmin] } },
58
65
  );
59
66
 
67
+ r.queryHandler("whoami", z.object({}), async (query) => ({ roles: query.user.roles }), {
68
+ access: { roles: ["system"] },
69
+ });
70
+
60
71
  r.job("writeAsSystem", { trigger: { manual: true }, retries: 0 }, async (payload, ctx) => {
61
72
  const result = await ctx.write("write-as-probe:write:admin-note", {
62
73
  note: payload["note"] as string, // @cast-boundary dynamic-key
@@ -64,6 +75,16 @@ const writeAsProbeFeature = defineFeature("writeAsProbe", (r) => {
64
75
  recordOutcome(writeOutcomes, result);
65
76
  });
66
77
 
78
+ // fw#2859 — JobContext.queryAs stays UNgated by the SYSTEM identity-switch gate.
79
+ r.job("queryAsSystemWhoami", { trigger: { manual: true }, retries: 0 }, async (_payload, ctx) => {
80
+ const result = (await ctx.queryAs(
81
+ createSystemUser(ctx.systemUser.tenantId),
82
+ "write-as-probe:query:whoami",
83
+ {},
84
+ )) as { roles: readonly string[] }; // @cast-boundary engine-payload
85
+ jobQueryAsSystemResults.push(result);
86
+ });
87
+
67
88
  r.job("writeAsActor", { trigger: { manual: true }, retries: 0 }, async (payload, ctx) => {
68
89
  const actor: SessionUser = {
69
90
  id: payload["actorId"] as string, // @cast-boundary dynamic-key
@@ -204,3 +225,28 @@ describe("JobContext.writeAs before attachDispatcher()", () => {
204
225
  }
205
226
  });
206
227
  });
228
+
229
+ describe("JobContext.queryAs stays ungated by the SYSTEM identity-switch gate (framework#2859)", () => {
230
+ test("ctx.queryAs(createSystemUser(...), whoami) succeeds from inside a job", async () => {
231
+ jobQueryAsSystemResults.length = 0;
232
+ const worker = createWorkerEntrypoint({
233
+ registry: createRegistry([writeAsProbeFeature]),
234
+ context: { db: testDb.db, redis: testRedis.redis },
235
+ jwtSecret: JWT,
236
+ redisUrl: redisUrl(),
237
+ queueNamePrefix: uniquePrefix("job-query-as-system"),
238
+ });
239
+
240
+ await worker.start();
241
+ try {
242
+ await worker.jobRunner.dispatch("write-as-probe:job:query-as-system-whoami", {});
243
+
244
+ await waitFor(() => {
245
+ expect(jobQueryAsSystemResults.length).toBe(1);
246
+ });
247
+ expect(jobQueryAsSystemResults[0]?.roles).toContain("system");
248
+ } finally {
249
+ await worker.stop();
250
+ }
251
+ });
252
+ });
@@ -135,7 +135,10 @@ const bridgeFeature = defineFeature("ctxbridge", (r) => {
135
135
 
136
136
  return created;
137
137
  },
138
- { access: { roles: ["Admin"] } },
138
+ {
139
+ access: { roles: ["Admin"] },
140
+ escapeHatch: { reason: "test fixture: writeAs(SYSTEM) creates the secret for the caller" },
141
+ },
139
142
  );
140
143
 
141
144
  // Two inserts, one via ctx.db (tx-bound) and one via ctx.dbOutsideTransaction
@@ -168,7 +171,10 @@ const bridgeFeature = defineFeature("ctxbridge", (r) => {
168
171
  owner: query.payload.owner,
169
172
  });
170
173
  },
171
- { access: { roles: ["Admin"] } },
174
+ {
175
+ access: { roles: ["Admin"] },
176
+ escapeHatch: { reason: "test fixture: queryAs(SYSTEM) reads the privileged token field" },
177
+ },
172
178
  );
173
179
 
174
180
  // Records whether ctx.db threw (should, once the request signal is
@@ -0,0 +1,352 @@
1
+ // fw#2859 — ctx.queryAs/ctx.writeAs to SYSTEM needs r.systemScope() or { escapeHatch }; a
2
+ // non-SYSTEM switch is ungated. Real HTTP calls + setupTestStack — never createTestDispatcher.
3
+
4
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
5
+ import { z } from "zod";
6
+ import { createEventStoreExecutor } from "../../db/event-store-executor";
7
+ import { buildEntityTable } from "../../db/table-builder";
8
+ import {
9
+ createEntity,
10
+ createSystemUser,
11
+ createTextField,
12
+ defineFeature,
13
+ HookPhases,
14
+ } from "../../engine";
15
+ import type { HandlerContext, SessionUser } from "../../engine/types";
16
+ import {
17
+ createTestUser,
18
+ setupTestStack,
19
+ type TestStack,
20
+ TestUsers,
21
+ unsafeCreateEntityTable,
22
+ } from "../../stack";
23
+
24
+ const user = TestUsers.user;
25
+ const otherUserNoRole: SessionUser = createTestUser({
26
+ id: 50,
27
+ tenantId: user.tenantId,
28
+ roles: ["User"],
29
+ });
30
+ const otherUserWithAdminRole: SessionUser = createTestUser({
31
+ id: 51,
32
+ tenantId: user.tenantId,
33
+ roles: ["Admin"],
34
+ });
35
+
36
+ const hookThingEntity = createEntity({
37
+ table: "idswitch_hook_things",
38
+ fields: {
39
+ label: createTextField({ personal: false, reason: "test_fixture", required: true }),
40
+ },
41
+ });
42
+ const hookThingTable = buildEntityTable("hookThing", hookThingEntity);
43
+ const hookThingExecutor = createEventStoreExecutor(hookThingTable, hookThingEntity, {
44
+ entityName: "hookThing",
45
+ });
46
+
47
+ const hookNoEscapeHatchOutcomes: Array<{ readonly threw: boolean }> = [];
48
+ const hookWithEscapeHatchOutcomes: Array<{ readonly ok: boolean }> = [];
49
+
50
+ const probeFeature = defineFeature("idswitch-probe", (r) => {
51
+ r.entity("hookThing", hookThingEntity);
52
+
53
+ // --- Targets ---
54
+ r.queryHandler(
55
+ "whoami",
56
+ z.object({}),
57
+ async (query) => ({
58
+ roles: query.user.roles,
59
+ tenantId: query.user.tenantId,
60
+ }),
61
+ { access: { roles: ["system", "User"] } },
62
+ );
63
+
64
+ r.writeHandler(
65
+ "whoami-write",
66
+ z.object({}),
67
+ async (event) => ({
68
+ isSuccess: true as const,
69
+ data: { roles: event.user.roles, tenantId: event.user.tenantId },
70
+ }),
71
+ { access: { roles: ["system", "User"] } },
72
+ );
73
+
74
+ r.queryHandler("admin-only", z.object({}), async () => ({ ok: true }), {
75
+ access: { roles: ["Admin"] },
76
+ });
77
+
78
+ // --- Direct SYSTEM switch: gated by the CALLING handler's own escapeHatch ---
79
+ r.writeHandler(
80
+ "write-as-system-no-hatch",
81
+ z.object({}),
82
+ async (event, ctx) =>
83
+ ctx.writeAs(createSystemUser(event.user.tenantId), "idswitch-probe:write:whoami-write", {}),
84
+ { access: { roles: ["User"] } },
85
+ );
86
+
87
+ r.writeHandler(
88
+ "write-as-system-with-hatch",
89
+ z.object({}),
90
+ async (event, ctx) =>
91
+ ctx.writeAs(createSystemUser(event.user.tenantId), "idswitch-probe:write:whoami-write", {}),
92
+ {
93
+ access: { roles: ["User"] },
94
+ escapeHatch: { reason: "test: needs SYSTEM to look itself up" },
95
+ },
96
+ );
97
+
98
+ r.queryHandler(
99
+ "query-as-system-no-hatch",
100
+ z.object({}),
101
+ async (query, ctx) =>
102
+ ctx.queryAs(createSystemUser(query.user.tenantId), "idswitch-probe:query:whoami", {}),
103
+ { access: { roles: ["User"] } },
104
+ );
105
+
106
+ r.queryHandler(
107
+ "query-as-system-with-hatch",
108
+ z.object({}),
109
+ async (query, ctx) =>
110
+ ctx.queryAs(createSystemUser(query.user.tenantId), "idswitch-probe:query:whoami", {}),
111
+ {
112
+ access: { roles: ["User"] },
113
+ escapeHatch: { reason: "test: needs SYSTEM to look itself up" },
114
+ },
115
+ );
116
+
117
+ // --- Non-SYSTEM switch: never gated by this feature, only the target's own access rule ---
118
+ r.queryHandler(
119
+ "query-as-normal-user",
120
+ z.object({ granted: z.boolean() }),
121
+ async (query, ctx) =>
122
+ ctx.queryAs(
123
+ query.payload.granted ? otherUserWithAdminRole : otherUserNoRole,
124
+ "idswitch-probe:query:admin-only",
125
+ {},
126
+ ),
127
+ { access: { roles: ["User"] } },
128
+ );
129
+
130
+ // --- Nested: no transitive grant ---
131
+ r.writeHandler(
132
+ "inner-no-hatch",
133
+ z.object({}),
134
+ async (event, ctx) =>
135
+ ctx.writeAs(createSystemUser(event.user.tenantId), "idswitch-probe:write:whoami-write", {}),
136
+ { access: { roles: ["system"] } },
137
+ );
138
+
139
+ r.writeHandler(
140
+ "inner-with-hatch",
141
+ z.object({}),
142
+ async (event, ctx) =>
143
+ ctx.writeAs(createSystemUser(event.user.tenantId), "idswitch-probe:write:whoami-write", {}),
144
+ { access: { roles: ["system"] }, escapeHatch: { reason: "test: inner needs SYSTEM" } },
145
+ );
146
+
147
+ r.writeHandler(
148
+ "outer-with-hatch",
149
+ z.object({ innerHasHatch: z.boolean() }),
150
+ async (event, ctx) =>
151
+ ctx.writeAs(
152
+ createSystemUser(event.user.tenantId),
153
+ event.payload.innerHasHatch
154
+ ? "idswitch-probe:write:inner-with-hatch"
155
+ : "idswitch-probe:write:inner-no-hatch",
156
+ {},
157
+ ),
158
+ { access: { roles: ["User"] }, escapeHatch: { reason: "test: outer needs SYSTEM" } },
159
+ );
160
+
161
+ // --- Hooks: gated independently of the handler they fire on ---
162
+ r.writeHandler(
163
+ "hook-target-with-hatch",
164
+ z.object({ label: z.string() }),
165
+ async (event, ctx) => hookThingExecutor.create(event.payload, event.user, ctx.db),
166
+ { access: { roles: ["User"] }, escapeHatch: { reason: "test: handler needs SYSTEM" } },
167
+ );
168
+
169
+ r.writeHandler(
170
+ "hook-target-without-hatch",
171
+ z.object({ label: z.string() }),
172
+ async (event, ctx) => hookThingExecutor.create(event.payload, event.user, ctx.db),
173
+ { access: { roles: ["User"] } },
174
+ );
175
+
176
+ // Hook on the WITH-hatch handler, but the hook itself declares NO
177
+ // escapeHatch — must NOT inherit the handler's grant.
178
+ r.hook(
179
+ "postSave",
180
+ "hook-target-with-hatch",
181
+ async (_result, ctx) => {
182
+ const handlerCtx = ctx as unknown as HandlerContext;
183
+ try {
184
+ await handlerCtx.writeAs(
185
+ createSystemUser(user.tenantId),
186
+ "idswitch-probe:write:whoami-write",
187
+ {},
188
+ );
189
+ hookNoEscapeHatchOutcomes.push({ threw: false });
190
+ } catch (err) {
191
+ hookNoEscapeHatchOutcomes.push({ threw: true });
192
+ throw err;
193
+ }
194
+ },
195
+ { phase: HookPhases.inTransaction },
196
+ );
197
+
198
+ // Hook on the WITHOUT-hatch handler, but the hook itself DOES declare
199
+ // escapeHatch — must reach SYSTEM even though the handler can't.
200
+ r.hook(
201
+ "postSave",
202
+ "hook-target-without-hatch",
203
+ async (_result, ctx) => {
204
+ const handlerCtx = ctx as unknown as HandlerContext;
205
+ const res = await handlerCtx.writeAs(
206
+ createSystemUser(user.tenantId),
207
+ "idswitch-probe:write:whoami-write",
208
+ {},
209
+ );
210
+ hookWithEscapeHatchOutcomes.push({ ok: res.isSuccess });
211
+ },
212
+ { phase: HookPhases.inTransaction, escapeHatch: { reason: "test: hook needs SYSTEM" } },
213
+ );
214
+ });
215
+
216
+ const systemScopeFeature = defineFeature("idswitch-probe-system", (r) => {
217
+ r.systemScope();
218
+
219
+ r.writeHandler(
220
+ "write-as-system",
221
+ z.object({}),
222
+ async (event, ctx) =>
223
+ ctx.writeAs(createSystemUser(event.user.tenantId), "idswitch-probe:write:whoami-write", {}),
224
+ { access: { roles: ["Admin"] } },
225
+ );
226
+ });
227
+
228
+ let stack: TestStack;
229
+
230
+ beforeAll(async () => {
231
+ stack = await setupTestStack({ features: [probeFeature, systemScopeFeature] });
232
+ await unsafeCreateEntityTable(stack.db, hookThingEntity);
233
+ });
234
+
235
+ afterAll(async () => {
236
+ await stack.cleanup();
237
+ });
238
+
239
+ describe("ctx.writeAs(SYSTEM, ...) — gated by the calling handler's escapeHatch", () => {
240
+ test("WITHOUT escapeHatch: fails with access_denied", async () => {
241
+ const err = await stack.http.writeErr(
242
+ "idswitch-probe:write:write-as-system-no-hatch",
243
+ {},
244
+ user,
245
+ );
246
+ expect(err.code).toBe("access_denied");
247
+ });
248
+
249
+ test("WITH escapeHatch: succeeds", async () => {
250
+ const result = await stack.http.writeOk<{ roles: readonly string[] }>(
251
+ "idswitch-probe:write:write-as-system-with-hatch",
252
+ {},
253
+ user,
254
+ );
255
+ expect(result.roles).toContain("system");
256
+ });
257
+ });
258
+
259
+ describe("ctx.queryAs(SYSTEM, ...) — gated by the calling handler's escapeHatch", () => {
260
+ test("WITHOUT escapeHatch: fails with access_denied", async () => {
261
+ const err = await stack.http.queryErr(
262
+ "idswitch-probe:query:query-as-system-no-hatch",
263
+ {},
264
+ user,
265
+ );
266
+ expect(err.code).toBe("access_denied");
267
+ });
268
+
269
+ test("WITH escapeHatch: succeeds", async () => {
270
+ const result = await stack.http.queryOk<{ roles: readonly string[] }>(
271
+ "idswitch-probe:query:query-as-system-with-hatch",
272
+ {},
273
+ user,
274
+ );
275
+ expect(result.roles).toContain("system");
276
+ });
277
+ });
278
+
279
+ describe("ctx.queryAs(nonSystemUser, ...) — never gated by this feature", () => {
280
+ test("delegates to the target's own access check: denied when the target user lacks the role", async () => {
281
+ const err = await stack.http.queryErr(
282
+ "idswitch-probe:query:query-as-normal-user",
283
+ { granted: false },
284
+ user,
285
+ );
286
+ expect(err.code).toBe("access_denied");
287
+ });
288
+
289
+ test("delegates to the target's own access check: allowed when the target user has the role", async () => {
290
+ const result = await stack.http.queryOk<{ ok: boolean }>(
291
+ "idswitch-probe:query:query-as-normal-user",
292
+ { granted: true },
293
+ user,
294
+ );
295
+ expect(result.ok).toBe(true);
296
+ });
297
+ });
298
+
299
+ describe("r.systemScope() feature — no escapeHatch needed", () => {
300
+ test("writeAs(SYSTEM) succeeds from a systemScope handler", async () => {
301
+ const result = await stack.http.writeOk<{ roles: readonly string[] }>(
302
+ "idswitch-probe-system:write:write-as-system",
303
+ {},
304
+ TestUsers.admin,
305
+ );
306
+ expect(result.roles).toContain("system");
307
+ });
308
+ });
309
+
310
+ describe("nested identity switches — no transitive grant", () => {
311
+ test("outer-with-hatch -> inner-no-hatch -> target: overall fails with access_denied", async () => {
312
+ const err = await stack.http.writeErr(
313
+ "idswitch-probe:write:outer-with-hatch",
314
+ { innerHasHatch: false },
315
+ user,
316
+ );
317
+ expect(err.code).toBe("access_denied");
318
+ });
319
+
320
+ test("outer-with-hatch -> inner-with-hatch -> target: succeeds (control)", async () => {
321
+ const result = await stack.http.writeOk<{ roles: readonly string[] }>(
322
+ "idswitch-probe:write:outer-with-hatch",
323
+ { innerHasHatch: true },
324
+ user,
325
+ );
326
+ expect(result.roles).toContain("system");
327
+ });
328
+ });
329
+
330
+ describe("hooks are gated independently of the handler they fire on", () => {
331
+ test("hook WITHOUT escapeHatch does not inherit the handler's grant: overall write fails", async () => {
332
+ hookNoEscapeHatchOutcomes.length = 0;
333
+ const err = await stack.http.writeErr(
334
+ "idswitch-probe:write:hook-target-with-hatch",
335
+ { label: "should-roll-back" },
336
+ user,
337
+ );
338
+ expect(err.code).toBe("access_denied");
339
+ expect(hookNoEscapeHatchOutcomes).toEqual([{ threw: true }]);
340
+ });
341
+
342
+ test("hook WITH escapeHatch reaches SYSTEM even when the handler itself has none", async () => {
343
+ hookWithEscapeHatchOutcomes.length = 0;
344
+ const result = await stack.http.writeOk<{ id: string }>(
345
+ "idswitch-probe:write:hook-target-without-hatch",
346
+ { label: "should-commit" },
347
+ user,
348
+ );
349
+ expect(result.id).toBeDefined();
350
+ expect(hookWithEscapeHatchOutcomes).toEqual([{ ok: true }]);
351
+ });
352
+ });
@@ -0,0 +1,195 @@
1
+ import { describe, expect, mock, test } from "bun:test";
2
+ import { createSystemUser, SYSTEM_ROLE, SYSTEM_USER_ID } from "../../engine";
3
+ import type { SessionUser } from "../../engine/types";
4
+ import type { TenantId } from "../../engine/types/identifiers";
5
+ import { AccessDeniedError, FrameworkReasons } from "../../errors";
6
+ import {
7
+ createGatedIdentitySwitch,
8
+ type IdentitySwitch,
9
+ isSystemIdentity,
10
+ isSystemIdentitySwitchAllowed,
11
+ withHookIdentitySwitchGrant,
12
+ } from "../system-identity-switch";
13
+
14
+ const TENANT = "00000000-0000-4000-8000-00000000ab01" as TenantId;
15
+
16
+ const normalUser: SessionUser = { id: crypto.randomUUID(), tenantId: TENANT, roles: ["User"] };
17
+ const systemUserById: SessionUser = { id: SYSTEM_USER_ID, tenantId: TENANT, roles: ["User"] };
18
+ const systemUserByRole: SessionUser = {
19
+ id: crypto.randomUUID(),
20
+ tenantId: TENANT,
21
+ roles: [SYSTEM_ROLE],
22
+ };
23
+
24
+ function makeUngated(): {
25
+ readonly ungated: IdentitySwitch;
26
+ readonly queryAsMock: ReturnType<typeof mock>;
27
+ readonly writeAsMock: ReturnType<typeof mock>;
28
+ } {
29
+ const queryAsMock = mock(async (_user: SessionUser, _qn: string, _payload: unknown) => ({
30
+ ok: true,
31
+ }));
32
+ const writeAsMock = mock(async (_user: SessionUser, _qn: string, _payload: unknown) => ({
33
+ isSuccess: true as const,
34
+ data: { ok: true },
35
+ }));
36
+ return {
37
+ ungated: { queryAs: queryAsMock, writeAs: writeAsMock },
38
+ queryAsMock,
39
+ writeAsMock,
40
+ };
41
+ }
42
+
43
+ describe("isSystemIdentity", () => {
44
+ test("SYSTEM_USER_ID alone counts as SYSTEM", () => {
45
+ expect(isSystemIdentity(systemUserById)).toBe(true);
46
+ });
47
+
48
+ test("SYSTEM_ROLE alone counts as SYSTEM", () => {
49
+ expect(isSystemIdentity(systemUserByRole)).toBe(true);
50
+ });
51
+
52
+ test("a normal user is not SYSTEM", () => {
53
+ expect(isSystemIdentity(normalUser)).toBe(false);
54
+ });
55
+
56
+ test("createSystemUser(...) is SYSTEM", () => {
57
+ expect(isSystemIdentity(createSystemUser(TENANT))).toBe(true);
58
+ });
59
+ });
60
+
61
+ describe("isSystemIdentitySwitchAllowed", () => {
62
+ test("SYSTEM target requires allowSystemIdentity=true", () => {
63
+ expect(isSystemIdentitySwitchAllowed(systemUserById, false)).toBe(false);
64
+ expect(isSystemIdentitySwitchAllowed(systemUserById, true)).toBe(true);
65
+ });
66
+
67
+ test("a non-SYSTEM target is always allowed", () => {
68
+ expect(isSystemIdentitySwitchAllowed(normalUser, false)).toBe(true);
69
+ expect(isSystemIdentitySwitchAllowed(normalUser, true)).toBe(true);
70
+ });
71
+ });
72
+
73
+ describe("createGatedIdentitySwitch", () => {
74
+ test("queryAs to SYSTEM throws AccessDeniedError with the reason code, no asUser/tenantId/payload leak", async () => {
75
+ const { ungated } = makeUngated();
76
+ const gated = createGatedIdentitySwitch('handler "x"', false, ungated);
77
+
78
+ let caught: unknown;
79
+ try {
80
+ await gated.queryAs(systemUserById, "some:target", { secret: "payload-value" });
81
+ } catch (err) {
82
+ caught = err;
83
+ }
84
+
85
+ expect(caught).toBeInstanceOf(AccessDeniedError);
86
+ const error = caught as AccessDeniedError;
87
+ expect(error.details).toEqual({ reason: FrameworkReasons.systemIdentitySwitchDenied });
88
+ expect(error.message).not.toContain(systemUserById.id);
89
+ expect(error.message).not.toContain(TENANT);
90
+ expect(error.message).not.toContain("payload-value");
91
+ });
92
+
93
+ test("writeAs to SYSTEM throws the same way (never falls back to a WriteResult failure)", async () => {
94
+ const { ungated } = makeUngated();
95
+ const gated = createGatedIdentitySwitch('handler "x"', false, ungated);
96
+
97
+ await expect(gated.writeAs(systemUserById, "some:target", {})).rejects.toBeInstanceOf(
98
+ AccessDeniedError,
99
+ );
100
+ });
101
+
102
+ test("allowSystemIdentity=true lets a SYSTEM switch pass through to the ungated pair", async () => {
103
+ const { ungated, queryAsMock, writeAsMock } = makeUngated();
104
+ const gated = createGatedIdentitySwitch('handler "x"', true, ungated);
105
+
106
+ await gated.queryAs(systemUserById, "q", { a: 1 });
107
+ await gated.writeAs(systemUserById, "w", { b: 2 });
108
+
109
+ expect(queryAsMock).toHaveBeenCalledWith(systemUserById, "q", { a: 1 });
110
+ expect(writeAsMock).toHaveBeenCalledWith(systemUserById, "w", { b: 2 });
111
+ });
112
+
113
+ test("a non-system asUser always passes through, regardless of allowSystemIdentity", async () => {
114
+ const { ungated, queryAsMock } = makeUngated();
115
+ const deniedByDefault = createGatedIdentitySwitch('handler "x"', false, ungated);
116
+
117
+ await deniedByDefault.queryAs(normalUser, "q", {});
118
+
119
+ expect(queryAsMock).toHaveBeenCalledWith(normalUser, "q", {});
120
+ });
121
+ });
122
+
123
+ describe("withHookIdentitySwitchGrant", () => {
124
+ test("hook without escapeHatch does NOT inherit the handler's SYSTEM grant", async () => {
125
+ const { ungated } = makeUngated();
126
+ // Simulates dispatch-shared.ts's buildHandlerContext for a handler that
127
+ // itself declared escapeHatch / is systemScope (allowSystemIdentity=true).
128
+ const handlerCtx = { ...createGatedIdentitySwitch('handler "outer"', true, ungated) };
129
+
130
+ const hookCtx = withHookIdentitySwitchGrant(
131
+ handlerCtx,
132
+ 'postSave hook of feature "f"',
133
+ undefined,
134
+ );
135
+
136
+ await expect(hookCtx.queryAs(systemUserById, "q", {})).rejects.toBeInstanceOf(
137
+ AccessDeniedError,
138
+ );
139
+ });
140
+
141
+ test("hook WITH escapeHatch is allowed even when the handler's own grant was false", async () => {
142
+ const { ungated, queryAsMock } = makeUngated();
143
+ // Handler itself has NO escapeHatch (allowSystemIdentity=false).
144
+ const handlerCtx = { ...createGatedIdentitySwitch('handler "outer"', false, ungated) };
145
+
146
+ const hookCtx = withHookIdentitySwitchGrant(handlerCtx, 'postSave hook of feature "f"', {
147
+ reason: "test hook needs SYSTEM",
148
+ });
149
+
150
+ await hookCtx.queryAs(systemUserById, "q", { a: 1 });
151
+ // Reaches the REAL ungated fn — not the handler's own (still-denying) gate.
152
+ expect(queryAsMock).toHaveBeenCalledWith(systemUserById, "q", { a: 1 });
153
+ });
154
+
155
+ test("a non-system asUser is always allowed through the hook's own gate", async () => {
156
+ const { ungated, queryAsMock } = makeUngated();
157
+ const handlerCtx = { ...createGatedIdentitySwitch('handler "outer"', false, ungated) };
158
+ const hookCtx = withHookIdentitySwitchGrant(handlerCtx, "hook", undefined);
159
+
160
+ await hookCtx.queryAs(normalUser, "q", {});
161
+ expect(queryAsMock).toHaveBeenCalledWith(normalUser, "q", {});
162
+ });
163
+
164
+ test("spreading the context ({...ctx}) still resolves back to the ungated pair", async () => {
165
+ const { ungated, queryAsMock } = makeUngated();
166
+ const handlerCtx = { ...createGatedIdentitySwitch('handler "outer"', true, ungated), extra: 1 };
167
+ const spread = { ...handlerCtx };
168
+
169
+ const hookCtx = withHookIdentitySwitchGrant(spread, "hook", { reason: "needs system" });
170
+ await hookCtx.queryAs(systemUserById, "q", {});
171
+
172
+ expect(queryAsMock).toHaveBeenCalledWith(systemUserById, "q", {});
173
+ });
174
+
175
+ test("a context without queryAs/writeAs is returned unchanged", () => {
176
+ const plainContext = { db: {} };
177
+ const result = withHookIdentitySwitchGrant(plainContext, "hook", { reason: "x" });
178
+ expect(result).toBe(plainContext);
179
+ });
180
+
181
+ test("an ungated context not built by the dispatcher bridge fails closed (unit-test stub)", async () => {
182
+ const stubQueryAs = mock(async (_user: SessionUser, _qn: string, _payload: unknown) => "ok");
183
+ const stubContext = { queryAs: stubQueryAs };
184
+
185
+ const hookCtx = withHookIdentitySwitchGrant(stubContext, "hook", undefined);
186
+ await expect(hookCtx.queryAs(systemUserById, "q", {})).rejects.toBeInstanceOf(
187
+ AccessDeniedError,
188
+ );
189
+ expect(stubQueryAs).not.toHaveBeenCalled();
190
+
191
+ const allowedHookCtx = withHookIdentitySwitchGrant(stubContext, "hook", { reason: "x" });
192
+ await allowedHookCtx.queryAs(systemUserById, "q", {});
193
+ expect(stubQueryAs).toHaveBeenCalledWith(systemUserById, "q", {});
194
+ });
195
+ });