Skip to content

Commit 125a00c

Browse files
committed
wip(spec): the connector read withholds 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 91b48bc commit 125a00c

4 files changed

Lines changed: 348 additions & 20 deletions

File tree

‎packages/metadata-protocol/src/protocol.diff-credential-redaction.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,75 @@ describe('#8671 — anti-vacuity: ablate the redactor', () => {
331331
expect(label.to).toBe('B');
332332
});
333333
});
334+
335+
describe('the connector history diff withholds root keys the entry shape does not declare', () => {
336+
const ROOT_V1 = 'TEST_PLACEHOLDER_ROOT_KEY_V1';
337+
const ROOT_V2 = 'TEST_PLACEHOLDER_ROOT_KEY_V2';
338+
const connector = (label: string, extra: Record<string, unknown>) => ({
339+
name: 'vendor_api',
340+
label,
341+
type: 'api',
342+
provider: 'rest',
343+
providerConfig: { baseUrl: 'https://api.vendor.example/v1' },
344+
auth: { type: 'bearer', credentialRef: 'VENDOR_API_TOKEN' },
345+
...extra,
346+
});
347+
const BUILTIN_CONNECTOR_REDACTOR = getMetadataTypeRedactor('connector')!;
348+
afterEach(() => registerMetadataTypeRedactor('connector', BUILTIN_CONNECTOR_REDACTOR));
349+
350+
async function connectorDiff(from: number, to: number) {
351+
const { engine, tables } = makeStubEngine();
352+
const base = { organization_id: null, type: 'connector', name: 'vendor_api' };
353+
const bodies = [
354+
connector('Vendor', {}),
355+
connector('Vendor', { apiKey: ROOT_V1 }),
356+
connector('Vendor API', { apiKey: ROOT_V2 }),
357+
];
358+
bodies.forEach((body, i) => {
359+
tables.sys_metadata_history!.push({
360+
...base,
361+
id: `h_${i + 1}`,
362+
version: i + 1,
363+
event_seq: i + 1,
364+
operation_type: i === 0 ? 'create' : 'update',
365+
metadata: JSON.stringify(body),
366+
checksum: hashSpec(body),
367+
recorded_at: new Date(i + 1).toISOString(),
368+
});
369+
});
370+
const protocol = new ObjectStackProtocolImplementation(engine);
371+
return await protocol.diffMetaItem({ type: 'connector', name: 'vendor_api', fromVersion: from, toVersion: to }) as any;
372+
}
373+
374+
const holdsRootValue = (payload: unknown) =>
375+
allStrings(payload).some((s) => s.includes(ROOT_V1) || s.includes(ROOT_V2));
376+
377+
it('withholds undeclared root keys on read: the history diff, all three buckets', async () => {
378+
const changed = await connectorDiff(2, 3);
379+
const rotated = entry(changed.changed, 'apiKey') as any;
380+
// The change is still reported; neither value is served.
381+
expect(rotated).toBeDefined();
382+
expect(rotated.from).toBeUndefined();
383+
expect(rotated.to).toBeUndefined();
384+
expect((entry(changed.changed, 'label') as any).to).toBe('Vendor API');
385+
expect(holdsRootValue(changed)).toBe(false);
386+
387+
const added = await connectorDiff(1, 2);
388+
expect(entry(added.added, 'apiKey')).toBeDefined();
389+
expect((entry(added.added, 'apiKey') as any).value).toBeUndefined();
390+
expect(holdsRootValue(added)).toBe(false);
391+
392+
const removed = await connectorDiff(2, 1);
393+
expect(entry(removed.removed, 'apiKey')).toBeDefined();
394+
expect((entry(removed.removed, 'apiKey') as any).value).toBeUndefined();
395+
expect(holdsRootValue(removed)).toBe(false);
396+
});
397+
398+
it('ABLATION — with an identity `connector` redactor the diff serves the values again', async () => {
399+
registerMetadataTypeRedactor('connector', (item) => ({ item, redactedKeys: [] }));
400+
const res = await connectorDiff(2, 3);
401+
const rotated = entry(res.changed, 'apiKey') as any;
402+
expect(rotated.from).toBe(ROOT_V1);
403+
expect(rotated.to).toBe(ROOT_V2);
404+
});
405+
});

‎packages/metadata-protocol/src/protocol.metadata-redaction.test.ts‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,3 +1370,144 @@ describe('#20611 — the runtime authoring gate reads a withheld-and-stored secr
13701370
expect(redactedPathsCarriedForward('view', { name: 'v' }, { name: 'v', secret: 'x' })).toEqual([]);
13711371
});
13721372
});
1373+
1374+
// ---------------------------------------------------------------------------
1375+
// The `connector` read: a top-level key the authored entry does not declare
1376+
// ---------------------------------------------------------------------------
1377+
//
1378+
// A row stored while the connector entry was open can hold a top-level key the
1379+
// entry does not declare. Seeded directly, never through `saveMetaItem`: the
1380+
// save door now refuses such a key, so only a direct seed reproduces the row.
1381+
1382+
const ROOT_KEY_VALUE = 'TEST_PLACEHOLDER_ROOT_KEY_VALUE';
1383+
const ROOT_PLAIN_VALUE = 'TEST_PLACEHOLDER_ROOT_PLAIN_VALUE';
1384+
1385+
function storedConnectorBody() {
1386+
return {
1387+
name: 'vendor_api',
1388+
label: 'Vendor API',
1389+
type: 'api',
1390+
provider: 'rest',
1391+
providerConfig: { baseUrl: 'https://api.vendor.example/v1' },
1392+
auth: { type: 'bearer', credentialRef: 'VENDOR_API_TOKEN' },
1393+
apiKey: ROOT_KEY_VALUE,
1394+
region: ROOT_PLAIN_VALUE,
1395+
};
1396+
}
1397+
1398+
function seedConnectorRow(rows: Map<string, Row>) {
1399+
const where = { type: 'connector', name: 'vendor_api', organization_id: null, package_id: null, state: 'active' };
1400+
const body = storedConnectorBody();
1401+
rows.set(keyOf(where), {
1402+
id: 'r_connector',
1403+
...where,
1404+
metadata: JSON.stringify(body),
1405+
checksum: hashSpec(body),
1406+
version: 1,
1407+
} as Row);
1408+
}
1409+
1410+
const storedConnector = (rows: Map<string, Row>) =>
1411+
JSON.parse(Array.from(rows.values()).find((r) => r.type === 'connector' && r.state === 'active')!.metadata);
1412+
1413+
function expectRootKeysWithheld(served: any) {
1414+
expect(served).not.toHaveProperty('apiKey');
1415+
expect(served).not.toHaveProperty('region');
1416+
expect(served.name).toBe('vendor_api');
1417+
expect(served.providerConfig).toEqual({ baseUrl: 'https://api.vendor.example/v1' });
1418+
}
1419+
1420+
function expectNoRootKeyValue(payload: unknown) {
1421+
const strings = allStrings(payload);
1422+
expect(strings.some((s) => s.includes(ROOT_KEY_VALUE) || s.includes(ROOT_PLAIN_VALUE))).toBe(false);
1423+
}
1424+
1425+
const BUILTIN_CONNECTOR_REDACTOR = getMetadataTypeRedactor('connector')!;
1426+
1427+
describe('the connector read withholds root keys the entry shape does not declare', () => {
1428+
afterEach(() => registerMetadataTypeRedactor('connector', BUILTIN_CONNECTOR_REDACTOR));
1429+
1430+
it('withholds undeclared root keys on read: the item read', async () => {
1431+
const { engine, rows } = makeStubEngine();
1432+
seedConnectorRow(rows);
1433+
const protocol = new ObjectStackProtocolImplementation(engine);
1434+
1435+
const res: any = await protocol.getMetaItem({ type: 'connector', name: 'vendor_api' });
1436+
expectRootKeysWithheld(res.item);
1437+
expectNoRootKeyValue(res);
1438+
// The badge is computed on the raw body: the row is still reported invalid.
1439+
expect(res.item._diagnostics.valid).toBe(false);
1440+
expect(storedConnector(rows).apiKey).toBe(ROOT_KEY_VALUE);
1441+
});
1442+
1443+
it('withholds undeclared root keys on read: the list read, both spellings', async () => {
1444+
const { engine, rows } = makeStubEngine();
1445+
seedConnectorRow(rows);
1446+
const protocol = new ObjectStackProtocolImplementation(engine);
1447+
1448+
for (const type of ['connector', 'connectors']) {
1449+
const res: any = await protocol.getMetaItems({ type });
1450+
const item = res.items.find((i: any) => i.name === 'vendor_api');
1451+
expect(item, type).toBeDefined();
1452+
expectRootKeysWithheld(item);
1453+
expectNoRootKeyValue(res);
1454+
}
1455+
});
1456+
1457+
it('withholds undeclared root keys on read: the layered read', async () => {
1458+
const { engine, rows } = makeStubEngine();
1459+
seedConnectorRow(rows);
1460+
const protocol = new ObjectStackProtocolImplementation(engine);
1461+
1462+
const res: any = await protocol.getMetaItemLayered({ type: 'connector', name: 'vendor_api' });
1463+
expect(res.overlay).not.toBeNull();
1464+
expectRootKeysWithheld(res.overlay);
1465+
expectRootKeysWithheld(res.effective);
1466+
expectNoRootKeyValue(res);
1467+
});
1468+
1469+
it('a save of the served form keeps the withheld keys at rest', async () => {
1470+
const { engine, rows } = makeStubEngine();
1471+
seedConnectorRow(rows);
1472+
const protocol = new ObjectStackProtocolImplementation(engine);
1473+
1474+
const served: any = (await protocol.getMetaItem({ type: 'connector', name: 'vendor_api' })).item;
1475+
await protocol.saveMetaItem({
1476+
type: 'connector',
1477+
name: 'vendor_api',
1478+
item: { ...served, label: 'Vendor API (edited)' },
1479+
});
1480+
1481+
const atRest = storedConnector(rows);
1482+
expect(atRest.label).toBe('Vendor API (edited)');
1483+
expect(atRest.apiKey).toBe(ROOT_KEY_VALUE);
1484+
expect(atRest.region).toBe(ROOT_PLAIN_VALUE);
1485+
const reread: any = await protocol.getMetaItem({ type: 'connector', name: 'vendor_api' });
1486+
expectRootKeysWithheld(reread.item);
1487+
});
1488+
1489+
it('an undeclared root key the author types is refused, and the row is unchanged', async () => {
1490+
const { engine, rows } = makeStubEngine();
1491+
seedConnectorRow(rows);
1492+
const protocol = new ObjectStackProtocolImplementation(engine);
1493+
1494+
const served: any = (await protocol.getMetaItem({ type: 'connector', name: 'vendor_api' })).item;
1495+
await expect(protocol.saveMetaItem({
1496+
type: 'connector',
1497+
name: 'vendor_api',
1498+
item: { ...served, apiKey: 'typed-by-hand' },
1499+
})).rejects.toMatchObject({ code: 'INVALID_METADATA', status: 422 });
1500+
expect(storedConnector(rows)).toEqual(storedConnectorBody());
1501+
});
1502+
1503+
it('ABLATION — with an identity `connector` redactor the item, list and layered reads carry the keys again', async () => {
1504+
registerMetadataTypeRedactor('connector', (item) => ({ item, redactedKeys: [] }));
1505+
const { engine, rows } = makeStubEngine();
1506+
seedConnectorRow(rows);
1507+
const protocol = new ObjectStackProtocolImplementation(engine);
1508+
1509+
expect(((await protocol.getMetaItem({ type: 'connector', name: 'vendor_api' })) as any).item.apiKey).toBe(ROOT_KEY_VALUE);
1510+
expect(((await protocol.getMetaItems({ type: 'connector' })) as any).items[0].apiKey).toBe(ROOT_KEY_VALUE);
1511+
expect(((await protocol.getMetaItemLayered({ type: 'connector', name: 'vendor_api' })) as any).effective.apiKey).toBe(ROOT_KEY_VALUE);
1512+
});
1513+
});

‎packages/spec/src/integration/connector-credential-redaction.test.ts‎

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import {
1313
CONNECTOR_CREDENTIAL_MAP_PATHS,
1414
CONNECTOR_CREDENTIAL_URL_QUERY_PARAM_NAMES,
1515
declaredConnectorAuthKeys,
16+
declaredConnectorEntryKeys,
1617
redactConnectorItem,
1718
} from './connector-credential-redaction';
19+
import { DeclarativeConnectorEntrySchema } from './connector.zod';
20+
import { METADATA_READ_DECORATIONS } from '../kernel/metadata-read-decorations';
1821

1922
const P = (n: number) => `TEST_PLACEHOLDER_${n}`;
2023

@@ -224,14 +227,23 @@ describe('the connector redactor — credentials inside a URL under `providerCon
224227
expect(redactConnectorItem(item)).toEqual({ item, redactedKeys: [] });
225228
});
226229

227-
it('arrays and dot-spelled keys are off the walk (the carry-forward cannot address them)', () => {
228-
const item = {
229-
...base,
230-
providerConfig: {
231-
servers: [`https://u:${P(39)}@a.example`],
232-
'v1.url': `https://u:${P(40)}@b.example`,
233-
},
234-
};
230+
it('withholds whole an array holding a URL with credential parts, at any depth', () => {
231+
const { item: served, redactedKeys } = at({
232+
servers: [`https://u:${P(39)}@a.example`, 'https://b.example'],
233+
mirrors: ['https://c.example', `https://d.example/v1?token=${P(42)}`],
234+
transport: { kind: 'http', fallbacks: [{ url: `https://u:${P(43)}@e.example` }] },
235+
include: ['search', 'https://f.example/v1?region=eu'],
236+
});
237+
expect(redactedKeys).toEqual(['providerConfig.mirrors', 'providerConfig.servers', 'providerConfig.transport.fallbacks']);
238+
expect(served.providerConfig).toEqual({
239+
transport: { kind: 'http' },
240+
include: ['search', 'https://f.example/v1?region=eu'],
241+
});
242+
expect(holds(served, P(39), P(42), P(43))).toBe(false);
243+
});
244+
245+
it('a dot-spelled key is off the walk (the carry-forward cannot address it)', () => {
246+
const item = { ...base, providerConfig: { 'v1.url': `https://u:${P(40)}@b.example` } };
235247
expect(redactConnectorItem(item)).toEqual({ item, redactedKeys: [] });
236248
});
237249

@@ -247,6 +259,55 @@ describe('the connector redactor — credentials inside a URL under `providerCon
247259
});
248260
});
249261

262+
describe('the connector redactor — top-level keys the entry does not declare', () => {
263+
it.each([
264+
['a credential-shaped scalar', { apiKey: P(50) }, ['apiKey']],
265+
['several credential-shaped keys', { token: P(51), clientSecret: P(52), password: P(53) }, ['clientSecret', 'password', 'token']],
266+
['a nested container', { credentials: { user: 'svc', secret: P(54) } }, ['credentials']],
267+
['a plain undeclared key', { region: 'eu-west-1' }, ['region']],
268+
] as const)('withholds undeclared root keys on read: %s', (_label, extra, keys) => {
269+
const item = { ...base, providerConfig: { baseUrl: 'https://v.example' }, ...extra };
270+
const { item: served, redactedKeys } = redactConnectorItem(item);
271+
expect(redactedKeys).toEqual(keys);
272+
for (const key of keys) expect(served).not.toHaveProperty(key);
273+
expect(holds(served, P(50), P(51), P(52), P(53), P(54), 'eu-west-1')).toBe(false);
274+
expect(served.providerConfig).toEqual({ baseUrl: 'https://v.example' });
275+
expect(served._diagnostics).toEqual(base._diagnostics);
276+
});
277+
278+
it('the declared set is the closed entry\'s own shape, and every declared key is served as stored', () => {
279+
const shape = (DeclarativeConnectorEntrySchema as unknown as { shape: Record<string, unknown> }).shape;
280+
expect([...declaredConnectorEntryKeys()].sort()).toEqual(Object.keys(shape).sort());
281+
for (const key of ['name', 'label', 'type', 'provider', 'providerConfig', 'auth', 'enabled', '_packageId', '_provenance']) {
282+
expect(declaredConnectorEntryKeys().has(key), key).toBe(true);
283+
}
284+
const item = { ...base, providerConfig: { baseUrl: 'https://v.example' }, retryConfig: { maxAttempts: 3 }, _packageId: 'com.vendor' };
285+
expect(redactConnectorItem(item)).toEqual({ item, redactedKeys: [] });
286+
});
287+
288+
it('the read decorations are the response\'s and are left alone', () => {
289+
const item = { ...base, _draft: true };
290+
for (const key of METADATA_READ_DECORATIONS) expect(item).toHaveProperty(key);
291+
expect(redactConnectorItem(item)).toEqual({ item, redactedKeys: [] });
292+
});
293+
294+
it('a cleared undeclared key is served as written and not reported', () => {
295+
const item = { ...base, apiKey: '', token: null };
296+
expect(redactConnectorItem(item)).toEqual({ item, redactedKeys: [] });
297+
});
298+
299+
it('withholds an undeclared root key beside the other positions, each named once', () => {
300+
const { item: served, redactedKeys } = redactConnectorItem({
301+
...base,
302+
apiKey: P(55),
303+
auth: { type: 'bearer', credentialRef: 'VENDOR_TOKEN', token: P(56) },
304+
providerConfig: { baseUrl: `https://u:${P(57)}@v.example`, headers: { Authorization: P(58) } },
305+
});
306+
expect(redactedKeys).toEqual(['apiKey', 'auth.token', 'providerConfig.baseUrl', 'providerConfig.headers']);
307+
expect(holds(served, P(55), P(56), P(57), P(58))).toBe(false);
308+
});
309+
});
310+
250311
describe('the connector redactor — purity and the untouched remainder', () => {
251312
it('never mutates its input, and leaves every other key (`_diagnostics` included) byte for byte', () => {
252313
const item = {

0 commit comments

Comments
 (0)