@coffre/conformance 0.14.7 → 0.16.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/conformance",
3
- "version": "0.14.7",
3
+ "version": "0.16.0",
4
4
  "description": "Checks a coffre deployment from outside: what it must never do, whatever code it runs.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -30,18 +30,16 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "jose": "6.2.3",
33
- "pg": "8.16.3",
34
- "@coffre/db": "0.14.7",
35
- "@coffre/cli": "0.14.7"
33
+ "@coffre/cli": "0.16.0",
34
+ "@coffre/db": "0.16.0"
36
35
  },
37
36
  "devDependencies": {
38
37
  "@types/node": "26.1.1",
39
- "@types/pg": "8.20.0",
40
38
  "oauth4webapi": "3.8.8",
41
39
  "tsdown": "0.23.0",
42
40
  "typescript": "5.9.3",
43
41
  "vite": "8.1.5",
44
- "@coffre/client": "0.14.7"
42
+ "@coffre/client": "0.16.0"
45
43
  },
46
44
  "scripts": {
47
45
  "build": "tsdown",
package/src/bundle.ts CHANGED
@@ -13,21 +13,18 @@ import { join, relative } from 'node:path';
13
13
  * A test holds this to the schema (@coffre/db).
14
14
  */
15
15
  export const SERVER_TABLES = [
16
- 'audit_chain_head',
17
- 'audit_log',
18
- 'capped_alerts',
19
- 'consumed_tokens',
20
- 'device_authorizations',
16
+ 'audit_archive',
17
+ 'device_logins',
21
18
  'dismissed_keys',
19
+ 'enrollment_codes',
20
+ 'log_head',
21
+ 'log_index',
22
+ 'log_pending',
23
+ 'log_tiles',
22
24
  'mcp_approvals',
23
- 'mcp_connections',
24
25
  'oauth_clients',
25
- 'secret_references',
26
26
  'secret_versions',
27
- 'service_bindings',
28
- 'vault_grants',
29
- 'vault_members',
30
- 'vault_releases',
27
+ 'spent_id_tokens',
31
28
  ];
32
29
 
33
30
  /** What only server code carries. */
@@ -35,7 +32,6 @@ const MARKERS: { label: string; pattern: RegExp }[] = [
35
32
  { label: 'drizzle-orm', pattern: /drizzle:[A-Z]/ },
36
33
  ...SERVER_TABLES.map((name) => ({ label: `the table ${name}`, pattern: new RegExp(`\\b${name}\\b`) })),
37
34
  { label: 'pg', pattern: /cloudflare:sockets|pg-protocol|pgpass/ },
38
- { label: 'libsql', pattern: /@libsql|libsql/ },
39
35
  { label: 'a COFFRE_ variable read', pattern: /env\s*(\.|\[\s*['"`])COFFRE_/ },
40
36
  ];
41
37
 
@@ -2,7 +2,7 @@
2
2
  // own pages, until they leave, and never too much at once.
3
3
  import { bearer } from '../browser.ts';
4
4
  import type { Deployment } from '../harness.ts';
5
- import { expect, refused } from '../report.ts';
5
+ import { expect, refused, until } from '../report.ts';
6
6
  import { BULK, canary, DEV, PROD, PROJECT, signIn, valuesIn, type Canaries, type People } from './people.ts';
7
7
  import { connectedApp, discovers } from './mcp.ts';
8
8
  import { pollDevice, startDevice } from './signin.ts';
@@ -97,21 +97,25 @@ export async function offboarding(deployment: Deployment, { admin, leaver, servi
97
97
  // An app they connected is listed to an owner, and removal disconnects it.
98
98
  const app = await connectedApp(deployment, leaver);
99
99
  const before = await admin.api.members.get(leaver.member);
100
- expect(before.live.apps === 1 && before.apps.length === 1, "the owner's report does not list the app the leaver connected", before.apps);
100
+ // A report that does not list apps (null) has not listed the app either.
101
+ expect(before.live.apps === 1 && before.apps?.length === 1, "the owner's report does not list the app the leaver connected", before.apps);
101
102
  const { revoked, report } = await admin.api.members.remove(leaver.member);
102
103
  expect(report !== null, "an owner's removal does not answer with the leaver's report");
103
- expect(revoked.apps === 1 && report.live.apps === 0 && report.apps.length === 0, 'removal does not say it disconnected their app', { revoked, apps: report.apps });
104
+ expect(revoked.apps === 1 && report.live.apps === 0 && report.apps?.length === 0, 'removal does not say it disconnected their app', { revoked, apps: report.apps });
104
105
  expect(!(await discovers(deployment, app)), "a removed member's app still reaches /mcp");
105
106
  const exposed = report.exposed.map((entry) => `${entry.project}/${entry.environment}/${entry.key}`).sort();
106
107
  const expected = Object.keys(valuesIn(canaries, DEV)).map((key) => `${DEV}/${key}`).sort();
107
108
  expect(same(exposed, expected), 'the offboarding report does not name what they read', report.exposed);
108
109
 
109
- expect((await leaver.browser.fetch('/api/me')).status === 401, "a removed member's browser session still works");
110
+ // Within the vault's memory of who they are, 2 seconds (docs/design/0.16.md, D218): no change clears it.
111
+ await until("the refusal of a removed member's browser session", async () => (await leaver.browser.fetch('/api/me')).status === 401, 5);
112
+ await until("the refusal of a removed member's CLI session", () => leaver.cli.secrets.list(DEV).then(() => false, () => true), 5);
110
113
  await refused("a removed member's CLI session read secrets", leaver.cli.secrets.list(DEV));
111
114
  const again = await signIn(deployment, leaver.browser, leaver.email);
112
115
  expect(!again.ok, 'a removed member signed in again');
113
116
 
114
117
  await admin.api.members.remove(service.member);
118
+ await until("the refusal of a removed service's token", () => service.api.secrets.reveal(DEV).then(() => false, () => true), 5);
115
119
  await refused("a removed service's token read secrets", service.api.secrets.reveal(DEV));
116
120
  await admin.api.members.add(leaver.member);
117
121
  await admin.api.access.set(leaver.member, { [DEV]: 'developer' });
@@ -176,7 +180,7 @@ export async function instanceRoles(deployment: Deployment, { admin, reader }: P
176
180
  ['an admin demoted the Owner', () => reader.api.members.add(admin.member, { role: 'admin' })],
177
181
  ['an admin removed the Owner', () => reader.api.members.remove(admin.member)],
178
182
  ] as const) {
179
- // Not that the Owner is the last, which an Owner hears too: that only an Owner may.
183
+ // Not that the Owner is the last, which an Owner hears too: that only an Owner may, `not_allowed`, as in 0.14.7.
180
184
  const refusal = await refused(what, call());
181
185
  expect(refusal.status === 403 && refusal.reason === 'not_allowed', `${what}, or was refused for another reason`, refusal);
182
186
  }
@@ -1,15 +1,17 @@
1
1
  // What is written down: every value opened, by the vault that opened it;
2
- // every version stored, naming the vault's wrap of its key; no value without
3
- // its entry; and a log changed behind coffre's back is caught.
4
- import { createHash } from 'node:crypto';
5
-
6
- import type { AuditEntryView, CoffreClient } from '@coffre/client';
2
+ // every version stored, naming the vault's wrap of its key; and no value
3
+ // without its entry; and each entry the audit page lists proven against the
4
+ // head the vault signed. 0.14's checks that a log changed behind coffre's
5
+ // back is caught asked `GET /audit/verification`, which 0.16 drops: the
6
+ // page's proofs and `coffre verify --full` read the log now
7
+ // (docs/design/0.16.md, "The server").
8
+ import { judgeProofs, type AuditEntryView, type CoffreClient, type ProofJudgment } from '@coffre/client';
7
9
 
8
10
  import { using } from '../database.ts';
9
11
  import type { Deployment } from '../harness.ts';
10
- import { expect, until } from '../report.ts';
11
- import { appendOnlyLifted, entryFields, insertRow, LOG_REFUSAL, logRefuses, query } from './storage.ts';
12
- import { DEV, PROJECT, valuesIn, type Canaries, type People } from './people.ts';
12
+ import { expect, until, PUBLISHED_SECONDS } from '../report.ts';
13
+ import { LOG_REFUSAL, logBodies, logRefuses, query, type Body } from './storage.ts';
14
+ import { canary, DEV, PROJECT, replaced, valuesIn, type Canaries, type People } from './people.ts';
13
15
 
14
16
  /**
15
17
  * Every returned value read, committed by the vault under the response's
@@ -18,18 +20,28 @@ import { DEV, PROJECT, valuesIn, type Canaries, type People } from './people.ts'
18
20
  */
19
21
  export async function revealAudited(deployment: Deployment, { admin, reader }: People, canaries: Canaries, purpose: 'reveal' | 'run'): Promise<string> {
20
22
  const path = purpose === 'reveal' ? `${DEV}/API_KEY` : DEV;
23
+ // 0.16 logs the first use of a version in a minute, by credential and purpose (docs/design/0.16.md, "What it
24
+ // promises"): a version the reader read a moment ago gets no new entry. Each value read is written afresh first.
25
+ const fresh = Object.fromEntries(Object.keys(purpose === 'reveal' ? { API_KEY: '' } : valuesIn(canaries, DEV)).map((key) => [key, canary()]));
26
+ await admin.api.secrets.set(DEV, fresh);
27
+ for (const [key, value] of Object.entries(fresh)) replaced(canaries, `${DEV}/${key}`, value);
21
28
  const { operationId, values } = await reader.api.secrets.reveal(path);
22
29
  expect(typeof operationId === 'string' && operationId.length > 0, 'the read returned no operation id');
23
30
  const expected = purpose === 'reveal' ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
24
31
  expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), 'the read returned different values', values);
25
- const entries = await everyAuditEntry(admin.api);
26
- const bundle = entries.filter((entry) => entry.operationId === operationId);
32
+ // 0.16 answers a change or a read with its request's id as its operation, and its entry names that request. The page
33
+ // lists what the signer has published, within its minute.
34
+ let bundle: AuditEntryView[] = [];
35
+ await until('the read on the audit page', async () => {
36
+ bundle = (await everyAuditEntry(admin.api)).filter((entry) => entry.requestId === operationId);
37
+ return bundle.length > 0;
38
+ }, PUBLISHED_SECONDS).catch(() => undefined);
27
39
  expect(bundle.length === 1, 'the read of one place is not logged as one entry', bundle);
28
40
  const [entry] = bundle as [AuditEntryView];
29
41
  const read = entry.secrets ?? [{ key: entry.key, version: entry.version }];
30
42
  expect(JSON.stringify(read.map((secret) => secret.key).sort()) === JSON.stringify(Object.keys(expected).sort()), 'the read does not list each value once', entry);
31
43
  expect(entry.author === 'vault' && entry.action === 'secret.read' && entry.decision === 'allow', "the read's entry is not the vault's allowed read", entry);
32
- expect(entry.metadata.purpose === purpose && entry.requestId !== null, 'the read does not name its request and its purpose', entry);
44
+ expect(entry.metadata.purpose === purpose, 'the read does not name its purpose', entry);
33
45
  const { keys: listed } = await admin.api.secrets.list(DEV);
34
46
  for (const secret of read) {
35
47
  expect(secret.version === listed.find((key) => key.key === secret.key)?.version, `the entry for ${String(secret.key)} names another version`, entry);
@@ -38,16 +50,14 @@ export async function revealAudited(deployment: Deployment, { admin, reader }: P
38
50
  expect(own.entries.some((found) => found.seq === entry.seq), `the read is not in ${String(secret.key)}'s log`, own.entries.slice(0, 5));
39
51
  }
40
52
  await using(deployment.database(), async (sql) => {
41
- const rows = await query<{ author: string; action: string; decision: string; request_id: string | null; secret_id: string | null; metadata: string }>(sql,
42
- 'SELECT author, action, decision, request_id, secret_id, metadata FROM audit_log WHERE operation_id = $1', [operationId]);
53
+ const rows = (await logBodies(sql)).filter((body) => body.request === operationId);
43
54
  expect(rows.length === 1, 'the committed read is not one entry', rows);
44
- const [row] = rows as [(typeof rows)[number]];
45
- expect(row.author === 'vault' && row.action === 'secret.read' && row.decision === 'allow' && row.request_id !== null, 'the committed read is not the vault release', row);
46
- const metadata = JSON.parse(row.metadata) as { purpose: string; version?: number; secrets?: { secretId: string; version: number }[] };
47
- const committed = metadata.secrets ?? [{ secretId: row.secret_id!, version: metadata.version! }];
48
- expect(metadata.purpose === purpose && committed.length === Object.keys(expected).length, 'the committed read does not name its purpose and each secret', row);
55
+ const [row] = rows as [Body];
56
+ const committed = (row.detail.keys ?? []) as { secret: string; version: number }[];
57
+ expect(row.action === 'secret.read' && row.detail.refused === undefined, 'the committed read is not the vault release', row);
58
+ expect(row.detail.purpose === purpose && committed.length === Object.keys(expected).length, 'the committed read does not name its purpose and each secret', row);
49
59
  for (const secret of committed) {
50
- const [current] = await query<{ current_version: number }>(sql, 'SELECT current_version FROM secrets WHERE id = $1', [secret.secretId]);
60
+ const [current] = await query<{ current_version: number }>(sql, 'SELECT current_version FROM secrets WHERE id = $1', [secret.secret]);
51
61
  expect(current?.current_version === secret.version, 'the committed read names another version', row);
52
62
  }
53
63
  });
@@ -55,114 +65,185 @@ export async function revealAudited(deployment: Deployment, { admin, reader }: P
55
65
  }
56
66
 
57
67
  /**
58
- * Verification must reach the stored head, not just some intact prefix:
59
- * the page's, from the newest checkpoint, and the walk from the first
60
- * entry, a stretch a request, which ends where it does. And the audit page
61
- * is sent with its verdict, rendered by the server, never a badge to come.
68
+ * Each entry the audit page lists is held to the head the vault vouched for
69
+ * in the page's own call (docs/design/0.16.md, "The log's head, when
70
+ * asked"): every entry the head numbers proves against it, only those
71
+ * past it are not proven, and the page's stretch lacks none of either tree.
72
+ * What 0.14 asked of `GET /audit/verification`, which 0.16 drops; judged as
73
+ * `coffre verify instance` judges it (`judgeProofs`). The signer signs on
74
+ * its own, so a head stands within its minute.
62
75
  */
63
- export async function verification(deployment: Deployment, { admin }: People): Promise<string> {
64
- const anchored = await admin.api.audit.verify();
65
- expect(anchored.ok && anchored.remaining === 0, 'the log does not verify from its newest checkpoint', anchored);
66
- const result = await admin.api.audit.verifyWhole();
67
- expect(result.ok && result.remaining === 0, 'the full log does not verify to its end', result);
68
- expect(result.through !== null && anchored.through !== null && result.through >= anchored.through, 'the walk from the first entry stopped short', { anchored, result });
69
- await using(deployment.database(), async (sql) => {
70
- const [head] = await sql.query<{ next_seq: string | number; head_hash: Uint8Array }>('SELECT next_seq, head_hash FROM audit_chain_head');
71
- const [last] = await sql.query<{ seq: string | number; hash: Uint8Array }>('SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1');
72
- expect(head !== undefined && last !== undefined, 'the head or newest entry is missing');
73
- expect(BigInt(head.next_seq) === BigInt(last.seq) + 1n, 'the head does not name the newest entry');
74
- expect(Buffer.from(head.head_hash).equals(Buffer.from(last.hash)), 'the head has another hash');
75
- expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), 'verification stopped before the head', { result, head: head.next_seq });
76
- expect(BigInt(result.entries) === BigInt(head.next_seq), 'verification did not count every entry', result);
76
+ export async function auditProven({ admin }: People): Promise<string> {
77
+ let judged: ProofJudgment | undefined;
78
+ // A batch the signer publishes, or a pruning committed, between the vault
79
+ // reading its head and the app reading the log can leave a page honestly
80
+ // racing (`judgeProofs`): asked again until it is not, and judged as it
81
+ // last read if it still is.
82
+ await until('a page past the race between its head and its log', async () => {
83
+ judged = judgeProofs(await admin.api.audit.list({ detail: '1', limit: 200 }));
84
+ return !judged.racing;
85
+ }, PUBLISHED_SECONDS).catch((error: unknown) => {
86
+ if (judged === undefined) throw error;
77
87
  });
78
- const page = await admin.browser.fetch('/audit');
79
- const html = await page.text();
80
- expect(page.status === 200 && html.includes('chain-seal') && !html.includes('Verifying'), 'the audit page was not sent with its verification result', { status: page.status });
81
- return `${result.entries} entries verified through the head at ${result.through}, and the audit page sent with its result`;
88
+ const { head, proven, failed, misplaced, missing } = judged!;
89
+ expect(head !== null, 'the audit page has no head');
90
+ expect(failed.length === 0, 'entries the head numbers do not prove against it', failed.slice(0, 5));
91
+ expect(misplaced.length === 0, 'an entry the head numbers is not proven, or one past it is', { head, misplaced: misplaced.slice(0, 5) });
92
+ expect(missing !== null && missing.length === 0, "the page's stretch of the log lacks entries, or the page cannot say", { head, missing: missing?.slice(0, 5) });
93
+ expect(proven > 0, 'no entry on the page is proven', { head });
94
+ return `${proven} entries proven against the head of ${head.size}, signed at ${head.signedAt}, and none missing`;
82
95
  }
83
96
 
84
97
  /**
85
- * Each heartbeat has the vault sign the log up to its last entry, in an
86
- * entry of its own, after checking that the prefix it signed last is still
87
- * there. Two, so the second covers the first; then the log is verified from
88
- * its first entry, every checkpoint with it.
98
+ * The audit page is sent with what its proofs found, rendered by the
99
+ * server, never a verdict to come: each entry's row says its proof in
100
+ * `data-proof` (`proven`, `unproven` or `failed`), which the page's port
101
+ * writes; at least one row proves, none fails, and a log that proves is
102
+ * not shown as one coffre could not check.
89
103
  */
90
- export async function checkpoints(deployment: Deployment, { admin }: People): Promise<string> {
91
- const before = await admin.api.audit.verify();
92
- expect(before.ok, 'the log does not verify', before);
93
- await deployment.scheduled();
94
- await deployment.scheduled();
95
- const after = await admin.api.audit.verify();
96
- expect(after.ok && after.checkpoint !== null, 'the audit log is not verified and checkpointed', after);
97
- expect(after.checkpoint.seq > (before.through ?? -1), 'the checkpoint does not cover what was written before it', { before, after });
98
- const { entries } = await admin.api.audit.list({ detail: '1', limit: 20 });
99
- const signed = entries.filter((entry) => entry.action === 'audit.checkpoint' && entry.decision === 'allow');
100
- expect(
101
- signed.length >= 2 && signed.every((entry) => entry.author === 'vault' && entry.detail),
102
- "the checkpoints are not the vault's entries, hidden as detail",
103
- signed,
104
- );
105
- const [newest, previous] = signed as [AuditEntryView, AuditEntryView];
106
- expect(newest.metadata.seq === after.checkpoint.seq, 'the newest checkpoint entry is not the one verified', { signed, after });
107
- expect(previous.seq <= after.checkpoint.seq, 'the newest checkpoint does not cover the one before it', signed);
108
- return `${after.entries} entries verified through ${after.through}, checkpointed at ${after.checkpoint.seq}`;
104
+ export async function auditPageProofs({ admin }: People): Promise<string> {
105
+ let status = 0;
106
+ let html = '';
107
+ let proofs: string[] = [];
108
+ // The race `auditProven` waits out, in the page's own call: a batch signed
109
+ // between its head and its rows can leave every row shown honestly
110
+ // unproven. So a page of rows none of which proves is asked again; an
111
+ // empty one, a failed proof or an error is final.
112
+ await until('an audit page with a row its head numbers', async () => {
113
+ const page = await admin.browser.fetch('/audit');
114
+ status = page.status;
115
+ html = await page.text();
116
+ proofs = [...html.matchAll(/data-proof="([a-z]+)"/g)].map((match) => match[1]);
117
+ return status !== 200 || proofs.length === 0 || proofs.some((proof) => proof !== 'unproven');
118
+ }, PUBLISHED_SECONDS);
119
+ expect(status === 200 && !html.includes('Verifying'), 'the audit page was not sent with its proofs', { status });
120
+ const proven = proofs.filter((proof) => proof === 'proven').length;
121
+ expect(proven > 0, "the audit page's rows say no proof, or none proven", { status, proofs: proofs.slice(0, 10) });
122
+ expect(!proofs.includes('failed'), 'the audit page shows an entry whose proof failed', { proofs: proofs.slice(0, 10) });
123
+ expect(!html.includes('could not check the log'), 'the audit page says coffre could not check the log', { status });
124
+ return `the audit page sent with its proofs, rendered by the server: ${proven} of ${proofs.length} rows proven`;
109
125
  }
110
126
 
127
+ /** A row of `log` as the database holds it: its bigints as text, put back as they were. */
128
+ type LogRow = { seq: string; kept: boolean; leaf: string; body: string };
129
+
111
130
  /**
112
- * Every version the app says it stored names the vault's entry for the key
113
- * it was sealed with, `key.wrap` for a write and `key.rewrap` for a restore,
114
- * a mark and a loosening:
115
- * the same member, request and operation, the same secret and version. And
116
- * every value read is the vault's to log: the app keeps no reads of its own.
131
+ * The newest signed entry of a tree cut out of the log, by the database's
132
+ * owner (docs/design/0.16.md, "What the minute's run checks of the log"):
133
+ * the deployed signer's next check, within its minute, says `cut` of that
134
+ * tree in its alert, in the processes' output, and the audit page proves
135
+ * the entry nowhere, the gap a hole in its stretch. Put back, the page
136
+ * proves it again. The change cut first, then the use, whose finding says
137
+ * the alarm ran a later check. Each tree is cut once in a run, so no
138
+ * earlier finding can stand for it. That a finding put right is let go and
139
+ * said again is the vault's own tests' (signer-host.test.ts). An entry cut
140
+ * further back, or the newest once another is signed after it, is the
141
+ * re-check's, which reads the log a day after its last walk (D222): not
142
+ * within a run (docs/conformance.md).
117
143
  */
144
+ export async function logCut(deployment: Deployment, { admin }: People): Promise<string> {
145
+ const said = (tree: string) => deployment.output().split(`- cut ${tree}:`).length - 1;
146
+ const out: LogRow[] = [];
147
+ const seqs = new Set<number>();
148
+ await using(deployment.database(), async (sql) => {
149
+ const cut = async (kept: boolean): Promise<LogRow> => {
150
+ // Nothing waits: an entry signed after the cut would end its tree again, and leave the cut to the walk.
151
+ await until('nothing waiting to be signed', async () => (await query(sql, 'SELECT 1 FROM log_pending LIMIT 1')).length === 0, PUBLISHED_SECONDS);
152
+ const tree = kept ? 'kept' : 'aging';
153
+ expect(said(tree) === 0, `the signer named a cut of the ${tree} tree before one was made`);
154
+ const [row] = await query<LogRow>(sql, 'DELETE FROM log WHERE kept = $1 AND leaf = (SELECT max(leaf) FROM log WHERE kept = $1) RETURNING seq::text, kept, leaf::text, body', [kept]);
155
+ expect(row !== undefined, `the log has no ${tree} entry to cut`);
156
+ out.push(row);
157
+ await until(`the signer's alert naming - cut ${tree}:`, async () => said(tree) > 0, PUBLISHED_SECONDS);
158
+ // The page's stretch lacks it, by its tree's count, and lists it nowhere as proven.
159
+ const page = await admin.api.audit.list({ detail: '1', limit: 200 });
160
+ const seq = Number(row.seq);
161
+ seqs.add(seq);
162
+ const hole = page.holes?.some((gap) => (gap.above === null || gap.above > seq) && (gap.below === null || gap.below < seq) && gap[tree] > 0);
163
+ expect(page.entries.every((entry) => entry.seq !== seq || entry.proof !== 'proven'), 'the audit page proves an entry cut out of the log', { seq });
164
+ expect(hole === true, "the audit page's stretch does not lack the entry cut out of the log", { seq, holes: page.holes });
165
+ return row;
166
+ };
167
+ const putBack = async (row: LogRow) => {
168
+ await query(sql, 'INSERT INTO log (seq, kept, leaf, body) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING', [row.seq, row.kept, row.leaf, row.body]);
169
+ out.splice(out.indexOf(row), 1);
170
+ };
171
+ try {
172
+ await putBack(await cut(true));
173
+ await putBack(await cut(false));
174
+ } finally {
175
+ for (const row of [...out]) await putBack(row);
176
+ }
177
+ });
178
+ // Every entry back, and proven against the head again.
179
+ let judged: ProofJudgment | undefined;
180
+ let listed: AuditEntryView[] = [];
181
+ await until('a page past the race between its head and its log', async () => {
182
+ const page = await admin.api.audit.list({ detail: '1', limit: 200 });
183
+ [judged, listed] = [judgeProofs(page), page.entries];
184
+ return !judged.racing;
185
+ }, PUBLISHED_SECONDS).catch((error: unknown) => {
186
+ if (judged === undefined) throw error;
187
+ });
188
+ const { failed, misplaced, missing } = judged!;
189
+ const unproven = [...seqs].filter((seq) => !listed.some((entry) => entry.seq === seq && entry.proof === 'proven'));
190
+ expect(unproven.length === 0 && failed.length === 0 && misplaced.length === 0 && missing !== null && missing.length === 0, 'the entries put back are not proven again', { unproven, failed: failed.slice(0, 5), misplaced: misplaced.slice(0, 5), missing });
191
+ return "the newest change and use cut out of the log, each named in the signer's alert and proven nowhere on the audit page; put back, proven again";
192
+ }
193
+
194
+ /**
195
+ * Every version stored is the vault's write: one entry of the log names
196
+ * it, its secret and its number, among the keys the change wrote: a
197
+ * `secret.write` or `secret.loosen`, or the `environment.create` of a fork
198
+ * that copied it, naming who wrote it and the request. 0.14's app and vault each logged their
199
+ * half of a write, the app's naming the vault's seal of its key; in 0.16
200
+ * the vault writes the version and its one entry together, and the app
201
+ * writes nothing (docs/design/0.16.md, "Four rules").
202
+ */
203
+ const WRITES = new Set(['secret.write', 'secret.loosen', 'environment.create']);
204
+
118
205
  export async function writesAgree(deployment: Deployment): Promise<string> {
119
206
  return using(deployment.database(), async (sql) => {
120
- const entries = await sql.query<{
121
- seq: number | string; author: string; action: string; decision: string; actor: string; metadata: string;
122
- project_id: string | null; environment_id: string | null; secret_id: string | null;
123
- request_id: string | null; operation_id: string | null; related_seq: string | number | null;
124
- }>('SELECT * FROM audit_log ORDER BY seq');
125
- const bySeq = new Map(entries.map((entry) => [String(entry.seq), entry]));
126
- const writes = entries.filter((entry) => entry.author === 'app' && entry.decision === 'allow'
127
- && (entry.action === 'secret.write' || entry.action === 'secret.restore' || entry.action === 'secret.loosen'));
128
- for (const entry of entries.filter((row) => row.author === 'app' && row.decision === 'allow')) {
129
- expect(entry.action !== 'secret.read', 'the app logged an allowed read of a value', entry);
130
- }
131
- for (const entry of writes) {
132
- // A write of a new value wraps a new key; a restore, a mark (which says the version it `kept`) and a loosening rewrap one.
133
- const seal = entry.action === 'secret.write' && JSON.parse(entry.metadata).kept === undefined ? 'key.wrap' : 'key.rewrap';
134
- const key = entry.related_seq === null ? undefined : bySeq.get(String(entry.related_seq));
135
- expect(key !== undefined && key.author === 'vault' && key.action === seal && key.decision === 'allow',
136
- `a ${entry.action} names no ${seal} of the vault's`, { entry, key });
137
- for (const field of ['actor', 'request_id', 'operation_id', 'project_id', 'environment_id'] as const) {
138
- expect(entry[field] === key[field], `the write and its seal have different ${field}`, { entry, key });
207
+ const written = new Map<string, number>();
208
+ for (const body of await logBodies(sql)) {
209
+ if (!WRITES.has(body.action) || body.detail.refused !== undefined) continue;
210
+ // Each is someone's, through one request the server passed the vault (docs/design/0.16.md, "The server").
211
+ const named = (field: unknown) => typeof field === 'string' && field !== '';
212
+ expect(named(body.actor) && named(body.request), `a committed ${body.action} names no actor or no request`, body);
213
+ for (const line of (body.detail.keys ?? []) as { secret?: string; version?: number }[]) {
214
+ if (line.version === undefined) continue;
215
+ const name = `${line.secret} ${line.version}`;
216
+ written.set(name, (written.get(name) ?? 0) + 1);
139
217
  }
140
- expect(entry.operation_id !== null && entry.request_id !== null, 'the committed write has no operation or request', entry);
141
- const metadata = JSON.parse(entry.metadata) as { version: number; key: string };
142
- const sealed = JSON.parse(key.metadata) as { secretId?: string; version: number; subject: string };
143
- expect((sealed.secretId ?? key.secret_id) === entry.secret_id && sealed.version === metadata.version
144
- && sealed.subject.split('/').at(-1) === metadata.key, 'the write names a seal of another secret or version', { entry, key });
145
218
  }
146
219
  const versions = await query<{ secret_id: string; version: number }>(sql,
147
220
  `SELECT v.secret_id, v.version FROM secret_versions v JOIN secrets s ON s.id = v.secret_id
148
221
  JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT]);
149
222
  expect(versions.length > 0, 'no stored versions were inspected');
150
223
  for (const version of versions) {
151
- const matching = writes.filter((entry) => entry.secret_id === version.secret_id
152
- && (JSON.parse(entry.metadata) as { version: number }).version === version.version);
153
- expect(matching.length === 1, 'a stored version has no unique app write entry', { version, matching });
224
+ const entries = written.get(`${version.secret_id} ${version.version}`) ?? 0;
225
+ expect(entries === 1, 'a stored version has no unique write entry', { version, entries });
154
226
  }
155
- return `${versions.length} stored versions, each with one app write naming the vault's seal in the same operation`;
227
+ return `${versions.length} stored versions, each written by one entry of the vault's, naming its actor and request`;
156
228
  });
157
229
  }
158
230
 
159
231
  /**
160
232
  * No value leaves without its entry: with the audit log refusing writes (a
161
233
  * trigger, as a full disk or a lost connection would), the vault cannot log
162
- * the read, so a reveal fails and carries nothing.
234
+ * the read, so a reveal fails and carries nothing. The vault logs a
235
+ * credential's read of a version once a minute (docs/design/0.16.md, "What
236
+ * it promises"), so the reveal asks for a key written just before, which no
237
+ * entry has said yet; it is archived after.
163
238
  */
239
+ const UNLOGGED = 'NO_AUDIT_NO_VALUE';
240
+
164
241
  export async function noAuditNoValue(deployment: Deployment, { admin }: People, canaries: Canaries): Promise<string> {
165
242
  let status = 0;
243
+ // Not a place's value for the checks after (it is gone at the end), but one for the scans to look for.
244
+ const value = canary();
245
+ canaries[`unlogged ${DEV}/${UNLOGGED}`] = value;
246
+ await admin.api.secrets.set(DEV, { [UNLOGGED]: value });
166
247
  await using(deployment.database(), (sql) => logRefuses(sql, async () => {
167
248
  const before = deployment.output().split(LOG_REFUSAL).length;
168
249
  const response = await admin.browser.send('POST', '/api/reveals', { path: DEV });
@@ -172,115 +253,17 @@ export async function noAuditNoValue(deployment: Deployment, { admin }: People,
172
253
  expect(status === 500 && body.error === 'internal_error' && body.reason === undefined,
173
254
  'the unlogged reveal did not fail as internal_error', { status, body });
174
255
  expect(body.message === 'something went wrong; see the server log', 'the audit failure exposed another reason', body);
175
- expect(!Object.values(canaries).some((value) => text.includes(value)), 'a reveal that could not be logged carried a value', text);
256
+ expect(!text.includes(value), 'a reveal that could not be logged carried the value it could not log', text);
257
+ expect(!Object.values(canaries).some((known) => text.includes(known)), 'a reveal that could not be logged carried a value', text);
176
258
  // A lost vault gives the same public error; the trigger must be its cause.
177
259
  await until('the injected audit refusal in the processes\' output', async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
178
260
  }, 'secret.read'));
179
261
  const after = await admin.api.secrets.reveal(DEV);
180
- expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`], 'reveals did not come back once the log took writes again');
262
+ expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`] && after.values[UNLOGGED] === value, 'reveals did not come back once the log took writes again');
263
+ await admin.api.secrets.set(DEV, { [UNLOGGED]: null });
181
264
  return `a reveal the audit log would not take: ${status} internal_error, its injected cause in the log, and no value`;
182
265
  }
183
266
 
184
- /** A publicly chained entry still needs the vault's MAC. */
185
- export async function forgedVaultEntry(deployment: Deployment, { admin }: People): Promise<string> {
186
- const verify = () => admin.api.audit.verify();
187
- expect((await verify()).ok, 'the log does not verify before the forged entry');
188
- await using(deployment.database(), async (sql) => {
189
- // An entry in the vault's name, chained after the last, with a MAC made up.
190
- const [head] = await sql.query<{ next_seq: number | string; head_hash: Uint8Array }>(
191
- 'SELECT next_seq, head_hash FROM audit_chain_head',
192
- );
193
- const [writer] = await sql.query<{ key_id: string }>("SELECT key_id FROM audit_log WHERE author = 'vault' ORDER BY seq DESC LIMIT 1");
194
- expect(writer !== undefined, 'no vault key id to copy');
195
- const seq = BigInt(head.next_seq);
196
- const forged = {
197
- seq, author: 'vault', key_id: writer.key_id, occurred_at: Date.now(), actor: 'user:forger@conformance.example',
198
- action: 'secret.read', decision: 'allow', metadata: '{}',
199
- };
200
- const prevHash = Buffer.from(head.head_hash);
201
- const mac = Buffer.alloc(32, 0x41);
202
- const hash = createHash('sha256').update('coffre.audit.chain.v2').update(prevHash).update(entryFields(forged)).update(mac).digest();
203
- await insertRow(sql, 'audit_log', { ...forged, prev_hash: prevHash, mac, hash });
204
- await query(sql, 'UPDATE audit_chain_head SET next_seq = $1, head_hash = $2', [seq + 1n, hash]);
205
- let inserted;
206
- try {
207
- inserted = await verify();
208
- } finally {
209
- await appendOnlyLifted(sql, () => query(sql, 'DELETE FROM audit_log WHERE seq = $1', [seq]));
210
- await query(sql, 'UPDATE audit_chain_head SET next_seq = $1, head_hash = $2', [seq, prevHash]);
211
- }
212
- expect(!inserted.ok && inserted.author === 'vault' && inserted.failedAtSeq === Number(seq), "an entry in the vault's name, chained but not by the vault, verifies", inserted);
213
- const removed = await verify();
214
- expect(removed.ok, 'the log did not verify once the entry was gone', removed);
215
- });
216
- return 'a publicly chained vault entry with a made-up MAC was refused at its sequence';
217
- }
218
-
219
- /**
220
- * Rewrite either author's first entry in place and require a fault at that
221
- * exact sequence: from the walk from the first entry, since the page's
222
- * check goes on from the newest checkpoint and reads nothing before it.
223
- */
224
- export async function rewrittenEntry(deployment: Deployment, { admin }: People, author: 'app' | 'vault'): Promise<string> {
225
- expect((await admin.api.audit.verifyWhole()).ok, 'the log does not verify before the rewrite');
226
- let seq = 0;
227
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
228
- const [row] = await query<{ seq: string | number; actor: string }>(sql,
229
- 'SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1', [author]);
230
- expect(row !== undefined, `no ${author} entry to rewrite`);
231
- seq = Number(row.seq);
232
- await query(sql, 'UPDATE audit_log SET actor = $1 WHERE seq = $2', ['user:nobody@conformance.example', row.seq]);
233
- try {
234
- const result = await admin.api.audit.verifyWhole();
235
- expect(!result.ok && result.failedAtSeq === Number(row.seq), 'a rewritten entry was not caught at its sequence', result);
236
- } finally {
237
- await query(sql, 'UPDATE audit_log SET actor = $1 WHERE seq = $2', [row.actor, row.seq]);
238
- }
239
- }));
240
- expect((await admin.api.audit.verifyWhole()).ok, 'the restored entry did not verify');
241
- return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
242
- }
243
-
244
- /** Remove an unreferenced entry, then put its exact bytes back. */
245
- export async function missingEntry(deployment: Deployment, { admin }: People, position: 'middle' | 'first' | 'batch'): Promise<string> {
246
- expect((await admin.api.audit.verify()).ok, 'the log does not verify before deletion');
247
- let seq = 0;
248
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
249
- const [row] = await sql.query(`SELECT * FROM audit_log WHERE ${position !== 'middle' ? `seq = ${position === 'first' ? 0 : 1000}` :
250
- "seq > 0 AND action = 'audit.heartbeat' AND NOT EXISTS (SELECT 1 FROM audit_log linked WHERE linked.related_seq = audit_log.seq)"} ORDER BY seq LIMIT 1`);
251
- expect(row !== undefined, 'no unreferenced entry to delete');
252
- seq = Number(row.seq);
253
- const linked = await query(sql, 'SELECT * FROM audit_log WHERE related_seq = $1', [row.seq]);
254
- await query(sql, 'UPDATE audit_log SET related_seq = NULL WHERE related_seq = $1', [row.seq]);
255
- await query(sql, 'DELETE FROM audit_log WHERE seq = $1', [row.seq]);
256
- try {
257
- const result = await admin.api.audit.verify();
258
- expect(!result.ok, 'a log with an entry missing verifies', result);
259
- if (position !== 'middle') expect(result.reason.includes(`sequence gap: expected seq ${position === 'first' ? 0 : 1000}`), 'the sequence gap was not reported', result);
260
- } finally {
261
- await insertRow(sql, 'audit_log', row);
262
- for (const entry of linked) await query(sql, 'UPDATE audit_log SET related_seq = $1 WHERE seq = $2', [entry.related_seq, entry.seq]);
263
- }
264
- }));
265
- expect((await admin.api.audit.verify()).ok, 'the restored log did not verify');
266
- const which = { first: 'the first', batch: 'the first past a page of 1,000', middle: 'a heartbeat' }[position];
267
- return `entry ${seq}, ${which}, deleted: caught, then put back`;
268
- }
269
-
270
- /** Last: keep the head, delete its newest entries, and require verification to notice. */
271
- export async function deletedTail(deployment: Deployment, { admin }: People): Promise<string> {
272
- const intact = await admin.api.audit.verify();
273
- expect(intact.ok && intact.checkpoint !== null, 'the log is not intact and checkpointed');
274
- const signed = intact.checkpoint.seq;
275
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
276
- await query(sql, 'UPDATE audit_log SET related_seq = NULL WHERE seq >= $1', [signed]);
277
- await query(sql, 'DELETE FROM audit_log WHERE seq >= $1', [signed]);
278
- }));
279
- const result = await admin.api.audit.verify();
280
- expect(!result.ok && result.author === 'app' && result.reason.includes('removed from the end'), 'the log verifies with its newest entries deleted', result);
281
- return 'the newest entries deleted, with the head left alone, were caught';
282
- }
283
-
284
267
  /** The whole log, detail included, newest first. */
285
268
  export async function everyAuditEntry(api: CoffreClient): Promise<AuditEntryView[]> {
286
269
  const all: AuditEntryView[] = [];