@coffre/conformance 0.15.0 → 0.16.1

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.
package/src/checks/mcp.ts CHANGED
@@ -6,10 +6,12 @@
6
6
  // ends it at the next call, and every step is in the log under the client.
7
7
  import { createHash, randomBytes } from 'node:crypto';
8
8
 
9
- import { CoffreError } from '@coffre/client';
9
+ import { CoffreError, type AuditEntryView } from '@coffre/client';
10
10
 
11
+ import { using } from '../database.ts';
11
12
  import type { Deployment } from '../harness.ts';
12
- import { expect } from '../report.ts';
13
+ import { expect, until, PUBLISHED_SECONDS } from '../report.ts';
14
+ import { logBodies } from './storage.ts';
13
15
 
14
16
  import { bearer } from '../browser.ts';
15
17
  import { canary, deviceLogin, DEV, personaOn, PROD, PROJECT, valuesIn, type Canaries, type People, type Person } from './people.ts';
@@ -188,7 +190,11 @@ export async function mcpConnect(deployment: Deployment, people: People): Promis
188
190
  // The replaced refresh token, again: the connection ends, and everything under it.
189
191
  await refused(deployment, 'a replaced refresh token', { grant_type: 'refresh_token', refresh_token: first.refresh_token, client_id: clientId });
190
192
  await refused(deployment, 'the current refresh token, after a reuse', { grant_type: 'refresh_token', refresh_token: next.refresh_token, client_id: clientId });
191
- expect(!(await discovers(deployment, next.access_token)), 'an access token outlived its connection, ended by a reused refresh token');
193
+ // A revocation takes up to the vault's memory, 2 seconds (D218): the token was asked about a moment ago. Asked every
194
+ // 250 ms until refused, for that and a second more.
195
+ for (const started = Date.now(); await discovers(deployment, next.access_token); await new Promise((resolve) => setTimeout(resolve, 250))) {
196
+ expect(Date.now() - started < 3000, `an access token outlived its connection, ended by a reused refresh token, by ${Date.now() - started} ms`);
197
+ }
192
198
 
193
199
  // A second: its code, presented twice, ends it.
194
200
  const asked = await connect(deployment, people.reader, clientId);
@@ -206,14 +212,34 @@ export async function mcpConnect(deployment: Deployment, people: People): Promis
206
212
  expect(!(await discovers(deployment, third.access_token)), 'an access token outlived its disconnection');
207
213
  expect((await people.reader.api.apps.list()).apps.length === 0, 'a disconnected app is still listed');
208
214
 
209
- // Every step in the log, under the client.
210
- const { entries } = await people.admin.api.audit.list({ actor: people.reader.member, detail: '1', limit: 500 });
211
- const mine = entries.filter((entry) => entry.metadata.clientId === clientId);
212
- const count = (action: string) => mine.filter((entry) => entry.action === action && entry.decision === 'allow').length;
213
- const counts = { connect: count('mcp.connect'), token: count('mcp.token'), disconnect: count('mcp.disconnect') };
214
- expect(counts.connect === 3 && counts.token === 4 && counts.disconnect === 3, 'the log does not hold each connection, token and disconnection', counts);
215
+ // Every step in the log, under the client. 0.16 names it `client`; a reused code or refresh token is the vault's
216
+ // own disconnect, with no actor, so the whole log is read; Connected apps' is the credential's end, `token.revoke`
217
+ // (docs/design/0.16.md, "MCP"). The page lists an entry once the signer has published it, within its minute.
218
+ let entries: AuditEntryView[] = [];
219
+ let mine: AuditEntryView[] = [];
220
+ let counts = {};
221
+ const done = () => {
222
+ mine = entries.filter((entry) => entry.metadata.client === clientId);
223
+ const count = (action: string) => mine.filter((entry) => entry.action === action && entry.decision === 'allow').length;
224
+ const ended = entries.filter((entry) => entry.action === 'token.revoke' && entry.decision === 'allow' && entry.metadata.kind === 'mcp' && entry.metadata.credential === listed[0]!.id);
225
+ counts = { connect: count('mcp.connect'), token: count('mcp.token'), disconnect: count('mcp.disconnect'), revoked: ended.length };
226
+ return JSON.stringify(counts) === JSON.stringify({ connect: 3, token: 4, disconnect: 2, revoked: 1 });
227
+ };
228
+ await until('the connections, tokens and disconnections on the log', async () => {
229
+ ({ entries } = await people.admin.api.audit.list({ detail: '1', limit: 500 }));
230
+ return done();
231
+ }, PUBLISHED_SECONDS).catch(() => undefined);
232
+ expect(done(), 'the log does not hold each connection, token and disconnection', counts);
215
233
  const reasons = mine.filter((entry) => entry.action === 'mcp.disconnect').map((entry) => entry.metadata.reason).sort();
216
- expect(JSON.stringify(reasons) === JSON.stringify(['code_reused', 'person', 'refresh_reused']), 'the disconnections do not say why', reasons);
234
+ expect(JSON.stringify(reasons) === JSON.stringify(['code_reused', 'refresh_reused']), 'the disconnections do not say why', reasons);
235
+ // The reader connected, redeemed and disconnected; a reuse's disconnect is the vault's own, with no one's name on it.
236
+ const by = (entry: AuditEntryView) => `${entry.action} ${entry.actorType}:${entry.actorId}`;
237
+ const reader = (entry: AuditEntryView) => entry.actorType === 'user' && entry.actorId === people.reader.email;
238
+ const vault = (entry: AuditEntryView) => entry.actorType === 'system' && entry.actorId === 'vault';
239
+ const counted = mine.filter((entry) => ['mcp.connect', 'mcp.token', 'mcp.disconnect'].includes(entry.action));
240
+ const misnamed = [...counted, ...entries.filter((entry) => entry.action === 'token.revoke' && entry.metadata.credential === listed[0]!.id)]
241
+ .filter((entry) => entry.decision === 'allow' && !(entry.action === 'mcp.disconnect' ? vault(entry) : reader(entry)));
242
+ expect(misnamed.length === 0, "the log names someone else for the reader's connections, or someone for a reuse's disconnect", misnamed.map(by));
217
243
  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';
218
244
  }
219
245
 
@@ -304,17 +330,27 @@ export async function mcpRead(deployment: Deployment, people: People, canaries:
304
330
  const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
305
331
  expect(leaked.length === 0, 'a Read tool answered with a secret value', leaked);
306
332
 
307
- // Every call in the log, under the client; the API's own refusal names the connection.
308
- const { entries } = await people.admin.api.audit.list({ actor: people.reader.member, detail: '1', limit: 500 });
309
- const mine = entries.filter((entry) => (entry.metadata.via as { clientId?: string } | undefined)?.clientId === clientId);
310
- const reads = mine.filter((entry) => entry.action === 'mcp.read').length;
311
- const refused = mine.filter((entry) => entry.action === 'mcp.call' && entry.decision === 'deny').length;
312
- expect(reads === 5 && refused === 1, 'the log does not hold each call under the client', { reads, refused });
313
- const api = mine.filter((entry) => !entry.action.startsWith('mcp.'));
314
- expect(api.length > 0, "the API's own entries for the calls do not name the connection", mine.map((entry) => entry.action));
315
- const shown = (await people.admin.api.audit.list({ actor: people.reader.member, limit: 500 })).entries;
316
- expect(!shown.some((entry) => entry.action === 'mcp.read'), 'read-only calls are not detail');
317
- return `on 2026-07-28 and 2025-11-25, the Read 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`;
333
+ // 0.16 writes no entry for a tools/call: the vault's use and change log what matters, and these calls read names alone
334
+ // (docs/design/0.16.md, "The server", MCP's OAuth). 0.14.7 logged each, `mcp.read` and `mcp.call`, under the client.
335
+ // Its connection's own entries aside, nothing names the client (`client`) or its connection (`via`): not on the
336
+ // page, once the connection's entry is published, nor among the entries signed or waiting, read from the database.
337
+ let entries: AuditEntryView[] = [];
338
+ const connections = new Set<unknown>();
339
+ await until('the connection on the log', async () => {
340
+ ({ entries } = await people.admin.api.audit.list({ actor: people.reader.member, detail: '1', limit: 500 }));
341
+ for (const entry of entries) if (entry.action === 'mcp.connect' && entry.metadata.client === clientId) connections.add(entry.metadata.connection);
342
+ return connections.size > 0;
343
+ }, PUBLISHED_SECONDS).catch(() => undefined);
344
+ expect(connections.size === 1, 'the connection is not in the log under its client', connections.size);
345
+ // Registering the client and connecting it log under the client: those are the connection's, not the calls'.
346
+ const own = new Set(['oauth.register', 'mcp.connect', 'mcp.token']);
347
+ const mine = entries.filter((entry) => (entry.metadata.client === clientId && !own.has(entry.action)) || connections.has(entry.metadata.via));
348
+ expect(mine.length === 0, 'a read-only tools/call left an entry under the client', mine.map((entry) => entry.action));
349
+ // The page lists only what the signer has published; an entry the calls wrote may still wait in `log_pending`.
350
+ const waiting = (await using(deployment.database(), logBodies)).filter((body) =>
351
+ (body.detail.client === clientId && !own.has(body.action)) || connections.has(body.detail.via));
352
+ expect(waiting.length === 0, 'a read-only tools/call left an entry under the client, signed or waiting to be', waiting.map((body) => body.action));
353
+ return `on 2026-07-28 and 2025-11-25, the Read tools as the reader: ${DEV} listed, ${PROD} refused, no value in any answer; headers held to the body; no entry for the calls, which read names alone`;
318
354
  }
319
355
 
320
356
  type ToolAnswer = {
@@ -403,7 +439,8 @@ export async function mcpChanges(deployment: Deployment, people: People, canarie
403
439
  expect(decided.status === 'approved' && (await archived('OLD')), 'Approve did not make the change', decided);
404
440
  const again = await changer.api.approvals.decide(approval!.id, { approve: true, digest: shown.digest, basis: shown.basis }).then(() => null, (error: unknown) => error);
405
441
  expect(again instanceof CoffreError && again.status === 409, 'an approval was decided twice', again);
406
- const reported = await call(write, 'archive_secret', { secret: `${place}/OLD` });
442
+ // A client with no requestState names its approval to be told how it ended (docs/design/0.16.md, "The server", an MCP client's changes).
443
+ const reported = await call(write, 'archive_secret', { secret: `${place}/OLD`, approval: approval!.id });
407
444
  expect(reported.result?.structuredContent?.status === 'approved' && reported.result.isError !== true, 'calling again does not report the approved change', reported.result);
408
445
 
409
446
  // With URL elicitation: asked to open the page, and the retry's requestState is that call's only.
@@ -446,12 +483,29 @@ export async function mcpChanges(deployment: Deployment, people: People, canarie
446
483
  // No value in any answer; each change made once, by the person, via the client and its approval.
447
484
  const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
448
485
  expect(leaked.length === 0, 'a change tool answered with a secret value', leaked);
449
- const { entries } = await admin.api.audit.list({ actor: changer.member, limit: 500 });
450
- const viaApproval = (action: string) => entries.filter((entry) => entry.action === action && entry.decision === 'allow' && typeof (entry.metadata.via as { approvalId?: unknown } | undefined)?.approvalId === 'string');
451
- const once = { archive: viaApproval('secret.archive').length, rename: viaApproval('secret.rename').length, write: viaApproval('secret.write').length };
452
- expect(once.archive === 1 && once.rename === 1 && once.write === 2, 'the changes are not each made once, via the client and its approval', once);
453
- const decisions = entries.filter((entry) => entry.action === 'mcp.approve').map((entry) => `${entry.decision} ${entry.reason ?? ''}`.trim()).sort();
454
- 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);
486
+ // 0.16's change entry names the connection (`via`) and the approval (`approval`) by their IDs; the page lists
487
+ // each once the signer has published it, within its minute.
488
+ let entries: AuditEntryView[] = [];
489
+ let once = {};
490
+ const changed = () => {
491
+ const connections = new Set(entries.filter((entry) => entry.action === 'mcp.connect' && entry.metadata.client === clientId).map((entry) => entry.metadata.connection));
492
+ const viaApproval = (action: string) => entries.filter((entry) => entry.action === action && entry.decision === 'allow' && connections.has(entry.metadata.via) && typeof entry.metadata.approval === 'string');
493
+ // 0.16 logs an archiving as a write whose key's line says `archived` (0.14.7 logged it as `secret.archive`).
494
+ const archives = (entry: AuditEntryView) => Array.isArray(entry.metadata.keys) && entry.metadata.keys.some((line: { archived?: unknown }) => line.archived === true);
495
+ const writes = viaApproval('secret.write');
496
+ once = { archive: writes.filter(archives).length, rename: viaApproval('secret.rename').length, write: writes.filter((entry) => !archives(entry)).length };
497
+ return JSON.stringify(once) === JSON.stringify({ archive: 1, rename: 1, write: 2 });
498
+ };
499
+ await until('the changes on the log', async () => {
500
+ ({ entries } = await admin.api.audit.list({ actor: changer.member, limit: 500 }));
501
+ return changed();
502
+ }, PUBLISHED_SECONDS).catch(() => undefined);
503
+ expect(changed(), 'the changes are not each made once, via the client and its approval', once);
504
+ // 0.16 logs a change's Approve as the change itself, naming its approval, and no `mcp.approve` beside it (that is a
505
+ // shown value's alone); the app refuses a decision already made or for another digest before the vault, which writes
506
+ // nothing (docs/design/0.16.md, "Where 0.14.7 answered otherwise"). 0.14.7 logged four approvals and those two refusals.
507
+ const decisions = entries.filter((entry) => entry.action === 'mcp.approve' || entry.action === 'mcp.deny').map((entry) => `${entry.action} ${entry.decision} ${entry.reason ?? ''}`.trim());
508
+ expect(decisions.length === 0, 'an approved change or a refused decision wrote an entry of its own', decisions);
455
509
  return `Read stepped up to Write, saying how as its result; Write ticked beyond what was asked; with Write, the link without URL elicitation and an elicitation with it; nothing changed until ${changer.email} approved on coffre's page, another person, another site and a CLI session 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`;
456
510
  }
457
511
 
@@ -483,7 +537,7 @@ export async function mcpValues(deployment: Deployment, people: People, canaries
483
537
  expect(page.kind === 'reveal', 'the approval page does not say it shows a value', page);
484
538
  const shown = await reader.api.approvals.decide(id!, { approve: true, digest: page.digest, basis: page.basis });
485
539
  expect(shown.shown.some((line) => line.value === dev.API_KEY), 'Reveal did not show the person the value');
486
- const told = await call(reading, 'show_secret_value', { secret: `${DEV}/API_KEY` });
540
+ const told = await call(reading, 'show_secret_value', { secret: `${DEV}/API_KEY`, approval: id });
487
541
  expect(told.result?.structuredContent?.status === 'approved', 'show_secret_value does not report the value as shown', told.result);
488
542
  const readLeaks = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
489
543
  expect(readLeaks.length === 0, 'a Read answer held a value', readLeaks);
@@ -1,13 +1,14 @@
1
1
  // Who holds what is the vault's record: an access change is its entry or
2
- // nothing, and a member row or grant written around it is refused at use.
3
- import { CoffreError } from '@coffre/client';
2
+ // nothing, and a member row or grant written around it is never honoured,
3
+ // or alerted on.
4
+ import { randomUUID } from 'node:crypto';
4
5
 
5
6
  import { Browser } from '../browser.ts';
6
- import { using } from '../database.ts';
7
+ import { using, type Sql } from '../database.ts';
7
8
  import type { Deployment } from '../harness.ts';
8
- import { expect, Skip, until } from '../report.ts';
9
+ import { expect, until } from '../report.ts';
9
10
  import { DEV, PROD, signIn, type People, type Person } from './people.ts';
10
- import { insertRow, logRefuses, query, restoreRow } from './storage.ts';
11
+ import { alerted, insertRow, logBodies, logRefuses, query, restoreRow } from './storage.ts';
11
12
 
12
13
  export async function member(deployment: Deployment, { admin }: People, name: string): Promise<Person> {
13
14
  const email = `${name}@conformance.example`;
@@ -19,74 +20,114 @@ export async function member(deployment: Deployment, { admin }: People, name: st
19
20
  return { email, member: principal, browser, api: browser.client() };
20
21
  }
21
22
 
22
- /** The row is refused at use, not only when someone explicitly verifies the log. */
23
+ /** The conformance project's prod environment, by id. */
24
+ async function prodId(sql: Sql): Promise<string> {
25
+ const [place] = await query<{ id: string }>(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
26
+ WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
27
+ expect(place !== undefined, 'no prod environment to forge a grant on');
28
+ return place.id;
29
+ }
30
+
31
+ /** A grant on prod, as the database's owner could write it: the member's own generation, and no MAC of the vault's. */
32
+ async function forgeGrant(sql: Sql, principal: string): Promise<string> {
33
+ const id = randomUUID();
34
+ await query(sql, `INSERT INTO grants (id, principal, generation, environment_id, role, granted_at, granted_by)
35
+ SELECT $1, id, generation, $2, 'viewer', now(), id FROM principals WHERE id = $3`, [id, await prodId(sql), principal]);
36
+ return id;
37
+ }
38
+
39
+ /**
40
+ * A use that reads nothing, refused as `refusal` says: 401 for a member row
41
+ * that is no longer the vault's, 403 for a place the member's sealed grants
42
+ * do not reach. The vault may answer from its memory of the caller for up
43
+ * to 2 seconds (D218), so it is asked again until then and a second more.
44
+ */
45
+ async function readsNothing(person: Person, path: string, values: string[], what: string, refusal: { status: 401; error: 'unauthenticated' } | { status: 403; reason: 'no_access' }): Promise<number> {
46
+ const started = Date.now();
47
+ for (;;) {
48
+ const response = await person.browser.send('POST', '/api/reveals', { path });
49
+ const text = await response.text();
50
+ expect(!response.ok, `${what} was honoured`, text);
51
+ expect(!values.some((value) => text.includes(value)), `${what} was refused with a value`, text);
52
+ const body = JSON.parse(text) as { error?: string; reason?: string };
53
+ const as = 'error' in refusal ? body.error === refusal.error : body.reason === refusal.reason;
54
+ if (response.status === refusal.status && as) return response.status;
55
+ expect(Date.now() - started < 3000, `${what} was refused as ${response.status}, not ${refusal.status}`, text);
56
+ await new Promise((resolve) => setTimeout(resolve, 250));
57
+ }
58
+ }
59
+
60
+ const NO_ACCESS = { status: 403, reason: 'no_access' } as const;
61
+
62
+ /**
63
+ * A member row or grant written around the vault: a forged grant or an
64
+ * edited member is never honoured, since its MAC is not the vault's; a
65
+ * genuine older grant put back is served until someone acts, as an
66
+ * ordinary revocation is until the vault's memory lapses (D216). The
67
+ * signer names a row inserted or put back in its alert within the minute;
68
+ * a row edited under the MAC it had is refused at use and not named, since
69
+ * the minute's check compares MACs (docs/design/0.16.md, "What it
70
+ * promises" and "How long the log is kept"). 0.14 refused all three at
71
+ * use and logged `vault.tampered`; 0.16 logs no such entry, and its
72
+ * members' `tampered` is always false (docs/design/0.16.md, "Members").
73
+ */
23
74
  export async function memberTampering(deployment: Deployment, people: People, kind: 'grant' | 'member' | 'stale'): Promise<string> {
24
75
  const person = await member(deployment, people, `tamper-${kind}`);
25
76
  const { admin } = people;
26
- const path = kind === 'grant' ? PROD : DEV;
27
- const protectedValues = Object.values((await admin.api.secrets.reveal(path)).values);
77
+ const protectedValues = Object.values((await admin.api.secrets.reveal(PROD)).values);
78
+ let refused = 0;
28
79
  await using(deployment.database(), async (sql) => {
29
- const [before] = await query(sql, 'SELECT * FROM vault_members WHERE principal = $1', [person.member]);
30
- const grants = await query(sql, 'SELECT * FROM vault_grants WHERE principal = $1', [person.member]);
80
+ const [before] = await query(sql, 'SELECT * FROM principals WHERE id = $1', [person.member]);
81
+ const grants = await query<{ id: string }>(sql, 'SELECT * FROM grants WHERE principal = $1', [person.member]);
31
82
  expect(before !== undefined && grants.length === 1, 'the fixture has no member row and grant');
32
- let restored = before;
33
83
  let held = grants;
34
- if (kind === 'stale') {
35
- await admin.api.access.set(person.member, { [DEV]: 'developer' });
36
- [restored] = await query(sql, 'SELECT * FROM vault_members WHERE principal = $1', [person.member]);
37
- held = await query(sql, 'SELECT * FROM vault_grants WHERE principal = $1', [person.member]);
38
- await restoreRow(sql, 'vault_members', before, 'principal');
39
- await query(sql, 'UPDATE vault_grants SET role = $1, granted_at = $2, granted_by = $3 WHERE principal = $4',
40
- [grants[0].role, grants[0].granted_at, grants[0].granted_by, person.member]);
41
- } else if (kind === 'member') {
42
- // The instance role, as the database's owner could set it: an Owner reads every value.
43
- await query(sql, "UPDATE vault_members SET role = 'owner' WHERE principal = $1", [person.member]);
44
- } else {
45
- const [place] = await query<{ id: string }>(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
46
- WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
47
- expect(place !== undefined, 'no prod environment to forge a grant on');
48
- await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by) VALUES ($1, $2, 'viewer', $3, $4)`,
49
- [person.member, place.id, Date.now(), person.member]);
50
- }
84
+ let named: [string, string] | undefined;
51
85
  try {
52
- const response = await person.browser.send('POST', '/api/reveals', { path });
53
- const text = await response.text();
54
- expect(response.status === 401 && (JSON.parse(text) as { error: string }).error === 'unauthenticated', 'the tampered credential was not refused', text);
55
- expect(!protectedValues.some((value) => text.includes(value)), 'the tampered refusal carried a value', text);
56
- const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
57
- AND subject_principal = $1 AND code = $2`, [person.member, kind === 'stale' ? 'stale' : 'mac']);
58
- expect(reports.length > 0, 'the tampering was not logged at use', reports);
59
- expect((await admin.api.members.get(person.member)).status === 'tampered', 'the refused member is not marked tampered');
60
- const verified = await admin.api.audit.verify();
61
- expect(!verified.ok && verified.author === 'vault' && verified.failedAtSeq === null, 'the tampered member verifies', verified);
86
+ if (kind === 'stale') {
87
+ // The viewer's grant, genuine and sealed, put back once the vault has made them a developer.
88
+ await admin.api.access.set(person.member, { [DEV]: 'developer' });
89
+ held = await query(sql, 'SELECT * FROM grants WHERE principal = $1', [person.member]);
90
+ await query(sql, 'DELETE FROM grants WHERE principal = $1', [person.member]);
91
+ for (const grant of grants) await insertRow(sql, 'grants', grant);
92
+ named = ['grants', grants[0]!.id];
93
+ // Served while it stands: the viewer's grant still reads dev.
94
+ expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== undefined, 'a genuine older grant put back was not served');
95
+ } else if (kind === 'member') {
96
+ // The instance role, as the database's owner could set it: an Owner reads every value.
97
+ await query(sql, "UPDATE principals SET role = 'owner' WHERE id = $1", [person.member]);
98
+ refused = await readsNothing(person, PROD, protectedValues, 'an edited member row', { status: 401, error: 'unauthenticated' });
99
+ } else {
100
+ named = ['grants', await forgeGrant(sql, person.member)];
101
+ refused = await readsNothing(person, PROD, protectedValues, 'a forged grant', NO_ACCESS);
102
+ }
103
+ if (named !== undefined) await alerted(deployment, ...named);
62
104
  } finally {
63
- await restoreRow(sql, 'vault_members', restored, 'principal');
64
- await query(sql, 'DELETE FROM vault_grants WHERE principal = $1', [person.member]);
65
- for (const grant of held) await insertRow(sql, 'vault_grants', grant);
105
+ await restoreRow(sql, 'principals', before, 'id');
106
+ await query(sql, 'DELETE FROM grants WHERE principal = $1', [person.member]);
107
+ for (const grant of held) await insertRow(sql, 'grants', grant);
66
108
  }
67
109
  });
68
110
  expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== undefined, 'restoring the member did not restore legitimate use');
69
- expect((await admin.api.audit.verify()).ok, 'the restored member did not verify');
70
- return `${kind === 'stale' ? 'a genuine older member row, put back,' : `a forged ${kind}`} refused at use as tampered, logged, then restored`;
111
+ if (kind === 'stale') return 'a genuine older grant, put back, served and named in the signer\'s alert, then restored';
112
+ if (kind === 'member') return `an edited member row refused at use (${refused}), then restored`;
113
+ return `a forged grant refused at use (${refused}) and named in the signer's alert, then restored`;
71
114
  }
72
115
 
73
- /** Access changes have one author, the vault that commits the changed rows. */
116
+ /** Admission, grant, revoke, removal and re-admission are each the vault's entry, naming the member. */
74
117
  export async function accessAuthorship(deployment: Deployment, people: People): Promise<string> {
75
118
  const person = await member(deployment, people, 'access-actions');
76
119
  const { admin } = people;
77
120
  await admin.api.access.set(person.member, { [DEV]: null });
78
121
  await admin.api.members.remove(person.member);
79
122
  await admin.api.members.add(person.member);
80
- await using(deployment.database(), async (sql) => {
81
- const access = await sql.query<{ author: string; action: string; subject_principal: string }>(`SELECT author, action, subject_principal FROM audit_log
82
- WHERE decision = 'allow' AND (action LIKE 'access.%' OR action LIKE 'member.%' OR action LIKE 'directory.%' OR action LIKE 'grant.%' OR action LIKE 'principal.%')`);
83
- expect(access.length > 0 && access.every((entry) => entry.author === 'vault'), 'an access change was written by the app', access.filter((entry) => entry.author !== 'vault'));
84
- const actions = access.filter((entry) => entry.subject_principal === person.member).map((entry) => entry.action);
85
- for (const action of ['member.add', 'member.remove', 'member.restore', 'access.grant', 'access.revoke']) {
86
- expect(actions.includes(action), `${action} left no vault entry`, actions);
87
- }
88
- });
89
- return 'admission, grant, revoke, removal and re-admission are only vault entries';
123
+ // Read from the database: an entry the signer has not taken yet is not on the audit page, and the app writes none.
124
+ const actions = (await using(deployment.database(), logBodies))
125
+ .filter((body) => body.detail.refused === undefined && body.objects.includes(`principal:${person.member}`))
126
+ .map((body) => body.action);
127
+ for (const action of ['member.add', 'member.remove', 'member.restore', 'access.grant', 'access.revoke']) {
128
+ expect(actions.includes(action), `${action} left no vault entry`, actions);
129
+ }
130
+ return 'admission, grant, revoke, removal and re-admission are each a vault entry';
90
131
  }
91
132
 
92
133
  /** A failure to append may not commit an access change. */
@@ -95,8 +136,8 @@ export async function noAuditNoAccess(deployment: Deployment, people: People): P
95
136
  const { admin } = people;
96
137
  await using(deployment.database(), async (sql) => {
97
138
  const snapshot = async () => ({
98
- members: await sql.query('SELECT * FROM vault_members ORDER BY principal'),
99
- grants: await sql.query('SELECT * FROM vault_grants ORDER BY principal, project_id, environment_id'),
139
+ members: await sql.query('SELECT * FROM principals ORDER BY id'),
140
+ grants: await sql.query('SELECT * FROM grants ORDER BY principal, project_id, environment_id'),
100
141
  });
101
142
  const before = await snapshot();
102
143
  await logRefuses(sql, async () => {
@@ -115,54 +156,59 @@ export async function noAuditNoAccess(deployment: Deployment, people: People): P
115
156
  return 'grant, removal and admission commit nothing while the log refuses their entries';
116
157
  }
117
158
 
118
- /** Hold the head after the vault checked a member, then forge a grant before it seals them. */
159
+ /**
160
+ * The vault's change lock (`CHANGE_LOCK` in @coffre/vault's change.ts): a
161
+ * change's write takes it, then writes each row only if its MAC is still
162
+ * the one it read (docs/design/0.16.md, "A change").
163
+ */
164
+ const CHANGE_LOCK = '109330110509669';
165
+
166
+ /**
167
+ * Hold the change lock while a change waits to write, and forge a grant
168
+ * meanwhile: the vault seals only the rows it decided on, so the forged
169
+ * one stays unsealed and reaches nothing. 0.14's race held its log's head,
170
+ * which a change of 0.16 never locks.
171
+ */
119
172
  export async function sealingRace(deployment: Deployment, people: People): Promise<string> {
120
- if (deployment.vaultRuntime === null) throw new Skip('SQLite prevents a write beside a decision; the vault suite covers review R2 on Postgres');
121
173
  const [login] = await using(deployment.vaultRuntime(), (sql) => sql.query<{ name: string }>('SELECT current_user AS name'));
122
174
  const person = await member(deployment, people, 'seal-race');
123
175
  const { admin } = people;
176
+ const protectedValues = Object.values((await admin.api.secrets.reveal(PROD)).values);
177
+ let forged: string | undefined;
124
178
  try {
125
179
  await using(deployment.database(), (holder) => using(deployment.database(), async (sql) => {
126
- const [place] = await query<{ id: string }>(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
127
- WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
128
- const [head] = await sql.query<{ next_seq: string | number }>('SELECT next_seq FROM audit_chain_head');
129
180
  const [owner] = await holder.query<{ pid: number }>('SELECT pg_backend_pid() AS pid');
130
- expect(place !== undefined, 'no prod environment for the sealing race');
131
181
  await holder.exec('BEGIN');
132
182
  let pending;
183
+ let outcome: { value: unknown } | { error: unknown } | undefined;
133
184
  try {
134
- await holder.query('SELECT 1 FROM audit_chain_head FOR UPDATE');
185
+ await holder.query(`SELECT pg_advisory_xact_lock(${CHANGE_LOCK})`);
135
186
  pending = admin.api.access.set(person.member, { [DEV]: 'developer' }).then(
136
187
  (value) => ({ value }), (error: unknown) => ({ error }),
137
188
  );
138
189
  // The blocking PID proves where the request stopped; no sleep guesses the window.
139
- await until('the vault decision waiting on our head', async () => {
190
+ await until("the vault's change waiting on the change lock", async () => {
140
191
  const waiting = await query(sql, `SELECT pid FROM pg_stat_activity WHERE datname = current_database()
141
192
  AND usename = $1 AND $2 = ANY(pg_blocking_pids(pid))`, [login.name, owner.pid]);
142
193
  return waiting.length > 0;
143
194
  }, 10);
144
- await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by)
145
- VALUES ($1, $2, 'viewer', $3, $4)`, [person.member, place.id, Date.now(), person.member]);
195
+ forged = await forgeGrant(sql, person.member);
146
196
  } finally {
147
197
  await holder.exec('ROLLBACK');
148
- if (pending !== undefined) await pending;
198
+ if (pending !== undefined) outcome = await pending;
149
199
  }
150
- expect(pending !== undefined, 'the access decision was not started');
151
- const result = await pending;
152
- expect('error' in result && result.error instanceof CoffreError && result.error.status === 403
153
- && result.error.code === 'vault_refused' && result.error.reason === 'tampered',
154
- 'a grant inserted during the decision was not refused as tampered', result);
155
- const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
156
- AND subject_principal = $1 AND code = 'mac' AND seq >= $2`, [person.member, head.next_seq]);
157
- expect(reports.length > 0, 'the grant inserted during the decision was not reported', reports);
158
- const response = await person.browser.send('POST', '/api/reveals', { path: PROD });
159
- const body = await response.json() as { error: string };
160
- expect(response.status === 401 && body.error === 'unauthenticated', 'the forged grant let its member read', body);
200
+ // The change went through once the lock was let go: otherwise the grant keeps no MAC whatever sealing does.
201
+ expect(outcome !== undefined && 'value' in outcome, 'the change that waited on the lock did not go through', outcome !== undefined && 'error' in outcome ? String(outcome.error) : outcome);
202
+ const [dev] = await query<{ role: string }>(sql, `SELECT g.role FROM grants g JOIN environments e ON e.id = g.environment_id
203
+ JOIN projects p ON p.id = e.project_id WHERE g.principal = $1 AND p.slug = 'conformance' AND e.slug = 'dev' AND length(g.mac) > 0`, [person.member]);
204
+ expect(dev?.role === 'developer', 'the change that waited on the lock did not make the member a developer on dev', dev);
205
+ const [row] = await query<{ mac: Uint8Array }>(sql, 'SELECT mac FROM grants WHERE id = $1', [forged]);
206
+ expect(row !== undefined && row.mac.length === 0, 'the change sealed the grant inserted while it waited', row);
207
+ await readsNothing(person, PROD, protectedValues, 'the grant inserted while the change waited', NO_ACCESS);
161
208
  }));
162
209
  } finally {
163
- // Removal recovers this disposable member even if a broken build sealed the forged grant.
210
+ if (forged !== undefined) await using(deployment.database(), (sql) => query(sql, 'DELETE FROM grants WHERE id = $1', [forged]));
164
211
  await admin.api.members.remove(person.member);
165
212
  }
166
- expect((await admin.api.audit.verify()).ok, 'the log did not verify after recovering the sealing-race member');
167
- return 'a grant inserted while the vault waited on the head was refused as tampered and logged; removal recovered the member';
213
+ return 'a grant inserted while a change waited on the change lock was not sealed by it, and reaches nothing; removal recovered the member';
168
214
  }
@@ -97,12 +97,7 @@ export async function pagesInBrowser(deployment: Deployment, admin: Person, brow
97
97
  const person = `/users/${encodeURIComponent(admin.email)}`;
98
98
  const pages = [
99
99
  ...PAGES,
100
- ['/settings', 'Settings', 'General'],
101
- ['/settings?tab=roles', 'Settings', 'Roles'],
102
- // Its global view, and the old Instance roles tab's URL, which lands there: the same tab lit.
103
- ['/settings?tab=roles&roles=global', 'Settings', 'Roles'],
104
- ['/settings?tab=instance-roles', 'Settings', 'Roles'],
105
- ['/settings?tab=keys', 'Settings', 'Keys'],
100
+ ['/settings', 'Settings', null],
106
101
  [person, admin.email, 'Access'],
107
102
  [`${person}/apps`, admin.email, 'Connected apps'],
108
103
  [`${person}/activity`, admin.email, 'Activity'],
@@ -131,7 +126,7 @@ export async function pagesInBrowser(deployment: Deployment, admin: Person, brow
131
126
  expect(ways.join(' then ') === 'Sign in with OIDC then Bearer tokens', `${path} does not show OIDC, then bearer tokens`, loaded.cards);
132
127
  expect(loaded.dialog?.includes('Removing revokes 0 grants, 0 bearer tokens') === true, `${path}: Remove… does not preview what it would revoke`, loaded.dialog);
133
128
  }
134
- if (path === '/settings?tab=keys') expect(loaded.cards.includes('Keys'), `${path} does not show what the keys are checked against`, loaded.cards);
129
+ if (path === '/settings') expect(loaded.cards.includes('Keys'), '/settings does not show what the keys are checked against', loaded.cards);
135
130
  if (path === `${person}/apps`) expect(loaded.cards.includes('Connected apps'), `${path} does not show an owner the person's connected apps`, loaded.cards);
136
131
  if (path === '/account/apps') {
137
132
  expect(loaded.cards.join(' then ') === 'Connect an app then Connected apps', `${path} does not show Connect an app, then the connected apps`, loaded.cards);
@@ -183,8 +178,7 @@ function pem(): string {
183
178
  /**
184
179
  * A row's value field, opened by the button `open` names, and `text`
185
180
  * pasted into it as the clipboard would, over the characters `select`
186
- * picks; then saved by the button `save` names, under the table. A new
187
- * secret is named first.
181
+ * picks; then the row saved by its own button. A new secret is named first.
188
182
  * Answers which field has the focus after the paste, and where its caret is.
189
183
  */
190
184
  const pasteInto = (steps: { open: string; name?: string; field: string; select?: [number, number]; text: string; save: string }) => `(async () => {
@@ -222,7 +216,7 @@ const pasteInto = (steps: { open: string; name?: string; field: string; select?:
222
216
 
223
217
  /**
224
218
  * A new secret's value typed as a person would: `first`, Shift+Enter while composing (which does nothing), Shift+Enter, then
225
- * `second` into whichever field the page has by then; then saved, under the table.
219
+ * `second` into whichever field the page has by then; then the row saved.
226
220
  * Answers which field has the focus after the line break, and where its caret is.
227
221
  */
228
222
  const typeLines = (steps: { name: string; field: string; first: string; second: string; save: string }) => `(async () => {
@@ -283,7 +277,7 @@ export async function pastedValues(deployment: Deployment, admin: Person, browse
283
277
  const chrome = await Chrome.open(executable);
284
278
  try {
285
279
  const caret = added.length;
286
- const adding = await chrome.load(page, admin.browser.cookies(), undefined, pasteInto({ open: 'Add secret', name: 'PASTED_KEY', field: 'Value for PASTED_KEY', text: added, save: 'Save' }));
280
+ const adding = await chrome.load(page, admin.browser.cookies(), undefined, pasteInto({ open: 'Add secret', name: 'PASTED_KEY', field: 'Value for PASTED_KEY', text: added, save: 'Save PASTED_KEY now' }));
287
281
  expect(adding.errors.length === 0, `${page} reported errors in the browser`, adding.errors.join('\n'));
288
282
  expect(
289
283
  JSON.stringify(adding.answer) === JSON.stringify({ field: 'textarea', label: 'Value for PASTED_KEY', caret: [caret, caret] }),
@@ -291,7 +285,7 @@ export async function pastedValues(deployment: Deployment, admin: Person, browse
291
285
  adding.answer,
292
286
  );
293
287
  const at = [8, 20] as const;
294
- const editing = await chrome.load(page, admin.browser.cookies(), undefined, pasteInto({ open: 'Edit EDITED_KEY', field: 'New value for EDITED_KEY', select: [...at], text: edit.replaceAll('\n', '\r\n'), save: 'Save' }));
288
+ const editing = await chrome.load(page, admin.browser.cookies(), undefined, pasteInto({ open: 'Edit EDITED_KEY', field: 'New value for EDITED_KEY', select: [...at], text: edit.replaceAll('\n', '\r\n'), save: 'Save EDITED_KEY now' }));
295
289
  expect(editing.errors.length === 0, `${page} reported errors in the browser`, editing.errors.join('\n'));
296
290
  const after = at[0] + edit.length;
297
291
  expect(
@@ -299,7 +293,7 @@ export async function pastedValues(deployment: Deployment, admin: Person, browse
299
293
  "a PEM key pasted into an edited value did not leave a textarea focused, its caret after the paste",
300
294
  editing.answer,
301
295
  );
302
- const typing = await chrome.load(page, admin.browser.cookies(), undefined, typeLines({ name: 'TYPED_KEY', field: 'Value for TYPED_KEY', first: 'abc', second: 'def', save: 'Save' }));
296
+ const typing = await chrome.load(page, admin.browser.cookies(), undefined, typeLines({ name: 'TYPED_KEY', field: 'Value for TYPED_KEY', first: 'abc', second: 'def', save: 'Save TYPED_KEY now' }));
303
297
  expect(typing.errors.length === 0, `${page} reported errors in the browser`, typing.errors.join('\n'));
304
298
  expect(
305
299
  JSON.stringify(typing.answer) === JSON.stringify({ field: 'textarea', label: 'Value for TYPED_KEY', value: 'abc\n', caret: [4, 4] }),
@@ -321,8 +315,7 @@ export async function pastedValues(deployment: Deployment, admin: Person, browse
321
315
  * A refused sign-in, said once: the page coffre sends the browser back to
322
316
  * shows why, rendered by the server, then takes it out of the address, so a
323
317
  * reload is a clean retry. The refusal is a real one: a member's address
324
- * that GitHub now gives another account, which names the provider the
325
- * address already signs in with.
318
+ * that GitHub now gives another account, said bare: 0.16 names no provider.
326
319
  */
327
320
  export async function signinErrorOnce(deployment: Deployment, admin: Person, browser: string | null): Promise<string> {
328
321
  const executable = chromeOrSkip(browser);
@@ -335,9 +328,10 @@ export async function signinErrorOnce(deployment: Deployment, admin: Person, bro
335
328
  const refused = await signIn(deployment, visitor, email);
336
329
  expect(!refused.ok && refused.error === 'account_mismatch', 'another GitHub account with a member\'s address was not refused as account_mismatch', refused);
337
330
  const back = new URL(refused.location, deployment.origin);
338
- expect(back.searchParams.get('with') === 'github', `the refusal does not name the provider the address signs in with: ${refused.location}`);
331
+ // 0.16's refusal is bare: it names no provider (docs/design/0.16.md, "Operations", signing in), where 0.14.7 added `with` and `via`.
332
+ expect(back.pathname === '/login' && back.search === '?error=account_mismatch', `the refusal is not sent back to /login?error=account_mismatch: ${refused.location}`);
339
333
 
340
- const said = 'already signs in with another GitHub account';
334
+ const said = 'already signs in with a different account';
341
335
  const html = await visitor.fetch(refused.location);
342
336
  expect(html.status === 200 && (await html.text()).includes(said), `${refused.location}, without scripts, does not say the address ${said}`, html.status);
343
337
  const chrome = await Chrome.open(executable);
@@ -123,6 +123,17 @@ export async function setUpLive(admin: Person, canaries: Canaries) {
123
123
  }
124
124
 
125
125
  /** The canaries in one environment, by key. */
126
+ /**
127
+ * A value written over one the checks made. The old one stays, under a name
128
+ * no place has (`valuesIn` never lists it), for the scans to look for: its
129
+ * version is still stored, and may still be in some output.
130
+ */
131
+ export function replaced(canaries: Canaries, path: string, value: string): void {
132
+ const was = canaries[path];
133
+ if (was !== undefined) canaries[`replaced ${Object.keys(canaries).length} ${path}`] = was;
134
+ canaries[path] = value;
135
+ }
136
+
126
137
  export function valuesIn(canaries: Canaries, environment: string): Record<string, string> {
127
138
  return Object.fromEntries(
128
139
  Object.entries(canaries)