@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/dist/main.js CHANGED
@@ -3,34 +3,22 @@ import { createRequire } from "node:module";
3
3
  import { delimiter, dirname, join, relative, resolve } from "node:path";
4
4
  import { inspect, parseArgs } from "node:util";
5
5
  import { spawn } from "node:child_process";
6
- import { createHash, createHmac, createPrivateKey, hkdfSync, randomBytes, randomUUID, sign } from "node:crypto";
6
+ import { createHash, randomBytes, randomUUID } from "node:crypto";
7
7
  import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
8
8
  import { homedir, tmpdir } from "node:os";
9
9
  import { setTimeout as setTimeout$1 } from "node:timers/promises";
10
- import { DatabaseSync } from "node:sqlite";
11
- import pg from "pg";
12
- import { postgresConnection } from "@coffre/db/connect";
10
+ import { postgresSession } from "@coffre/db/connect";
13
11
  //#region src/database.ts
14
- async function postgres(url) {
15
- const client = new pg.Client(postgresConnection(url));
16
- await client.connect();
12
+ async function postgres(url, schema = "public") {
13
+ const client = postgresSession(url);
14
+ await client.unsafe("SELECT 1");
17
15
  return {
18
- engine: "postgres",
19
- query: async (statement, params = []) => (await client.query(statement, params)).rows,
20
- exec: async (statements) => void await client.query(statements),
16
+ schema,
17
+ query: async (statement, params = []) => [...await client.unsafe(statement, params)],
18
+ exec: async (statements) => void await client.unsafe(statements),
21
19
  close: () => client.end()
22
20
  };
23
21
  }
24
- /** A SQLite file, perhaps open in another process: it waits out their locks. */
25
- function sqlite(file) {
26
- const db = new DatabaseSync(file, { timeout: 5e3 });
27
- return {
28
- engine: "sqlite",
29
- query: async (statement, params = []) => db.prepare(statement).all(...params),
30
- exec: async (statements) => db.exec(statements),
31
- close: async () => db.close()
32
- };
33
- }
34
22
  async function using(open, use) {
35
23
  const sql = await open;
36
24
  try {
@@ -85,7 +73,7 @@ function drainingConfig(server) {
85
73
  }
86
74
  if (import.meta.main) console.log(drainingConfig(resolve(process.argv[2])));
87
75
  //#endregion
88
- //#region ../client/dist/src-BCKSeCER.js
76
+ //#region ../client/dist/src-JtgcfCXD.js
89
77
  /** Two colons or more, in hex: an IPv6 address, as `::1` or `2001:db8::1:443` with its port. */
90
78
  const IPV6 = /[0-9a-f]*:[0-9a-f]*:[0-9a-f:]*/i;
91
79
  const TIMEOUT_OR_REFUSAL = /* @__PURE__ */ new Set([
@@ -132,6 +120,21 @@ var Unreachable = class extends Error {
132
120
  this.origin = origin;
133
121
  }
134
122
  };
123
+ function judgeProofs(page) {
124
+ const { head, entries } = page;
125
+ const within = (entry) => head !== null && entry.seq < head.size;
126
+ const numbered = entries.filter(within).length;
127
+ const pruned = page.holes !== null && page.holes.length > 0 && page.holes.every((hole) => hole.kept === 0);
128
+ return {
129
+ head,
130
+ numbered,
131
+ proven: entries.filter((entry) => entry.proof === "proven").length,
132
+ failed: entries.filter((entry) => entry.proof === "failed"),
133
+ misplaced: entries.filter((entry) => entry.proof !== "failed" && within(entry) !== (entry.proof === "proven")),
134
+ missing: page.holes,
135
+ racing: numbered === 0 || pruned
136
+ };
137
+ }
135
138
  /** The API's error shape, `{ error, message }`, with the HTTP status. */
136
139
  var CoffreError = class extends Error {
137
140
  status;
@@ -276,7 +279,7 @@ function createClient(options) {
276
279
  update: (path, patch) => call("PATCH /secrets/:project/:environment/:key", place(path), patch),
277
280
  /**
278
281
  * In a fork, a key back to its parent's value (`true`), its own kept,
279
- * or its own served again (`false`); `from` holds the parent's.
282
+ * or its own served again (`false`); the answer says which, `inherit`.
280
283
  */
281
284
  inherit: (path, inherit) => call("PATCH /secrets/:project/:environment/:key", place(path), { inherit })
282
285
  },
@@ -338,7 +341,7 @@ function createClient(options) {
338
341
  return send(method, `${route}?dryRun=1`, input);
339
342
  },
340
343
  create: (member, input) => call("POST /members/:member/bindings", { member }, input),
341
- /** The service's bindings on an issuer that new keys would replace, and the keys as checked; writes nothing. */
344
+ /** The service's bindings on an issuer that new keys would replace, the keys as checked, and the places they would withhold; writes nothing. */
342
345
  previewKeys: (member, input) => {
343
346
  const [method, route] = address("POST /members/:member/bindings/keys", { member });
344
347
  return send(method, `${route}?dryRun=1`, input);
@@ -354,6 +357,21 @@ function createClient(options) {
354
357
  /** The repositories the deployment's GitHub App is installed on, private ones too; `{ app: null }` without one. */
355
358
  repositories: () => call("GET /workloads/repositories", {})
356
359
  },
360
+ /**
361
+ * Freezes: every use under a target refused from its next request, its
362
+ * sessions kept, until lifted. A target is `instance`, `service:<name>`,
363
+ * `binding:<id>` or `session:<id>`.
364
+ */
365
+ freezes: {
366
+ /** The freezes standing that you may know of: the instance's, and those of the service accounts you manage. */
367
+ list: () => call("GET /freezes", {}),
368
+ freeze: (target, reason) => call("POST /freezes", {}, {
369
+ target,
370
+ reason
371
+ }),
372
+ /** Lifts it; a target that is not frozen stays so. */
373
+ unfreeze: (target) => call("DELETE /freezes/:target", { target })
374
+ },
357
375
  access: {
358
376
  /** What they should hold at each place; the server applies the difference. `null` revokes. */
359
377
  set: (member, access) => call("PATCH /access/:member", { member }, access) },
@@ -461,33 +479,8 @@ set: (member, access) => call("PATCH /access/:member", { member }, access) },
461
479
  },
462
480
  audit: {
463
481
  list: (query = {}) => call("GET /audit", {}, query),
464
- /**
465
- * Whether the log holds, from the newest checkpoint; or, with `from`, a
466
- * stretch of the walk from the first entry: `start`, then each `next`
467
- * until it is null (`verifyWhole`).
468
- */
469
- verify: (query = {}) => call("GET /audit/verification", {}, query),
470
- /**
471
- * The walk from the first entry to the head, a stretch a request, each
472
- * as long as the server walks in one: the first stretch that fails, or
473
- * the last. `onStretch` sees each verdict as it comes.
474
- */
475
- verifyWhole: async (onStretch = () => {}) => {
476
- for (let from = "start";;) {
477
- const verdict = await call("GET /audit/verification", {}, { from });
478
- onStretch(verdict);
479
- if (!verdict.ok || verdict.next === void 0 || verdict.next === null) return verdict;
480
- from = verdict.next;
481
- }
482
- },
483
482
  /** What an escrowed key is checked against: public material, for admins and owners of the whole instance. */
484
- keys: () => call("GET /audit/keys", {}),
485
- /**
486
- * Accept that entries `from` to `to` are lost for good, as an Owner of
487
- * the whole instance: the audit watch's finding, inside the range, stays
488
- * in the log, and its anchor goes on past them.
489
- */
490
- acceptLoss: (range) => call("POST /audit/loss", {}, range)
483
+ keys: () => call("GET /audit/keys", {})
491
484
  }
492
485
  };
493
486
  }
@@ -528,8 +521,11 @@ async function until(what, probe, seconds, alive = () => true) {
528
521
  var Report = class {
529
522
  #results = [];
530
523
  #width;
531
- constructor(width = 19) {
524
+ #unserved;
525
+ /** `unserved`: checks skipped by name, each with why (../unserved.ts). */
526
+ constructor(width = 19, unserved = {}) {
532
527
  this.#width = width;
528
+ this.#unserved = unserved;
533
529
  }
534
530
  get failed() {
535
531
  return this.#results.filter((result) => result.status === "FAIL").map((result) => result.name);
@@ -540,6 +536,10 @@ var Report = class {
540
536
  * against a deployment that is not in the state it expects.
541
537
  */
542
538
  async check(name, needs, run) {
539
+ if (Object.hasOwn(this.#unserved, name)) {
540
+ this.#print(name, "skip", `not served in 0.16 yet: ${this.#unserved[name]}`);
541
+ return;
542
+ }
543
543
  const missing = Object.keys(needs).filter((key) => needs[key] === void 0 || needs[key] === null);
544
544
  if (missing.length > 0) {
545
545
  this.#print(name, "skip", `no ${missing.join(" or ")}: an earlier check failed`);
@@ -619,7 +619,7 @@ async function boot(kind, at, options) {
619
619
  port + 1,
620
620
  port + 2
621
621
  ]) if (await fetch(`http://127.0.0.1:${taken}/`).then(() => true, () => false)) throw new Failure(`port ${taken} is taken; pass --port for a free run of three`);
622
- if (kind === "workers" && (options.postgres === void 0 || options.runtime === void 0 || options.vaultRuntime === void 0)) throw new Failure("workers run on Postgres: pass --postgres <owner URL>, --runtime <coffre_runtime URL> and --vault-runtime <coffre_vault_runtime URL>");
622
+ if (options.postgres === void 0 || options.runtime === void 0 || options.vaultRuntime === void 0) throw new Failure("a deployment runs on Postgres: pass --postgres <owner URL>, --runtime <coffre_runtime URL> and --vault-runtime <coffre_vault_runtime URL>");
623
623
  const scratch = mkdtempSync(join(tmpdir(), `coffre-conformance-${kind}-`));
624
624
  const shell = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith("COFFRE_")));
625
625
  const children = [];
@@ -717,11 +717,13 @@ async function boot(kind, at, options) {
717
717
  if (error instanceof Failure) throw new Failure(error.message, `${error.detail ?? ""}\n${seen}`.trim());
718
718
  throw error;
719
719
  }
720
- async function startWorkers() {
720
+ /**
721
+ * The run's own database, made and migrated, as each login reaches it: migrated as the deployment's pipeline does
722
+ * before it deploys, by its own CLI's `coffre migrate`, in its folder, the owner's URL piped in.
723
+ */
724
+ async function ownDatabase() {
721
725
  const name = `coffre_conformance_${randomBytes(4).toString("hex")}`;
722
726
  const owner = withDatabase(options.postgres, name);
723
- const runtime = withDatabase(options.runtime, name);
724
- const vaultRuntime = withDatabase(options.vaultRuntime, name);
725
727
  await using(postgres(withDatabase(options.postgres, "postgres")), (sql) => sql.exec(`CREATE DATABASE ${name}`));
726
728
  cleanup = () => using(postgres(withDatabase(options.postgres, "postgres")), (sql) => sql.exec(`DROP DATABASE IF EXISTS ${name} WITH (FORCE)`));
727
729
  await run(process.execPath, [
@@ -729,6 +731,14 @@ async function boot(kind, at, options) {
729
731
  "migrate",
730
732
  "--yes"
731
733
  ], {}, `${owner}\n`);
734
+ return {
735
+ owner,
736
+ runtime: withDatabase(options.runtime, name),
737
+ vaultRuntime: withDatabase(options.vaultRuntime, name)
738
+ };
739
+ }
740
+ async function startWorkers() {
741
+ const { owner, runtime, vaultRuntime } = await ownDatabase();
732
742
  await run(bin("vite"), ["build", "app"]);
733
743
  const state = join(scratch, "state");
734
744
  const startWrangler = () => start("wrangler", bin("wrangler"), [
@@ -777,7 +787,7 @@ async function boot(kind, at, options) {
777
787
  origin,
778
788
  idp,
779
789
  owner: OWNER,
780
- beforeFirstBeat: true,
790
+ fresh: true,
781
791
  async scheduled(options = {}) {
782
792
  const response = await fetch(`${origin}/cdn-cgi/local/scheduled?cron=*/5+*+*+*+*`);
783
793
  if (!response.ok && !options.allowFailure) throw new Failure(`the scheduled handler answered ${response.status}`, `${await response.text()}\n${logs()}`);
@@ -791,21 +801,19 @@ async function boot(kind, at, options) {
791
801
  database: () => postgres(owner),
792
802
  runtime: () => postgres(runtime),
793
803
  vaultRuntime: () => postgres(vaultRuntime),
794
- databaseFile: null,
795
804
  clientDir: join(dir, "app/dist/client"),
796
805
  output,
797
806
  stop
798
807
  };
799
808
  }
800
809
  async function startNode() {
801
- const database = join(scratch, "coffre.db");
810
+ const { owner, runtime, vaultRuntime } = await ownDatabase();
802
811
  const socket = join(scratch, "vault.sock");
803
- await run(bin("coffre-server"), ["migrate", `file:${database}`]);
804
812
  const startVault = async () => {
805
813
  rmSync(socket, { force: true });
806
814
  const started = start("vault", process.execPath, ["src/vault.ts"], {
807
815
  VAULT_SOCKET: socket,
808
- DATABASE_URL: `file:${database}`,
816
+ DATABASE_URL: vaultRuntime,
809
817
  VAULT_KEY_ID: KEYS.VAULT_KEY_ID,
810
818
  VAULT_KEY: KEYS.VAULT_KEY,
811
819
  ...OWNER_VARS
@@ -822,9 +830,8 @@ async function boot(kind, at, options) {
822
830
  ], {
823
831
  PORT: String(port),
824
832
  PUBLIC_URL: origin,
825
- DATABASE_URL: `file:${database}`,
833
+ DATABASE_URL: runtime,
826
834
  VAULT_SOCKET: socket,
827
- AUDIT_ANCHOR: join(scratch, "anchor.json"),
828
835
  APP_KEY: KEYS.APP_KEY,
829
836
  ALLOW_LOOPBACK_ISSUERS_FOR_DEVELOPMENT: "true",
830
837
  ...github
@@ -835,7 +842,7 @@ async function boot(kind, at, options) {
835
842
  origin,
836
843
  idp,
837
844
  owner: OWNER,
838
- beforeFirstBeat: false,
845
+ fresh: false,
839
846
  async scheduled() {
840
847
  await halt(server);
841
848
  server = startServer();
@@ -852,10 +859,9 @@ async function boot(kind, at, options) {
852
859
  vault: vault.child.pid,
853
860
  server: server.child.pid
854
861
  }),
855
- database: async () => sqlite(database),
856
- runtime: null,
857
- vaultRuntime: null,
858
- databaseFile: database,
862
+ database: () => postgres(owner),
863
+ runtime: () => postgres(runtime),
864
+ vaultRuntime: () => postgres(vaultRuntime),
859
865
  clientDir: join(dir, "app/dist/client"),
860
866
  output,
861
867
  stop
@@ -868,6 +874,9 @@ function withDatabase(url, database) {
868
874
  return parsed.href;
869
875
  }
870
876
  //#endregion
877
+ //#region src/unserved.ts
878
+ const UNSERVED = { "ready once adopted": "/readyz says only that the database is migrated: whether the vault has adopted it and the signer signed is still to design: both are stored, and the vault's `about` reads nothing (docs/design/0.16.md, \"Readiness\")" };
879
+ //#endregion
871
880
  //#region src/browser.ts
872
881
  var Browser = class {
873
882
  origin;
@@ -1043,6 +1052,16 @@ async function setUpLive(admin, canaries) {
1043
1052
  };
1044
1053
  }
1045
1054
  /** The canaries in one environment, by key. */
1055
+ /**
1056
+ * A value written over one the checks made. The old one stays, under a name
1057
+ * no place has (`valuesIn` never lists it), for the scans to look for: its
1058
+ * version is still stored, and may still be in some output.
1059
+ */
1060
+ function replaced(canaries, path, value) {
1061
+ const was = canaries[path];
1062
+ if (was !== void 0) canaries[`replaced ${Object.keys(canaries).length} ${path}`] = was;
1063
+ canaries[path] = value;
1064
+ }
1046
1065
  function valuesIn(canaries, environment) {
1047
1066
  return Object.fromEntries(Object.entries(canaries).filter(([path]) => path.startsWith(`${environment}/`)).map(([path, value]) => [path.slice(environment.length + 1), value]));
1048
1067
  }
@@ -1139,6 +1158,81 @@ async function pageLoad(person) {
1139
1158
  return `${paths.length} calls at once, each answered`;
1140
1159
  }
1141
1160
  //#endregion
1161
+ //#region src/checks/storage.ts
1162
+ const TABLES = [
1163
+ "projects",
1164
+ "environments",
1165
+ "secrets",
1166
+ "secret_versions",
1167
+ "principals",
1168
+ "grants",
1169
+ "log_pending",
1170
+ "log",
1171
+ "log_head",
1172
+ "identities",
1173
+ "credentials",
1174
+ "device_logins"
1175
+ ];
1176
+ /** Inspect every table, including any a deployment added. Missing shared tables fail. */
1177
+ async function tables(sql) {
1178
+ const names = (await sql.query(`SELECT table_name AS name FROM information_schema.tables WHERE table_schema = $1 AND table_type = 'BASE TABLE'`, [sql.schema])).map((row) => row.name);
1179
+ const missing = TABLES.filter((name) => !names.includes(name));
1180
+ expect(missing.length === 0, "shared tables could not be inspected", missing);
1181
+ return names;
1182
+ }
1183
+ function quoted(name) {
1184
+ return `"${name.replaceAll("\"", "\"\"")}"`;
1185
+ }
1186
+ /** Both drivers return binary columns as bytes; casting records to text hides bytea. */
1187
+ async function scanTables(sql, names, look) {
1188
+ for (const name of names) for (const row of await sql.query(`SELECT * FROM ${quoted(name)}`)) for (const value of Object.values(row)) look(`the database's ${name} table`, value instanceof Uint8Array ? value : JSON.stringify(value) ?? "");
1189
+ }
1190
+ /** Parameters use one spelling in the checks, whatever the database. */
1191
+ function query(sql, statement, params = []) {
1192
+ return sql.query(statement, params);
1193
+ }
1194
+ /** Insert a saved or forged row as it is, but for its generated columns. */
1195
+ async function insertRow(sql, table, row) {
1196
+ const fields = Object.keys(row).filter((field) => field !== "active_subject");
1197
+ await query(sql, `INSERT INTO ${quoted(table)} (${fields.map(quoted).join(", ")}) VALUES (${fields.map((_, i) => `$${i + 1}`).join(", ")})`, fields.map((field) => row[field]));
1198
+ }
1199
+ /** Put a saved row back without changing its bytes or its generated columns. */
1200
+ async function restoreRow(sql, table, row, key) {
1201
+ const fields = Object.keys(row).filter((field) => field !== key && field !== "active_subject");
1202
+ await query(sql, `UPDATE ${quoted(table)} SET ${fields.map((field, i) => `${quoted(field)} = $${i + 1}`).join(", ")} WHERE ${quoted(key)} = $${fields.length + 1}`, [...fields.map((field) => row[field]), row[key]]);
1203
+ }
1204
+ const LOG_REFUSAL = "coffre-conformance: the log refuses writes";
1205
+ /**
1206
+ * `work` with the log refusing appends, of one action or all, as a full
1207
+ * disk or a lost connection would, without changing the deployment's code:
1208
+ * a trigger on `log_pending`, where the vault appends each entry, dropped
1209
+ * after.
1210
+ */
1211
+ async function logRefuses(sql, work, action) {
1212
+ const when = action === void 0 ? "" : ` WHEN ((NEW.body::jsonb)->>'action' = '${action}')`;
1213
+ await sql.exec(`CREATE FUNCTION conformance_refuse() RETURNS trigger LANGUAGE plpgsql AS $$
1214
+ BEGIN RAISE EXCEPTION '${LOG_REFUSAL}'; END $$;
1215
+ CREATE TRIGGER conformance_refuse BEFORE INSERT ON log_pending FOR EACH ROW${when} EXECUTE FUNCTION conformance_refuse();`);
1216
+ try {
1217
+ return await work();
1218
+ } finally {
1219
+ await sql.exec("DROP TRIGGER conformance_refuse ON log_pending; DROP FUNCTION conformance_refuse();");
1220
+ }
1221
+ }
1222
+ /** Every entry, signed (`log`) or waiting to be (`log_pending`), oldest first, read in one statement. */
1223
+ async function logBodies(sql) {
1224
+ return (await sql.query("SELECT body FROM (SELECT seq, 0 AS waiting, body FROM log UNION ALL SELECT id, 1, body FROM log_pending) entries ORDER BY waiting, seq")).map((row) => JSON.parse(row.body));
1225
+ }
1226
+ /**
1227
+ * Wait for the signer's alert naming a row (`nameOf` in @coffre/vault:
1228
+ * its table and key), in the processes' output, where a deployment with no
1229
+ * `alert` of its own sends it. Its check runs each minute.
1230
+ */
1231
+ async function alerted(deployment, table, ...key) {
1232
+ const named = `- row ${JSON.stringify([table, ...key])}:`;
1233
+ await until(`the signer's alert naming ${named}`, async () => deployment.output().includes(named), 150);
1234
+ }
1235
+ //#endregion
1142
1236
  //#region src/checks/mcp.ts
1143
1237
  const NAME = "Conformance MCP client";
1144
1238
  /** A native client's redirect, on loopback. A custom scheme is left out of a registration (D37). */
@@ -1343,7 +1437,7 @@ async function mcpConnect(deployment, people) {
1343
1437
  refresh_token: next.refresh_token,
1344
1438
  client_id: clientId
1345
1439
  });
1346
- expect(!await discovers(deployment, next.access_token), "an access token outlived its connection, ended by a reused refresh token");
1440
+ for (const started = Date.now(); await discovers(deployment, next.access_token); await new Promise((resolve) => setTimeout(resolve, 250))) expect(Date.now() - started < 3e3, `an access token outlived its connection, ended by a reused refresh token, by ${Date.now() - started} ms`);
1347
1441
  const asked = await connect(deployment, people.reader, clientId);
1348
1442
  const second = await redeem(deployment, clientId, asked);
1349
1443
  await refused(deployment, "a code redeemed already", {
@@ -1360,25 +1454,45 @@ async function mcpConnect(deployment, people) {
1360
1454
  await people.reader.api.apps.disconnect(listed[0].id);
1361
1455
  expect(!await discovers(deployment, third.access_token), "an access token outlived its disconnection");
1362
1456
  expect((await people.reader.api.apps.list()).apps.length === 0, "a disconnected app is still listed");
1363
- const { entries } = await people.admin.api.audit.list({
1364
- actor: people.reader.member,
1365
- detail: "1",
1366
- limit: 500
1367
- });
1368
- const mine = entries.filter((entry) => entry.metadata.clientId === clientId);
1369
- const count = (action) => mine.filter((entry) => entry.action === action && entry.decision === "allow").length;
1370
- const counts = {
1371
- connect: count("mcp.connect"),
1372
- token: count("mcp.token"),
1373
- disconnect: count("mcp.disconnect")
1457
+ let entries = [];
1458
+ let mine = [];
1459
+ let counts = {};
1460
+ const done = () => {
1461
+ mine = entries.filter((entry) => entry.metadata.client === clientId);
1462
+ const count = (action) => mine.filter((entry) => entry.action === action && entry.decision === "allow").length;
1463
+ const ended = entries.filter((entry) => entry.action === "token.revoke" && entry.decision === "allow" && entry.metadata.kind === "mcp" && entry.metadata.credential === listed[0].id);
1464
+ counts = {
1465
+ connect: count("mcp.connect"),
1466
+ token: count("mcp.token"),
1467
+ disconnect: count("mcp.disconnect"),
1468
+ revoked: ended.length
1469
+ };
1470
+ return JSON.stringify(counts) === JSON.stringify({
1471
+ connect: 3,
1472
+ token: 4,
1473
+ disconnect: 2,
1474
+ revoked: 1
1475
+ });
1374
1476
  };
1375
- expect(counts.connect === 3 && counts.token === 4 && counts.disconnect === 3, "the log does not hold each connection, token and disconnection", counts);
1477
+ await until("the connections, tokens and disconnections on the log", async () => {
1478
+ ({entries} = await people.admin.api.audit.list({
1479
+ detail: "1",
1480
+ limit: 500
1481
+ }));
1482
+ return done();
1483
+ }, 90).catch(() => void 0);
1484
+ expect(done(), "the log does not hold each connection, token and disconnection", counts);
1376
1485
  const reasons = mine.filter((entry) => entry.action === "mcp.disconnect").map((entry) => entry.metadata.reason).sort();
1377
- expect(JSON.stringify(reasons) === JSON.stringify([
1378
- "code_reused",
1379
- "person",
1380
- "refresh_reused"
1381
- ]), "the disconnections do not say why", reasons);
1486
+ expect(JSON.stringify(reasons) === JSON.stringify(["code_reused", "refresh_reused"]), "the disconnections do not say why", reasons);
1487
+ const by = (entry) => `${entry.action} ${entry.actorType}:${entry.actorId}`;
1488
+ const reader = (entry) => entry.actorType === "user" && entry.actorId === people.reader.email;
1489
+ const vault = (entry) => entry.actorType === "system" && entry.actorId === "vault";
1490
+ const misnamed = [...mine.filter((entry) => [
1491
+ "mcp.connect",
1492
+ "mcp.token",
1493
+ "mcp.disconnect"
1494
+ ].includes(entry.action)), ...entries.filter((entry) => entry.action === "token.revoke" && entry.metadata.credential === listed[0].id)].filter((entry) => entry.decision === "allow" && !(entry.action === "mcp.disconnect" ? vault(entry) : reader(entry)));
1495
+ expect(misnamed.length === 0, "the log names someone else for the reader's connections, or someone for a reuse's disconnect", misnamed.map(by));
1382
1496
  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";
1383
1497
  }
1384
1498
  /** A 2026-07-28 request as a client sends it: its headers say what its body does; `capabilities` are the client's. */
@@ -1484,25 +1598,28 @@ async function mcpRead(deployment, people, canaries) {
1484
1598
  expect(legacy.status === 200 && legacyBody.result?.isError !== true, `a 2025-11-25 tools/call answered ${legacy.status}`, legacyBody);
1485
1599
  const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1486
1600
  expect(leaked.length === 0, "a Read tool answered with a secret value", leaked);
1487
- const { entries } = await people.admin.api.audit.list({
1488
- actor: people.reader.member,
1489
- detail: "1",
1490
- limit: 500
1491
- });
1492
- const mine = entries.filter((entry) => entry.metadata.via?.clientId === clientId);
1493
- const reads = mine.filter((entry) => entry.action === "mcp.read").length;
1494
- const refused = mine.filter((entry) => entry.action === "mcp.call" && entry.decision === "deny").length;
1495
- expect(reads === 5 && refused === 1, "the log does not hold each call under the client", {
1496
- reads,
1497
- refused
1498
- });
1499
- expect(mine.filter((entry) => !entry.action.startsWith("mcp.")).length > 0, "the API's own entries for the calls do not name the connection", mine.map((entry) => entry.action));
1500
- const shown = (await people.admin.api.audit.list({
1501
- actor: people.reader.member,
1502
- limit: 500
1503
- })).entries;
1504
- expect(!shown.some((entry) => entry.action === "mcp.read"), "read-only calls are not detail");
1505
- 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`;
1601
+ let entries = [];
1602
+ const connections = /* @__PURE__ */ new Set();
1603
+ await until("the connection on the log", async () => {
1604
+ ({entries} = await people.admin.api.audit.list({
1605
+ actor: people.reader.member,
1606
+ detail: "1",
1607
+ limit: 500
1608
+ }));
1609
+ for (const entry of entries) if (entry.action === "mcp.connect" && entry.metadata.client === clientId) connections.add(entry.metadata.connection);
1610
+ return connections.size > 0;
1611
+ }, 90).catch(() => void 0);
1612
+ expect(connections.size === 1, "the connection is not in the log under its client", connections.size);
1613
+ const own = /* @__PURE__ */ new Set([
1614
+ "oauth.register",
1615
+ "mcp.connect",
1616
+ "mcp.token"
1617
+ ]);
1618
+ const mine = entries.filter((entry) => entry.metadata.client === clientId && !own.has(entry.action) || connections.has(entry.metadata.via));
1619
+ expect(mine.length === 0, "a read-only tools/call left an entry under the client", mine.map((entry) => entry.action));
1620
+ const waiting = (await using(deployment.database(), logBodies)).filter((body) => body.detail.client === clientId && !own.has(body.action) || connections.has(body.detail.via));
1621
+ 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));
1622
+ 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`;
1506
1623
  }
1507
1624
  /**
1508
1625
  * Changes, as a client asks for them (design section 14, checks 2 and 3):
@@ -1603,7 +1720,10 @@ async function mcpChanges(deployment, people, canaries) {
1603
1720
  basis: shown.basis
1604
1721
  }).then(() => null, (error) => error);
1605
1722
  expect(again instanceof CoffreError && again.status === 409, "an approval was decided twice", again);
1606
- const reported = await call(write, "archive_secret", { secret: `${place}/OLD` });
1723
+ const reported = await call(write, "archive_secret", {
1724
+ secret: `${place}/OLD`,
1725
+ approval: approval.id
1726
+ });
1607
1727
  expect(reported.result?.structuredContent?.status === "approved" && reported.result.isError !== true, "calling again does not report the approved change", reported.result);
1608
1728
  const elicits = { elicitation: { url: {} } };
1609
1729
  const asked = await call(write, "rename_secret", {
@@ -1669,26 +1789,34 @@ async function mcpChanges(deployment, people, canaries) {
1669
1789
  });
1670
1790
  const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1671
1791
  expect(leaked.length === 0, "a change tool answered with a secret value", leaked);
1672
- const { entries } = await admin.api.audit.list({
1673
- actor: changer.member,
1674
- limit: 500
1675
- });
1676
- const viaApproval = (action) => entries.filter((entry) => entry.action === action && entry.decision === "allow" && typeof entry.metadata.via?.approvalId === "string");
1677
- const once = {
1678
- archive: viaApproval("secret.archive").length,
1679
- rename: viaApproval("secret.rename").length,
1680
- write: viaApproval("secret.write").length
1792
+ let entries = [];
1793
+ let once = {};
1794
+ const changed = () => {
1795
+ const connections = new Set(entries.filter((entry) => entry.action === "mcp.connect" && entry.metadata.client === clientId).map((entry) => entry.metadata.connection));
1796
+ const viaApproval = (action) => entries.filter((entry) => entry.action === action && entry.decision === "allow" && connections.has(entry.metadata.via) && typeof entry.metadata.approval === "string");
1797
+ const archives = (entry) => Array.isArray(entry.metadata.keys) && entry.metadata.keys.some((line) => line.archived === true);
1798
+ const writes = viaApproval("secret.write");
1799
+ once = {
1800
+ archive: writes.filter(archives).length,
1801
+ rename: viaApproval("secret.rename").length,
1802
+ write: writes.filter((entry) => !archives(entry)).length
1803
+ };
1804
+ return JSON.stringify(once) === JSON.stringify({
1805
+ archive: 1,
1806
+ rename: 1,
1807
+ write: 2
1808
+ });
1681
1809
  };
1682
- expect(once.archive === 1 && once.rename === 1 && once.write === 2, "the changes are not each made once, via the client and its approval", once);
1683
- const decisions = entries.filter((entry) => entry.action === "mcp.approve").map((entry) => `${entry.decision} ${entry.reason ?? ""}`.trim()).sort();
1684
- expect(JSON.stringify(decisions) === JSON.stringify([
1685
- "allow",
1686
- "allow",
1687
- "allow",
1688
- "allow",
1689
- "deny approved",
1690
- "deny changed"
1691
- ]), "the decisions, and the two refused, are not in the log", decisions);
1810
+ await until("the changes on the log", async () => {
1811
+ ({entries} = await admin.api.audit.list({
1812
+ actor: changer.member,
1813
+ limit: 500
1814
+ }));
1815
+ return changed();
1816
+ }, 90).catch(() => void 0);
1817
+ expect(changed(), "the changes are not each made once, via the client and its approval", once);
1818
+ const decisions = entries.filter((entry) => entry.action === "mcp.approve" || entry.action === "mcp.deny").map((entry) => `${entry.action} ${entry.decision} ${entry.reason ?? ""}`.trim());
1819
+ expect(decisions.length === 0, "an approved change or a refused decision wrote an entry of its own", decisions);
1692
1820
  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`;
1693
1821
  }
1694
1822
  /**
@@ -1727,7 +1855,10 @@ async function mcpValues(deployment, people, canaries) {
1727
1855
  digest: page.digest,
1728
1856
  basis: page.basis
1729
1857
  })).shown.some((line) => line.value === dev.API_KEY), "Reveal did not show the person the value");
1730
- const told = await call(reading, "show_secret_value", { secret: `${DEV}/API_KEY` });
1858
+ const told = await call(reading, "show_secret_value", {
1859
+ secret: `${DEV}/API_KEY`,
1860
+ approval: id
1861
+ });
1731
1862
  expect(told.result?.structuredContent?.status === "approved", "show_secret_value does not report the value as shown", told.result);
1732
1863
  const readLeaks = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1733
1864
  expect(readLeaks.length === 0, "a Read answer held a value", readLeaks);
@@ -1740,141 +1871,6 @@ async function mcpValues(deployment, people, canaries) {
1740
1871
  return `Read stepped up for values and showed ${DEV}/API_KEY to ${reader.email} on coffre's page only; Reveal values answered ${DEV}'s values, warning first, and refused ${PROD}`;
1741
1872
  }
1742
1873
  //#endregion
1743
- //#region src/checks/storage.ts
1744
- const TABLES = [
1745
- "projects",
1746
- "environments",
1747
- "secrets",
1748
- "secret_versions",
1749
- "vault_members",
1750
- "vault_grants",
1751
- "audit_log",
1752
- "audit_chain_head",
1753
- "identities",
1754
- "credentials",
1755
- "device_authorizations"
1756
- ];
1757
- /** Inspect every table, including any a deployment added. Missing shared tables fail. */
1758
- async function tables(sql) {
1759
- const names = (await sql.query(sql.engine === "postgres" ? `SELECT table_name AS name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'` : `SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`)).map((row) => row.name);
1760
- const missing = TABLES.filter((name) => !names.includes(name));
1761
- expect(missing.length === 0, "shared tables could not be inspected", missing);
1762
- return names;
1763
- }
1764
- function quoted(name) {
1765
- return `"${name.replaceAll("\"", "\"\"")}"`;
1766
- }
1767
- /** Both drivers return binary columns as bytes; casting records to text hides bytea. */
1768
- async function scanTables(sql, names, look) {
1769
- for (const name of names) for (const row of await sql.query(`SELECT * FROM ${quoted(name)}`)) for (const value of Object.values(row)) look(`the database's ${name} table`, value instanceof Uint8Array ? value : JSON.stringify(value) ?? "");
1770
- }
1771
- /** Parameters use one spelling in the checks, whatever the database. */
1772
- function query(sql, statement, params = []) {
1773
- return sql.query(sql.engine === "sqlite" ? statement.replace(/\$\d+/g, "?") : statement, params);
1774
- }
1775
- /** Insert a saved or forged row as it is, but for its generated columns. */
1776
- async function insertRow(sql, table, row) {
1777
- const fields = Object.keys(row).filter((field) => field !== "active_subject");
1778
- await query(sql, `INSERT INTO ${quoted(table)} (${fields.map(quoted).join(", ")}) VALUES (${fields.map((_, i) => `$${i + 1}`).join(", ")})`, fields.map((field) => row[field]));
1779
- }
1780
- /** Put a saved row back without changing its bytes or its generated columns. */
1781
- async function restoreRow(sql, table, row, key) {
1782
- const fields = Object.keys(row).filter((field) => field !== key && field !== "active_subject");
1783
- await query(sql, `UPDATE ${quoted(table)} SET ${fields.map((field, i) => `${quoted(field)} = $${i + 1}`).join(", ")} WHERE ${quoted(key)} = $${fields.length + 1}`, [...fields.map((field) => row[field]), row[key]]);
1784
- }
1785
- /** Keep a deliberate rewrite, and its restoration, invisible until each is complete. */
1786
- async function transaction(sql, work) {
1787
- await sql.exec("BEGIN");
1788
- try {
1789
- const result = await work();
1790
- await sql.exec("COMMIT");
1791
- return result;
1792
- } catch (error) {
1793
- await sql.exec("ROLLBACK");
1794
- throw error;
1795
- }
1796
- }
1797
- /**
1798
- * `work` with the audit log's append-only triggers lifted, as only its owner
1799
- * can: Postgres disables them for the session, SQLite drops them and makes
1800
- * them again after.
1801
- */
1802
- async function appendOnlyLifted(sql, work) {
1803
- if (sql.engine === "postgres") {
1804
- await sql.exec("ALTER TABLE audit_log DISABLE TRIGGER USER");
1805
- try {
1806
- return await work();
1807
- } finally {
1808
- await sql.exec("ALTER TABLE audit_log ENABLE TRIGGER USER");
1809
- }
1810
- }
1811
- const triggers = await sql.query(`SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = 'audit_log'`);
1812
- for (const trigger of triggers) await sql.exec(`DROP TRIGGER ${trigger.name}`);
1813
- try {
1814
- return await work();
1815
- } finally {
1816
- for (const trigger of triggers) await sql.exec(trigger.sql);
1817
- }
1818
- }
1819
- const LOG_REFUSAL = "coffre-conformance: the log refuses writes";
1820
- /**
1821
- * `work` with the audit log refusing appends, of one action or all, as a
1822
- * full disk or a lost connection would, without changing the deployment's
1823
- * code: a trigger, dropped after.
1824
- */
1825
- async function logRefuses(sql, work, action) {
1826
- const when = action === void 0 ? "" : ` WHEN (NEW.action = '${action}')`;
1827
- const create = sql.engine === "postgres" ? `CREATE FUNCTION conformance_refuse() RETURNS trigger LANGUAGE plpgsql AS $$
1828
- BEGIN RAISE EXCEPTION '${LOG_REFUSAL}'; END $$;
1829
- CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log FOR EACH ROW${when} EXECUTE FUNCTION conformance_refuse();` : `CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log${when}
1830
- BEGIN SELECT RAISE(ABORT, '${LOG_REFUSAL}'); END;`;
1831
- await sql.exec(create);
1832
- try {
1833
- return await work();
1834
- } finally {
1835
- await sql.exec(sql.engine === "postgres" ? "DROP TRIGGER conformance_refuse ON audit_log; DROP FUNCTION conformance_refuse();" : "DROP TRIGGER conformance_refuse;");
1836
- }
1837
- }
1838
- /** The log's fields in the order its format (`coffre.audit.v2`) encodes them. */
1839
- const ENTRY_FIELDS = [
1840
- "seq",
1841
- "author",
1842
- "key_id",
1843
- "occurred_at",
1844
- "actor",
1845
- "action",
1846
- "decision",
1847
- "code",
1848
- "subject_principal",
1849
- "project_id",
1850
- "environment_id",
1851
- "secret_id",
1852
- "secret_version_id",
1853
- "operation_id",
1854
- "request_id",
1855
- "source_ip",
1856
- "related_seq",
1857
- "metadata"
1858
- ];
1859
- /**
1860
- * An entry's fields as its MAC and hash cover them, written here from the
1861
- * format rather than taken from coffre, so a check does not trust the code
1862
- * it checks: each field length-prefixed in order, null as length -1.
1863
- */
1864
- function entryFields(row) {
1865
- return Buffer.concat(ENTRY_FIELDS.flatMap((field) => {
1866
- const header = Buffer.alloc(4);
1867
- const value = row[field];
1868
- if (value === null || value === void 0) {
1869
- header.writeInt32BE(-1);
1870
- return [header];
1871
- }
1872
- const bytes = Buffer.from(String(value));
1873
- header.writeInt32BE(bytes.length);
1874
- return [header, bytes];
1875
- }));
1876
- }
1877
- //#endregion
1878
1874
  //#region src/checks/members.ts
1879
1875
  async function member(deployment, { admin }, name) {
1880
1876
  const email = `${name}@conformance.example`;
@@ -1890,83 +1886,117 @@ async function member(deployment, { admin }, name) {
1890
1886
  api: browser.client()
1891
1887
  };
1892
1888
  }
1893
- /** The row is refused at use, not only when someone explicitly verifies the log. */
1889
+ /** The conformance project's prod environment, by id. */
1890
+ async function prodId(sql) {
1891
+ const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
1892
+ WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
1893
+ expect(place !== void 0, "no prod environment to forge a grant on");
1894
+ return place.id;
1895
+ }
1896
+ /** A grant on prod, as the database's owner could write it: the member's own generation, and no MAC of the vault's. */
1897
+ async function forgeGrant(sql, principal) {
1898
+ const id = randomUUID();
1899
+ await query(sql, `INSERT INTO grants (id, principal, generation, environment_id, role, granted_at, granted_by)
1900
+ SELECT $1, id, generation, $2, 'viewer', now(), id FROM principals WHERE id = $3`, [
1901
+ id,
1902
+ await prodId(sql),
1903
+ principal
1904
+ ]);
1905
+ return id;
1906
+ }
1907
+ /**
1908
+ * A use that reads nothing, refused as `refusal` says: 401 for a member row
1909
+ * that is no longer the vault's, 403 for a place the member's sealed grants
1910
+ * do not reach. The vault may answer from its memory of the caller for up
1911
+ * to 2 seconds (D218), so it is asked again until then and a second more.
1912
+ */
1913
+ async function readsNothing(person, path, values, what, refusal) {
1914
+ const started = Date.now();
1915
+ for (;;) {
1916
+ const response = await person.browser.send("POST", "/api/reveals", { path });
1917
+ const text = await response.text();
1918
+ expect(!response.ok, `${what} was honoured`, text);
1919
+ expect(!values.some((value) => text.includes(value)), `${what} was refused with a value`, text);
1920
+ const body = JSON.parse(text);
1921
+ const as = "error" in refusal ? body.error === refusal.error : body.reason === refusal.reason;
1922
+ if (response.status === refusal.status && as) return response.status;
1923
+ expect(Date.now() - started < 3e3, `${what} was refused as ${response.status}, not ${refusal.status}`, text);
1924
+ await new Promise((resolve) => setTimeout(resolve, 250));
1925
+ }
1926
+ }
1927
+ const NO_ACCESS = {
1928
+ status: 403,
1929
+ reason: "no_access"
1930
+ };
1931
+ /**
1932
+ * A member row or grant written around the vault: a forged grant or an
1933
+ * edited member is never honoured, since its MAC is not the vault's; a
1934
+ * genuine older grant put back is served until someone acts, as an
1935
+ * ordinary revocation is until the vault's memory lapses (D216). The
1936
+ * signer names a row inserted or put back in its alert within the minute;
1937
+ * a row edited under the MAC it had is refused at use and not named, since
1938
+ * the minute's check compares MACs (docs/design/0.16.md, "What it
1939
+ * promises" and "How long the log is kept"). 0.14 refused all three at
1940
+ * use and logged `vault.tampered`; 0.16 logs no such entry, and its
1941
+ * members' `tampered` is always false (docs/design/0.16.md, "Members").
1942
+ */
1894
1943
  async function memberTampering(deployment, people, kind) {
1895
1944
  const person = await member(deployment, people, `tamper-${kind}`);
1896
1945
  const { admin } = people;
1897
- const path = kind === "grant" ? PROD : DEV;
1898
- const protectedValues = Object.values((await admin.api.secrets.reveal(path)).values);
1946
+ const protectedValues = Object.values((await admin.api.secrets.reveal(PROD)).values);
1947
+ let refused = 0;
1899
1948
  await using(deployment.database(), async (sql) => {
1900
- const [before] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
1901
- const grants = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
1949
+ const [before] = await query(sql, "SELECT * FROM principals WHERE id = $1", [person.member]);
1950
+ const grants = await query(sql, "SELECT * FROM grants WHERE principal = $1", [person.member]);
1902
1951
  expect(before !== void 0 && grants.length === 1, "the fixture has no member row and grant");
1903
- let restored = before;
1904
1952
  let held = grants;
1905
- if (kind === "stale") {
1906
- await admin.api.access.set(person.member, { [DEV]: "developer" });
1907
- [restored] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
1908
- held = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
1909
- await restoreRow(sql, "vault_members", before, "principal");
1910
- await query(sql, "UPDATE vault_grants SET role = $1, granted_at = $2, granted_by = $3 WHERE principal = $4", [
1911
- grants[0].role,
1912
- grants[0].granted_at,
1913
- grants[0].granted_by,
1914
- person.member
1915
- ]);
1916
- } else if (kind === "member") await query(sql, "UPDATE vault_members SET role = 'owner' WHERE principal = $1", [person.member]);
1917
- else {
1918
- const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
1919
- WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
1920
- expect(place !== void 0, "no prod environment to forge a grant on");
1921
- await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by) VALUES ($1, $2, 'viewer', $3, $4)`, [
1922
- person.member,
1923
- place.id,
1924
- Date.now(),
1925
- person.member
1926
- ]);
1927
- }
1953
+ let named;
1928
1954
  try {
1929
- const response = await person.browser.send("POST", "/api/reveals", { path });
1930
- const text = await response.text();
1931
- expect(response.status === 401 && JSON.parse(text).error === "unauthenticated", "the tampered credential was not refused", text);
1932
- expect(!protectedValues.some((value) => text.includes(value)), "the tampered refusal carried a value", text);
1933
- const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
1934
- AND subject_principal = $1 AND code = $2`, [person.member, kind === "stale" ? "stale" : "mac"]);
1935
- expect(reports.length > 0, "the tampering was not logged at use", reports);
1936
- expect((await admin.api.members.get(person.member)).status === "tampered", "the refused member is not marked tampered");
1937
- const verified = await admin.api.audit.verify();
1938
- expect(!verified.ok && verified.author === "vault" && verified.failedAtSeq === null, "the tampered member verifies", verified);
1955
+ if (kind === "stale") {
1956
+ await admin.api.access.set(person.member, { [DEV]: "developer" });
1957
+ held = await query(sql, "SELECT * FROM grants WHERE principal = $1", [person.member]);
1958
+ await query(sql, "DELETE FROM grants WHERE principal = $1", [person.member]);
1959
+ for (const grant of grants) await insertRow(sql, "grants", grant);
1960
+ named = ["grants", grants[0].id];
1961
+ expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "a genuine older grant put back was not served");
1962
+ } else if (kind === "member") {
1963
+ await query(sql, "UPDATE principals SET role = 'owner' WHERE id = $1", [person.member]);
1964
+ refused = await readsNothing(person, PROD, protectedValues, "an edited member row", {
1965
+ status: 401,
1966
+ error: "unauthenticated"
1967
+ });
1968
+ } else {
1969
+ named = ["grants", await forgeGrant(sql, person.member)];
1970
+ refused = await readsNothing(person, PROD, protectedValues, "a forged grant", NO_ACCESS);
1971
+ }
1972
+ if (named !== void 0) await alerted(deployment, ...named);
1939
1973
  } finally {
1940
- await restoreRow(sql, "vault_members", restored, "principal");
1941
- await query(sql, "DELETE FROM vault_grants WHERE principal = $1", [person.member]);
1942
- for (const grant of held) await insertRow(sql, "vault_grants", grant);
1974
+ await restoreRow(sql, "principals", before, "id");
1975
+ await query(sql, "DELETE FROM grants WHERE principal = $1", [person.member]);
1976
+ for (const grant of held) await insertRow(sql, "grants", grant);
1943
1977
  }
1944
1978
  });
1945
1979
  expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "restoring the member did not restore legitimate use");
1946
- expect((await admin.api.audit.verify()).ok, "the restored member did not verify");
1947
- return `${kind === "stale" ? "a genuine older member row, put back," : `a forged ${kind}`} refused at use as tampered, logged, then restored`;
1980
+ if (kind === "stale") return "a genuine older grant, put back, served and named in the signer's alert, then restored";
1981
+ if (kind === "member") return `an edited member row refused at use (${refused}), then restored`;
1982
+ return `a forged grant refused at use (${refused}) and named in the signer's alert, then restored`;
1948
1983
  }
1949
- /** Access changes have one author, the vault that commits the changed rows. */
1984
+ /** Admission, grant, revoke, removal and re-admission are each the vault's entry, naming the member. */
1950
1985
  async function accessAuthorship(deployment, people) {
1951
1986
  const person = await member(deployment, people, "access-actions");
1952
1987
  const { admin } = people;
1953
1988
  await admin.api.access.set(person.member, { [DEV]: null });
1954
1989
  await admin.api.members.remove(person.member);
1955
1990
  await admin.api.members.add(person.member);
1956
- await using(deployment.database(), async (sql) => {
1957
- const access = await sql.query(`SELECT author, action, subject_principal FROM audit_log
1958
- WHERE decision = 'allow' AND (action LIKE 'access.%' OR action LIKE 'member.%' OR action LIKE 'directory.%' OR action LIKE 'grant.%' OR action LIKE 'principal.%')`);
1959
- expect(access.length > 0 && access.every((entry) => entry.author === "vault"), "an access change was written by the app", access.filter((entry) => entry.author !== "vault"));
1960
- const actions = access.filter((entry) => entry.subject_principal === person.member).map((entry) => entry.action);
1961
- for (const action of [
1962
- "member.add",
1963
- "member.remove",
1964
- "member.restore",
1965
- "access.grant",
1966
- "access.revoke"
1967
- ]) expect(actions.includes(action), `${action} left no vault entry`, actions);
1968
- });
1969
- return "admission, grant, revoke, removal and re-admission are only vault entries";
1991
+ const actions = (await using(deployment.database(), logBodies)).filter((body) => body.detail.refused === void 0 && body.objects.includes(`principal:${person.member}`)).map((body) => body.action);
1992
+ for (const action of [
1993
+ "member.add",
1994
+ "member.remove",
1995
+ "member.restore",
1996
+ "access.grant",
1997
+ "access.revoke"
1998
+ ]) expect(actions.includes(action), `${action} left no vault entry`, actions);
1999
+ return "admission, grant, revoke, removal and re-admission are each a vault entry";
1970
2000
  }
1971
2001
  /** A failure to append may not commit an access change. */
1972
2002
  async function noAuditNoAccess(deployment, people) {
@@ -1974,8 +2004,8 @@ async function noAuditNoAccess(deployment, people) {
1974
2004
  const { admin } = people;
1975
2005
  await using(deployment.database(), async (sql) => {
1976
2006
  const snapshot = async () => ({
1977
- members: await sql.query("SELECT * FROM vault_members ORDER BY principal"),
1978
- grants: await sql.query("SELECT * FROM vault_grants ORDER BY principal, project_id, environment_id")
2007
+ members: await sql.query("SELECT * FROM principals ORDER BY id"),
2008
+ grants: await sql.query("SELECT * FROM grants ORDER BY principal, project_id, environment_id")
1979
2009
  });
1980
2010
  const before = await snapshot();
1981
2011
  await logRefuses(sql, async () => {
@@ -1992,62 +2022,59 @@ async function noAuditNoAccess(deployment, people) {
1992
2022
  expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "legitimate reads did not recover");
1993
2023
  return "grant, removal and admission commit nothing while the log refuses their entries";
1994
2024
  }
1995
- /** Hold the head after the vault checked a member, then forge a grant before it seals them. */
2025
+ /**
2026
+ * The vault's change lock (`CHANGE_LOCK` in @coffre/vault's change.ts): a
2027
+ * change's write takes it, then writes each row only if its MAC is still
2028
+ * the one it read (docs/design/0.16.md, "A change").
2029
+ */
2030
+ const CHANGE_LOCK = "109330110509669";
2031
+ /**
2032
+ * Hold the change lock while a change waits to write, and forge a grant
2033
+ * meanwhile: the vault seals only the rows it decided on, so the forged
2034
+ * one stays unsealed and reaches nothing. 0.14's race held its log's head,
2035
+ * which a change of 0.16 never locks.
2036
+ */
1996
2037
  async function sealingRace(deployment, people) {
1997
- if (deployment.vaultRuntime === null) throw new Skip("SQLite prevents a write beside a decision; the vault suite covers review R2 on Postgres");
1998
2038
  const [login] = await using(deployment.vaultRuntime(), (sql) => sql.query("SELECT current_user AS name"));
1999
2039
  const person = await member(deployment, people, "seal-race");
2000
2040
  const { admin } = people;
2041
+ const protectedValues = Object.values((await admin.api.secrets.reveal(PROD)).values);
2042
+ let forged;
2001
2043
  try {
2002
2044
  await using(deployment.database(), (holder) => using(deployment.database(), async (sql) => {
2003
- const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
2004
- WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
2005
- const [head] = await sql.query("SELECT next_seq FROM audit_chain_head");
2006
2045
  const [owner] = await holder.query("SELECT pg_backend_pid() AS pid");
2007
- expect(place !== void 0, "no prod environment for the sealing race");
2008
2046
  await holder.exec("BEGIN");
2009
2047
  let pending;
2048
+ let outcome;
2010
2049
  try {
2011
- await holder.query("SELECT 1 FROM audit_chain_head FOR UPDATE");
2050
+ await holder.query(`SELECT pg_advisory_xact_lock(${CHANGE_LOCK})`);
2012
2051
  pending = admin.api.access.set(person.member, { [DEV]: "developer" }).then((value) => ({ value }), (error) => ({ error }));
2013
- await until("the vault decision waiting on our head", async () => {
2052
+ await until("the vault's change waiting on the change lock", async () => {
2014
2053
  return (await query(sql, `SELECT pid FROM pg_stat_activity WHERE datname = current_database()
2015
2054
  AND usename = $1 AND $2 = ANY(pg_blocking_pids(pid))`, [login.name, owner.pid])).length > 0;
2016
2055
  }, 10);
2017
- await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by)
2018
- VALUES ($1, $2, 'viewer', $3, $4)`, [
2019
- person.member,
2020
- place.id,
2021
- Date.now(),
2022
- person.member
2023
- ]);
2056
+ forged = await forgeGrant(sql, person.member);
2024
2057
  } finally {
2025
2058
  await holder.exec("ROLLBACK");
2026
- if (pending !== void 0) await pending;
2059
+ if (pending !== void 0) outcome = await pending;
2027
2060
  }
2028
- expect(pending !== void 0, "the access decision was not started");
2029
- const result = await pending;
2030
- expect("error" in result && result.error instanceof CoffreError && result.error.status === 403 && result.error.code === "vault_refused" && result.error.reason === "tampered", "a grant inserted during the decision was not refused as tampered", result);
2031
- const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
2032
- AND subject_principal = $1 AND code = 'mac' AND seq >= $2`, [person.member, head.next_seq]);
2033
- expect(reports.length > 0, "the grant inserted during the decision was not reported", reports);
2034
- const response = await person.browser.send("POST", "/api/reveals", { path: PROD });
2035
- const body = await response.json();
2036
- expect(response.status === 401 && body.error === "unauthenticated", "the forged grant let its member read", body);
2061
+ expect(outcome !== void 0 && "value" in outcome, "the change that waited on the lock did not go through", outcome !== void 0 && "error" in outcome ? String(outcome.error) : outcome);
2062
+ const [dev] = await query(sql, `SELECT g.role FROM grants g JOIN environments e ON e.id = g.environment_id
2063
+ 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]);
2064
+ expect(dev?.role === "developer", "the change that waited on the lock did not make the member a developer on dev", dev);
2065
+ const [row] = await query(sql, "SELECT mac FROM grants WHERE id = $1", [forged]);
2066
+ expect(row !== void 0 && row.mac.length === 0, "the change sealed the grant inserted while it waited", row);
2067
+ await readsNothing(person, PROD, protectedValues, "the grant inserted while the change waited", NO_ACCESS);
2037
2068
  }));
2038
2069
  } finally {
2070
+ if (forged !== void 0) await using(deployment.database(), (sql) => query(sql, "DELETE FROM grants WHERE id = $1", [forged]));
2039
2071
  await admin.api.members.remove(person.member);
2040
2072
  }
2041
- expect((await admin.api.audit.verify()).ok, "the log did not verify after recovering the sealing-race member");
2042
- return "a grant inserted while the vault waited on the head was refused as tampered and logged; removal recovered the member";
2073
+ return "a grant inserted while a change waited on the change lock was not sealed by it, and reaches nothing; removal recovered the member";
2043
2074
  }
2044
2075
  //#endregion
2045
2076
  //#region src/checks/signin.ts
2046
2077
  const hash = (value) => createHash("sha256").update(value).digest();
2047
- async function reported(deployment, id) {
2048
- const event = new RegExp(`auth_row_tampered[^}]*${id}`);
2049
- await until(`the sign-in row failure report for ${id}`, async () => event.test(deployment.output()), 10);
2050
- }
2051
2078
  /** Knowing the database password and a chosen token must not mint a credential. */
2052
2079
  async function forgedCredential(deployment, people) {
2053
2080
  const person = await member(deployment, people, "forged-credential");
@@ -2066,7 +2093,7 @@ async function forgedCredential(deployment, people) {
2066
2093
  try {
2067
2094
  const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${token}` } });
2068
2095
  expect(response.status === 401, "an owner-forged credential authenticated", await response.text());
2069
- await reported(deployment, forged.id);
2096
+ await alerted(deployment, "credentials", forged.id);
2070
2097
  } finally {
2071
2098
  await query(sql, "DELETE FROM credentials WHERE id = $1", [forged.id]);
2072
2099
  }
@@ -2091,7 +2118,7 @@ async function forgedIdentity(deployment, people) {
2091
2118
  try {
2092
2119
  await signIn(deployment, browser, email).catch(() => {});
2093
2120
  expect((await browser.fetch("/api/me")).status === 401, "an owner-forged identity minted a session");
2094
- await reported(deployment, forged.id);
2121
+ await alerted(deployment, "identities", forged.id);
2095
2122
  } finally {
2096
2123
  await query(sql, "DELETE FROM credentials WHERE identity_id = $1", [forged.id]);
2097
2124
  await query(sql, "DELETE FROM identities WHERE id = $1", [forged.id]);
@@ -2116,16 +2143,22 @@ function pollDevice(deployment, code) {
2116
2143
  body: JSON.stringify({ device_code: code })
2117
2144
  });
2118
2145
  }
2119
- /** An authentic pending row, changed to approved around the app, grants nothing. */
2146
+ /**
2147
+ * An authentic pending row, changed to approved around the vault, grants
2148
+ * nothing: its MAC is the pending row's. Edited under the MAC it had, it is
2149
+ * refused, not named in the signer's alert, whose minute's check compares
2150
+ * MACs (docs/design/0.16.md, "What it promises" and "How long the log
2151
+ * is kept").
2152
+ */
2120
2153
  async function forgedApproval(deployment, people) {
2121
2154
  const person = await member(deployment, people, "forged-approval");
2122
2155
  const device = await startDevice(deployment);
2123
2156
  await using(deployment.database(), async (sql) => {
2124
- const [row] = await query(sql, "SELECT * FROM device_authorizations WHERE user_code = $1", [device.user_code]);
2125
- const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [person.member]);
2157
+ const [row] = await query(sql, "SELECT * FROM device_logins WHERE user_code = $1", [device.user_code]);
2158
+ const [standing] = await query(sql, "SELECT generation FROM principals WHERE id = $1", [person.member]);
2126
2159
  expect(row !== void 0, "no device row to forge");
2127
- await query(sql, `UPDATE device_authorizations SET decision = 'approved', decided_at = $1, principal = $2, generation = $3 WHERE id = $4`, [
2128
- sql.engine === "sqlite" ? Date.now() : /* @__PURE__ */ new Date(),
2160
+ await query(sql, `UPDATE device_logins SET decision = 'approved', decided_at = $1, principal = $2, generation = $3 WHERE id = $4`, [
2161
+ /* @__PURE__ */ new Date(),
2129
2162
  person.member,
2130
2163
  standing.generation,
2131
2164
  row.id
@@ -2134,17 +2167,20 @@ async function forgedApproval(deployment, people) {
2134
2167
  const response = await pollDevice(deployment, device.device_code);
2135
2168
  const body = await response.text();
2136
2169
  expect(!response.ok && !body.includes("access_token"), "an owner-forged approval minted a session", body);
2137
- await reported(deployment, String(row.id));
2138
2170
  } finally {
2139
- await query(sql, "DELETE FROM device_authorizations WHERE id = $1", [row.id]);
2171
+ await query(sql, "DELETE FROM device_logins WHERE id = $1", [row.id]);
2140
2172
  }
2141
2173
  });
2142
2174
  const control = await startDevice(deployment);
2143
2175
  await person.api.deviceLogins.decide(control.user_code, true);
2144
2176
  expect((await pollDevice(deployment, control.device_code)).ok, "a genuine approval no longer works");
2145
- return "an owner-edited device approval grants nothing and is reported; a genuine approval works";
2177
+ return "an owner-edited device approval grants nothing; a genuine approval works";
2146
2178
  }
2147
- /** Changing an old credential's generation cannot bring its chosen token back. */
2179
+ /**
2180
+ * Changing an old credential's generation cannot bring its chosen token
2181
+ * back: its MAC covers the generation it was issued under. Like any row
2182
+ * edited under the MAC it had, it is refused, not alerted on (as above).
2183
+ */
2148
2184
  async function editedGeneration(deployment, people) {
2149
2185
  const principal = "token:generation-mac";
2150
2186
  const { admin } = people;
@@ -2158,7 +2194,7 @@ async function editedGeneration(deployment, people) {
2158
2194
  await admin.api.members.add(principal);
2159
2195
  await admin.api.access.set(principal, { "conformance/dev": "viewer" });
2160
2196
  const [current] = await query(sql, "SELECT * FROM credentials WHERE id = $1", [old.id]);
2161
- const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [principal]);
2197
+ const [standing] = await query(sql, "SELECT generation FROM principals WHERE id = $1", [principal]);
2162
2198
  await restoreRow(sql, "credentials", {
2163
2199
  ...old,
2164
2200
  generation: standing.generation
@@ -2166,14 +2202,13 @@ async function editedGeneration(deployment, people) {
2166
2202
  try {
2167
2203
  const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${issued.token}` } });
2168
2204
  expect(response.status === 401, "editing the credential generation revived the token", await response.text());
2169
- await reported(deployment, String(old.id));
2170
2205
  } finally {
2171
2206
  await restoreRow(sql, "credentials", current, "id");
2172
2207
  }
2173
2208
  });
2174
2209
  const fresh = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
2175
2210
  expect((await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${fresh.token}` } })).ok, "a newly issued token does not work");
2176
- return "an old token with its generation edited to the current membership is refused and reported";
2211
+ return "an old token with its generation edited to the current membership is refused";
2177
2212
  }
2178
2213
  //#endregion
2179
2214
  //#region src/checks/access.ts
@@ -2294,20 +2329,22 @@ async function offboarding(deployment, { admin, leaver, service }, canaries) {
2294
2329
  await leaver.cli.secrets.reveal(DEV);
2295
2330
  const app = await connectedApp(deployment, leaver);
2296
2331
  const before = await admin.api.members.get(leaver.member);
2297
- expect(before.live.apps === 1 && before.apps.length === 1, "the owner's report does not list the app the leaver connected", before.apps);
2332
+ expect(before.live.apps === 1 && before.apps?.length === 1, "the owner's report does not list the app the leaver connected", before.apps);
2298
2333
  const { revoked, report } = await admin.api.members.remove(leaver.member);
2299
2334
  expect(report !== null, "an owner's removal does not answer with the leaver's report");
2300
- expect(revoked.apps === 1 && report.live.apps === 0 && report.apps.length === 0, "removal does not say it disconnected their app", {
2335
+ expect(revoked.apps === 1 && report.live.apps === 0 && report.apps?.length === 0, "removal does not say it disconnected their app", {
2301
2336
  revoked,
2302
2337
  apps: report.apps
2303
2338
  });
2304
2339
  expect(!await discovers(deployment, app), "a removed member's app still reaches /mcp");
2305
2340
  const exposed = report.exposed.map((entry) => `${entry.project}/${entry.environment}/${entry.key}`).sort();
2306
2341
  expect(same(exposed, Object.keys(valuesIn(canaries, DEV)).map((key) => `${DEV}/${key}`).sort()), "the offboarding report does not name what they read", report.exposed);
2307
- expect((await leaver.browser.fetch("/api/me")).status === 401, "a removed member's browser session still works");
2342
+ await until("the refusal of a removed member's browser session", async () => (await leaver.browser.fetch("/api/me")).status === 401, 5);
2343
+ await until("the refusal of a removed member's CLI session", () => leaver.cli.secrets.list(DEV).then(() => false, () => true), 5);
2308
2344
  await refused$1("a removed member's CLI session read secrets", leaver.cli.secrets.list(DEV));
2309
2345
  expect(!(await signIn(deployment, leaver.browser, leaver.email)).ok, "a removed member signed in again");
2310
2346
  await admin.api.members.remove(service.member);
2347
+ await until("the refusal of a removed service's token", () => service.api.secrets.reveal(DEV).then(() => false, () => true), 5);
2311
2348
  await refused$1("a removed service's token read secrets", service.api.secrets.reveal(DEV));
2312
2349
  await admin.api.members.add(leaver.member);
2313
2350
  await admin.api.access.set(leaver.member, { [DEV]: "developer" });
@@ -2408,11 +2445,18 @@ function sorted(value) {
2408
2445
  */
2409
2446
  async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
2410
2447
  const path = purpose === "reveal" ? `${DEV}/API_KEY` : DEV;
2448
+ const fresh = Object.fromEntries(Object.keys(purpose === "reveal" ? { API_KEY: "" } : valuesIn(canaries, DEV)).map((key) => [key, canary()]));
2449
+ await admin.api.secrets.set(DEV, fresh);
2450
+ for (const [key, value] of Object.entries(fresh)) replaced(canaries, `${DEV}/${key}`, value);
2411
2451
  const { operationId, values } = await reader.api.secrets.reveal(path);
2412
2452
  expect(typeof operationId === "string" && operationId.length > 0, "the read returned no operation id");
2413
2453
  const expected = purpose === "reveal" ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
2414
2454
  expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), "the read returned different values", values);
2415
- const bundle = (await everyAuditEntry(admin.api)).filter((entry) => entry.operationId === operationId);
2455
+ let bundle = [];
2456
+ await until("the read on the audit page", async () => {
2457
+ bundle = (await everyAuditEntry(admin.api)).filter((entry) => entry.requestId === operationId);
2458
+ return bundle.length > 0;
2459
+ }, 90).catch(() => void 0);
2416
2460
  expect(bundle.length === 1, "the read of one place is not logged as one entry", bundle);
2417
2461
  const [entry] = bundle;
2418
2462
  const read = entry.secrets ?? [{
@@ -2421,7 +2465,7 @@ async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
2421
2465
  }];
2422
2466
  expect(JSON.stringify(read.map((secret) => secret.key).sort()) === JSON.stringify(Object.keys(expected).sort()), "the read does not list each value once", entry);
2423
2467
  expect(entry.author === "vault" && entry.action === "secret.read" && entry.decision === "allow", "the read's entry is not the vault's allowed read", entry);
2424
- expect(entry.metadata.purpose === purpose && entry.requestId !== null, "the read does not name its request and its purpose", entry);
2468
+ expect(entry.metadata.purpose === purpose, "the read does not name its purpose", entry);
2425
2469
  const { keys: listed } = await admin.api.secrets.list(DEV);
2426
2470
  for (const secret of read) {
2427
2471
  expect(secret.version === listed.find((key) => key.key === secret.key)?.version, `the entry for ${String(secret.key)} names another version`, entry);
@@ -2432,144 +2476,214 @@ async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
2432
2476
  expect(own.entries.some((found) => found.seq === entry.seq), `the read is not in ${String(secret.key)}'s log`, own.entries.slice(0, 5));
2433
2477
  }
2434
2478
  await using(deployment.database(), async (sql) => {
2435
- const rows = await query(sql, "SELECT author, action, decision, request_id, secret_id, metadata FROM audit_log WHERE operation_id = $1", [operationId]);
2479
+ const rows = (await logBodies(sql)).filter((body) => body.request === operationId);
2436
2480
  expect(rows.length === 1, "the committed read is not one entry", rows);
2437
2481
  const [row] = rows;
2438
- expect(row.author === "vault" && row.action === "secret.read" && row.decision === "allow" && row.request_id !== null, "the committed read is not the vault release", row);
2439
- const metadata = JSON.parse(row.metadata);
2440
- const committed = metadata.secrets ?? [{
2441
- secretId: row.secret_id,
2442
- version: metadata.version
2443
- }];
2444
- expect(metadata.purpose === purpose && committed.length === Object.keys(expected).length, "the committed read does not name its purpose and each secret", row);
2482
+ const committed = row.detail.keys ?? [];
2483
+ expect(row.action === "secret.read" && row.detail.refused === void 0, "the committed read is not the vault release", row);
2484
+ expect(row.detail.purpose === purpose && committed.length === Object.keys(expected).length, "the committed read does not name its purpose and each secret", row);
2445
2485
  for (const secret of committed) {
2446
- const [current] = await query(sql, "SELECT current_version FROM secrets WHERE id = $1", [secret.secretId]);
2486
+ const [current] = await query(sql, "SELECT current_version FROM secrets WHERE id = $1", [secret.secret]);
2447
2487
  expect(current?.current_version === secret.version, "the committed read names another version", row);
2448
2488
  }
2449
2489
  });
2450
2490
  return `${read.length} values read by the vault for ${purpose}, in one entry under ${operationId}`;
2451
2491
  }
2452
2492
  /**
2453
- * Verification must reach the stored head, not just some intact prefix:
2454
- * the page's, from the newest checkpoint, and the walk from the first
2455
- * entry, a stretch a request, which ends where it does. And the audit page
2456
- * is sent with its verdict, rendered by the server, never a badge to come.
2493
+ * Each entry the audit page lists is held to the head the vault vouched for
2494
+ * in the page's own call (docs/design/0.16.md, "The log's head, when
2495
+ * asked"): every entry the head numbers proves against it, only those
2496
+ * past it are not proven, and the page's stretch lacks none of either tree.
2497
+ * What 0.14 asked of `GET /audit/verification`, which 0.16 drops; judged as
2498
+ * `coffre verify instance` judges it (`judgeProofs`). The signer signs on
2499
+ * its own, so a head stands within its minute.
2457
2500
  */
2458
- async function verification(deployment, { admin }) {
2459
- const anchored = await admin.api.audit.verify();
2460
- expect(anchored.ok && anchored.remaining === 0, "the log does not verify from its newest checkpoint", anchored);
2461
- const result = await admin.api.audit.verifyWhole();
2462
- expect(result.ok && result.remaining === 0, "the full log does not verify to its end", result);
2463
- expect(result.through !== null && anchored.through !== null && result.through >= anchored.through, "the walk from the first entry stopped short", {
2464
- anchored,
2465
- result
2501
+ async function auditProven({ admin }) {
2502
+ let judged;
2503
+ await until("a page past the race between its head and its log", async () => {
2504
+ judged = judgeProofs(await admin.api.audit.list({
2505
+ detail: "1",
2506
+ limit: 200
2507
+ }));
2508
+ return !judged.racing;
2509
+ }, 90).catch((error) => {
2510
+ if (judged === void 0) throw error;
2466
2511
  });
2467
- await using(deployment.database(), async (sql) => {
2468
- const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
2469
- const [last] = await sql.query("SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1");
2470
- expect(head !== void 0 && last !== void 0, "the head or newest entry is missing");
2471
- expect(BigInt(head.next_seq) === BigInt(last.seq) + 1n, "the head does not name the newest entry");
2472
- expect(Buffer.from(head.head_hash).equals(Buffer.from(last.hash)), "the head has another hash");
2473
- expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), "verification stopped before the head", {
2474
- result,
2475
- head: head.next_seq
2476
- });
2477
- expect(BigInt(result.entries) === BigInt(head.next_seq), "verification did not count every entry", result);
2512
+ const { head, proven, failed, misplaced, missing } = judged;
2513
+ expect(head !== null, "the audit page has no head");
2514
+ expect(failed.length === 0, "entries the head numbers do not prove against it", failed.slice(0, 5));
2515
+ expect(misplaced.length === 0, "an entry the head numbers is not proven, or one past it is", {
2516
+ head,
2517
+ misplaced: misplaced.slice(0, 5)
2518
+ });
2519
+ expect(missing !== null && missing.length === 0, "the page's stretch of the log lacks entries, or the page cannot say", {
2520
+ head,
2521
+ missing: missing?.slice(0, 5)
2478
2522
  });
2479
- const page = await admin.browser.fetch("/audit");
2480
- const html = await page.text();
2481
- expect(page.status === 200 && html.includes("chain-seal") && !html.includes("Verifying"), "the audit page was not sent with its verification result", { status: page.status });
2482
- return `${result.entries} entries verified through the head at ${result.through}, and the audit page sent with its result`;
2523
+ expect(proven > 0, "no entry on the page is proven", { head });
2524
+ return `${proven} entries proven against the head of ${head.size}, signed at ${head.signedAt}, and none missing`;
2483
2525
  }
2484
2526
  /**
2485
- * Each heartbeat has the vault sign the log up to its last entry, in an
2486
- * entry of its own, after checking that the prefix it signed last is still
2487
- * there. Two, so the second covers the first; then the log is verified from
2488
- * its first entry, every checkpoint with it.
2527
+ * The audit page is sent with what its proofs found, rendered by the
2528
+ * server, never a verdict to come: each entry's row says its proof in
2529
+ * `data-proof` (`proven`, `unproven` or `failed`), which the page's port
2530
+ * writes; at least one row proves, none fails, and a log that proves is
2531
+ * not shown as one coffre could not check.
2489
2532
  */
2490
- async function checkpoints(deployment, { admin }) {
2491
- const before = await admin.api.audit.verify();
2492
- expect(before.ok, "the log does not verify", before);
2493
- await deployment.scheduled();
2494
- await deployment.scheduled();
2495
- const after = await admin.api.audit.verify();
2496
- expect(after.ok && after.checkpoint !== null, "the audit log is not verified and checkpointed", after);
2497
- expect(after.checkpoint.seq > (before.through ?? -1), "the checkpoint does not cover what was written before it", {
2498
- before,
2499
- after
2533
+ async function auditPageProofs({ admin }) {
2534
+ let status = 0;
2535
+ let html = "";
2536
+ let proofs = [];
2537
+ await until("an audit page with a row its head numbers", async () => {
2538
+ const page = await admin.browser.fetch("/audit");
2539
+ status = page.status;
2540
+ html = await page.text();
2541
+ proofs = [...html.matchAll(/data-proof="([a-z]+)"/g)].map((match) => match[1]);
2542
+ return status !== 200 || proofs.length === 0 || proofs.some((proof) => proof !== "unproven");
2543
+ }, 90);
2544
+ expect(status === 200 && !html.includes("Verifying"), "the audit page was not sent with its proofs", { status });
2545
+ const proven = proofs.filter((proof) => proof === "proven").length;
2546
+ expect(proven > 0, "the audit page's rows say no proof, or none proven", {
2547
+ status,
2548
+ proofs: proofs.slice(0, 10)
2500
2549
  });
2501
- const { entries } = await admin.api.audit.list({
2502
- detail: "1",
2503
- limit: 20
2550
+ expect(!proofs.includes("failed"), "the audit page shows an entry whose proof failed", { proofs: proofs.slice(0, 10) });
2551
+ expect(!html.includes("could not check the log"), "the audit page says coffre could not check the log", { status });
2552
+ return `the audit page sent with its proofs, rendered by the server: ${proven} of ${proofs.length} rows proven`;
2553
+ }
2554
+ /**
2555
+ * The newest signed entry of a tree cut out of the log, by the database's
2556
+ * owner (docs/design/0.16.md, "What the minute's run checks of the log"):
2557
+ * the deployed signer's next check, within its minute, says `cut` of that
2558
+ * tree in its alert, in the processes' output, and the audit page proves
2559
+ * the entry nowhere, the gap a hole in its stretch. Put back, the page
2560
+ * proves it again. The change cut first, then the use, whose finding says
2561
+ * the alarm ran a later check. Each tree is cut once in a run, so no
2562
+ * earlier finding can stand for it. That a finding put right is let go and
2563
+ * said again is the vault's own tests' (signer-host.test.ts). An entry cut
2564
+ * further back, or the newest once another is signed after it, is the
2565
+ * re-check's, which reads the log a day after its last walk (D222): not
2566
+ * within a run (docs/conformance.md).
2567
+ */
2568
+ async function logCut(deployment, { admin }) {
2569
+ const said = (tree) => deployment.output().split(`- cut ${tree}:`).length - 1;
2570
+ const out = [];
2571
+ const seqs = /* @__PURE__ */ new Set();
2572
+ await using(deployment.database(), async (sql) => {
2573
+ const cut = async (kept) => {
2574
+ await until("nothing waiting to be signed", async () => (await query(sql, "SELECT 1 FROM log_pending LIMIT 1")).length === 0, 90);
2575
+ const tree = kept ? "kept" : "aging";
2576
+ expect(said(tree) === 0, `the signer named a cut of the ${tree} tree before one was made`);
2577
+ const [row] = await query(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]);
2578
+ expect(row !== void 0, `the log has no ${tree} entry to cut`);
2579
+ out.push(row);
2580
+ await until(`the signer's alert naming - cut ${tree}:`, async () => said(tree) > 0, 90);
2581
+ const page = await admin.api.audit.list({
2582
+ detail: "1",
2583
+ limit: 200
2584
+ });
2585
+ const seq = Number(row.seq);
2586
+ seqs.add(seq);
2587
+ const hole = page.holes?.some((gap) => (gap.above === null || gap.above > seq) && (gap.below === null || gap.below < seq) && gap[tree] > 0);
2588
+ expect(page.entries.every((entry) => entry.seq !== seq || entry.proof !== "proven"), "the audit page proves an entry cut out of the log", { seq });
2589
+ expect(hole === true, "the audit page's stretch does not lack the entry cut out of the log", {
2590
+ seq,
2591
+ holes: page.holes
2592
+ });
2593
+ return row;
2594
+ };
2595
+ const putBack = async (row) => {
2596
+ await query(sql, "INSERT INTO log (seq, kept, leaf, body) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING", [
2597
+ row.seq,
2598
+ row.kept,
2599
+ row.leaf,
2600
+ row.body
2601
+ ]);
2602
+ out.splice(out.indexOf(row), 1);
2603
+ };
2604
+ try {
2605
+ await putBack(await cut(true));
2606
+ await putBack(await cut(false));
2607
+ } finally {
2608
+ for (const row of [...out]) await putBack(row);
2609
+ }
2610
+ });
2611
+ let judged;
2612
+ let listed = [];
2613
+ await until("a page past the race between its head and its log", async () => {
2614
+ const page = await admin.api.audit.list({
2615
+ detail: "1",
2616
+ limit: 200
2617
+ });
2618
+ [judged, listed] = [judgeProofs(page), page.entries];
2619
+ return !judged.racing;
2620
+ }, 90).catch((error) => {
2621
+ if (judged === void 0) throw error;
2504
2622
  });
2505
- const signed = entries.filter((entry) => entry.action === "audit.checkpoint" && entry.decision === "allow");
2506
- expect(signed.length >= 2 && signed.every((entry) => entry.author === "vault" && entry.detail), "the checkpoints are not the vault's entries, hidden as detail", signed);
2507
- const [newest, previous] = signed;
2508
- expect(newest.metadata.seq === after.checkpoint.seq, "the newest checkpoint entry is not the one verified", {
2509
- signed,
2510
- after
2623
+ const { failed, misplaced, missing } = judged;
2624
+ const unproven = [...seqs].filter((seq) => !listed.some((entry) => entry.seq === seq && entry.proof === "proven"));
2625
+ expect(unproven.length === 0 && failed.length === 0 && misplaced.length === 0 && missing !== null && missing.length === 0, "the entries put back are not proven again", {
2626
+ unproven,
2627
+ failed: failed.slice(0, 5),
2628
+ misplaced: misplaced.slice(0, 5),
2629
+ missing
2511
2630
  });
2512
- expect(previous.seq <= after.checkpoint.seq, "the newest checkpoint does not cover the one before it", signed);
2513
- return `${after.entries} entries verified through ${after.through}, checkpointed at ${after.checkpoint.seq}`;
2631
+ 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";
2514
2632
  }
2515
2633
  /**
2516
- * Every version the app says it stored names the vault's entry for the key
2517
- * it was sealed with, `key.wrap` for a write and `key.rewrap` for a restore,
2518
- * a mark and a loosening:
2519
- * the same member, request and operation, the same secret and version. And
2520
- * every value read is the vault's to log: the app keeps no reads of its own.
2634
+ * Every version stored is the vault's write: one entry of the log names
2635
+ * it, its secret and its number, among the keys the change wrote: a
2636
+ * `secret.write` or `secret.loosen`, or the `environment.create` of a fork
2637
+ * that copied it, naming who wrote it and the request. 0.14's app and vault each logged their
2638
+ * half of a write, the app's naming the vault's seal of its key; in 0.16
2639
+ * the vault writes the version and its one entry together, and the app
2640
+ * writes nothing (docs/design/0.16.md, "Four rules").
2521
2641
  */
2642
+ const WRITES = /* @__PURE__ */ new Set([
2643
+ "secret.write",
2644
+ "secret.loosen",
2645
+ "environment.create"
2646
+ ]);
2522
2647
  async function writesAgree(deployment) {
2523
2648
  return using(deployment.database(), async (sql) => {
2524
- const entries = await sql.query("SELECT * FROM audit_log ORDER BY seq");
2525
- const bySeq = new Map(entries.map((entry) => [String(entry.seq), entry]));
2526
- const writes = entries.filter((entry) => entry.author === "app" && entry.decision === "allow" && (entry.action === "secret.write" || entry.action === "secret.restore" || entry.action === "secret.loosen"));
2527
- for (const entry of entries.filter((row) => row.author === "app" && row.decision === "allow")) expect(entry.action !== "secret.read", "the app logged an allowed read of a value", entry);
2528
- for (const entry of writes) {
2529
- const seal = entry.action === "secret.write" && JSON.parse(entry.metadata).kept === void 0 ? "key.wrap" : "key.rewrap";
2530
- const key = entry.related_seq === null ? void 0 : bySeq.get(String(entry.related_seq));
2531
- expect(key !== void 0 && key.author === "vault" && key.action === seal && key.decision === "allow", `a ${entry.action} names no ${seal} of the vault's`, {
2532
- entry,
2533
- key
2534
- });
2535
- for (const field of [
2536
- "actor",
2537
- "request_id",
2538
- "operation_id",
2539
- "project_id",
2540
- "environment_id"
2541
- ]) expect(entry[field] === key[field], `the write and its seal have different ${field}`, {
2542
- entry,
2543
- key
2544
- });
2545
- expect(entry.operation_id !== null && entry.request_id !== null, "the committed write has no operation or request", entry);
2546
- const metadata = JSON.parse(entry.metadata);
2547
- const sealed = JSON.parse(key.metadata);
2548
- expect((sealed.secretId ?? key.secret_id) === entry.secret_id && sealed.version === metadata.version && sealed.subject.split("/").at(-1) === metadata.key, "the write names a seal of another secret or version", {
2549
- entry,
2550
- key
2551
- });
2649
+ const written = /* @__PURE__ */ new Map();
2650
+ for (const body of await logBodies(sql)) {
2651
+ if (!WRITES.has(body.action) || body.detail.refused !== void 0) continue;
2652
+ const named = (field) => typeof field === "string" && field !== "";
2653
+ expect(named(body.actor) && named(body.request), `a committed ${body.action} names no actor or no request`, body);
2654
+ for (const line of body.detail.keys ?? []) {
2655
+ if (line.version === void 0) continue;
2656
+ const name = `${line.secret} ${line.version}`;
2657
+ written.set(name, (written.get(name) ?? 0) + 1);
2658
+ }
2552
2659
  }
2553
2660
  const versions = await query(sql, `SELECT v.secret_id, v.version FROM secret_versions v JOIN secrets s ON s.id = v.secret_id
2554
2661
  JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT$1]);
2555
2662
  expect(versions.length > 0, "no stored versions were inspected");
2556
2663
  for (const version of versions) {
2557
- const matching = writes.filter((entry) => entry.secret_id === version.secret_id && JSON.parse(entry.metadata).version === version.version);
2558
- expect(matching.length === 1, "a stored version has no unique app write entry", {
2664
+ const entries = written.get(`${version.secret_id} ${version.version}`) ?? 0;
2665
+ expect(entries === 1, "a stored version has no unique write entry", {
2559
2666
  version,
2560
- matching
2667
+ entries
2561
2668
  });
2562
2669
  }
2563
- return `${versions.length} stored versions, each with one app write naming the vault's seal in the same operation`;
2670
+ return `${versions.length} stored versions, each written by one entry of the vault's, naming its actor and request`;
2564
2671
  });
2565
2672
  }
2566
2673
  /**
2567
2674
  * No value leaves without its entry: with the audit log refusing writes (a
2568
2675
  * trigger, as a full disk or a lost connection would), the vault cannot log
2569
- * the read, so a reveal fails and carries nothing.
2676
+ * the read, so a reveal fails and carries nothing. The vault logs a
2677
+ * credential's read of a version once a minute (docs/design/0.16.md, "What
2678
+ * it promises"), so the reveal asks for a key written just before, which no
2679
+ * entry has said yet; it is archived after.
2570
2680
  */
2681
+ const UNLOGGED = "NO_AUDIT_NO_VALUE";
2571
2682
  async function noAuditNoValue(deployment, { admin }, canaries) {
2572
2683
  let status = 0;
2684
+ const value = canary();
2685
+ canaries[`unlogged ${DEV}/${UNLOGGED}`] = value;
2686
+ await admin.api.secrets.set(DEV, { [UNLOGGED]: value });
2573
2687
  await using(deployment.database(), (sql) => logRefuses(sql, async () => {
2574
2688
  const before = deployment.output().split(LOG_REFUSAL).length;
2575
2689
  const response = await admin.browser.send("POST", "/api/reveals", { path: DEV });
@@ -2581,118 +2695,15 @@ async function noAuditNoValue(deployment, { admin }, canaries) {
2581
2695
  body
2582
2696
  });
2583
2697
  expect(body.message === "something went wrong; see the server log", "the audit failure exposed another reason", body);
2584
- expect(!Object.values(canaries).some((value) => text.includes(value)), "a reveal that could not be logged carried a value", text);
2698
+ expect(!text.includes(value), "a reveal that could not be logged carried the value it could not log", text);
2699
+ expect(!Object.values(canaries).some((known) => text.includes(known)), "a reveal that could not be logged carried a value", text);
2585
2700
  await until("the injected audit refusal in the processes' output", async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
2586
2701
  }, "secret.read"));
2587
- expect((await admin.api.secrets.reveal(DEV)).values.API_KEY === canaries[`${DEV}/API_KEY`], "reveals did not come back once the log took writes again");
2702
+ const after = await admin.api.secrets.reveal(DEV);
2703
+ expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`] && after.values[UNLOGGED] === value, "reveals did not come back once the log took writes again");
2704
+ await admin.api.secrets.set(DEV, { [UNLOGGED]: null });
2588
2705
  return `a reveal the audit log would not take: ${status} internal_error, its injected cause in the log, and no value`;
2589
2706
  }
2590
- /** A publicly chained entry still needs the vault's MAC. */
2591
- async function forgedVaultEntry(deployment, { admin }) {
2592
- const verify = () => admin.api.audit.verify();
2593
- expect((await verify()).ok, "the log does not verify before the forged entry");
2594
- await using(deployment.database(), async (sql) => {
2595
- const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
2596
- const [writer] = await sql.query("SELECT key_id FROM audit_log WHERE author = 'vault' ORDER BY seq DESC LIMIT 1");
2597
- expect(writer !== void 0, "no vault key id to copy");
2598
- const seq = BigInt(head.next_seq);
2599
- const forged = {
2600
- seq,
2601
- author: "vault",
2602
- key_id: writer.key_id,
2603
- occurred_at: Date.now(),
2604
- actor: "user:forger@conformance.example",
2605
- action: "secret.read",
2606
- decision: "allow",
2607
- metadata: "{}"
2608
- };
2609
- const prevHash = Buffer.from(head.head_hash);
2610
- const mac = Buffer.alloc(32, 65);
2611
- const hash = createHash("sha256").update("coffre.audit.chain.v2").update(prevHash).update(entryFields(forged)).update(mac).digest();
2612
- await insertRow(sql, "audit_log", {
2613
- ...forged,
2614
- prev_hash: prevHash,
2615
- mac,
2616
- hash
2617
- });
2618
- await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq + 1n, hash]);
2619
- let inserted;
2620
- try {
2621
- inserted = await verify();
2622
- } finally {
2623
- await appendOnlyLifted(sql, () => query(sql, "DELETE FROM audit_log WHERE seq = $1", [seq]));
2624
- await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq, prevHash]);
2625
- }
2626
- 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);
2627
- const removed = await verify();
2628
- expect(removed.ok, "the log did not verify once the entry was gone", removed);
2629
- });
2630
- return "a publicly chained vault entry with a made-up MAC was refused at its sequence";
2631
- }
2632
- /**
2633
- * Rewrite either author's first entry in place and require a fault at that
2634
- * exact sequence: from the walk from the first entry, since the page's
2635
- * check goes on from the newest checkpoint and reads nothing before it.
2636
- */
2637
- async function rewrittenEntry(deployment, { admin }, author) {
2638
- expect((await admin.api.audit.verifyWhole()).ok, "the log does not verify before the rewrite");
2639
- let seq = 0;
2640
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2641
- const [row] = await query(sql, "SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1", [author]);
2642
- expect(row !== void 0, `no ${author} entry to rewrite`);
2643
- seq = Number(row.seq);
2644
- await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", ["user:nobody@conformance.example", row.seq]);
2645
- try {
2646
- const result = await admin.api.audit.verifyWhole();
2647
- expect(!result.ok && result.failedAtSeq === Number(row.seq), "a rewritten entry was not caught at its sequence", result);
2648
- } finally {
2649
- await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", [row.actor, row.seq]);
2650
- }
2651
- }));
2652
- expect((await admin.api.audit.verifyWhole()).ok, "the restored entry did not verify");
2653
- return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
2654
- }
2655
- /** Remove an unreferenced entry, then put its exact bytes back. */
2656
- async function missingEntry(deployment, { admin }, position) {
2657
- expect((await admin.api.audit.verify()).ok, "the log does not verify before deletion");
2658
- let seq = 0;
2659
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2660
- const [row] = await sql.query(`SELECT * FROM audit_log WHERE ${position !== "middle" ? `seq = ${position === "first" ? 0 : 1e3}` : "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`);
2661
- expect(row !== void 0, "no unreferenced entry to delete");
2662
- seq = Number(row.seq);
2663
- const linked = await query(sql, "SELECT * FROM audit_log WHERE related_seq = $1", [row.seq]);
2664
- await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE related_seq = $1", [row.seq]);
2665
- await query(sql, "DELETE FROM audit_log WHERE seq = $1", [row.seq]);
2666
- try {
2667
- const result = await admin.api.audit.verify();
2668
- expect(!result.ok, "a log with an entry missing verifies", result);
2669
- if (position !== "middle") expect(result.reason.includes(`sequence gap: expected seq ${position === "first" ? 0 : 1e3}`), "the sequence gap was not reported", result);
2670
- } finally {
2671
- await insertRow(sql, "audit_log", row);
2672
- for (const entry of linked) await query(sql, "UPDATE audit_log SET related_seq = $1 WHERE seq = $2", [entry.related_seq, entry.seq]);
2673
- }
2674
- }));
2675
- expect((await admin.api.audit.verify()).ok, "the restored log did not verify");
2676
- const which = {
2677
- first: "the first",
2678
- batch: "the first past a page of 1,000",
2679
- middle: "a heartbeat"
2680
- }[position];
2681
- return `entry ${seq}, ${which}, deleted: caught, then put back`;
2682
- }
2683
- /** Last: keep the head, delete its newest entries, and require verification to notice. */
2684
- async function deletedTail(deployment, { admin }) {
2685
- const intact = await admin.api.audit.verify();
2686
- expect(intact.ok && intact.checkpoint !== null, "the log is not intact and checkpointed");
2687
- const signed = intact.checkpoint.seq;
2688
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2689
- await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE seq >= $1", [signed]);
2690
- await query(sql, "DELETE FROM audit_log WHERE seq >= $1", [signed]);
2691
- }));
2692
- const result = await admin.api.audit.verify();
2693
- expect(!result.ok && result.author === "app" && result.reason.includes("removed from the end"), "the log verifies with its newest entries deleted", result);
2694
- return "the newest entries deleted, with the head left alone, were caught";
2695
- }
2696
2707
  /** The whole log, detail included, newest first. */
2697
2708
  async function everyAuditEntry(api) {
2698
2709
  const all = [];
@@ -2708,69 +2719,6 @@ async function everyAuditEntry(api) {
2708
2719
  }
2709
2720
  }
2710
2721
  //#endregion
2711
- //#region src/checks/checkpoints.ts
2712
- /** Leave one old signature invalid, reseal the chain, and make later checkpoints valid. */
2713
- async function earlierCheckpoint(deployment, { admin }) {
2714
- expect((await admin.api.audit.verifyWhole()).ok, "the log does not verify before checkpoint tampering");
2715
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2716
- const rows = await sql.query("SELECT * FROM audit_log ORDER BY seq");
2717
- const signed = rows.filter((row) => row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow");
2718
- expect(signed.length >= 2, "two checkpoints are needed");
2719
- const [head] = await sql.query("SELECT * FROM audit_chain_head");
2720
- const seed = Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.VAULT_KEY, "base64"), Buffer.alloc(0), "coffre.vault.signing-key.v1", 32));
2721
- const signingKey = createPrivateKey({
2722
- key: Buffer.concat([Buffer.from("302e020100300506032b657004220420", "hex"), seed]),
2723
- format: "der",
2724
- type: "pkcs8"
2725
- });
2726
- const keys = {
2727
- app: Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.APP_KEY, "base64"), Buffer.alloc(0), "coffre.audit.app.v2", 32)),
2728
- vault: Buffer.from(hkdfSync("sha256", seed, Buffer.alloc(0), "coffre.audit.vault.v2", 32))
2729
- };
2730
- const hashes = /* @__PURE__ */ new Map();
2731
- let previous = Buffer.alloc(32);
2732
- try {
2733
- await transaction(sql, async () => {
2734
- for (const original of rows) {
2735
- const row = { ...original };
2736
- if (row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow") {
2737
- const checkpoint = JSON.parse(String(row.metadata));
2738
- if (row.seq === signed[0].seq) checkpoint.signature = Buffer.alloc(64).toString("base64");
2739
- else {
2740
- checkpoint.hash = hashes.get(BigInt(checkpoint.seq));
2741
- checkpoint.signature = sign(null, Buffer.from(`coffre.checkpoint.v3|${checkpoint.seq}|${checkpoint.hash}|${checkpoint.signedAt}`), signingKey).toString("base64");
2742
- }
2743
- row.metadata = JSON.stringify(checkpoint);
2744
- }
2745
- const fields = entryFields(row);
2746
- row.prev_hash = previous;
2747
- row.mac = createHmac("sha256", keys[row.author]).update("coffre.audit.mac.v2").update(previous).update(fields).digest();
2748
- previous = createHash("sha256").update("coffre.audit.chain.v2").update(previous).update(fields).update(row.mac).digest();
2749
- row.hash = previous;
2750
- hashes.set(BigInt(String(row.seq)), previous.toString("hex"));
2751
- await restoreRow(sql, "audit_log", row, "seq");
2752
- }
2753
- await restoreRow(sql, "audit_chain_head", {
2754
- ...head,
2755
- head_hash: previous
2756
- }, "only_row");
2757
- });
2758
- const result = await admin.api.audit.verifyWhole();
2759
- expect(!result.ok && result.author === "vault" && result.failedAtSeq === Number(signed[0].seq) && result.reason === "a checkpoint the vault did not sign", "a valid later checkpoint hid an invalid earlier one", result);
2760
- } finally {
2761
- await transaction(sql, async () => {
2762
- for (const row of rows) await restoreRow(sql, "audit_log", row, "seq");
2763
- await restoreRow(sql, "audit_chain_head", head, "only_row");
2764
- });
2765
- keys.app.fill(0);
2766
- keys.vault.fill(0);
2767
- seed.fill(0);
2768
- }
2769
- }));
2770
- expect((await admin.api.audit.verifyWhole()).ok, "restoring the signed prefixes did not restore verification");
2771
- return "an invalid earlier checkpoint was caught despite valid MACs and a valid later checkpoint";
2772
- }
2773
- //#endregion
2774
2722
  //#region src/checks/deletion.ts
2775
2723
  const PROJECT = "conformance-gone";
2776
2724
  const SERVICE$1 = "token:conformance-gone-ci";
@@ -2840,18 +2788,24 @@ async function deletion(deployment, cli, admin) {
2840
2788
  expect((await admin.api.projects.list()).projects.every((project) => !project.slug.startsWith(PROJECT)), "a deleted project is still listed");
2841
2789
  expect((await admin.api.members.get(SERVICE$1)).live.grants === 0, "a grant on the deleted project is still live");
2842
2790
  const revealed = await admin.api.secrets.reveal(`${tombstone.slug}/prod`).then(() => null, (error) => error);
2843
- expect(revealed instanceof CoffreError && revealed.status === 404, "a deleted project is still revealed by its tombstone", revealed);
2791
+ expect(revealed instanceof CoffreError && (revealed.status === 404 || revealed.status === 400), "a deleted project is still revealed by its tombstone", revealed);
2844
2792
  const fresh = value();
2845
2793
  await admin.api.projects.create(PROJECT, { name: "Gone, again" });
2846
2794
  await admin.api.environments.create(`${PROJECT}/prod`, { name: "prod" });
2847
2795
  await admin.api.secrets.set(`${PROJECT}/prod`, { API_KEY: fresh });
2848
2796
  expect(await projectId(deployment, PROJECT) !== id, "the new project took the deleted one's row");
2849
2797
  expect((await admin.api.secrets.reveal(`${PROJECT}/prod/API_KEY`)).values.API_KEY === fresh, "the new project does not read its own value");
2850
- const { entries } = await admin.api.audit.list({
2851
- path: tombstone.slug,
2852
- limit: 500
2853
- });
2854
- expect(entries.some((entry) => entry.action === "project.delete" && entry.decision === "allow"), "the deletion is not in the log under its tombstone", entries.slice(0, 5));
2798
+ let page;
2799
+ const deleted = () => page !== void 0 && !(page instanceof Error) && page.entries.some((entry) => entry.action === "project.delete" && entry.decision === "allow");
2800
+ await until("the deletion on the log under its tombstone", async () => {
2801
+ page = await admin.api.audit.list({
2802
+ path: tombstone.slug,
2803
+ limit: 500
2804
+ }).catch((error) => error instanceof Error ? error : new Error(String(error)));
2805
+ return page instanceof Error || deleted();
2806
+ }, 90).catch(() => void 0);
2807
+ expect(!(page instanceof Error), "the log cannot be read under the tombstone's slug", page instanceof Error ? page.message : page);
2808
+ expect(deleted(), "the deletion is not in the log under its tombstone", page?.entries.slice(0, 5));
2855
2809
  await admin.api.environments.update(`${PROJECT}/prod`, { archived: true });
2856
2810
  await run([
2857
2811
  "environments",
@@ -2859,13 +2813,11 @@ async function deletion(deployment, cli, admin) {
2859
2813
  `${PROJECT}/prod`,
2860
2814
  "--apply"
2861
2815
  ]);
2862
- await admin.api.environments.create(`${PROJECT}/prod`, { name: "prod, again" });
2816
+ await admin.api.environments.create(`${PROJECT}/prod`, {});
2863
2817
  const environments = (await admin.api.projects.list()).projects.find((project) => project.slug === PROJECT)?.environments ?? [];
2864
- expect(environments.length === 1 && environments[0].name === "prod, again", "a deleted environment is still listed, or its slug is not free", environments);
2818
+ expect(environments.length === 1 && environments[0].slug === "prod" && environments[0].details?.secretCount === 0, "a deleted environment is still listed, or its slug is not free", environments);
2865
2819
  expect((await sealed(deployment, await projectId(deployment, PROJECT))).sealed === 0, "a deleted environment still holds a value");
2866
- const verified = await admin.api.audit.verify();
2867
- expect(verified.ok, "the log does not verify after the deletions", verified);
2868
- return `${PROJECT} and ${PROJECT}/prod deleted by the CLI: 5 versions erased, a grant revoked, both unlisted and their slugs reused; ${verified.entries} entries verify`;
2820
+ return `${PROJECT} and ${PROJECT}/prod deleted by the CLI: 5 versions erased, a grant revoked, both unlisted and their slugs reused`;
2869
2821
  }
2870
2822
  //#endregion
2871
2823
  //#region src/checks/environments.ts
@@ -3018,20 +2970,37 @@ async function references(deployment, { admin, reader, leaver }, canaries) {
3018
2970
  expect(sourceKey !== void 0, "prod holds no secret to refer to");
3019
2971
  const source = `${PROD}/${sourceKey}`;
3020
2972
  await refused$1("a viewer on dev read prod directly", reader.api.secrets.reveal(source));
3021
- expect(await outcome(leaver.api.secrets.set(DEV, { [HELD]: { ref: source } })) === "403 missing_secret_read", "a developer on dev who cannot read prod made a reference to prod's secret");
2973
+ const made = await outcome(leaver.api.secrets.set(DEV, { [HELD]: { ref: source } }));
2974
+ expect(made === "403 no_access", "a developer on dev who cannot read prod made a reference to prod's secret", made);
3022
2975
  await admin.api.secrets.set(DEV, { [HELD]: { ref: source } });
3023
2976
  try {
3024
- expect((await reader.api.secrets.reveal(`${DEV}/${HELD}`)).values[HELD] === canaries[source], "a viewer on dev did not read prod's value through dev's reference");
3025
- const logged = async (path) => (await admin.api.audit.list({
3026
- path,
3027
- limit: 200
3028
- })).entries.filter((entry) => entry.action === "secret.read" && entry.actorId === reader.email && entry.metadata.via !== void 0).length;
3029
- expect(await logged(PROD) >= 1 && await logged(DEV) >= 1, "the read through the reference is not in both the source's log and the holder's");
2977
+ const read = await reader.api.secrets.reveal(`${DEV}/${HELD}`);
2978
+ expect(read.values[HELD] === canaries[source], "a viewer on dev did not read prod's value through dev's reference");
2979
+ const through = (entry) => entry.requestId === read.operationId && entry.action === "secret.read" && entry.actorType === "user" && entry.actorId === reader.email && Array.isArray(entry.metadata.keys) && entry.metadata.keys.some((key) => typeof key === "object" && key !== null && "through" in key);
2980
+ const seen = {
2981
+ [PROD]: false,
2982
+ [DEV]: false,
2983
+ [source]: false
2984
+ };
2985
+ const shown = {};
2986
+ await until("the read through the reference on the logs", async () => {
2987
+ for (const path of Object.keys(seen)) seen[path] ||= (shown[path] = (await admin.api.audit.list({
2988
+ path,
2989
+ limit: 200
2990
+ })).entries).some(through);
2991
+ return Object.values(seen).every(Boolean);
2992
+ }, 90).catch(() => void 0);
2993
+ expect(seen[PROD] && seen[DEV], "the reader's read through the reference is not in both the source's log and the holder's", {
2994
+ seen,
2995
+ shown: Object.fromEntries(Object.entries(shown).map(([path, entries]) => [path, entries.slice(0, 5).map((entry) => `${entry.seq} ${entry.action} ${entry.actorId} ${entry.requestId}`)]))
2996
+ });
3030
2997
  const archives = [[source, (archived) => admin.api.secrets.update(source, { archived })], [PROD, (archived) => admin.api.environments.update(PROD, { archived })]];
3031
2998
  for (const [what, archive] of archives) {
3032
2999
  const refusal = await archive(true).then(() => null, (error) => error);
3033
3000
  if (refusal === null) await archive(false);
3034
- expect(refusal instanceof CoffreError && refusal.status === 409 && refusal.message.includes(`${DEV}/${HELD} reads ${source}`), `${what} was archived, or refused without naming the reference, while a reference read it`, refusal instanceof Error ? refusal.message : refusal);
3001
+ expect(refusal instanceof CoffreError && refusal.status === 409 && refusal.reason === "referenced", `${what} was archived, or refused other than as referenced, while a reference read it`, refusal instanceof Error ? refusal.message : refusal);
3002
+ const holders = (await admin.api.references.list(what)).references.filter((reference) => reference.source === source).map((reference) => reference.holder);
3003
+ expect(holders.includes(`${DEV}/${HELD}`), `${what}'s references do not name ${DEV}/${HELD}, the reference that blocks its archive`, holders);
3035
3004
  }
3036
3005
  await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
3037
3006
  const underArchived = await outcome(admin.api.secrets.update(source, { archived: true })).finally(() => admin.api.secrets.update(`${DEV}/${HELD}`, { archived: false }));
@@ -3044,46 +3013,35 @@ async function references(deployment, { admin, reader, leaver }, canaries) {
3044
3013
  }
3045
3014
  expect((await reader.api.secrets.reveal(`${DEV}/${HELD}`)).values[HELD] === canaries[source], "a reference did not read its source again once both were restored");
3046
3015
  const forged = await using(deployment.database(), async (sql) => {
3047
- const [holder] = await query(sql, "SELECT id, project_id, environment_id FROM secrets WHERE key = $1", [HELD]);
3048
- const [target] = await query(sql, "SELECT s.id, s.project_id, s.environment_id FROM secrets s JOIN environments e ON e.id = s.environment_id WHERE s.key = $1 AND e.slug = $2", [sourceKey, PROD.split("/")[1]]);
3049
- const [entry] = await query(sql, "SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'access.grant' AND decision = 'allow' ORDER BY seq DESC LIMIT 1");
3016
+ const [holder] = await query(sql, "SELECT project_id, environment_id FROM secrets WHERE key = $1", [HELD]);
3017
+ const [target] = await query(sql, "SELECT s.id FROM secrets s JOIN environments e ON e.id = s.environment_id WHERE s.key = $1 AND e.slug = $2", [sourceKey, PROD.split("/")[1]]);
3050
3018
  const id = randomUUID();
3051
- await query(sql, "INSERT INTO secrets (id, project_id, environment_id, key, current_version) VALUES ($1, $2, $3, $4, 0)", [
3019
+ const now = /* @__PURE__ */ new Date();
3020
+ await query(sql, `INSERT INTO secrets (id, project_id, environment_id, key, created_at, updated_at, versions, source_id, source_set_at, source_set_by)
3021
+ VALUES ($1, $2, $3, $4, $5, $5, 0, $6, $5, $7)`, [
3052
3022
  id,
3053
3023
  holder.project_id,
3054
3024
  holder.environment_id,
3055
- FORGED
3056
- ]);
3057
- await query(sql, `INSERT INTO secret_references (id, project_id, environment_id, secret_id, source_project_id, source_environment_id, source_secret_id, created_seq, created_by)
3058
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [
3059
- randomUUID(),
3060
- holder.project_id,
3061
- holder.environment_id,
3062
- id,
3063
- target.project_id,
3064
- target.environment_id,
3025
+ FORGED,
3026
+ now,
3065
3027
  target.id,
3066
- String(entry.seq),
3067
3028
  admin.member
3068
3029
  ]);
3069
3030
  return id;
3070
3031
  });
3071
3032
  try {
3072
3033
  const forgedRead = await outcome(reader.api.secrets.reveal(`${DEV}/${FORGED}`));
3073
- expect([
3074
- "404 no_value",
3075
- "404 not_found",
3076
- "403 bad_claim"
3077
- ].includes(forgedRead), `a reference row the vault never made was read: ${forgedRead}`);
3034
+ expect(forgedRead === "404 unknown_key", `a reference row the vault never made was read: ${forgedRead}`);
3078
3035
  } finally {
3079
- await using(deployment.database(), async (sql) => {
3080
- await query(sql, "DELETE FROM secret_references WHERE secret_id = $1", [forged]);
3081
- await query(sql, "DELETE FROM secrets WHERE id = $1", [forged]);
3082
- });
3036
+ await using(deployment.database(), (sql) => query(sql, "DELETE FROM secrets WHERE id = $1", [forged]));
3083
3037
  }
3084
3038
  await admin.api.references.break(`${DEV}/${HELD}`);
3085
- const after = await outcome(reader.api.secrets.reveal(`${DEV}/${HELD}`));
3086
- expect(after.startsWith("409 "), `a broken reference still read: ${after}`);
3039
+ const broken = await reader.api.secrets.reveal(`${DEV}/${HELD}`).then((answer) => answer, (error) => error);
3040
+ const said = broken instanceof CoffreError ? `${broken.status} ${broken.reason ?? broken.code}` : "let through";
3041
+ expect(said === "404 no_value" && !JSON.stringify(broken instanceof Error ? {
3042
+ ...broken,
3043
+ message: broken.message
3044
+ } : broken).includes(canaries[source]), `a broken reference still read, or was refused other than as holding no value: ${said}`, broken instanceof Error ? broken.message : broken);
3087
3045
  } finally {
3088
3046
  await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
3089
3047
  }
@@ -3110,10 +3068,14 @@ async function loosening({ admin, reader, leaver }) {
3110
3068
  try {
3111
3069
  await refused$1("a viewer read a sensitive value", reader.api.secrets.reveal(path));
3112
3070
  expect((await leaver.api.secrets.list(DEV)).loosenable === false, "a developer on dev is told they make a sensitive value a secret in place");
3113
- await refused$1("a developer on dev made a sensitive value a secret in place", leaver.api.secrets.set(DEV, { [LOOSE]: { sensitivity: "secret" } }));
3071
+ const theirs = await loosen(leaver);
3072
+ expect(theirs.status >= 400 && theirs.status < 500, `a developer on dev made a sensitive value a secret in place, answered ${theirs.status}`, await theirs.text());
3114
3073
  expect((await admin.api.secrets.list(DEV)).loosenable === true, "the admin, an Owner of the whole instance, is told they do not loosen");
3115
- const marked = await admin.api.secrets.set(DEV, { [LOOSE]: { sensitivity: "secret" } });
3116
- expect(!JSON.stringify(marked).includes(value), "the admin's loosening answered the value", marked);
3074
+ const answer = await loosen(admin);
3075
+ const text = await answer.text();
3076
+ expect(answer.ok, `the admin's loosening answered ${answer.status}`, text);
3077
+ expect(!text.includes(value), "the admin's loosening answered the value", text);
3078
+ const marked = JSON.parse(text);
3117
3079
  expect(JSON.stringify(marked.keys[LOOSE]) === JSON.stringify({
3118
3080
  version: 2,
3119
3081
  sensitivity: "secret"
@@ -3121,17 +3083,29 @@ async function loosening({ admin, reader, leaver }) {
3121
3083
  expect((await reader.api.secrets.reveal(path)).values[LOOSE] === value, "the reader did not read the loosened value as dev's secret");
3122
3084
  const versions = (await admin.api.secrets.history(path)).versions.map((version) => version.sensitivity);
3123
3085
  expect(versions.join() === "secret,sensitive", "the loosening changed the old version too, or made none", versions);
3124
- const logged = (await admin.api.audit.list({
3125
- path,
3126
- limit: 50
3127
- })).entries.filter((entry) => entry.action === "secret.loosen");
3128
- expect(logged.length === 1 && logged[0].metadata.from === "sensitive" && logged[0].metadata.to === "secret", "the loosening is not its own entry, saying both levels", logged);
3086
+ let logged = [];
3087
+ await until("the loosening on the audit page", async () => {
3088
+ logged = (await admin.api.audit.list({
3089
+ path,
3090
+ limit: 50
3091
+ })).entries.filter((entry) => entry.action === "secret.loosen");
3092
+ return logged.length > 0;
3093
+ }, 90).catch(() => void 0);
3094
+ expect((logged.length === 1 ? logged[0].metadata.keys : void 0)?.some((line) => line.key === LOOSE && line.was === "sensitive" && line.nature === "secret") === true, "the loosening is not its own entry, saying both levels", logged);
3129
3095
  } finally {
3130
3096
  await admin.api.secrets.set(DEV, { [LOOSE]: null });
3131
3097
  }
3132
3098
  return "the admin makes a sensitive value a secret in place, answered no value, as a new version, logged as secret.loosen; a developer on dev may not";
3133
3099
  }
3134
3100
  /**
3101
+ * `{ sensitivity }` alone, a nature changed in place, as the page sends it:
3102
+ * the client's types carry it again once 0.16 serves it (UNSERVED, in
3103
+ * ../unserved.ts).
3104
+ */
3105
+ function loosen(person) {
3106
+ return person.browser.send("PATCH", `/api/secrets/${DEV}`, { [LOOSE]: { sensitivity: "secret" } });
3107
+ }
3108
+ /**
3135
3109
  * Within a project, key names are not secret; values are. What dev is
3136
3110
  * missing is compared with every sibling: the reader, a viewer on dev who
3137
3111
  * cannot read prod, is told prod has the key, as the admin is, and nothing
@@ -3160,35 +3134,17 @@ async function missingKeys({ admin, reader }) {
3160
3134
  }
3161
3135
  //#endregion
3162
3136
  //#region src/checks/logins.ts
3163
- const ZEROS = "decode(repeat('00', 32), 'hex')";
3164
- const appendAs = (author) => `INSERT INTO audit_log
3165
- (seq, author, key_id, occurred_at, actor, action, decision, prev_hash, mac, hash)
3166
- VALUES (9000000000, '${author}', '${author}:0', 0, 'system:conformance', 'probe', 'allow', ${ZEROS}, ${ZEROS}, ${ZEROS})`;
3167
- const IMMUTABLE = [
3168
- "UPDATE audit_log SET action = 'rewritten' WHERE author = 'app'",
3169
- "UPDATE audit_log SET action = 'rewritten' WHERE author = 'vault'",
3170
- "DELETE FROM audit_log",
3171
- "TRUNCATE audit_log",
3172
- "DROP TABLE audit_log",
3173
- "UPDATE secret_versions SET id = id",
3174
- "DELETE FROM secret_versions",
3175
- "DELETE FROM vault_members",
3137
+ /** What neither login may do: make or drop a table, empty one, or turn off the database's own checks. */
3138
+ const BYPASSES = [
3176
3139
  "CREATE TABLE conformance_probe (id integer)",
3177
3140
  "CREATE TEMP TABLE conformance_probe (id integer)",
3178
- "ALTER TABLE audit_log DISABLE TRIGGER USER",
3141
+ "DROP TABLE log",
3142
+ "TRUNCATE log",
3143
+ "TRUNCATE secret_versions",
3144
+ "ALTER TABLE log_pending DISABLE TRIGGER USER",
3179
3145
  "SET session_replication_role = 'replica'",
3180
3146
  "SET ROLE coffre_owner"
3181
3147
  ];
3182
- /** A column of each app table, for an update that would change nothing, if it were allowed. */
3183
- const APP_COLUMNS = {
3184
- projects: "id",
3185
- environments: "id",
3186
- secrets: "id",
3187
- secret_versions: "id",
3188
- identities: "id",
3189
- credentials: "id",
3190
- device_authorizations: "id"
3191
- };
3192
3148
  async function refuses(sql, statements) {
3193
3149
  for (const statement of statements) {
3194
3150
  await sql.exec("BEGIN");
@@ -3205,7 +3161,7 @@ async function refuses(sql, statements) {
3205
3161
  }
3206
3162
  return statements.length;
3207
3163
  }
3208
- /** Neither table nor column grants may let a login write outside its component. */
3164
+ /** Neither table nor column grants may let a login write these tables. */
3209
3165
  async function noWrites(sql, names) {
3210
3166
  const columns = await sql.query(`SELECT table_name, column_name,
3211
3167
  has_column_privilege(current_user, quote_ident(table_name), column_name, 'INSERT') AS insertable,
@@ -3221,137 +3177,42 @@ async function noWrites(sql, names) {
3221
3177
  expect(inspected.every((column) => !column.insertable && !column.updatable && !column.deletable && !column.truncatable), `the login has a column write privilege on ${name}`, inspected);
3222
3178
  }
3223
3179
  }
3180
+ /** The app's login writes no table: every row is the vault's. */
3224
3181
  async function appLogin(deployment) {
3225
- if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
3226
- expect(deployment.runtime !== null, "the app runtime login is missing");
3227
- const protectedTables = (await using(deployment.database(), tables)).filter((name) => name.startsWith("vault_"));
3182
+ const all = await using(deployment.database(), tables);
3228
3183
  return using(deployment.runtime(), async (sql) => {
3229
- await noWrites(sql, protectedTables);
3184
+ await noWrites(sql, all);
3230
3185
  const statements = [
3231
- ...IMMUTABLE,
3232
- appendAs("vault"),
3186
+ ...BYPASSES,
3233
3187
  "SET ROLE coffre_vault",
3234
- "DELETE FROM secrets"
3188
+ "INSERT INTO log_pending (kept, body, mac) VALUES (true, '{}', decode(repeat('00', 36), 'hex'))",
3189
+ "UPDATE log SET body = body",
3190
+ "DELETE FROM log",
3191
+ "UPDATE log_head SET size = size",
3192
+ "UPDATE secret_versions SET id = id",
3193
+ "DELETE FROM secrets",
3194
+ "UPDATE principals SET role = role",
3195
+ "UPDATE grants SET role = role"
3235
3196
  ];
3236
- for (const table of protectedTables) statements.push(`INSERT INTO ${table} SELECT * FROM ${table} LIMIT 1`, `UPDATE ${table} SET principal = principal`, `DELETE FROM ${table}`, `TRUNCATE ${table}`);
3237
- return `${await refuses(sql, statements)} writes and bypasses refused; no member or grant write privileges`;
3197
+ for (const table of all) statements.push(`DELETE FROM ${quoted(table)}`, `TRUNCATE ${quoted(table)}`);
3198
+ return `${await refuses(sql, statements)} writes and bypasses refused; no write privilege on any of ${all.length} tables`;
3238
3199
  });
3239
3200
  }
3201
+ /** The vault's login writes coffre's rows, never 0.14's archived log, and empties no table. */
3240
3202
  async function vaultLogin(deployment) {
3241
- if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
3242
- expect(deployment.vaultRuntime !== null, "the vault runtime login is missing");
3243
- const appTables = (await using(deployment.database(), tables)).filter((name) => ![
3244
- "vault_members",
3245
- "vault_grants",
3246
- "audit_log",
3247
- "audit_chain_head"
3248
- ].includes(name));
3203
+ const all = await using(deployment.database(), tables);
3249
3204
  return using(deployment.vaultRuntime(), async (sql) => {
3250
- await noWrites(sql, appTables);
3251
- const [head] = await sql.query(`SELECT
3252
- has_table_privilege(current_user, 'audit_chain_head', 'INSERT') AS insertable,
3253
- has_table_privilege(current_user, 'audit_chain_head', 'DELETE') AS deletable,
3254
- has_table_privilege(current_user, 'audit_chain_head', 'TRUNCATE') AS truncatable,
3255
- has_column_privilege(current_user, 'audit_chain_head', 'only_row', 'UPDATE') AS identity_updatable`);
3256
- expect(!head.insertable && !head.deletable && !head.truncatable && !head.identity_updatable, "the vault can replace the head row", head);
3205
+ await noWrites(sql, ["audit_archive"]);
3257
3206
  const statements = [
3258
- ...IMMUTABLE,
3259
- appendAs("app"),
3207
+ ...BYPASSES,
3260
3208
  "SET ROLE coffre_app",
3261
- "UPDATE audit_chain_head SET only_row = only_row",
3262
- "DELETE FROM audit_chain_head",
3263
- "TRUNCATE audit_chain_head",
3264
- "UPDATE vault_members SET principal = principal",
3265
- "UPDATE vault_grants SET role = role"
3209
+ "UPDATE audit_archive SET action = action",
3210
+ "DELETE FROM audit_archive",
3211
+ "INSERT INTO audit_archive SELECT * FROM audit_archive LIMIT 1"
3266
3212
  ];
3267
- for (const table of appTables) {
3268
- const column = APP_COLUMNS[table];
3269
- if (column !== void 0) statements.push(`UPDATE ${quoted(table)} SET ${column} = ${column}`);
3270
- }
3271
- return `${await refuses(sql, statements)} writes and bypasses refused; app tables have no write privileges`;
3272
- });
3273
- }
3274
- //#endregion
3275
- //#region src/checks/readiness.ts
3276
- async function red(deployment) {
3277
- await until("readiness red", async () => (await fetch(`${deployment.origin}/readyz`)).status === 503, 10);
3278
- const body = await (await fetch(`${deployment.origin}/readyz`)).json();
3279
- expect(body.ok === false && body.checkpointed === false, "readiness did not report the missing checkpoint", body);
3280
- }
3281
- async function refusedCheckpoint(deployment) {
3282
- expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before the checkpoint refusal");
3283
- await using(deployment.database(), (sql) => logRefuses(sql, async () => {
3284
- const [before] = await sql.query("SELECT seq FROM audit_log WHERE action = 'audit.heartbeat' ORDER BY seq DESC LIMIT 1");
3285
- await deployment.scheduled({ allowFailure: true });
3286
- await until("the new heartbeat", async () => {
3287
- const [after] = await sql.query("SELECT seq FROM audit_log WHERE action = 'audit.heartbeat' ORDER BY seq DESC LIMIT 1");
3288
- return BigInt(after.seq) > BigInt(before.seq);
3289
- }, 10);
3290
- await red(deployment);
3291
- }, "audit.checkpoint"));
3292
- await deployment.scheduled();
3293
- await until("readiness recovered", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
3294
- return "a committed heartbeat with a refused checkpoint turns readiness red, then recovers";
3295
- }
3296
- async function missingCheckpoint(deployment) {
3297
- expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before removing checkpoints");
3298
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
3299
- const rows = await sql.query("SELECT * FROM audit_log WHERE action = 'audit.checkpoint' ORDER BY seq");
3300
- expect(rows.length > 0, "no checkpoints to remove");
3301
- await sql.exec("DELETE FROM audit_log WHERE action = 'audit.checkpoint'");
3302
- try {
3303
- await deployment.scheduled({ allowFailure: true });
3304
- await red(deployment);
3305
- } finally {
3306
- for (const row of rows) await insertRow(sql, "audit_log", row);
3307
- }
3308
- }));
3309
- await deployment.scheduled();
3310
- await until("readiness recovered", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
3311
- return "checkpoints deleted: the next beat finds them gone and readiness is red; restored, the next beat recovers";
3312
- }
3313
- /** A hole before the latest signed prefix must stop the next scheduled checkpoint. */
3314
- async function middleCut(deployment, { admin }) {
3315
- expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before the middle cut");
3316
- let seq = 0;
3317
- await using(deployment.database(), async (sql) => {
3318
- const [head] = await sql.query("SELECT next_seq FROM audit_chain_head");
3319
- const [latest] = await sql.query("SELECT seq, metadata FROM audit_log WHERE author = 'vault' AND action = 'audit.checkpoint' AND decision = 'allow' ORDER BY seq DESC LIMIT 1");
3320
- expect(latest !== void 0, "no signed prefix to cut from");
3321
- const [heartbeat] = await query(sql, `SELECT h.seq FROM audit_log h JOIN audit_log c ON c.seq = h.seq + 1
3322
- WHERE h.action = 'audit.heartbeat' AND h.seq > 0 AND c.author = 'vault' AND c.action = 'audit.checkpoint'
3323
- AND c.decision = 'allow' AND c.seq < $1 AND NOT EXISTS
3324
- (SELECT 1 FROM audit_log linked WHERE linked.related_seq IN (h.seq, c.seq) AND linked.seq <> c.seq) ORDER BY h.seq LIMIT 1`, [JSON.parse(latest.metadata).seq]);
3325
- expect(heartbeat !== void 0, "no older heartbeat and checkpoint to cut");
3326
- seq = Number(heartbeat.seq);
3327
- const kept = await query(sql, "SELECT * FROM audit_log WHERE seq >= $1 AND seq <= $2 ORDER BY seq", [seq, seq + 1]);
3328
- expect(kept.length === 2, "the middle cut needs two adjacent entries");
3329
- await appendOnlyLifted(sql, async () => {
3330
- await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE seq = $1", [seq + 1]);
3331
- await query(sql, "DELETE FROM audit_log WHERE seq >= $1 AND seq <= $2", [seq, seq + 1]);
3332
- try {
3333
- await deployment.scheduled({ allowFailure: true });
3334
- let checkpoint;
3335
- await until("the checkpoint after the cut", async () => {
3336
- [checkpoint] = await query(sql, `SELECT decision, code, metadata FROM audit_log
3337
- WHERE author = 'vault' AND action = 'audit.checkpoint' AND seq >= $1 ORDER BY seq LIMIT 1`, [head.next_seq]);
3338
- return checkpoint !== void 0;
3339
- }, 10);
3340
- expect(checkpoint?.decision === "deny" && checkpoint.code === "log_broken", "the checkpoint signed over a middle cut", checkpoint);
3341
- const detail = JSON.parse(checkpoint.metadata);
3342
- expect(detail.reason.includes(`sequence gap: expected seq ${seq}`), "the checkpoint did not name the middle gap", detail);
3343
- await red(deployment);
3344
- } finally {
3345
- await transaction(sql, async () => {
3346
- for (const row of kept) await insertRow(sql, "audit_log", row);
3347
- });
3348
- }
3349
- });
3213
+ for (const table of all) statements.push(`TRUNCATE ${quoted(table)}`);
3214
+ return `${await refuses(sql, statements)} writes and bypasses refused; 0.14's archived log not writable`;
3350
3215
  });
3351
- await deployment.scheduled();
3352
- await until("readiness recovered after the middle cut", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
3353
- expect((await admin.api.audit.verify()).ok, "the restored middle cut did not verify");
3354
- return `entries ${seq} and ${seq + 1}, before the latest signed prefix, cut: checkpoint log_broken, readiness red; restored and recovered`;
3355
3216
  }
3356
3217
  //#endregion
3357
3218
  //#region ../client/dist/routes.js
@@ -3414,7 +3275,6 @@ function getCalls({ places, secrets, members, services }) {
3414
3275
  detail: "1"
3415
3276
  }
3416
3277
  }],
3417
- "GET /audit/verification": [{ params: {} }],
3418
3278
  "GET /audit/keys": [{ params: {} }]
3419
3279
  };
3420
3280
  }
@@ -3557,9 +3417,9 @@ async function canaryScan(deployment, people, canaries) {
3557
3417
  await using(deployment.database(), async (sql) => {
3558
3418
  const names = await tables(sql);
3559
3419
  const control = canary();
3560
- await sql.exec(`CREATE TABLE conformance_canary_probe (value ${sql.engine === "postgres" ? "bytea" : "BLOB"})`);
3420
+ await sql.exec("CREATE TABLE conformance_canary_probe (value bytea)");
3561
3421
  try {
3562
- await sql.query(`INSERT INTO conformance_canary_probe VALUES (${sql.engine === "postgres" ? "$1" : "?"})`, [Buffer.from(control)]);
3422
+ await sql.query("INSERT INTO conformance_canary_probe VALUES ($1)", [Buffer.from(control)]);
3563
3423
  let found = false;
3564
3424
  await scanTables(sql, ["conformance_canary_probe"], (_, bytes) => {
3565
3425
  found ||= Buffer.from(bytes).includes(control);
@@ -3571,20 +3431,11 @@ async function canaryScan(deployment, people, canaries) {
3571
3431
  await scanTables(sql, names, look);
3572
3432
  stored.push(`${names.length} tables, with a binary positive control`);
3573
3433
  });
3574
- if (deployment.databaseFile !== null) {
3575
- look("the database's file", files(deployment.databaseFile));
3576
- stored.push("the database file and WAL");
3577
- }
3578
3434
  look("the processes' output", deployment.output());
3579
3435
  stored.push("the processes' output");
3580
3436
  expect(leaks.length === 0, "a value was found outside a reveal", leaks);
3581
3437
  return `${answers} answers from ${Object.keys(routes).length} GET routes and ${paths.length} pages as ${callers.length} callers, and ${stored.join(", ")}: no value`;
3582
3438
  }
3583
- /** A SQLite file and its write-ahead log, as bytes: what anyone with the disk would read. */
3584
- function files(path) {
3585
- expect(existsSync(path), `the store file could not be found: ${path}`);
3586
- return Buffer.concat([path, `${path}-wal`].filter((file) => existsSync(file)).map((file) => readFileSync(file)));
3587
- }
3588
3439
  //#endregion
3589
3440
  //#region src/checks/cli.ts
3590
3441
  /** What `coffre verify instance` makes and finds on an instance, as an owner: its own place, service and key. */
@@ -3688,7 +3539,7 @@ function marks(output) {
3688
3539
  const lines = [...output.matchAll(/^ {2}([✓✗–]) (\S.{18}) /gm)];
3689
3540
  return Object.fromEntries(lines.map(([, mark, name]) => [name.trim(), mark]));
3690
3541
  }
3691
- /** The token's run, from CI: as no one, then as the token, which is no owner and skips verification. */
3542
+ /** The token's run, from CI: as no one, then as the token, which reads no audit log but its project's, so holds none to its head. */
3692
3543
  async function verifyWithToken(cli, live) {
3693
3544
  const { canary } = live;
3694
3545
  const login = await cli.run([
@@ -3706,9 +3557,9 @@ async function verifyWithToken(cli, live) {
3706
3557
  ], {}, `${canary.value}\n`);
3707
3558
  const seen = marks(run.output);
3708
3559
  expect(run.code === 0, `coffre verify instance with a token exited ${run.code}`, run.output);
3709
- expect(seen["token verification"] === "–", "a token that is no owner did not skip verification", run.output);
3560
+ expect(!("owner log proofs" in seen), "a token's run held the log to its head, which only an owner's reads whole", run.output);
3710
3561
  expect(!run.output.includes(live.token) && !run.output.includes(canary.value), "the run printed its token or its canary", run.output);
3711
- return `${Object.values(seen).filter((mark) => mark === "✓").length} checks ok as no one and as the token, its verification skipped; neither the token nor the canary shown`;
3562
+ return `${Object.values(seen).filter((mark) => mark === "✓").length} checks ok as no one and as the token; neither the token nor the canary shown`;
3712
3563
  }
3713
3564
  /** What a run as an owner must leave as it was: every member and their grants, but its service; every project and environment. */
3714
3565
  async function everythingElse(api) {
@@ -3744,6 +3595,7 @@ async function verifyAsOwner(cli) {
3744
3595
  const run = await cli.run(["verify", "instance"]);
3745
3596
  const seen = marks(run.output);
3746
3597
  expect(run.code === 0, `coffre verify instance as the admin exited ${run.code}`, run.output);
3598
+ expect(seen["owner log proofs"] === "✓", "the run did not hold the log's newest entries to the head the vault signed", run.output);
3747
3599
  const skipped = Object.entries(seen).filter(([, mark]) => mark !== "✓").map(([name]) => name);
3748
3600
  expect(skipped.length === 0, `the run skipped ${skipped.join(", ")}`, run.output);
3749
3601
  expect(!SHOWN.test(run.output) && !run.output.includes(session), "the run printed a credential, the session or its canary", SHOWN.exec(run.output)?.[0]);
@@ -3803,58 +3655,55 @@ async function verifyLeftovers(cli, admin, setups, before, kept) {
3803
3655
  return `the second run found all; ${PROBE.service} holds no credential; the CLI's session signed in; ${place}/${kept.key} untouched; nothing else changed; ${entries.length} entries by ${PROBE.service} stay`;
3804
3656
  }
3805
3657
  /**
3806
- * `coffre verify keys`, with the admin's session, the keys piped in: this
3807
- * deployment's pass; a wrong vault key fails and the right app key passes;
3808
- * a malformed app key fails. No key is ever printed.
3658
+ * `coffre verify keys`, with the admin's session, the key piped in: this
3659
+ * deployment's vault key passes, its vault ID as given; a wrong key and a
3660
+ * malformed one each fail, named. No key is ever printed.
3809
3661
  */
3810
3662
  async function verifyKeys(cli) {
3811
3663
  const wrong = Buffer.alloc(32, 7).toString("base64");
3812
3664
  const runs = [
3813
3665
  {
3814
3666
  args: ["--vault-id", KEYS.VAULT_KEY_ID],
3815
- input: `${KEYS.VAULT_KEY}\n${KEYS.APP_KEY}\n`,
3667
+ input: `${KEYS.VAULT_KEY}\n`,
3816
3668
  code: 0,
3817
- vault: "✓",
3818
- app: "✓"
3669
+ key: "✓"
3819
3670
  },
3820
3671
  {
3821
3672
  args: [],
3822
- input: `${wrong}\n${KEYS.APP_KEY}\n`,
3673
+ input: `${wrong}\n`,
3823
3674
  code: 1,
3824
- vault: "✗",
3825
- app: "✓",
3826
- said: /✗ vault key +not this instance's vault key/
3675
+ key: "✗",
3676
+ said: /✗ key +not this instance's/
3827
3677
  },
3828
3678
  {
3829
3679
  args: [],
3830
- input: `${KEYS.VAULT_KEY}\nnot-a-key\n`,
3680
+ input: "not-a-key\n",
3831
3681
  code: 1,
3832
- vault: "✓",
3833
- app: "✗",
3834
- said: /✗ app key +not an app key/
3682
+ key: "✗",
3683
+ said: /✗ key +not a key/
3835
3684
  }
3836
3685
  ];
3837
- for (const { args, input, code, vault, app, said } of runs) {
3686
+ for (const { args, input, code, key, said } of runs) {
3838
3687
  const run = await cli.run([
3839
3688
  "verify",
3840
3689
  "keys",
3841
3690
  ...args
3842
3691
  ], {}, input);
3843
- const seen = marks(run.output);
3844
- expect(run.code === code && seen["vault key"] === vault && seen["app key"] === app, `coffre verify keys exited ${run.code}`, run.output);
3845
- if (said !== void 0) expect(said.test(run.output), "it did not say which key is not right", run.output);
3846
- for (const key of [
3692
+ expect(run.code === code && marks(run.output)["key"] === key, `coffre verify keys exited ${run.code}`, run.output);
3693
+ if (said !== void 0) expect(said.test(run.output), "it did not say the key is not right", run.output);
3694
+ for (const shown of [
3847
3695
  KEYS.VAULT_KEY,
3848
3696
  KEYS.APP_KEY,
3849
3697
  wrong,
3850
3698
  "not-a-key"
3851
- ]) expect(!run.output.includes(key), "a key was printed", run.output);
3699
+ ]) expect(!run.output.includes(shown), "a key was printed", run.output);
3852
3700
  }
3853
- return "this deployment's keys pass, vault ID included; a wrong vault key and a malformed app key are each named; no key shown";
3701
+ return "this deployment's vault key passes, vault ID included; a wrong key and a malformed one are each named; no key shown";
3854
3702
  }
3855
3703
  /**
3856
3704
  * Everything the API does, the CLI does: with the admin's `coffre login`
3857
- * session, a project and its environment, a secret set and renamed, a
3705
+ * session, a project and its environment, made twice, the second time
3706
+ * changing nothing, a secret set and renamed, a
3858
3707
  * service admitted and granted, a token issued into a 0600 file, then
3859
3708
  * piped to `coffre login --token` in a CLI of the service's own, read
3860
3709
  * with, revoked after its preview, and refused from then on; the grant
@@ -3878,6 +3727,17 @@ async function manageByCli(cli) {
3878
3727
  as(ci, ", as the service"),
3879
3728
  as(helper, ", with a personal token")
3880
3729
  ];
3730
+ const revoked = async (who, label, args) => {
3731
+ for (const started = Date.now();; await setTimeout$1(250)) {
3732
+ const run = await who.run(args, {});
3733
+ const waited = Date.now() - started;
3734
+ if (run.code === 0 && waited < 3e3) continue;
3735
+ expect(run.code === 1, `coffre ${args.join(" ")}${label} exited ${run.code} ${waited} ms after its token was revoked, not 1`, run.output);
3736
+ expect(/does not know this bearer token/.test(run.output), `a revoked token${label} was not refused plainly`, run.output);
3737
+ steps.push(`${args.slice(0, 2).join(" ")}${label}`);
3738
+ return;
3739
+ }
3740
+ };
3881
3741
  try {
3882
3742
  const value = `cli-value-${randomBytes(8).toString("hex")}`;
3883
3743
  await coffre([
@@ -3892,6 +3752,16 @@ async function manageByCli(cli) {
3892
3752
  "create",
3893
3753
  "conformance-cli/ci"
3894
3754
  ]);
3755
+ const again = await coffre([
3756
+ "projects",
3757
+ "create",
3758
+ "conformance-cli"
3759
+ ]) + await coffre([
3760
+ "environments",
3761
+ "create",
3762
+ "conformance-cli/ci"
3763
+ ]);
3764
+ expect(again.includes("conformance-cli exists already: nothing changed") && again.includes("conformance-cli/ci exists already: nothing changed"), "making a project or an environment that is there already did not say nothing changed", again);
3895
3765
  await coffre(["set", "conformance-cli/ci/API_KEY"], value);
3896
3766
  await coffre([
3897
3767
  "rename",
@@ -3969,8 +3839,7 @@ async function manageByCli(cli) {
3969
3839
  listed.id,
3970
3840
  "--apply"
3971
3841
  ]);
3972
- const gone = await service(["get", "conformance-cli/ci/API_TOKEN"], void 0, 1);
3973
- expect(/does not know this bearer token/.test(gone), "a revoked token was not refused plainly", gone);
3842
+ await revoked(ci, ", as the service", ["get", "conformance-cli/ci/API_TOKEN"]);
3974
3843
  const personal = join(cli.home, "agent-token");
3975
3844
  await coffre([
3976
3845
  "tokens",
@@ -4012,7 +3881,7 @@ async function manageByCli(cli) {
4012
3881
  mine.id,
4013
3882
  "--apply"
4014
3883
  ]);
4015
- await agent(["get", "conformance-cli/ci/API_TOKEN"], void 0, 1);
3884
+ await revoked(helper, ", with a personal token", ["get", "conformance-cli/ci/API_TOKEN"]);
4016
3885
  await coffre([
4017
3886
  "revoke",
4018
3887
  "conformance-cli",
@@ -4628,8 +4497,7 @@ async function pastedValues(deployment, admin, browser) {
4628
4497
  * A refused sign-in, said once: the page coffre sends the browser back to
4629
4498
  * shows why, rendered by the server, then takes it out of the address, so a
4630
4499
  * reload is a clean retry. The refusal is a real one: a member's address
4631
- * that GitHub now gives another account, which names the provider the
4632
- * address already signs in with.
4500
+ * that GitHub now gives another account, said bare: 0.16 names no provider.
4633
4501
  */
4634
4502
  async function signinErrorOnce(deployment, admin, browser) {
4635
4503
  const executable = chromeOrSkip(browser);
@@ -4642,8 +4510,8 @@ async function signinErrorOnce(deployment, admin, browser) {
4642
4510
  const refused = await signIn(deployment, visitor, email);
4643
4511
  expect(!refused.ok && refused.error === "account_mismatch", "another GitHub account with a member's address was not refused as account_mismatch", refused);
4644
4512
  const back = new URL(refused.location, deployment.origin);
4645
- expect(back.searchParams.get("with") === "github", `the refusal does not name the provider the address signs in with: ${refused.location}`);
4646
- const said = "already signs in with another GitHub account";
4513
+ expect(back.pathname === "/login" && back.search === "?error=account_mismatch", `the refusal is not sent back to /login?error=account_mismatch: ${refused.location}`);
4514
+ const said = "already signs in with a different account";
4647
4515
  const html = await visitor.fetch(refused.location);
4648
4516
  expect(html.status === 200 && (await html.text()).includes(said), `${refused.location}, without scripts, does not say the address ${said}`, html.status);
4649
4517
  const chrome = await Chrome.open(executable);
@@ -4674,21 +4542,18 @@ const MARKERS = [
4674
4542
  pattern: /drizzle:[A-Z]/
4675
4543
  },
4676
4544
  ...[
4677
- "audit_chain_head",
4678
- "audit_log",
4679
- "capped_alerts",
4680
- "consumed_tokens",
4681
- "device_authorizations",
4545
+ "audit_archive",
4546
+ "device_logins",
4682
4547
  "dismissed_keys",
4548
+ "enrollment_codes",
4549
+ "log_head",
4550
+ "log_index",
4551
+ "log_pending",
4552
+ "log_tiles",
4683
4553
  "mcp_approvals",
4684
- "mcp_connections",
4685
4554
  "oauth_clients",
4686
- "secret_references",
4687
4555
  "secret_versions",
4688
- "service_bindings",
4689
- "vault_grants",
4690
- "vault_members",
4691
- "vault_releases"
4556
+ "spent_id_tokens"
4692
4557
  ].map((name) => ({
4693
4558
  label: `the table ${name}`,
4694
4559
  pattern: new RegExp(`\\b${name}\\b`)
@@ -4697,10 +4562,6 @@ const MARKERS = [
4697
4562
  label: "pg",
4698
4563
  pattern: /cloudflare:sockets|pg-protocol|pgpass/
4699
4564
  },
4700
- {
4701
- label: "libsql",
4702
- pattern: /@libsql|libsql/
4703
- },
4704
4565
  {
4705
4566
  label: "a COFFRE_ variable read",
4706
4567
  pattern: /env\s*(\.|\[\s*['"`])COFFRE_/
@@ -4730,19 +4591,28 @@ function clientFiles(dir) {
4730
4591
  }
4731
4592
  //#endregion
4732
4593
  //#region src/checks/surface.ts
4594
+ /**
4595
+ * A fresh deployment is not ready until its vault has adopted the database
4596
+ * and the signer has signed its first entry, what the app learns from the
4597
+ * vault's `about` (docs/design/0.16.md, "Readiness"). 0.14 asked the same of
4598
+ * its heartbeat and checkpoint, which 0.16 drops. Run first, while the
4599
+ * deployment is still fresh.
4600
+ */
4601
+ async function readyOnceAdopted(deployment) {
4602
+ const { origin } = deployment;
4603
+ if (!deployment.fresh) return "not a fresh deployment: its database was adopted before the suite";
4604
+ const early = await fetch(`${origin}/readyz`);
4605
+ const body = await early.text();
4606
+ expect(early.status === 503, `/readyz answered ${early.status} before the vault adopted the database`, body);
4607
+ await until("/readyz", async () => (await fetch(`${origin}/readyz`)).ok, 90);
4608
+ return "/readyz red until the vault adopted the database and signed its first entry, then green";
4609
+ }
4733
4610
  async function health(deployment) {
4734
4611
  const { origin } = deployment;
4735
4612
  const live = await fetch(`${origin}/livez`);
4736
4613
  expect(live.ok, `/livez answered ${live.status}`);
4737
- if (deployment.beforeFirstBeat) {
4738
- const early = await fetch(`${origin}/readyz`);
4739
- expect(early.status === 503, `/readyz answered ${early.status} before any heartbeat`, await early.text());
4740
- await deployment.scheduled();
4741
- }
4742
4614
  await until("/readyz", async () => (await fetch(`${origin}/readyz`)).ok, 20);
4743
- const ready = await (await fetch(`${origin}/readyz`)).json();
4744
- expect(ready.checkpointed === true, "/readyz passed without a checkpoint covering the heartbeat", ready);
4745
- return deployment.beforeFirstBeat ? "/livez, and /readyz only once the heartbeat ran and was checkpointed" : "/livez and /readyz";
4615
+ return "/livez and /readyz";
4746
4616
  }
4747
4617
  /** The headers coffre sets on everything it answers, but the policy, whose nonce is each response's own. */
4748
4618
  const SECURED = {
@@ -4901,16 +4771,19 @@ async function runSignsIn(deployment, admin, canaries) {
4901
4771
  expect(lifetime > 0 && lifetime <= 305e3, `the credential lives ${Math.round(lifetime / 1e3)} s, not five minutes`);
4902
4772
  const { values } = await bearer(deployment.origin, token).secrets.reveal(`${DEV}/API_KEY`);
4903
4773
  expect(values.API_KEY === canaries[`${DEV}/API_KEY`], "the credential did not read the value as the service");
4904
- const { entries } = await admin.api.audit.list({
4905
- actor: RUNNER,
4906
- detail: "1"
4907
- });
4908
- const exchanged = entries.find((entry) => entry.action === "token.exchange");
4909
- const read = entries.find((entry) => entry.author === "vault" && entry.action === "secret.read" && entry.key === "API_KEY");
4774
+ let entries = [];
4775
+ const exchangedIn = () => entries.find((entry) => entry.action === "token.exchange");
4776
+ const readIn = () => entries.find((entry) => entry.author === "vault" && entry.action === "secret.read" && entry.key === "API_KEY");
4777
+ await until("the exchange and the read in the log", async () => {
4778
+ ({entries} = await admin.api.audit.list({
4779
+ actor: RUNNER,
4780
+ detail: "1"
4781
+ }));
4782
+ return exchangedIn() !== void 0 && readIn() !== void 0;
4783
+ }, 90).catch(() => void 0);
4784
+ const [exchanged, read] = [exchangedIn(), readIn()];
4910
4785
  expect(exchanged !== void 0 && read !== void 0, "the exchange or the read is not in the log", entries.map((entry) => entry.action));
4911
- const credentialId = exchanged.metadata.credentialId;
4912
- expect(typeof credentialId === "string", "the exchange did not log its credential", exchanged.metadata);
4913
- expect(read.metadata.credentialId === credentialId, "the vault's entry for the read does not name the credential", read.metadata);
4786
+ expect(typeof exchanged.metadata.credential === "string", "the exchange did not log its credential", exchanged.metadata);
4914
4787
  expect(read.run?.exchangeSeq === exchanged.seq && read.run.claims.run_id === GITHUB_RUN.run_id && read.run.claims.repository === GITHUB_RUN.repository, "the read does not lead to its run", read.run);
4915
4788
  return {
4916
4789
  detail: `a five-minute credential for run ${GITHUB_RUN.run_id} of ${GITHUB_RUN.repository}; its read is logged with it, and leads to the run`,
@@ -4977,10 +4850,7 @@ async function runsRefused(deployment, admin) {
4977
4850
  "no_match"
4978
4851
  ]
4979
4852
  ];
4980
- const before = (await admin.api.audit.list({
4981
- actor: RUNNER,
4982
- detail: "1"
4983
- })).entries.filter((entry) => entry.action === "token.exchange").length;
4853
+ const from = Date.now();
4984
4854
  for (const [what, token, reason] of cases) {
4985
4855
  const answer = await exchange(deployment, token);
4986
4856
  expect(answer.status === 401 && answer.reason === reason, `a token ${what} was not refused as ${reason}`, shown(answer));
@@ -4988,11 +4858,18 @@ async function runsRefused(deployment, admin) {
4988
4858
  }
4989
4859
  const stranger = await exchange(deployment, await mint(deployment), "token:conformance-nobody");
4990
4860
  expect(stranger.status === 401 && stranger.reason === "no_match", "a token was taken for a service no binding names", shown(stranger));
4991
- const after = (await admin.api.audit.list({
4992
- actor: RUNNER,
4993
- detail: "1"
4994
- })).entries.filter((entry) => entry.action === "token.exchange").length;
4995
- expect(after === before, `${after - before} refused exchanges were logged as exchanges`);
4861
+ const to = Date.now();
4862
+ expect((await exchange(deployment, await mint(deployment))).status === 200, "a fresh token was refused");
4863
+ let exchanges = [];
4864
+ await until("an exchange taken after the refusals in the log", async () => {
4865
+ exchanges = (await admin.api.audit.list({
4866
+ actor: RUNNER,
4867
+ detail: "1"
4868
+ })).entries.filter((entry) => entry.action === "token.exchange");
4869
+ return exchanges.some((entry) => Date.parse(entry.occurredAt) >= to);
4870
+ }, 90);
4871
+ const logged = exchanges.filter((entry) => Date.parse(entry.occurredAt) >= from && Date.parse(entry.occurredAt) < to).length;
4872
+ expect(logged === 0, `${logged} refused exchanges were logged as exchanges`);
4996
4873
  return `${cases.length + 1} refused, each for its reason, none saying what the binding expects, none logged: ${cases.map(([what]) => what).join(", ")}, for another service`;
4997
4874
  }
4998
4875
  /**
@@ -5070,7 +4947,11 @@ async function keysUploaded(deployment, admin, canaries) {
5070
4947
  jwks: { keys },
5071
4948
  label: "conformance pod"
5072
4949
  });
5073
- expect(made.binding.keys?.length === 2 && /^sha256:/.test(made.binding.jwksUri), "the binding does not show the keys uploaded with it", made.binding);
4950
+ const listed = (await admin.api.bindings.list(POD)).bindings.find((binding) => binding.id === made.binding.id);
4951
+ expect(listed?.keys?.length === 2 && /^sha256:/.test(listed.jwksUri), "the binding does not show the keys uploaded with it", {
4952
+ made,
4953
+ listed
4954
+ });
5074
4955
  const now = Math.floor(Date.now() / 1e3);
5075
4956
  const pod = (alg, uid = POD_ACCOUNT.uid, times = {}) => mint(deployment, {
5076
4957
  iss: CLUSTER,
@@ -5095,7 +4976,7 @@ async function keysUploaded(deployment, admin, canaries) {
5095
4976
  nbf: now,
5096
4977
  exp: now + 3601
5097
4978
  }), POD);
5098
- expect(long.status === 401 && long.reason === "too_long", "a token that lives past the hour was taken", shown(long));
4979
+ expect(long.status === 401 && long.reason === "too_old", "a token that lives past the hour was taken", shown(long));
5099
4980
  await admin.api.bindings.replaceKeys(POD, {
5100
4981
  issuer: CLUSTER,
5101
4982
  jwks: { keys: keys.filter((key) => key.alg === "ES256") }
@@ -5106,17 +4987,21 @@ async function keysUploaded(deployment, admin, canaries) {
5106
4987
  expect(kept.status === 200, "a token signed by the key kept was refused", shown(kept));
5107
4988
  return `${POD} trusts ${CLUSTER} by its uploaded keys: a pod's token taken by its account's UID; another UID, and a token living past the hour, refused; the RS256 key replaced away, its tokens refused and ES256's taken`;
5108
4989
  }
5109
- /** Removing the binding ends the credentials it issued at once, and the next run gets none. */
4990
+ /**
4991
+ * Removing the binding ends the credentials it issued within the vault's
4992
+ * memory of who they are, 2 seconds (docs/design/0.16.md, D218), and the
4993
+ * next run gets none at once.
4994
+ */
5110
4995
  async function runUnbound(deployment, admin, bindingId, credential) {
5111
4996
  const me = (token) => fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${token}` } }).then((response) => response.status);
5112
4997
  expect(await me(credential) === 200, "the credential did not work before its binding was removed");
5113
4998
  await admin.api.bindings.remove(RUNNER, bindingId);
5114
- expect(await me(credential) === 401, "the credential still worked once its binding was removed");
4999
+ await until("the credential refused once its binding was removed", async () => await me(credential) === 401, 5);
5115
5000
  const next = await exchange(deployment, await mint(deployment));
5116
5001
  expect(next.status === 401 && next.reason === "no_match", "a run was let in once its binding was removed", shown(next));
5117
5002
  const { bindings } = await admin.api.bindings.list(RUNNER);
5118
5003
  expect(bindings.length === 0, "the removed binding is still listed", bindings);
5119
- return "its credential refused at once, and the next run refused as no binding matching";
5004
+ return "its credential refused within the vault's memory, and the next run refused as no binding matching";
5120
5005
  }
5121
5006
  /**
5122
5007
  * One address may exchange only so often: tokens that would be refused
@@ -5147,7 +5032,8 @@ async function tokensUnlogged(deployment) {
5147
5032
  //#region src/suite.ts
5148
5033
  /** The names of the checks that failed. */
5149
5034
  async function conform(deployment, options) {
5150
- const report = new Report();
5035
+ const report = new Report(void 0, UNSERVED);
5036
+ await report.check("ready once adopted", {}, () => readyOnceAdopted(deployment));
5151
5037
  await report.check("health", {}, () => health(deployment));
5152
5038
  await report.check("browser bundle", {}, () => browserBundle(deployment));
5153
5039
  const admin = await report.check("sign-in", {}, () => signInAdmin(deployment));
@@ -5262,14 +5148,12 @@ async function conform(deployment, options) {
5262
5148
  }, ({ admin, binding, credential }) => runUnbound(deployment, admin, binding, credential));
5263
5149
  await report.check("exchanges limited", { binding }, () => exchangesLimited(deployment));
5264
5150
  await report.check("tokens unlogged", { binding }, () => tokensUnlogged(deployment));
5265
- await report.check("checkpoints", { people }, ({ people }) => checkpoints(deployment, people));
5266
5151
  await report.check("keys behind writes", { people }, ({ people }) => writesAgree(deployment));
5267
5152
  await report.check("no audit, no value", all, ({ people, canaries }) => noAuditNoValue(deployment, people, canaries));
5268
5153
  await report.check("access authorship", { people }, ({ people }) => accessAuthorship(deployment, people));
5269
5154
  await report.check("no audit, no access", { people }, ({ people }) => noAuditNoAccess(deployment, people));
5270
- await report.check("full verification", { people }, ({ people }) => verification(deployment, people));
5271
- await report.check("checkpoint refused", {}, () => refusedCheckpoint(deployment));
5272
- await report.check("checkpoint missing", {}, () => missingCheckpoint(deployment));
5155
+ await report.check("audit proven", { people }, ({ people }) => auditProven(people));
5156
+ await report.check("audit page proofs", { people }, ({ people }) => auditPageProofs(people));
5273
5157
  await report.check("app login", {}, () => appLogin(deployment));
5274
5158
  await report.check("vault login", {}, () => vaultLogin(deployment));
5275
5159
  await report.check("forged grant", { people }, ({ people }) => memberTampering(deployment, people, "grant"));
@@ -5280,33 +5164,25 @@ async function conform(deployment, options) {
5280
5164
  await report.check("forged identity", { people }, ({ people }) => forgedIdentity(deployment, people));
5281
5165
  await report.check("forged approval", { people }, ({ people }) => forgedApproval(deployment, people));
5282
5166
  await report.check("edited generation", { people }, ({ people }) => editedGeneration(deployment, people));
5167
+ await report.check("log cut", { people }, ({ people }) => logCut(deployment, people));
5283
5168
  await report.check("canary scan", all, ({ people, canaries }) => canaryScan(deployment, people, canaries));
5284
- for (const author of ["app", "vault"]) await report.check(`${author} rewritten`, { people }, ({ people }) => rewrittenEntry(deployment, people, author));
5285
- await report.check("vault forged", { people }, ({ people }) => forgedVaultEntry(deployment, people));
5286
- await report.check("middle gap", { people }, ({ people }) => missingEntry(deployment, people, "middle"));
5287
- await report.check("middle cut", { people }, ({ people }) => middleCut(deployment, people));
5288
- await report.check("first gap", { people }, ({ people }) => missingEntry(deployment, people, "first"));
5289
- await report.check("batch gap", { people }, ({ people }) => missingEntry(deployment, people, "batch"));
5290
- await report.check("earlier checkpoint", { people }, ({ people }) => earlierCheckpoint(deployment, people));
5291
- await report.check("tail deleted", { people }, ({ people }) => deletedTail(deployment, people));
5292
5169
  return report.failed;
5293
5170
  }
5294
5171
  //#endregion
5295
5172
  //#region src/main.ts
5296
5173
  const USAGE = `usage:
5297
- coffre-conformance workers [<dir>] --postgres <owner URL> --runtime <coffre_runtime URL>
5174
+ coffre-conformance workers|node [<dir>] --postgres <owner URL> --runtime <coffre_runtime URL>
5298
5175
  --vault-runtime <coffre_vault_runtime URL> [options]
5299
- coffre-conformance node [<dir>] [options]
5300
5176
 
5301
5177
  <dir> the deployment, as \`coffre init\` wrote it (default: here)
5302
5178
  --port <n> coffre's port; the IdP gets the next, wrangler's inspector the one after (3082)
5303
5179
  --bulk-limit <n> the vault's bulkLimit count, when not the default (1000)
5304
5180
  --browser <path> Chrome or Chromium, to load the pages in (default: one found here;
5305
5181
  without one, that check is skipped)
5306
- --postgres <url> workers: a Postgres login that may create databases; one is made for the run
5307
- --runtime <url> workers: the same server as coffre_runtime, the login the app runs as
5182
+ --postgres <url> a Postgres login that may create databases; one is made for the run
5183
+ --runtime <url> the same server as coffre_runtime, the login the app runs as
5308
5184
  --vault-runtime <url>
5309
- workers: the same server as coffre_vault_runtime, the login the vault runs as
5185
+ the same server as coffre_vault_runtime, the login the vault runs as
5310
5186
 
5311
5187
  To check an instance that is running already, from outside, use the CLI:
5312
5188
  \`coffre verify instance\`.`;