Skip to content

Commit fe7a496

Browse files
committed
test(dogfood): the connector read doors withhold root keys the entry shape does not declare
Claude-Session: https://claude.ai/code/session_01AavokzJ5DndAwitDXvKy4U Co-authored-by: Claude <noreply@anthropic.com>
1 parent 125a00c commit fe7a496

1 file changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// The connector metadata read doors, booted on the showcase: a stored row
4+
// holding a top-level key the connector entry does not declare is served
5+
// without that key, and a save of the served form keeps it at rest.
6+
//
7+
// - READ: the row is seeded through the system context (the save door refuses
8+
// such a key, so that is the only way the row is written). The item read,
9+
// the layered view, the list (both spellings) and the history diff serve
10+
// neither the key's value nor the key, to a member and to an administrator.
11+
// - CARRY-FORWARD: a save of the served form succeeds, keeps both keys at rest,
12+
// and the next read still withholds them.
13+
//
14+
// Every value below is an obviously-fake placeholder, and every assertion on
15+
// one is its absence from what a door served, or its presence at rest.
16+
17+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
18+
import { type VerifyStack } from '@objectstack/verify';
19+
import { bootShowcase } from './showcase-boot.js';
20+
21+
const SYSTEM = { isSystem: true, positions: [], permissions: [] };
22+
23+
const PLACEHOLDERS = {
24+
credentialShaped: 'TEST_PLACEHOLDER_ROOT_CREDENTIAL_SHAPED',
25+
plain: 'TEST_PLACEHOLDER_ROOT_PLAIN',
26+
} as const;
27+
const ALL_PLACEHOLDERS = Object.values(PLACEHOLDERS);
28+
29+
const ROW = 'crk_root_keys_stored';
30+
31+
const rowsOf = (r: any): any[] => (Array.isArray(r) ? r : Array.isArray(r?.records) ? r.records : Array.isArray(r?.value) ? r.value : []);
32+
const bodyOf = (row: any): Record<string, any> =>
33+
typeof row?.metadata === 'string' ? JSON.parse(row.metadata) : (row?.metadata ?? {});
34+
const itemOf = (json: any): Record<string, any> => (json?.item ?? json?.data?.item ?? json?.data ?? json) as Record<string, any>;
35+
const holdsAPlaceholder = (v: unknown): boolean => {
36+
const text = typeof v === 'string' ? v : JSON.stringify(v ?? null);
37+
return ALL_PLACEHOLDERS.some((p) => text.includes(p));
38+
};
39+
40+
describe('connector metadata read doors: root keys the entry does not declare are withheld', () => {
41+
let stack: VerifyStack;
42+
let admin: string;
43+
let member: string;
44+
let ql: any;
45+
46+
const stored = async (name: string) =>
47+
rowsOf(await ql.find('sys_metadata', { where: { type: 'connector', name }, context: SYSTEM }));
48+
49+
const instance = (name: string, over: Record<string, unknown> = {}) => ({
50+
name,
51+
label: `CRK ${name}`,
52+
type: 'api',
53+
description: 'A provider-bound instance for the root-key read pins.',
54+
provider: 'rest',
55+
providerConfig: { baseUrl: 'http://127.0.0.1:9/v1' },
56+
auth: { type: 'bearer', credentialRef: 'CRK_CREDENTIAL_REF' },
57+
...over,
58+
});
59+
60+
/** One door read: 200, names the connector (positive control), and serves no placeholder. */
61+
const expectWithheld = async (token: string, path: string, names: readonly string[]) => {
62+
const res = await stack.apiAs(token, 'GET', path);
63+
const text = await res.text();
64+
expect(res.status, `${path}: ${text.slice(0, 200)}`).toBe(200);
65+
for (const name of names) expect(text.includes(name), `${path} names ${name}`).toBe(true);
66+
expect(holdsAPlaceholder(text), `${path} withholds every undeclared root key`).toBe(false);
67+
return text;
68+
};
69+
70+
const expectRootKeysAbsent = (served: Record<string, any>, at: string) => {
71+
expect(served, at).not.toHaveProperty('apiKey');
72+
expect(served, at).not.toHaveProperty('region');
73+
expect(served.provider, at).toBe('rest');
74+
};
75+
76+
beforeAll(async () => {
77+
stack = await bootShowcase({});
78+
ql = await stack.kernel.getServiceAsync<any>('objectql');
79+
admin = await stack.signIn();
80+
member = await stack.signUp('crk-member@connector-root-keys.test');
81+
82+
// A clean save makes the row; the system context then writes the legacy body over it.
83+
const put = await stack.apiAs(admin, 'PUT', `/meta/connector/${ROW}`, instance(ROW));
84+
expect(put.status, await put.clone().text()).toBe(200);
85+
const [row] = await stored(ROW);
86+
await ql.update('sys_metadata', {
87+
id: row.id,
88+
metadata: JSON.stringify({
89+
...instance(ROW, { label: 'CRK root keys (seeded)' }),
90+
apiKey: PLACEHOLDERS.credentialShaped,
91+
region: PLACEHOLDERS.plain,
92+
}),
93+
}, { context: SYSTEM });
94+
}, 180_000);
95+
96+
afterAll(async () => {
97+
await stack?.stop?.();
98+
});
99+
100+
it('guard the guard: the stored row holds both undeclared root keys', async () => {
101+
const body = bodyOf((await stored(ROW))[0]);
102+
expect(body.label).toBe('CRK root keys (seeded)');
103+
expect(body.apiKey).toBe(PLACEHOLDERS.credentialShaped);
104+
expect(body.region).toBe(PLACEHOLDERS.plain);
105+
});
106+
107+
it('withholds undeclared root keys on read: the item read and the layered view, for a member and an administrator', async () => {
108+
for (const token of [member, admin]) {
109+
const text = await expectWithheld(token, `/meta/connector/${ROW}`, [ROW, 'CRK root keys (seeded)']);
110+
const served = itemOf(JSON.parse(text));
111+
expectRootKeysAbsent(served, 'item');
112+
expect(served.providerConfig).toEqual({ baseUrl: 'http://127.0.0.1:9/v1' });
113+
114+
const layered = JSON.parse(await expectWithheld(token, `/meta/connector/${ROW}/layers`, [ROW]));
115+
const layers = layered?.data ?? layered;
116+
expectRootKeysAbsent(layers.effective, 'layers.effective');
117+
if (layers.overlay) expectRootKeysAbsent(layers.overlay, 'layers.overlay');
118+
}
119+
});
120+
121+
it('withholds undeclared root keys on read: the list, both spellings, for a member and an administrator', async () => {
122+
for (const token of [member, admin]) {
123+
await expectWithheld(token, '/meta/connector', [ROW]);
124+
await expectWithheld(token, '/meta/connectors', [ROW]);
125+
}
126+
});
127+
128+
it('a save of the served form keeps the keys at rest; the next read and the history diff still withhold them', async () => {
129+
const read = await stack.apiAs(admin, 'GET', `/meta/connector/${ROW}`);
130+
const served = itemOf(await read.json());
131+
expectRootKeysAbsent(served, 'served');
132+
133+
const body = Object.fromEntries(Object.entries(served).filter(([k]) => !k.startsWith('_')));
134+
const resave = await stack.apiAs(admin, 'PUT', `/meta/connector/${ROW}`, { ...body, label: 'CRK root keys (edited)' });
135+
expect(resave.status, await resave.clone().text()).toBe(200);
136+
137+
const atRest = bodyOf((await stored(ROW))[0]);
138+
expect(atRest.label).toBe('CRK root keys (edited)');
139+
expect(atRest.apiKey).toBe(PLACEHOLDERS.credentialShaped);
140+
expect(atRest.region).toBe(PLACEHOLDERS.plain);
141+
142+
const reread = itemOf(JSON.parse(await expectWithheld(admin, `/meta/connector/${ROW}`, ['CRK root keys (edited)'])));
143+
expectRootKeysAbsent(reread, 'reread');
144+
145+
// The history now holds the clean first save and the re-save that kept the keys.
146+
const diffText = await expectWithheld(admin, `/meta/connector/${ROW}/diff`, ['CRK root keys (edited)']);
147+
const diff = JSON.parse(diffText);
148+
const answer = diff?.data ?? diff;
149+
const added = (answer.added ?? []) as Array<{ path: string; value?: unknown }>;
150+
const apiKey = added.find((e) => e.path === 'apiKey');
151+
expect(apiKey, diffText.slice(0, 400)).toBeDefined();
152+
expect(apiKey!.value).toBeUndefined();
153+
});
154+
155+
it('the stored row is never redacted: redaction is a serving act', async () => {
156+
const body = bodyOf((await stored(ROW))[0]);
157+
expect(body.apiKey).toBe(PLACEHOLDERS.credentialShaped);
158+
expect(body.region).toBe(PLACEHOLDERS.plain);
159+
});
160+
});

0 commit comments

Comments
 (0)