@coffre/conformance 0.2.0 → 0.4.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.
@@ -0,0 +1,475 @@
1
+ // An MCP client connecting as Claude does (docs/design/mcp.md, section 14,
2
+ // checks 1 and 6): told to sign in by a 401, it reads both metadata
3
+ // documents, registers, sends a person to the consent page, and redeems the
4
+ // code. Its token is good at /mcp and nowhere else, nothing else is good
5
+ // there, a reused code or refresh token ends the connection, disconnecting
6
+ // ends it at the next call, and every step is in the log under the client.
7
+ import { createHash, randomBytes } from 'node:crypto';
8
+
9
+ import { CoffreError } from '@coffre/client';
10
+
11
+ import type { Deployment } from '../harness.ts';
12
+ import { expect } from '../report.ts';
13
+
14
+ import { canary, DEV, personaOn, PROD, PROJECT, valuesIn, type Canaries, type People, type Person } from './people.ts';
15
+
16
+ const NAME = 'Conformance MCP client';
17
+ /** A native client's redirect, on loopback. A custom scheme is left out of a registration (D37). */
18
+ const REDIRECT = 'http://127.0.0.1:33418/callback';
19
+ /** A `state` the router would read as a number: it must come back as sent. */
20
+ const STATE = '1e5';
21
+
22
+ /** What every 2026-07-28 request carries: its revision and the client's capabilities. */
23
+ const ENVELOPE = { 'io.modelcontextprotocol/protocolVersion': '2026-07-28', 'io.modelcontextprotocol/clientCapabilities': {} };
24
+
25
+ type Tokens = { access_token: string; refresh_token: string; scope: string; token_type: string; expires_in: number };
26
+
27
+ const base64url = (bytes: Buffer) => bytes.toString('base64url');
28
+
29
+ async function json(response: Response): Promise<Record<string, unknown>> {
30
+ const text = await response.text();
31
+ try {
32
+ return JSON.parse(text) as Record<string, unknown>;
33
+ } catch {
34
+ return { text };
35
+ }
36
+ }
37
+
38
+ /** A JSON-RPC message to /mcp, with the headers a client sends. */
39
+ function mcp(deployment: Deployment, headers: Record<string, string>, method = 'server/discover', params: Record<string, unknown> = {}): Promise<Response> {
40
+ return fetch(`${deployment.origin}/mcp`, {
41
+ method: 'POST',
42
+ headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream', ...headers },
43
+ body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }),
44
+ });
45
+ }
46
+
47
+ function token(deployment: Deployment, fields: Record<string, string>): Promise<Response> {
48
+ return fetch(`${deployment.origin}/api/oauth/token`, {
49
+ method: 'POST',
50
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
51
+ body: new URLSearchParams(fields),
52
+ });
53
+ }
54
+
55
+ /** The person approves on the consent page, as its Approve does; the code comes back on the redirect. */
56
+ async function connect(deployment: Deployment, person: Person, clientId: string, scope = 'browse'): Promise<{ code: string; verifier: string }> {
57
+ const verifier = base64url(randomBytes(32));
58
+ const request = {
59
+ client_id: clientId,
60
+ redirect_uri: REDIRECT,
61
+ response_type: 'code',
62
+ code_challenge: base64url(createHash('sha256').update(verifier).digest()),
63
+ code_challenge_method: 'S256',
64
+ state: STATE,
65
+ scope,
66
+ resource: `${deployment.origin}/mcp`,
67
+ };
68
+ const view = await person.api.oauth.describe(request);
69
+ expect(view.status === 'ready', 'the consent page refused a good request', view);
70
+ expect(view.client.registration === 'dcr' && view.client.name === NAME && view.scopes.join(' ') === scope && view.loopbackOnly,
71
+ 'the consent page does not show the registered client as it is', view);
72
+ const { redirect } = await person.api.oauth.decide({ request, approve: true, scopes: scope.split(' ') });
73
+ const back = new URL(redirect);
74
+ expect(`${back.origin}${back.pathname}` === REDIRECT, `the answer went to ${back.origin}${back.pathname}, not the redirect`, redirect);
75
+ expect(back.searchParams.get('state') === STATE && back.searchParams.get('iss') === deployment.origin,
76
+ 'the answer does not carry the state as sent and the issuer', redirect);
77
+ const code = back.searchParams.get('code');
78
+ expect(code !== null, 'an approval carried no code', redirect);
79
+ return { code, verifier };
80
+ }
81
+
82
+ async function redeem(deployment: Deployment, clientId: string, { code, verifier }: { code: string; verifier: string }): Promise<Tokens> {
83
+ const answer = await token(deployment, {
84
+ grant_type: 'authorization_code',
85
+ code,
86
+ code_verifier: verifier,
87
+ redirect_uri: REDIRECT,
88
+ client_id: clientId,
89
+ resource: `${deployment.origin}/mcp`,
90
+ });
91
+ const body = await json(answer);
92
+ expect(answer.status === 200 && typeof body.access_token === 'string' && typeof body.refresh_token === 'string',
93
+ `redeeming the code answered ${answer.status}`, body);
94
+ expect(answer.headers.get('cache-control') === 'no-store', 'tokens were answered without cache-control: no-store');
95
+ return body as unknown as Tokens;
96
+ }
97
+
98
+ async function refused(deployment: Deployment, what: string, fields: Record<string, string>): Promise<void> {
99
+ const answer = await token(deployment, fields);
100
+ const body = await json(answer);
101
+ expect(answer.status === 400 && body.error === 'invalid_grant', `${what} answered ${answer.status}, not invalid_grant`, body);
102
+ }
103
+
104
+ async function discovers(deployment: Deployment, access: string): Promise<boolean> {
105
+ const answer = await mcp(deployment, { authorization: `Bearer ${access}`, 'mcp-protocol-version': '2026-07-28', 'mcp-method': 'server/discover' }, 'server/discover', { _meta: ENVELOPE });
106
+ if (answer.status === 401) return false;
107
+ const body = await json(answer);
108
+ expect(answer.status === 200 && Array.isArray((body.result as { supportedVersions?: unknown } | undefined)?.supportedVersions),
109
+ `server/discover answered ${answer.status}`, body);
110
+ return true;
111
+ }
112
+
113
+ export async function mcpConnect(deployment: Deployment, people: People): Promise<string> {
114
+ const { origin } = deployment;
115
+
116
+ // Without a token: 401, and where to find out how to get one.
117
+ const anonymous = await mcp(deployment, {});
118
+ const challenge = anonymous.headers.get('www-authenticate') ?? '';
119
+ expect(anonymous.status === 401, `/mcp answered ${anonymous.status} without a token, not 401`);
120
+ expect(challenge.includes(`resource_metadata="${origin}/.well-known/oauth-protected-resource/mcp"`) && challenge.includes('scope="browse"'),
121
+ "the 401's challenge does not name the resource metadata and Browse", challenge);
122
+ const resource = await json(await fetch(`${origin}/.well-known/oauth-protected-resource/mcp`));
123
+ expect(resource.resource === `${origin}/mcp` && (resource.authorization_servers as unknown[] | undefined)?.[0] === origin,
124
+ 'the protected resource metadata does not name /mcp and coffre as its issuer', resource);
125
+ const server = await json(await fetch(`${origin}/.well-known/oauth-authorization-server`));
126
+ expect(server.issuer === origin && server.authorization_endpoint === `${origin}/oauth/authorize`, 'the authorization server metadata is wrong', server);
127
+ for (const [field, value] of [
128
+ ['code_challenge_methods_supported', 'S256'],
129
+ ['token_endpoint_auth_methods_supported', 'none'],
130
+ ['grant_types_supported', 'refresh_token'],
131
+ ] as const) {
132
+ expect((server[field] as unknown[] | undefined)?.includes(value), `the authorization server metadata leaves ${value} out of ${field}`, server);
133
+ }
134
+ expect(server.client_id_metadata_document_supported === true, 'the authorization server metadata does not offer metadata documents', server);
135
+
136
+ // A client registers: the custom scheme is left out, the loopback kept.
137
+ const registered = await fetch(`${origin}/api/oauth/register`, {
138
+ method: 'POST',
139
+ headers: { 'content-type': 'application/json' },
140
+ body: JSON.stringify({ client_name: NAME, redirect_uris: [REDIRECT, 'conformance-app://callback'], token_endpoint_auth_method: 'none' }),
141
+ });
142
+ const client = await json(registered);
143
+ expect(registered.status === 201 && typeof client.client_id === 'string', `registering answered ${registered.status}`, client);
144
+ expect(JSON.stringify(client.redirect_uris) === JSON.stringify([REDIRECT]), 'the registration kept a redirect it should have left out', client);
145
+ const clientId = client.client_id as string;
146
+
147
+ // The consent page renders for the person; another site cannot answer it for them.
148
+ const page = await people.reader.browser.fetch(`/oauth/authorize?${new URLSearchParams({ client_id: clientId, redirect_uri: REDIRECT })}`);
149
+ expect(page.status === 200, `the consent page answered ${page.status}`);
150
+ const forged = await people.reader.browser.fetch('/api/oauth/authorizations', {
151
+ method: 'POST',
152
+ headers: { origin: 'https://attacker.example', 'sec-fetch-site': 'cross-site', 'content-type': 'application/json' },
153
+ body: JSON.stringify({ request: { client_id: clientId, redirect_uri: REDIRECT }, approve: true }),
154
+ });
155
+ expect(forged.status === 403, `another site's approval answered ${forged.status}, not 403`);
156
+
157
+ // The first connection: its code redeemed, then its refresh token rotated.
158
+ const first = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
159
+ expect(first.access_token.startsWith('coffre_mcp_') && first.refresh_token.startsWith('coffre_mcr_') && first.scope === 'browse',
160
+ 'the tokens are not the shapes and scope coffre issues', { scope: first.scope });
161
+ expect(await discovers(deployment, first.access_token), 'a fresh access token was refused at /mcp');
162
+
163
+ // Audience: the MCP token is no API credential, and no other is good at /mcp.
164
+ const atApi = await fetch(`${origin}/api/me`, { headers: { authorization: `Bearer ${first.access_token}` } });
165
+ expect(atApi.status === 401, `an MCP token at /api answered ${atApi.status}, not 401`);
166
+ const cli = await mcp(deployment, { authorization: `Bearer ${people.leaver.cliToken}` });
167
+ expect(cli.status === 401, `a CLI session at /mcp answered ${cli.status}, not 401`);
168
+ const browser = await people.reader.browser.fetch('/mcp', {
169
+ method: 'POST',
170
+ headers: { 'content-type': 'application/json', origin },
171
+ body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'server/discover' }),
172
+ });
173
+ expect(browser.status === 401, `a browser's cookie at /mcp answered ${browser.status}, not 401`);
174
+ const foreign = await mcp(deployment, { authorization: `Bearer ${first.access_token}`, origin: 'https://attacker.example' });
175
+ expect(foreign.status === 403, `/mcp answered ${foreign.status} to another site's page, not 403`);
176
+
177
+ const rotate = (refresh: string) => token(deployment, { grant_type: 'refresh_token', refresh_token: refresh, client_id: clientId });
178
+ const rotated = await rotate(first.refresh_token);
179
+ const next = (await json(rotated)) as unknown as Tokens;
180
+ expect(rotated.status === 200 && next.refresh_token !== first.refresh_token, `refreshing answered ${rotated.status}, or the same refresh token`);
181
+ expect(await discovers(deployment, next.access_token), 'a refreshed access token was refused');
182
+ // The replaced refresh token, again: the connection ends, and everything under it.
183
+ await refused(deployment, 'a replaced refresh token', { grant_type: 'refresh_token', refresh_token: first.refresh_token, client_id: clientId });
184
+ await refused(deployment, 'the current refresh token, after a reuse', { grant_type: 'refresh_token', refresh_token: next.refresh_token, client_id: clientId });
185
+ expect(!(await discovers(deployment, next.access_token)), 'an access token outlived its connection, ended by a reused refresh token');
186
+
187
+ // A second: its code, presented twice, ends it.
188
+ const asked = await connect(deployment, people.reader, clientId);
189
+ const second = await redeem(deployment, clientId, asked);
190
+ await refused(deployment, 'a code redeemed already', {
191
+ grant_type: 'authorization_code', code: asked.code, code_verifier: asked.verifier, redirect_uri: REDIRECT, client_id: clientId,
192
+ });
193
+ expect(!(await discovers(deployment, second.access_token)), 'an access token outlived its connection, ended by a reused code');
194
+
195
+ // A third, listed under Connected apps, and disconnected there.
196
+ const third = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
197
+ const listed = (await people.reader.api.apps.list()).apps;
198
+ expect(listed.length === 1 && listed[0]!.name === NAME && listed[0]!.registration === 'dcr', 'Connected apps does not list the one live connection', listed);
199
+ await people.reader.api.apps.disconnect(listed[0]!.id);
200
+ expect(!(await discovers(deployment, third.access_token)), 'an access token outlived its disconnection');
201
+ expect((await people.reader.api.apps.list()).apps.length === 0, 'a disconnected app is still listed');
202
+
203
+ // Every step in the log, under the client.
204
+ const { entries } = await people.admin.api.audit.list({ actor: people.reader.member, detail: '1', limit: 500 });
205
+ const mine = entries.filter((entry) => entry.metadata.clientId === clientId);
206
+ const count = (action: string) => mine.filter((entry) => entry.action === action && entry.decision === 'allow').length;
207
+ const counts = { connect: count('mcp.connect'), token: count('mcp.token'), disconnect: count('mcp.disconnect') };
208
+ expect(counts.connect === 3 && counts.token === 4 && counts.disconnect === 3, 'the log does not hold each connection, token and disconnection', counts);
209
+ const reasons = mine.filter((entry) => entry.action === 'mcp.disconnect').map((entry) => entry.metadata.reason).sort();
210
+ expect(JSON.stringify(reasons) === JSON.stringify(['code_reused', 'person', 'refresh_reused']), 'the disconnections do not say why', reasons);
211
+ return '401 and both metadata documents; registered, approved, redeemed and discovered; only at /mcp, and only its token there; a reused refresh token, a reused code and a disconnection each end the connection, and the log says so';
212
+ }
213
+
214
+ /** A 2026-07-28 request as a client sends it: its headers say what its body does; `capabilities` are the client's. */
215
+ async function modern(deployment: Deployment, access: string, method: string, params: Record<string, unknown> = {}, name?: string, capabilities: Record<string, unknown> = {}) {
216
+ const response = await mcp(deployment, {
217
+ authorization: `Bearer ${access}`,
218
+ 'mcp-protocol-version': '2026-07-28',
219
+ 'mcp-method': method,
220
+ ...(name === undefined ? {} : { 'mcp-name': name }),
221
+ }, method, { ...params, _meta: { ...ENVELOPE, 'io.modelcontextprotocol/clientCapabilities': capabilities } });
222
+ return { response, body: await json(response) };
223
+ }
224
+
225
+ /** A client registered for one check, by the name every check's client has. */
226
+ async function register(deployment: Deployment): Promise<string> {
227
+ const registered = await json(await fetch(`${deployment.origin}/api/oauth/register`, {
228
+ method: 'POST',
229
+ headers: { 'content-type': 'application/json' },
230
+ body: JSON.stringify({ client_name: NAME, redirect_uris: [REDIRECT] }),
231
+ }));
232
+ return registered.client_id as string;
233
+ }
234
+
235
+ /**
236
+ * The Browse tools, as a client calls them (design section 14, checks 1, 4
237
+ * and 5): on 2026-07-28 and through a 2025-11-25 `initialize`, as the
238
+ * reader, who sees dev and not prod. No value appears in any answer, the
239
+ * headers must agree with the body, and every call is in the log under the
240
+ * client, the API's own entries naming the connection.
241
+ */
242
+ export async function mcpBrowse(deployment: Deployment, people: People, canaries: Canaries): Promise<string> {
243
+ const clientId = await register(deployment);
244
+ const { access_token: access } = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
245
+ const answers: string[] = [];
246
+
247
+ // 2026-07-28: discover, list, call.
248
+ const discovered = await modern(deployment, access, 'server/discover');
249
+ answers.push(JSON.stringify(discovered.body));
250
+ expect(discovered.response.status === 200, `server/discover answered ${discovered.response.status}`, discovered.body);
251
+ const listed = await modern(deployment, access, 'tools/list');
252
+ const result = listed.body.result as { tools?: { name: string; annotations?: { readOnlyHint?: boolean } }[]; cacheScope?: string; ttlMs?: number } | undefined;
253
+ const names = result?.tools?.map((tool) => tool.name) ?? [];
254
+ expect(names.includes('list_secrets') && names.includes('run_with_secrets') && result?.cacheScope === 'public' && typeof result.ttlMs === 'number',
255
+ 'tools/list does not list the Browse tools, cacheable', listed.body);
256
+ const callTool = async (tool: string, args: Record<string, unknown>) => {
257
+ const { response, body } = await modern(deployment, access, 'tools/call', { name: tool, arguments: args }, tool);
258
+ answers.push(JSON.stringify(body));
259
+ expect(response.status === 200, `tools/call ${tool} answered ${response.status}`, body);
260
+ return body.result as { structuredContent?: Record<string, unknown>; content?: { text?: string }[]; isError?: boolean };
261
+ };
262
+ const dev = await callTool('list_secrets', { environment: DEV });
263
+ const keys = ((dev.structuredContent?.keys ?? []) as { key: string }[]).map((key) => key.key);
264
+ expect(['API_KEY', 'DATABASE_URL'].every((key) => keys.includes(key)), `list_secrets on ${DEV} does not list its keys`, keys);
265
+ const prod = await callTool('list_secrets', { environment: PROD });
266
+ expect(prod.isError === true, `the reader listed ${PROD}'s keys through MCP, which they cannot`, prod);
267
+ const run = await callTool('run_with_secrets', { environment: DEV, command: 'npm test' });
268
+ expect(run.content?.[0]?.text?.includes(`coffre run ${DEV} -- npm test`) === true, 'run_with_secrets does not say how to run the command', run);
269
+ for (const tool of ['whoami', 'list_projects']) await callTool(tool, {});
270
+ await callTool('secret_history', { secret: `${DEV}/API_KEY` });
271
+
272
+ // The headers say what the body does, or the request is refused.
273
+ const mismatched = await mcp(deployment, {
274
+ authorization: `Bearer ${access}`, 'mcp-protocol-version': '2026-07-28', 'mcp-method': 'tools/call', 'mcp-name': 'list_projects',
275
+ }, 'tools/call', { name: 'whoami', arguments: {}, _meta: ENVELOPE });
276
+ const mismatch = await json(mismatched);
277
+ expect(mismatched.status === 400 && (mismatch.error as { code?: number } | undefined)?.code === -32020, `a Mcp-Name naming another tool answered ${mismatched.status}`, mismatch);
278
+
279
+ // 2025-11-25: initialize, with no session, then the same tools.
280
+ const initialized = await mcp(deployment, { authorization: `Bearer ${access}` }, 'initialize', {
281
+ protocolVersion: '2025-11-25', capabilities: {}, clientInfo: { name: 'coffre-conformance', version: '1' },
282
+ });
283
+ const init = await json(initialized);
284
+ expect(initialized.status === 200 && (init.result as { protocolVersion?: string } | undefined)?.protocolVersion === '2025-11-25',
285
+ `initialize for 2025-11-25 answered ${initialized.status}`, init);
286
+ expect(initialized.headers.get('mcp-session-id') === null, 'initialize minted a session: coffre keeps none');
287
+ const legacy = await mcp(deployment, { authorization: `Bearer ${access}`, 'mcp-protocol-version': '2025-11-25' }, 'tools/call', { name: 'list_secrets', arguments: { environment: DEV } });
288
+ const legacyBody = await json(legacy);
289
+ answers.push(JSON.stringify(legacyBody));
290
+ expect(legacy.status === 200 && (legacyBody.result as { isError?: boolean } | undefined)?.isError !== true, `a 2025-11-25 tools/call answered ${legacy.status}`, legacyBody);
291
+
292
+ // No value in any answer.
293
+ const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
294
+ expect(leaked.length === 0, 'a Browse tool answered with a secret value', leaked);
295
+
296
+ // Every call in the log, under the client; the API's own refusal names the connection.
297
+ const { entries } = await people.admin.api.audit.list({ actor: people.reader.member, detail: '1', limit: 500 });
298
+ const mine = entries.filter((entry) => (entry.metadata.via as { clientId?: string } | undefined)?.clientId === clientId);
299
+ const reads = mine.filter((entry) => entry.action === 'mcp.read').length;
300
+ const refused = mine.filter((entry) => entry.action === 'mcp.call' && entry.decision === 'deny').length;
301
+ expect(reads === 6 && refused === 1, 'the log does not hold each call under the client', { reads, refused });
302
+ const api = mine.filter((entry) => !entry.action.startsWith('mcp.'));
303
+ expect(api.length > 0, "the API's own entries for the calls do not name the connection", mine.map((entry) => entry.action));
304
+ const shown = (await people.admin.api.audit.list({ actor: people.reader.member, limit: 500 })).entries;
305
+ expect(!shown.some((entry) => entry.action === 'mcp.read'), 'read-only calls are not detail');
306
+ return `on 2026-07-28 and 2025-11-25, the Browse tools as the reader: ${DEV} listed, ${PROD} refused, no value in any answer; headers held to the body; ${reads} reads as detail and the refusal shown, each under the client`;
307
+ }
308
+
309
+ type ToolAnswer = {
310
+ resultType?: string;
311
+ structuredContent?: { status?: string; approval?: { id: string; url: string } };
312
+ content?: { text?: string }[];
313
+ isError?: boolean;
314
+ inputRequests?: { approve?: { method: string; params: { mode: string; url: string; message: string } } };
315
+ requestState?: string;
316
+ };
317
+
318
+ /**
319
+ * Changes, as a client asks for them (design section 14, checks 2 and 3):
320
+ * Browse cannot ask at all, and with Write nothing changes until the person
321
+ * approves on coffre's page. A client without URL elicitation gets the link
322
+ * and calls again; one with it is asked to open the page, and retries with
323
+ * its requestState, which no other call may use. Only the person decides,
324
+ * with the digest the page showed; the page makes the change, once, and its
325
+ * entries name the client and the approval. A value comes from the page.
326
+ */
327
+ export async function mcpChanges(deployment: Deployment, people: People, canaries: Canaries): Promise<string> {
328
+ const { admin } = people;
329
+ const place = `${PROJECT}-mcp/dev`;
330
+ await admin.api.projects.create(`${PROJECT}-mcp`, { name: 'MCP changes' });
331
+ await admin.api.environments.create(place, { name: 'Development' });
332
+ const values = { KEEP: canary(), OLD: canary() };
333
+ for (const [key, value] of Object.entries(values)) canaries[`${place}/${key}`] = value;
334
+ await admin.api.secrets.set(place, values);
335
+ const changer = await personaOn(deployment, admin, 'changer', { [`${PROJECT}-mcp`]: 'maintainer' });
336
+ const clientId = await register(deployment);
337
+ const answers: string[] = [];
338
+ const call = async (access: string, tool: string, args: Record<string, unknown>, more: Record<string, unknown> = {}, capabilities: Record<string, unknown> = {}) => {
339
+ const { response, body } = await modern(deployment, access, 'tools/call', { name: tool, arguments: args, ...more }, tool, capabilities);
340
+ answers.push(JSON.stringify(body));
341
+ return { status: response.status, challenge: response.headers.get('www-authenticate') ?? '', result: body.result as ToolAnswer | undefined, error: body.error as { code?: number } | undefined };
342
+ };
343
+ const archived = async (key: string) => (await admin.api.secrets.history(`${place}/${key}`)).archived;
344
+
345
+ // Browse cannot ask for a change: 403, and the step-up's scopes; nothing reached the API.
346
+ const browse = (await redeem(deployment, clientId, await connect(deployment, changer, clientId))).access_token;
347
+ const stepUp = await call(browse, 'archive_secret', { secret: `${place}/OLD` });
348
+ expect(stepUp.status === 403 && stepUp.challenge.includes('error="insufficient_scope"') && stepUp.challenge.includes('scope="browse write"'),
349
+ `archive_secret with Browse answered ${stepUp.status}, not a step-up to Write`, stepUp.challenge);
350
+ expect(!(await archived('OLD')), 'archive_secret with Browse archived the secret');
351
+
352
+ // With Write, and no URL elicitation: the link, and nothing changed.
353
+ const write = (await redeem(deployment, clientId, await connect(deployment, changer, clientId, 'browse write'))).access_token;
354
+ const linked = await call(write, 'archive_secret', { secret: `${place}/OLD` });
355
+ const approval = linked.result?.structuredContent?.approval;
356
+ expect(linked.status === 200 && linked.result?.structuredContent?.status === 'pending' && approval?.url === `${deployment.origin}/approvals/${approval?.id}`,
357
+ 'a change without URL elicitation was not answered with its approval link', linked.result);
358
+ expect(linked.result?.content?.[0]?.text?.includes(approval!.url) === true, 'the link is not in the text the model reads', linked.result);
359
+ expect(!(await archived('OLD')), 'a change was made before its approval');
360
+
361
+ // Only its person decides it, and only the change the page showed.
362
+ const theirs = await people.reader.api.approvals.get(approval!.id).then(() => null, (error: unknown) => error);
363
+ expect(theirs instanceof CoffreError && theirs.status === 403, "another person opened someone else's approval", theirs);
364
+ const page = await changer.browser.fetch(`/approvals/${approval!.id}`);
365
+ expect(page.status === 200, `the approval page answered ${page.status}`);
366
+ const shown = (await changer.api.approvals.get(approval!.id)).approval;
367
+ expect(shown.status === 'pending' && shown.summary === `archive ${place}/OLD` && shown.client.name === NAME, 'the approval page does not show the change asked for', shown);
368
+ const forgedDigest = await changer.api.approvals.decide(approval!.id, { approve: true, digest: '0'.repeat(64) }).then(() => null, (error: unknown) => error);
369
+ expect(forgedDigest instanceof CoffreError && forgedDigest.status === 409, 'a decision on a change the page did not show was taken', forgedDigest);
370
+ const forgedSite = await changer.browser.fetch(`/api/approvals/${approval!.id}`, {
371
+ method: 'POST',
372
+ headers: { origin: 'https://attacker.example', 'sec-fetch-site': 'cross-site', 'content-type': 'application/json' },
373
+ body: JSON.stringify({ approve: true, digest: shown.digest }),
374
+ });
375
+ expect(forgedSite.status === 403, `another site's approval answered ${forgedSite.status}, not 403`);
376
+ expect(!(await archived('OLD')), 'a refused decision changed something');
377
+
378
+ const decided = await changer.api.approvals.decide(approval!.id, { approve: true, digest: shown.digest });
379
+ expect(decided.status === 'approved' && (await archived('OLD')), 'Approve did not make the change', decided);
380
+ const again = await changer.api.approvals.decide(approval!.id, { approve: true, digest: shown.digest }).then(() => null, (error: unknown) => error);
381
+ expect(again instanceof CoffreError && again.status === 409, 'an approval was decided twice', again);
382
+ const reported = await call(write, 'archive_secret', { secret: `${place}/OLD` });
383
+ expect(reported.result?.structuredContent?.status === 'approved' && reported.result.isError !== true, 'calling again does not report the approved change', reported.result);
384
+
385
+ // With URL elicitation: asked to open the page, and the retry's requestState is that call's only.
386
+ const elicits = { elicitation: { url: {} } };
387
+ const asked = await call(write, 'rename_secret', { secret: `${place}/KEEP`, newKey: 'KEPT' }, {}, elicits);
388
+ const request = asked.result?.inputRequests?.approve;
389
+ expect(asked.result?.resultType === 'input_required' && request?.method === 'elicitation/create' && request.params.mode === 'url' && typeof asked.result.requestState === 'string',
390
+ 'a change with URL elicitation was not answered with an input_required URL elicitation', asked.result);
391
+ const id = request!.params.url.split('/').at(-1)!;
392
+ const replayed = await call(write, 'rename_secret', { secret: `${place}/KEEP`, newKey: 'OTHER' }, { requestState: asked.result!.requestState, inputResponses: { approve: { action: 'accept' } } }, elicits);
393
+ expect(replayed.error?.code === -32602, 'a requestState was accepted for another call', replayed);
394
+ const renamedEarly = await admin.api.secrets.list(place);
395
+ expect(renamedEarly.keys.some((key) => key.key === 'KEEP'), 'a change was made before its approval', renamedEarly.keys);
396
+ const second = (await changer.api.approvals.get(id)).approval;
397
+ expect((await changer.api.approvals.decide(id, { approve: true, digest: second.digest })).status === 'approved', 'the second approval was not made');
398
+ const retried = await call(write, 'rename_secret', { secret: `${place}/KEEP`, newKey: 'KEPT' }, { requestState: asked.result!.requestState, inputResponses: { approve: { action: 'accept' } } }, elicits);
399
+ expect(retried.result?.structuredContent?.status === 'approved', 'the retry does not report the approved change', retried.result);
400
+
401
+ // A value, from the page only.
402
+ const typed = canary();
403
+ canaries[`${place}/TYPED`] = typed;
404
+ const valueAsked = await call(write, 'request_secret_value', { secret: `${place}/TYPED` });
405
+ const valueId = valueAsked.result!.structuredContent!.approval!.id;
406
+ const valuePage = (await changer.api.approvals.get(valueId)).approval;
407
+ expect(valuePage.asks?.value !== undefined, 'the approval page does not ask for the value');
408
+ await changer.api.approvals.decide(valueId, { approve: true, digest: valuePage.digest, value: typed });
409
+ expect((await admin.api.secrets.reveal(`${place}/TYPED`)).values.TYPED === typed, 'the value typed on the page was not written');
410
+ await call(write, 'request_secret_value', { secret: `${place}/TYPED` });
411
+
412
+ // A value coffre makes, approved by the person, reaches nobody.
413
+ const generating = await call(write, 'generate_secret_value', { secret: `${place}/GENERATED`, alphabet: 'hex' });
414
+ const generatedId = generating.result!.structuredContent!.approval!.id;
415
+ const generatedPage = (await changer.api.approvals.get(generatedId)).approval;
416
+ const generated = await changer.api.approvals.decide(generatedId, { approve: true, digest: generatedPage.digest });
417
+ const made = (await admin.api.secrets.reveal(`${place}/GENERATED`)).values.GENERATED ?? '';
418
+ canaries[`${place}/GENERATED`] = made;
419
+ expect(/^[0-9a-f]{64}$/.test(made) && generated.shown.length === 0 && !JSON.stringify(generated).includes(made), 'generate_secret_value did not make a 64-character hex value, shown to no one');
420
+ await call(write, 'generate_secret_value', { secret: `${place}/GENERATED`, alphabet: 'hex' });
421
+
422
+ // No value in any answer; each change made once, by the person, via the client and its approval.
423
+ const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
424
+ expect(leaked.length === 0, 'a change tool answered with a secret value', leaked);
425
+ const { entries } = await admin.api.audit.list({ actor: changer.member, limit: 500 });
426
+ const viaApproval = (action: string) => entries.filter((entry) => entry.action === action && entry.decision === 'allow' && typeof (entry.metadata.via as { approvalId?: unknown } | undefined)?.approvalId === 'string');
427
+ const once = { archive: viaApproval('secret.archive').length, rename: viaApproval('secret.rename').length, write: viaApproval('secret.write').length };
428
+ expect(once.archive === 1 && once.rename === 1 && once.write === 2, 'the changes are not each made once, via the client and its approval', once);
429
+ const decisions = entries.filter((entry) => entry.action === 'mcp.approve').map((entry) => `${entry.decision} ${entry.reason ?? ''}`.trim()).sort();
430
+ expect(JSON.stringify(decisions) === JSON.stringify(['allow', 'allow', 'allow', 'allow', 'deny approved', 'deny changed']), 'the decisions, and the two refused, are not in the log', decisions);
431
+ return `Browse stepped up to Write; with Write, the link without URL elicitation and an elicitation with it; nothing changed until ${changer.email} approved on coffre's page, another person and another site refused, a replayed requestState refused; each change made once, via the client and its approval; a value typed on the page only, and one coffre made reaching no one`;
432
+ }
433
+
434
+ /**
435
+ * Values (design section 14, checks 2 and 4): Browse cannot read one, Read
436
+ * values reads what the person may, a value shown on coffre's page goes to
437
+ * the person and not the client, and one a client asks coffre to make
438
+ * reaches no one.
439
+ */
440
+ export async function mcpValues(deployment: Deployment, people: People, canaries: Canaries): Promise<string> {
441
+ const { reader } = people;
442
+ const clientId = await register(deployment);
443
+ const answers: string[] = [];
444
+ const call = async (access: string, tool: string, args: Record<string, unknown>) => {
445
+ const { response, body } = await modern(deployment, access, 'tools/call', { name: tool, arguments: args }, tool);
446
+ answers.push(JSON.stringify(body));
447
+ return { status: response.status, challenge: response.headers.get('www-authenticate') ?? '', result: body.result as ToolAnswer & { structuredContent?: { values?: Record<string, string> } } | undefined };
448
+ };
449
+ const dev = valuesIn(canaries, DEV);
450
+
451
+ // Browse: no value to the model, but one shown to the person on coffre's page.
452
+ const browse = (await redeem(deployment, clientId, await connect(deployment, reader, clientId))).access_token;
453
+ const stepUp = await call(browse, 'read_secret_values', { path: DEV });
454
+ expect(stepUp.status === 403 && stepUp.challenge.includes('scope="browse read-values"'), `read_secret_values with Browse answered ${stepUp.status}, not a step-up`, stepUp.challenge);
455
+ const show = await call(browse, 'show_secret_value', { secret: `${DEV}/API_KEY` });
456
+ const id = show.result?.structuredContent?.approval?.id;
457
+ expect(show.result?.structuredContent?.status === 'pending' && id !== undefined, 'show_secret_value did not open an approval', show.result);
458
+ const page = (await reader.api.approvals.get(id!)).approval;
459
+ expect(page.kind === 'reveal', 'the approval page does not say it shows a value', page);
460
+ const shown = await reader.api.approvals.decide(id!, { approve: true, digest: page.digest });
461
+ expect(shown.shown.some((line) => line.value === dev.API_KEY), 'Reveal did not show the person the value');
462
+ const told = await call(browse, 'show_secret_value', { secret: `${DEV}/API_KEY` });
463
+ expect(told.result?.structuredContent?.status === 'approved', 'show_secret_value does not report the value as shown', told.result);
464
+ const browseLeaks = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
465
+ expect(browseLeaks.length === 0, 'a Browse answer held a value', browseLeaks);
466
+
467
+ // Read values: the values the person may read, and no others.
468
+ const reading = (await redeem(deployment, clientId, await connect(deployment, reader, clientId, 'browse read-values'))).access_token;
469
+ const read = await call(reading, 'read_secret_values', { path: DEV });
470
+ expect(JSON.stringify(read.result?.structuredContent?.values) === JSON.stringify(dev), `read_secret_values on ${DEV} did not answer its values`);
471
+ expect(read.result?.content?.[0]?.text?.startsWith('These values are now part of this conversation') === true, 'read_secret_values does not warn first', read.result?.content);
472
+ const prod = await call(reading, 'read_secret_values', { path: PROD });
473
+ expect(prod.result?.isError === true && !JSON.stringify(prod.result).includes(canaries[`${PROD}/API_KEY`]!), `the reader read ${PROD} through MCP`, prod.result);
474
+ return `Browse stepped up for values and showed ${DEV}/API_KEY to ${reader.email} on coffre's page only; Read values answered ${DEV}'s values, warning first, and refused ${PROD}`;
475
+ }