Skip to content

Commit 2f1dace

Browse files
committed
test(dogfood): pin the hook capability refusal at the real data door
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5o5GCE3vGxq6G24dZu
1 parent 272753b commit 2f1dace

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// [#26903] A hook refused for a missing capability, end to end through the real
4+
// stack: QuickJS capability gate → ObjectQL triggerHooks → the data door → the
5+
// HTTP body a client reads.
6+
//
7+
// The gate stays as strict as it is. What this file pins is the ANSWER: a save
8+
// on an object whose `beforeInsert` body calls `ctx.api` without `api.read`
9+
// used to get `500 INTERNAL_ERROR` ("Internal server error"), telling neither
10+
// the caller nor the author which hook or which capability. It is a refusal
11+
// now, `403 INSUFFICIENT_PRIVILEGES` (a standard catalog code), naming both,
12+
// and nothing is written. Under `onError: 'log'` the save proceeds. The same
13+
// body WITH the capability runs.
14+
//
15+
// The composition pins (every gate, the log line) live in
16+
// `packages/runtime/src/sandbox/hook-capability-denial-is-a-refusal.integration.test.ts`;
17+
// this file is the one place the whole HTTP chain runs.
18+
19+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
20+
import { bootStack, type VerifyStack } from '@objectstack/verify';
21+
import { defineStack } from '@objectstack/spec';
22+
import { ObjectSchema, Field } from '@objectstack/spec/data';
23+
24+
const READ_SOURCE = "const rows = await ctx.api.object('hcr_note').find({}); ctx.input.title = ctx.input.title + ' (' + rows.length + ')';";
25+
26+
const note = (name: string, label: string) =>
27+
ObjectSchema.create({ name, label, fields: { title: Field.text({ label: 'Title', required: true }) } });
28+
29+
const hcrStack = defineStack({
30+
manifest: {
31+
id: 'com.dogfood.hook-capability-refusal',
32+
namespace: 'hcr',
33+
version: '0.0.0',
34+
type: 'app',
35+
name: 'Hook Capability Refusal Fixture',
36+
description: 'Sandboxed hooks calling ctx.api with and without the api.read capability.',
37+
},
38+
objects: [note('hcr_note', 'Note'), note('hcr_logged', 'Logged'), note('hcr_granted', 'Granted')],
39+
hooks: [
40+
{
41+
name: 'hcr_note_guard',
42+
object: 'hcr_note',
43+
events: ['beforeInsert'],
44+
body: { language: 'js', source: READ_SOURCE, capabilities: [] },
45+
},
46+
{
47+
name: 'hcr_logged_guard',
48+
object: 'hcr_logged',
49+
events: ['beforeInsert'],
50+
onError: 'log',
51+
body: { language: 'js', source: READ_SOURCE, capabilities: [] },
52+
},
53+
{
54+
name: 'hcr_granted_guard',
55+
object: 'hcr_granted',
56+
events: ['beforeInsert'],
57+
body: { language: 'js', source: READ_SOURCE, capabilities: ['api.read'] },
58+
},
59+
],
60+
});
61+
62+
describe('objectstack verify: a hook refused for a missing capability (#26903)', () => {
63+
let stack: VerifyStack;
64+
let token: string;
65+
66+
beforeAll(async () => {
67+
stack = await bootStack(hcrStack);
68+
token = await stack.signIn();
69+
}, 60_000);
70+
71+
afterAll(async () => {
72+
await stack?.stop();
73+
});
74+
75+
const titles = async (object: string): Promise<string[]> => {
76+
const r = await stack.apiAs(token, 'GET', `/data/${object}`);
77+
expect(r.status).toBe(200);
78+
const body = (await r.json()) as any;
79+
return (body.records ?? body.data ?? []).map((row: any) => row.title);
80+
};
81+
82+
it('onError: abort — POST answers 403 INSUFFICIENT_PRIVILEGES naming the hook and api.read', async () => {
83+
const r = await stack.apiAs(token, 'POST', '/data/hcr_note', { title: 'refused' });
84+
const body = (await r.json()) as any;
85+
expect(r.status, JSON.stringify(body)).toBe(403);
86+
expect(body.code).toBe('INSUFFICIENT_PRIVILEGES');
87+
expect(body.error).toContain("capability 'api.read' not granted to hook 'hcr_note_guard'");
88+
expect(body.error).toContain("adding 'api.read' to the hook's body.capabilities");
89+
expect(JSON.stringify(body)).not.toMatch(/threw:|SandboxError|Internal server error/);
90+
});
91+
92+
it('ground truth: the refused save wrote nothing', async () => {
93+
expect(await titles('hcr_note')).toEqual([]);
94+
});
95+
96+
it('onError: log — the save proceeds', async () => {
97+
const r = await stack.apiAs(token, 'POST', '/data/hcr_logged', { title: 'logged' });
98+
expect(r.status, await r.clone().text()).toBeLessThan(300);
99+
expect(await titles('hcr_logged')).toEqual(['logged']);
100+
});
101+
102+
it('control — the same body WITH api.read runs', async () => {
103+
const r = await stack.apiAs(token, 'POST', '/data/hcr_granted', { title: 'granted' });
104+
expect(r.status, await r.clone().text()).toBeLessThan(300);
105+
expect(await titles('hcr_granted')).toEqual(['granted (0)']);
106+
});
107+
});

0 commit comments

Comments
 (0)