Skip to content

Commit 9fe4e5f

Browse files
committed
feat(spec): the connector read withholds cleared and dot-spelled keys; carry-forward keeps dot-spelled root keys at rest
Claude-Session: https://claude.ai/code/session_01AavokzJ5DndAwitDXvKy4U Co-authored-by: Claude <noreply@anthropic.com>
1 parent acbd2ec commit 9fe4e5f

5 files changed

Lines changed: 295 additions & 32 deletions

File tree

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

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,41 @@ export function redactedPathsCarriedForward(type: string, incoming: unknown, sto
516516
* {@link redactedPathsCarriedForward}: the grafted body, and where each graft
517517
* landed. `landed` is empty exactly when `out` is `incoming` by reference.
518518
*/
519+
/**
520+
* A `redactedKeys` entry resolved against the STORED body into the container
521+
* hops that reach the key and the key itself — which may carry dots.
522+
*
523+
* `redactedKeys` joins each path on dots, so a key whose own name carries a dot
524+
* (a connector root key spelled `v1.url`, or one held under `providerConfig`)
525+
* is indistinguishable from a nested path by the string alone. The two are told
526+
* apart by what the stored body actually holds, trying the partitions in a
527+
* fixed order: the standard split FIRST — every dot a separator, the only
528+
* reading before dotted keys were withheld — then progressively longer trailing
529+
* keys, ending with the whole string as a single key. The first partition whose
530+
* stored container holds the key wins, so a nested position always shadows a
531+
* dotted-key alias of the same spelling (the alias is then left to the author's
532+
* word, never carried onto the wrong position). `undefined` when no partition
533+
* reaches a present value — the caller reads that as "nothing at rest to carry".
534+
*/
535+
function resolveRedactedPath(
536+
stored: unknown,
537+
path: string,
538+
): { hops: PathHop[]; key: string } | undefined {
539+
const segments = path.split('.');
540+
for (let cut = segments.length - 1; cut >= 0; cut -= 1) {
541+
const key = segments.slice(cut).join('.');
542+
// resolveHops reads the last entry as the key and the rest as container
543+
// hops, so a placeholder last entry leaves exactly `cut` container hops.
544+
const hops = resolveHops(stored, [...segments.slice(0, cut), key]);
545+
if (!hops) continue;
546+
const container = containerAt(stored, hops);
547+
if (container && Object.prototype.hasOwnProperty.call(container, key) && container[key] !== undefined) {
548+
return { hops, key };
549+
}
550+
}
551+
return undefined;
552+
}
553+
519554
function planCarryForward<T>(type: string, incoming: T, stored: unknown): { out: T; landed: string[] } {
520555
const unchanged = { out: incoming, landed: [] as string[] };
521556
if (!incoming || typeof incoming !== 'object' || Array.isArray(incoming)) return unchanged;
@@ -535,11 +570,15 @@ function planCarryForward<T>(type: string, incoming: T, stored: unknown): { out:
535570
// Dotted, item-relative — the registry's documented contract for
536571
// `redactedKeys` (`config.password`; an array hop is an index into the
537572
// stored body, `nodes.0.config.secret`, resolved to an identity by
538-
// {@link resolveHops}).
539-
const segments = path.split('.');
540-
const key = segments[segments.length - 1] as string;
541-
const hops = resolveHops(stored, segments);
542-
if (!hops) continue;
573+
// {@link resolveHops}). A key whose OWN name carries a dot (a connector
574+
// root key spelled `v1.url`, or one under `providerConfig`) cannot be
575+
// told from a nested path by the join alone, so the split is tried
576+
// first — every dot a separator, the existing reading — and only when
577+
// that resolves to nothing at rest is a longer trailing key tried,
578+
// ending with the whole string as a single key. {@link resolveRedactedPath}.
579+
const resolved = resolveRedactedPath(stored, path);
580+
if (!resolved) continue;
581+
const { hops, key } = resolved;
543582

544583
const storedParent = containerAt(stored, hops);
545584
const storedValue = storedParent?.[key];

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,118 @@ describe('the connector read withholds root keys the entry shape does not declar
15121512
});
15131513
});
15141514

1515+
// ---------------------------------------------------------------------------
1516+
// The carry-forward keeps a withheld key whose own name carries a dot
1517+
// ---------------------------------------------------------------------------
1518+
//
1519+
// `redactedKeys` joins each path on dots, so a key spelled `v1.url` reads like
1520+
// a nested path `v1` → `url`. A save of the served form must still keep it at
1521+
// rest: the carry-forward tries the standard split first, then the whole
1522+
// string as a single key.
1523+
1524+
const DOTTED_ROOT_VALUE = 'TEST_PLACEHOLDER_DOTTED_ROOT_VALUE';
1525+
const DOTTED_PROVIDER_URL = 'https://svc:TEST_PLACEHOLDER_DOTTED_PROVIDER@api.vendor.example/v1';
1526+
1527+
function storedDottedConnectorBody() {
1528+
return {
1529+
name: 'vendor_api',
1530+
label: 'Vendor API',
1531+
type: 'api',
1532+
provider: 'rest',
1533+
providerConfig: { baseUrl: 'https://api.vendor.example/v1', 'v1.url': DOTTED_PROVIDER_URL },
1534+
auth: { type: 'bearer', credentialRef: 'VENDOR_API_TOKEN' },
1535+
'x.y': DOTTED_ROOT_VALUE,
1536+
};
1537+
}
1538+
1539+
function seedDottedConnectorRow(rows: Map<string, Row>) {
1540+
const where = { type: 'connector', name: 'vendor_api', organization_id: null, package_id: null, state: 'active' };
1541+
const body = storedDottedConnectorBody();
1542+
rows.set(keyOf(where), {
1543+
id: 'r_connector_dotted', ...where, metadata: JSON.stringify(body), checksum: hashSpec(body), version: 1,
1544+
} as Row);
1545+
}
1546+
1547+
describe('the connector carry-forward keeps a withheld dot-spelled key at rest', () => {
1548+
const BUILTIN_CONNECTOR_REDACTOR = getMetadataTypeRedactor('connector')!;
1549+
afterEach(() => registerMetadataTypeRedactor('connector', BUILTIN_CONNECTOR_REDACTOR));
1550+
1551+
it('the served read withholds the dot-spelled root key and the dot-spelled providerConfig URL', async () => {
1552+
const { engine, rows } = makeStubEngine();
1553+
seedDottedConnectorRow(rows);
1554+
const protocol = new ObjectStackProtocolImplementation(engine);
1555+
const served: any = (await protocol.getMetaItem({ type: 'connector', name: 'vendor_api' })).item;
1556+
expect(served['x.y']).toBeUndefined();
1557+
expect(served.providerConfig['v1.url']).toBeUndefined();
1558+
expect(served.providerConfig.baseUrl).toBe('https://api.vendor.example/v1');
1559+
expect(allStrings(served)).not.toContain(DOTTED_ROOT_VALUE);
1560+
});
1561+
1562+
it('carryForwardRedactedValues restores both dot-spelled positions into a served body', () => {
1563+
const stored = storedDottedConnectorBody();
1564+
const served: any = redactMetadataItem('connector', stored);
1565+
const out: any = carryForwardRedactedValues('connector', served, stored);
1566+
expect(out['x.y']).toBe(DOTTED_ROOT_VALUE);
1567+
expect(out.providerConfig['v1.url']).toBe(DOTTED_PROVIDER_URL);
1568+
// redactedPathsCarriedForward names the same positions it fills.
1569+
expect(redactedPathsCarriedForward('connector', served, stored).sort())
1570+
.toEqual(['providerConfig.v1.url', 'x.y']);
1571+
});
1572+
1573+
it('a save of the served form keeps both dot-spelled values at rest', async () => {
1574+
const { engine, rows } = makeStubEngine();
1575+
seedDottedConnectorRow(rows);
1576+
const protocol = new ObjectStackProtocolImplementation(engine);
1577+
const served: any = (await protocol.getMetaItem({ type: 'connector', name: 'vendor_api' })).item;
1578+
await protocol.saveMetaItem({ type: 'connector', name: 'vendor_api', item: { ...served, label: 'Vendor API (edited)' } });
1579+
const atRest = storedConnector(rows);
1580+
expect(atRest.label).toBe('Vendor API (edited)');
1581+
expect(atRest['x.y']).toBe(DOTTED_ROOT_VALUE);
1582+
expect(atRest.providerConfig['v1.url']).toBe(DOTTED_PROVIDER_URL);
1583+
});
1584+
1585+
it('the author`s own value wins — a new dot-spelled providerConfig URL is not refused by the entry (it is not a top-level key) and replaces the stored one', async () => {
1586+
// A dot-spelled key UNDER providerConfig is a provider setting, not a
1587+
// top-level entry key, so the closed entry does not refuse it; the
1588+
// author editing it is the author's word.
1589+
const { engine, rows } = makeStubEngine();
1590+
seedDottedConnectorRow(rows);
1591+
const protocol = new ObjectStackProtocolImplementation(engine);
1592+
const served: any = (await protocol.getMetaItem({ type: 'connector', name: 'vendor_api' })).item;
1593+
const edited = { ...served, providerConfig: { ...served.providerConfig, 'v1.url': 'https://api.vendor.example/v2' } };
1594+
await protocol.saveMetaItem({ type: 'connector', name: 'vendor_api', item: edited });
1595+
expect(storedConnector(rows).providerConfig['v1.url']).toBe('https://api.vendor.example/v2');
1596+
});
1597+
1598+
it('the dotted-alias ambiguity is explicit: a nested position and a same-spelled dotted key collide, and neither is carried', () => {
1599+
// Two stored positions whose `redactedKeys` entry is the IDENTICAL
1600+
// string `providerConfig.v1.url`: the nested `{ v1: { url } }` (its URL
1601+
// rewritten in place) and the literal `'v1.url'` (withheld whole). The
1602+
// two cannot be told apart from one dotted string, so the carry-forward
1603+
// refuses to guess — its two-onto-one guard carries NEITHER rather than
1604+
// graft a stored credential onto the wrong position. The read stays
1605+
// fail-closed (both secrets absent from what is served); a save of the
1606+
// served form does not restore either, which is the honest outcome of a
1607+
// genuinely ambiguous spelling.
1608+
const nestedUrl = 'https://svc:TEST_PLACEHOLDER_NESTED@api.vendor.example/nested';
1609+
const literalUrl = 'https://svc:TEST_PLACEHOLDER_LITERAL@api.vendor.example/literal';
1610+
const stored = {
1611+
name: 'vendor_api', label: 'Vendor API', type: 'api', provider: 'rest',
1612+
auth: { type: 'bearer', credentialRef: 'VENDOR_API_TOKEN' },
1613+
providerConfig: { v1: { url: nestedUrl }, 'v1.url': literalUrl },
1614+
};
1615+
const served: any = redactMetadataItem('connector', stored);
1616+
expect(allStrings(served)).not.toContain('TEST_PLACEHOLDER_NESTED');
1617+
expect(allStrings(served)).not.toContain('TEST_PLACEHOLDER_LITERAL');
1618+
// The same string names both withheld positions.
1619+
const redactedForStored = (getMetadataTypeRedactor('connector')!)(stored as any).redactedKeys;
1620+
expect(redactedForStored.filter((p) => p === 'providerConfig.v1.url')).toHaveLength(2);
1621+
// The collision trips the two-onto-one guard: nothing is carried.
1622+
expect(redactedPathsCarriedForward('connector', served, stored)).toEqual([]);
1623+
expect(carryForwardRedactedValues('connector', served, stored)).toBe(served);
1624+
});
1625+
});
1626+
15151627
describe('the read exits withhold the declared ssl secret member of a datasource', () => {
15161628
/** The stored client key, which no read exit may serve; the public members it sits beside. */
15171629
const SSL_KEY = 'TEST_PLACEHOLDER_SSL_KEY';

‎packages/qa/dogfood/test/connector-meta-door-root-key-read.dogfood.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ const SYSTEM = { isSystem: true, positions: [], permissions: [] };
2323
const PLACEHOLDERS = {
2424
credentialShaped: 'TEST_PLACEHOLDER_ROOT_CREDENTIAL_SHAPED',
2525
plain: 'TEST_PLACEHOLDER_ROOT_PLAIN',
26+
dottedRoot: 'TEST_PLACEHOLDER_DOTTED_ROOT',
27+
dottedUrl: 'TEST_PLACEHOLDER_DOTTED_PROVIDER_URL',
2628
} as const;
2729
const ALL_PLACEHOLDERS = Object.values(PLACEHOLDERS);
2830

2931
const ROW = 'crk_root_keys_stored';
32+
const DOTTED = 'crk_dotted_keys_stored';
3033

3134
const rowsOf = (r: any): any[] => (Array.isArray(r) ? r : Array.isArray(r?.records) ? r.records : Array.isArray(r?.value) ? r.value : []);
3235
const bodyOf = (row: any): Record<string, any> =>
@@ -157,4 +160,57 @@ describe('connector metadata read doors: root keys the entry does not declare ar
157160
expect(body.apiKey).toBe(PLACEHOLDERS.credentialShaped);
158161
expect(body.region).toBe(PLACEHOLDERS.plain);
159162
});
163+
164+
it('a cleared undeclared root key is withheld so a plain read then save answers 200, not 422', async () => {
165+
const put = await stack.apiAs(admin, 'PUT', `/meta/connector/crk_cleared`, instance('crk_cleared'));
166+
expect(put.status, await put.clone().text()).toBe(200);
167+
const [row] = await stored('crk_cleared');
168+
await ql.update('sys_metadata', {
169+
id: row.id,
170+
metadata: JSON.stringify({ ...instance('crk_cleared', { label: 'CRK cleared (seeded)' }), apiKey: '', extra: {} }),
171+
}, { context: SYSTEM });
172+
173+
const text = await expectWithheld(admin, `/meta/connector/crk_cleared`, ['crk_cleared']);
174+
const served = itemOf(JSON.parse(text));
175+
expect(served).not.toHaveProperty('apiKey');
176+
expect(served).not.toHaveProperty('extra');
177+
178+
const body = Object.fromEntries(Object.entries(served).filter(([k]) => !k.startsWith('_')));
179+
const resave = await stack.apiAs(admin, 'PUT', `/meta/connector/crk_cleared`, { ...body, label: 'CRK cleared (edited)' });
180+
expect(resave.status, await resave.clone().text()).toBe(200);
181+
});
182+
183+
it('a withheld dot-spelled root key and a dot-spelled providerConfig URL survive a save of the served form', async () => {
184+
const put = await stack.apiAs(admin, 'PUT', `/meta/connector/${DOTTED}`, instance(DOTTED));
185+
expect(put.status, await put.clone().text()).toBe(200);
186+
const [row] = await stored(DOTTED);
187+
const dottedUrl = `https://svc:${PLACEHOLDERS.dottedUrl}@api.vendor.example/v1`;
188+
await ql.update('sys_metadata', {
189+
id: row.id,
190+
metadata: JSON.stringify({
191+
...instance(DOTTED, { label: 'CRK dotted (seeded)' }),
192+
providerConfig: { baseUrl: 'https://api.vendor.example/v1', 'v1.url': dottedUrl },
193+
'x.y': PLACEHOLDERS.dottedRoot,
194+
}),
195+
}, { context: SYSTEM });
196+
197+
const text = await expectWithheld(admin, `/meta/connector/${DOTTED}`, ['CRK dotted (seeded)']);
198+
const served = itemOf(JSON.parse(text));
199+
expect(served).not.toHaveProperty('x.y');
200+
expect(served.providerConfig).not.toHaveProperty('v1.url');
201+
202+
const body = Object.fromEntries(Object.entries(served).filter(([k]) => !k.startsWith('_')));
203+
const resave = await stack.apiAs(admin, 'PUT', `/meta/connector/${DOTTED}`, { ...body, label: 'CRK dotted (edited)' });
204+
expect(resave.status, await resave.clone().text()).toBe(200);
205+
206+
const atRest = bodyOf((await stored(DOTTED))[0]);
207+
expect(atRest.label).toBe('CRK dotted (edited)');
208+
expect(atRest['x.y']).toBe(PLACEHOLDERS.dottedRoot);
209+
expect(atRest.providerConfig['v1.url']).toBe(dottedUrl);
210+
211+
// And the next read still withholds both.
212+
const reread = itemOf(JSON.parse(await expectWithheld(admin, `/meta/connector/${DOTTED}`, ['CRK dotted (edited)'])));
213+
expect(reread).not.toHaveProperty('x.y');
214+
expect(reread.providerConfig).not.toHaveProperty('v1.url');
215+
});
160216
});

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,15 @@ describe('the connector redactor — credentials inside a URL under `providerCon
242242
expect(holds(served, P(39), P(42), P(43))).toBe(false);
243243
});
244244

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` } };
247-
expect(redactConnectorItem(item)).toEqual({ item, redactedKeys: [] });
245+
it('withholds whole a dot-spelled key under providerConfig that holds a credential URL (fail closed)', () => {
246+
const { item: served, redactedKeys } = at({
247+
'v1.url': `https://u:${P(40)}@b.example`,
248+
'legacy.list': ['https://c.example', `https://d.example/v1?token=${P(44)}`],
249+
'plain.note': 'https://e.example/v1?region=eu',
250+
});
251+
expect(redactedKeys).toEqual(['providerConfig.legacy.list', 'providerConfig.v1.url']);
252+
expect(served.providerConfig).toEqual({ 'plain.note': 'https://e.example/v1?region=eu' });
253+
expect(holds(served, P(40), P(44))).toBe(false);
248254
});
249255

250256
it('a map withheld whole is not walked for URLs: it is reported once, as the map', () => {
@@ -291,9 +297,19 @@ describe('the connector redactor — top-level keys the entry does not declare',
291297
expect(redactConnectorItem(item)).toEqual({ item, redactedKeys: [] });
292298
});
293299

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: [] });
300+
it('withholds a cleared undeclared key too, and names it (the save door refuses it whatever its value)', () => {
301+
const item = { ...base, apiKey: '', token: null, extra: {} };
302+
const { item: served, redactedKeys } = redactConnectorItem(item);
303+
expect(redactedKeys).toEqual(['apiKey', 'extra', 'token']);
304+
for (const key of ['apiKey', 'token', 'extra']) expect(served).not.toHaveProperty(key);
305+
});
306+
307+
it('withholds an undeclared root key whose own name carries a dot, and names it whole', () => {
308+
const item = { ...base, 'x.y': P(59) };
309+
const { item: served, redactedKeys } = redactConnectorItem(item);
310+
expect(redactedKeys).toEqual(['x.y']);
311+
expect(served).not.toHaveProperty('x.y');
312+
expect(holds(served, P(59))).toBe(false);
297313
});
298314

299315
it('withholds an undeclared root key beside the other positions, each named once', () => {

0 commit comments

Comments
 (0)