@coffre/conformance 0.15.0 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -3,41 +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
10
  import { postgresSession } from "@coffre/db/connect";
12
11
  //#region src/database.ts
13
- async function postgres(url) {
12
+ async function postgres(url, schema = "public") {
14
13
  const client = postgresSession(url);
15
14
  await client.unsafe("SELECT 1");
16
15
  return {
17
- engine: "postgres",
16
+ schema,
18
17
  query: async (statement, params = []) => [...await client.unsafe(statement, params)],
19
18
  exec: async (statements) => void await client.unsafe(statements),
20
19
  close: () => client.end()
21
20
  };
22
21
  }
23
- /**
24
- * A SQLite file, perhaps open in another process: it waits out their locks.
25
- * An integer past 2^53, as 0.15's log numbers are, reads as a bigint; any other, as a number.
26
- */
27
- function sqlite(file) {
28
- const db = new DatabaseSync(file, { timeout: 5e3 });
29
- const exact = (value) => typeof value === "bigint" && value <= BigInt(Number.MAX_SAFE_INTEGER) && value >= BigInt(Number.MIN_SAFE_INTEGER) ? Number(value) : value;
30
- return {
31
- engine: "sqlite",
32
- query: async (statement, params = []) => {
33
- const prepared = db.prepare(statement);
34
- prepared.setReadBigInts(true);
35
- return prepared.all(...params).map((row) => Object.fromEntries(Object.entries(row).map(([name, value]) => [name, exact(value)])));
36
- },
37
- exec: async (statements) => db.exec(statements),
38
- close: async () => db.close()
39
- };
40
- }
41
22
  async function using(open, use) {
42
23
  const sql = await open;
43
24
  try {
@@ -92,7 +73,7 @@ function drainingConfig(server) {
92
73
  }
93
74
  if (import.meta.main) console.log(drainingConfig(resolve(process.argv[2])));
94
75
  //#endregion
95
- //#region ../client/dist/src-Dwhw_Cc4.js
76
+ //#region ../client/dist/src-JtgcfCXD.js
96
77
  /** Two colons or more, in hex: an IPv6 address, as `::1` or `2001:db8::1:443` with its port. */
97
78
  const IPV6 = /[0-9a-f]*:[0-9a-f]*:[0-9a-f:]*/i;
98
79
  const TIMEOUT_OR_REFUSAL = /* @__PURE__ */ new Set([
@@ -139,21 +120,33 @@ var Unreachable = class extends Error {
139
120
  this.origin = origin;
140
121
  }
141
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
+ }
142
138
  /** The API's error shape, `{ error, message }`, with the HTTP status. */
143
139
  var CoffreError = class extends Error {
144
140
  status;
145
141
  code;
146
142
  /** The vault's own code when it refused: `no_grant`, `removed`, `bulk_limit`, ... */
147
143
  reason;
148
- /** When to ask again, in seconds, when the answer says (`Retry-After`): a 503's, a 429's. */
149
- retryAfter;
150
- constructor(status, code, message, reason, retryAfter) {
144
+ constructor(status, code, message, reason) {
151
145
  super(message);
152
146
  this.name = "CoffreError";
153
147
  this.status = status;
154
148
  this.code = code;
155
149
  this.reason = reason;
156
- this.retryAfter = retryAfter;
157
150
  }
158
151
  };
159
152
  /** `market/prod/KEY` as route parameters. */
@@ -196,7 +189,7 @@ function createClient(options) {
196
189
  const payload = await response.json().catch(() => null);
197
190
  if (!response.ok) {
198
191
  const error = payload;
199
- throw new CoffreError(response.status, typeof error?.error === "string" ? error.error : "http_error", typeof error?.message === "string" ? error.message : `request failed with status ${response.status}`, typeof error?.reason === "string" ? error.reason : void 0, response.headers.get("retry-after") ?? void 0);
192
+ throw new CoffreError(response.status, typeof error?.error === "string" ? error.error : "http_error", typeof error?.message === "string" ? error.message : `request failed with status ${response.status}`, typeof error?.reason === "string" ? error.reason : void 0);
200
193
  }
201
194
  return payload;
202
195
  }
@@ -286,7 +279,7 @@ function createClient(options) {
286
279
  update: (path, patch) => call("PATCH /secrets/:project/:environment/:key", place(path), patch),
287
280
  /**
288
281
  * In a fork, a key back to its parent's value (`true`), its own kept,
289
- * or its own served again (`false`); `from` holds the parent's.
282
+ * or its own served again (`false`); the answer says which, `inherit`.
290
283
  */
291
284
  inherit: (path, inherit) => call("PATCH /secrets/:project/:environment/:key", place(path), { inherit })
292
285
  },
@@ -348,7 +341,7 @@ function createClient(options) {
348
341
  return send(method, `${route}?dryRun=1`, input);
349
342
  },
350
343
  create: (member, input) => call("POST /members/:member/bindings", { member }, input),
351
- /** 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. */
352
345
  previewKeys: (member, input) => {
353
346
  const [method, route] = address("POST /members/:member/bindings/keys", { member });
354
347
  return send(method, `${route}?dryRun=1`, input);
@@ -364,6 +357,21 @@ function createClient(options) {
364
357
  /** The repositories the deployment's GitHub App is installed on, private ones too; `{ app: null }` without one. */
365
358
  repositories: () => call("GET /workloads/repositories", {})
366
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
+ },
367
375
  access: {
368
376
  /** What they should hold at each place; the server applies the difference. `null` revokes. */
369
377
  set: (member, access) => call("PATCH /access/:member", { member }, access) },
@@ -471,61 +479,8 @@ set: (member, access) => call("PATCH /access/:member", { member }, access) },
471
479
  },
472
480
  audit: {
473
481
  list: (query = {}) => call("GET /audit", {}, query),
474
- /**
475
- * Whether the log holds, from the newest checkpoint; or, with `from`, a
476
- * stretch of the walk from the first entry: `start`, then each `next`
477
- * until it is null (`verifyWhole`).
478
- */
479
- verify: (query = {}) => call("GET /audit/verification", {}, query),
480
- /**
481
- * The walk from the first entry to the head, a stretch a request, each
482
- * as long as the server walks in one: the first stretch that fails, or
483
- * the last. `onStretch` sees each verdict as it comes.
484
- */
485
- verifyWhole: async (onStretch = () => {}) => {
486
- for (let from = "start";;) {
487
- const verdict = await call("GET /audit/verification", {}, { from });
488
- onStretch(verdict);
489
- if (!verdict.ok || verdict.next === void 0 || verdict.next === null) return verdict;
490
- from = verdict.next;
491
- }
492
- },
493
- /**
494
- * Whether 0.15's log holds (the vault's `verifyTrees`): its newest signed
495
- * checkpoint and every entry since. `kept` false where the instance
496
- * keeps 0.14's log alone, a server older than 0.15 (no such route)
497
- * included.
498
- */
499
- verifyTrees: () => call("GET /audit/trees", {}).catch((error) => {
500
- if (error instanceof CoffreError && error.status === 404) return { kept: false };
501
- throw error;
502
- }),
503
- /**
504
- * The reads the audit pages list held to the uses 0.15's log sealed, a
505
- * stretch a request, from the first to as far as the projector has
506
- * listed: the first stretch that fails, or the last. `kept` false where
507
- * the instance keeps 0.14's log alone, a server older than 0.15 (no
508
- * such route) included.
509
- */
510
- verifyReads: async (onStretch = () => {}) => {
511
- for (let from = "start";;) {
512
- const verdict = await call("GET /audit/reads", {}, { from }).catch((error) => {
513
- if (error instanceof CoffreError && error.status === 404) return { kept: false };
514
- throw error;
515
- });
516
- onStretch(verdict);
517
- if (!verdict.kept || !verdict.ok || verdict.next === null) return verdict;
518
- from = verdict.next;
519
- }
520
- },
521
482
  /** What an escrowed key is checked against: public material, for admins and owners of the whole instance. */
522
- keys: () => call("GET /audit/keys", {}),
523
- /**
524
- * Accept that entries `from` to `to` are lost for good, as an Owner of
525
- * the whole instance: the audit watch's finding, inside the range, stays
526
- * in the log, and its anchor goes on past them.
527
- */
528
- acceptLoss: (range) => call("POST /audit/loss", {}, range)
483
+ keys: () => call("GET /audit/keys", {})
529
484
  }
530
485
  };
531
486
  }
@@ -566,8 +521,11 @@ async function until(what, probe, seconds, alive = () => true) {
566
521
  var Report = class {
567
522
  #results = [];
568
523
  #width;
569
- constructor(width = 19) {
524
+ #unserved;
525
+ /** `unserved`: checks skipped by name, each with why (../unserved.ts). */
526
+ constructor(width = 19, unserved = {}) {
570
527
  this.#width = width;
528
+ this.#unserved = unserved;
571
529
  }
572
530
  get failed() {
573
531
  return this.#results.filter((result) => result.status === "FAIL").map((result) => result.name);
@@ -578,6 +536,10 @@ var Report = class {
578
536
  * against a deployment that is not in the state it expects.
579
537
  */
580
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
+ }
581
543
  const missing = Object.keys(needs).filter((key) => needs[key] === void 0 || needs[key] === null);
582
544
  if (missing.length > 0) {
583
545
  this.#print(name, "skip", `no ${missing.join(" or ")}: an earlier check failed`);
@@ -657,12 +619,7 @@ async function boot(kind, at, options) {
657
619
  port + 1,
658
620
  port + 2
659
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`);
660
- if (kind === "workers" && [
661
- options.postgres,
662
- options.runtime,
663
- options.vaultRuntime,
664
- options.hotRuntime
665
- ].includes(void 0)) throw new Failure("workers run on Postgres: pass --postgres <owner URL>, --runtime <coffre_runtime URL>, --vault-runtime <coffre_vault_runtime URL> and --hot-runtime <coffre_vault_hot 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>");
666
623
  const scratch = mkdtempSync(join(tmpdir(), `coffre-conformance-${kind}-`));
667
624
  const shell = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith("COFFRE_")));
668
625
  const children = [];
@@ -750,15 +707,9 @@ async function boot(kind, at, options) {
750
707
  force: true
751
708
  });
752
709
  }
753
- /**
754
- * The door open: requests answered, not held. The first after a start cuts 0.15's log over from 0.14, and the
755
- * others answer 503 meanwhile, with Retry-After, as a deployment's clients see; a check's requests come after.
756
- */
757
- const doorOpen = () => until(`${origin}/api/me answered, not held`, async () => (await fetch(`${origin}/api/me`)).status !== 503, 60, alive);
758
710
  try {
759
711
  const deployment = kind === "workers" ? await startWorkers() : await startNode();
760
712
  await until(`${origin}/livez`, async () => (await fetch(`${origin}/livez`)).ok, 120, alive);
761
- await doorOpen();
762
713
  return deployment;
763
714
  } catch (error) {
764
715
  const seen = logs();
@@ -766,12 +717,13 @@ async function boot(kind, at, options) {
766
717
  if (error instanceof Failure) throw new Failure(error.message, `${error.detail ?? ""}\n${seen}`.trim());
767
718
  throw error;
768
719
  }
769
- 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() {
770
725
  const name = `coffre_conformance_${randomBytes(4).toString("hex")}`;
771
726
  const owner = withDatabase(options.postgres, name);
772
- const runtime = withDatabase(options.runtime, name);
773
- const vaultRuntime = withDatabase(options.vaultRuntime, name);
774
- const hotRuntime = withDatabase(options.hotRuntime, name);
775
727
  await using(postgres(withDatabase(options.postgres, "postgres")), (sql) => sql.exec(`CREATE DATABASE ${name}`));
776
728
  cleanup = () => using(postgres(withDatabase(options.postgres, "postgres")), (sql) => sql.exec(`DROP DATABASE IF EXISTS ${name} WITH (FORCE)`));
777
729
  await run(process.execPath, [
@@ -779,6 +731,14 @@ async function boot(kind, at, options) {
779
731
  "migrate",
780
732
  "--yes"
781
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();
782
742
  await run(bin("vite"), ["build", "app"]);
783
743
  const state = join(scratch, "state");
784
744
  const startWrangler = () => start("wrangler", bin("wrangler"), [
@@ -819,7 +779,6 @@ async function boot(kind, at, options) {
819
779
  ...OWNER_VARS,
820
780
  CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE: runtime,
821
781
  CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_VAULT_HYPERDRIVE: vaultRuntime,
822
- CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HOT_HYPERDRIVE: hotRuntime,
823
782
  WRANGLER_SEND_METRICS: "false"
824
783
  });
825
784
  let wrangler = startWrangler();
@@ -828,7 +787,7 @@ async function boot(kind, at, options) {
828
787
  origin,
829
788
  idp,
830
789
  owner: OWNER,
831
- beforeFirstBeat: true,
790
+ fresh: true,
832
791
  async scheduled(options = {}) {
833
792
  const response = await fetch(`${origin}/cdn-cgi/local/scheduled?cron=*/5+*+*+*+*`);
834
793
  if (!response.ok && !options.allowFailure) throw new Failure(`the scheduled handler answered ${response.status}`, `${await response.text()}\n${logs()}`);
@@ -837,30 +796,24 @@ async function boot(kind, at, options) {
837
796
  await halt(wrangler);
838
797
  wrangler = startWrangler();
839
798
  await until(`${origin}/livez`, async () => (await fetch(`${origin}/livez`)).ok, 120, alive);
840
- await doorOpen();
841
799
  },
842
800
  serving: () => ({ wrangler: wrangler.child.pid }),
843
801
  database: () => postgres(owner),
844
802
  runtime: () => postgres(runtime),
845
803
  vaultRuntime: () => postgres(vaultRuntime),
846
- hotRuntime: () => postgres(hotRuntime),
847
- databaseFile: null,
848
804
  clientDir: join(dir, "app/dist/client"),
849
805
  output,
850
806
  stop
851
807
  };
852
808
  }
853
809
  async function startNode() {
854
- const database = join(scratch, "coffre.db");
810
+ const { owner, runtime, vaultRuntime } = await ownDatabase();
855
811
  const socket = join(scratch, "vault.sock");
856
- await run(bin("coffre-server"), ["migrate", `file:${database}`]);
857
812
  const startVault = async () => {
858
813
  rmSync(socket, { force: true });
859
814
  const started = start("vault", process.execPath, ["src/vault.ts"], {
860
815
  VAULT_SOCKET: socket,
861
- DATABASE_URL: `file:${database}`,
862
- HOT_DATABASE_URL: `file:${database}`,
863
- CHECKPOINTER_MEMORY: join(scratch, "checkpointer-memory"),
816
+ DATABASE_URL: vaultRuntime,
864
817
  VAULT_KEY_ID: KEYS.VAULT_KEY_ID,
865
818
  VAULT_KEY: KEYS.VAULT_KEY,
866
819
  ...OWNER_VARS
@@ -877,9 +830,8 @@ async function boot(kind, at, options) {
877
830
  ], {
878
831
  PORT: String(port),
879
832
  PUBLIC_URL: origin,
880
- DATABASE_URL: `file:${database}`,
833
+ DATABASE_URL: runtime,
881
834
  VAULT_SOCKET: socket,
882
- AUDIT_ANCHOR: join(scratch, "anchor.json"),
883
835
  APP_KEY: KEYS.APP_KEY,
884
836
  ALLOW_LOOPBACK_ISSUERS_FOR_DEVELOPMENT: "true",
885
837
  ...github
@@ -890,12 +842,11 @@ async function boot(kind, at, options) {
890
842
  origin,
891
843
  idp,
892
844
  owner: OWNER,
893
- beforeFirstBeat: false,
845
+ fresh: false,
894
846
  async scheduled() {
895
847
  await halt(server);
896
848
  server = startServer();
897
849
  await until("the restarted server", async () => (await fetch(`${origin}/livez`)).ok, 30, alive);
898
- await doorOpen();
899
850
  },
900
851
  async restart() {
901
852
  await halt(server);
@@ -903,17 +854,14 @@ async function boot(kind, at, options) {
903
854
  vault = await startVault();
904
855
  server = startServer();
905
856
  await until("the restarted server", async () => (await fetch(`${origin}/livez`)).ok, 30, alive);
906
- await doorOpen();
907
857
  },
908
858
  serving: () => ({
909
859
  vault: vault.child.pid,
910
860
  server: server.child.pid
911
861
  }),
912
- database: async () => sqlite(database),
913
- runtime: null,
914
- vaultRuntime: null,
915
- hotRuntime: null,
916
- databaseFile: database,
862
+ database: () => postgres(owner),
863
+ runtime: () => postgres(runtime),
864
+ vaultRuntime: () => postgres(vaultRuntime),
917
865
  clientDir: join(dir, "app/dist/client"),
918
866
  output,
919
867
  stop
@@ -926,6 +874,9 @@ function withDatabase(url, database) {
926
874
  return parsed.href;
927
875
  }
928
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
929
880
  //#region src/browser.ts
930
881
  var Browser = class {
931
882
  origin;
@@ -1101,6 +1052,16 @@ async function setUpLive(admin, canaries) {
1101
1052
  };
1102
1053
  }
1103
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
+ }
1104
1065
  function valuesIn(canaries, environment) {
1105
1066
  return Object.fromEntries(Object.entries(canaries).filter(([path]) => path.startsWith(`${environment}/`)).map(([path, value]) => [path.slice(environment.length + 1), value]));
1106
1067
  }
@@ -1197,6 +1158,81 @@ async function pageLoad(person) {
1197
1158
  return `${paths.length} calls at once, each answered`;
1198
1159
  }
1199
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
1200
1236
  //#region src/checks/mcp.ts
1201
1237
  const NAME = "Conformance MCP client";
1202
1238
  /** A native client's redirect, on loopback. A custom scheme is left out of a registration (D37). */
@@ -1401,7 +1437,7 @@ async function mcpConnect(deployment, people) {
1401
1437
  refresh_token: next.refresh_token,
1402
1438
  client_id: clientId
1403
1439
  });
1404
- 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`);
1405
1441
  const asked = await connect(deployment, people.reader, clientId);
1406
1442
  const second = await redeem(deployment, clientId, asked);
1407
1443
  await refused(deployment, "a code redeemed already", {
@@ -1418,25 +1454,45 @@ async function mcpConnect(deployment, people) {
1418
1454
  await people.reader.api.apps.disconnect(listed[0].id);
1419
1455
  expect(!await discovers(deployment, third.access_token), "an access token outlived its disconnection");
1420
1456
  expect((await people.reader.api.apps.list()).apps.length === 0, "a disconnected app is still listed");
1421
- const { entries } = await people.admin.api.audit.list({
1422
- actor: people.reader.member,
1423
- detail: "1",
1424
- limit: 500
1425
- });
1426
- const mine = entries.filter((entry) => entry.metadata.clientId === clientId);
1427
- const count = (action) => mine.filter((entry) => entry.action === action && entry.decision === "allow").length;
1428
- const counts = {
1429
- connect: count("mcp.connect"),
1430
- token: count("mcp.token"),
1431
- 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
+ });
1432
1476
  };
1433
- 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);
1434
1485
  const reasons = mine.filter((entry) => entry.action === "mcp.disconnect").map((entry) => entry.metadata.reason).sort();
1435
- expect(JSON.stringify(reasons) === JSON.stringify([
1436
- "code_reused",
1437
- "person",
1438
- "refresh_reused"
1439
- ]), "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));
1440
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";
1441
1497
  }
1442
1498
  /** A 2026-07-28 request as a client sends it: its headers say what its body does; `capabilities` are the client's. */
@@ -1542,25 +1598,28 @@ async function mcpRead(deployment, people, canaries) {
1542
1598
  expect(legacy.status === 200 && legacyBody.result?.isError !== true, `a 2025-11-25 tools/call answered ${legacy.status}`, legacyBody);
1543
1599
  const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1544
1600
  expect(leaked.length === 0, "a Read tool answered with a secret value", leaked);
1545
- const { entries } = await people.admin.api.audit.list({
1546
- actor: people.reader.member,
1547
- detail: "1",
1548
- limit: 500
1549
- });
1550
- const mine = entries.filter((entry) => entry.metadata.via?.clientId === clientId);
1551
- const reads = mine.filter((entry) => entry.action === "mcp.read").length;
1552
- const refused = mine.filter((entry) => entry.action === "mcp.call" && entry.decision === "deny").length;
1553
- expect(reads === 5 && refused === 1, "the log does not hold each call under the client", {
1554
- reads,
1555
- refused
1556
- });
1557
- 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));
1558
- const shown = (await people.admin.api.audit.list({
1559
- actor: people.reader.member,
1560
- limit: 500
1561
- })).entries;
1562
- expect(!shown.some((entry) => entry.action === "mcp.read"), "read-only calls are not detail");
1563
- 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`;
1564
1623
  }
1565
1624
  /**
1566
1625
  * Changes, as a client asks for them (design section 14, checks 2 and 3):
@@ -1661,7 +1720,10 @@ async function mcpChanges(deployment, people, canaries) {
1661
1720
  basis: shown.basis
1662
1721
  }).then(() => null, (error) => error);
1663
1722
  expect(again instanceof CoffreError && again.status === 409, "an approval was decided twice", again);
1664
- 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
+ });
1665
1727
  expect(reported.result?.structuredContent?.status === "approved" && reported.result.isError !== true, "calling again does not report the approved change", reported.result);
1666
1728
  const elicits = { elicitation: { url: {} } };
1667
1729
  const asked = await call(write, "rename_secret", {
@@ -1727,26 +1789,34 @@ async function mcpChanges(deployment, people, canaries) {
1727
1789
  });
1728
1790
  const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1729
1791
  expect(leaked.length === 0, "a change tool answered with a secret value", leaked);
1730
- const { entries } = await admin.api.audit.list({
1731
- actor: changer.member,
1732
- limit: 500
1733
- });
1734
- const viaApproval = (action) => entries.filter((entry) => entry.action === action && entry.decision === "allow" && typeof entry.metadata.via?.approvalId === "string");
1735
- const once = {
1736
- archive: viaApproval("secret.archive").length,
1737
- rename: viaApproval("secret.rename").length,
1738
- 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
+ });
1739
1809
  };
1740
- expect(once.archive === 1 && once.rename === 1 && once.write === 2, "the changes are not each made once, via the client and its approval", once);
1741
- const decisions = entries.filter((entry) => entry.action === "mcp.approve").map((entry) => `${entry.decision} ${entry.reason ?? ""}`.trim()).sort();
1742
- expect(JSON.stringify(decisions) === JSON.stringify([
1743
- "allow",
1744
- "allow",
1745
- "allow",
1746
- "allow",
1747
- "deny approved",
1748
- "deny changed"
1749
- ]), "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);
1750
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`;
1751
1821
  }
1752
1822
  /**
@@ -1785,7 +1855,10 @@ async function mcpValues(deployment, people, canaries) {
1785
1855
  digest: page.digest,
1786
1856
  basis: page.basis
1787
1857
  })).shown.some((line) => line.value === dev.API_KEY), "Reveal did not show the person the value");
1788
- 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
+ });
1789
1862
  expect(told.result?.structuredContent?.status === "approved", "show_secret_value does not report the value as shown", told.result);
1790
1863
  const readLeaks = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1791
1864
  expect(readLeaks.length === 0, "a Read answer held a value", readLeaks);
@@ -1798,150 +1871,6 @@ async function mcpValues(deployment, people, canaries) {
1798
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}`;
1799
1872
  }
1800
1873
  //#endregion
1801
- //#region src/checks/storage.ts
1802
- const TABLES = [
1803
- "projects",
1804
- "environments",
1805
- "secrets",
1806
- "secret_versions",
1807
- "vault_members",
1808
- "vault_grants",
1809
- "audit_log",
1810
- "audit_chain_head",
1811
- "identities",
1812
- "credentials",
1813
- "device_authorizations"
1814
- ];
1815
- /** Inspect every table, including any a deployment added. Missing shared tables fail. */
1816
- async function tables(sql) {
1817
- 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);
1818
- const missing = TABLES.filter((name) => !names.includes(name));
1819
- expect(missing.length === 0, "shared tables could not be inspected", missing);
1820
- return names;
1821
- }
1822
- function quoted(name) {
1823
- return `"${name.replaceAll("\"", "\"\"")}"`;
1824
- }
1825
- /** Both drivers return binary columns as bytes; casting records to text hides bytea. */
1826
- async function scanTables(sql, names, look) {
1827
- 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 : typeof value === "bigint" ? String(value) : JSON.stringify(value) ?? "");
1828
- }
1829
- /** Parameters use one spelling in the checks, whatever the database. */
1830
- function query(sql, statement, params = []) {
1831
- return sql.query(sql.engine === "sqlite" ? statement.replace(/\$\d+/g, "?") : statement, params);
1832
- }
1833
- /** Insert a saved or forged row as it is, but for its generated columns. */
1834
- async function insertRow(sql, table, row) {
1835
- const fields = Object.keys(row).filter((field) => field !== "active_subject");
1836
- await query(sql, `INSERT INTO ${quoted(table)} (${fields.map(quoted).join(", ")}) VALUES (${fields.map((_, i) => `$${i + 1}`).join(", ")})`, fields.map((field) => row[field]));
1837
- }
1838
- /** Put a saved row back without changing its bytes or its generated columns. */
1839
- async function restoreRow(sql, table, row, key) {
1840
- const fields = Object.keys(row).filter((field) => field !== key && field !== "active_subject");
1841
- 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]]);
1842
- }
1843
- /** Keep a deliberate rewrite, and its restoration, invisible until each is complete. */
1844
- async function transaction(sql, work) {
1845
- await sql.exec("BEGIN");
1846
- try {
1847
- const result = await work();
1848
- await sql.exec("COMMIT");
1849
- return result;
1850
- } catch (error) {
1851
- await sql.exec("ROLLBACK");
1852
- throw error;
1853
- }
1854
- }
1855
- /**
1856
- * `work` with the audit log's append-only triggers lifted, as only its owner
1857
- * can: Postgres disables them for the session, SQLite drops them and makes
1858
- * them again after.
1859
- */
1860
- async function appendOnlyLifted(sql, work) {
1861
- if (sql.engine === "postgres") {
1862
- await sql.exec("ALTER TABLE audit_log DISABLE TRIGGER USER");
1863
- try {
1864
- return await work();
1865
- } finally {
1866
- await sql.exec("ALTER TABLE audit_log ENABLE TRIGGER USER");
1867
- }
1868
- }
1869
- const triggers = await sql.query(`SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = 'audit_log'`);
1870
- for (const trigger of triggers) await sql.exec(`DROP TRIGGER ${trigger.name}`);
1871
- try {
1872
- return await work();
1873
- } finally {
1874
- for (const trigger of triggers) await sql.exec(trigger.sql);
1875
- }
1876
- }
1877
- const LOG_REFUSAL = "coffre-conformance: the log refuses writes";
1878
- /**
1879
- * `work` with a log refusing appends, as a full disk or a lost connection
1880
- * would, without changing the deployment's code: a trigger, dropped after.
1881
- * 0.14's, `audit_log`, of one action or all; or 0.15's uses, `log_uses`.
1882
- *
1883
- * On Postgres, each statement first locks the table itself, as every reader
1884
- * does: `log_uses` is partitioned by day, and a trigger on it is one on each
1885
- * day, which `DROP TRIGGER` locks newest first, while a query reading several
1886
- * days locks them oldest first. A use, or the reads' projector, holding one
1887
- * day and waiting on the next deadlocked with the drop (seen in CI; 15 in 50
1888
- * drops against uses and the projector, none in 2,703 with the table locked
1889
- * first).
1890
- */
1891
- async function logRefuses(sql, work, action, table = "audit_log") {
1892
- const when = action === void 0 ? "" : ` WHEN (NEW.action = '${action}')`;
1893
- const create = sql.engine === "postgres" ? `LOCK TABLE ${table} IN SHARE ROW EXCLUSIVE MODE;
1894
- CREATE FUNCTION conformance_refuse() RETURNS trigger LANGUAGE plpgsql AS $$
1895
- BEGIN RAISE EXCEPTION '${LOG_REFUSAL}'; END $$;
1896
- CREATE TRIGGER conformance_refuse BEFORE INSERT ON ${table} FOR EACH ROW${when} EXECUTE FUNCTION conformance_refuse();` : `CREATE TRIGGER conformance_refuse BEFORE INSERT ON ${table}${when}
1897
- BEGIN SELECT RAISE(ABORT, '${LOG_REFUSAL}'); END;`;
1898
- await sql.exec(create);
1899
- try {
1900
- return await work();
1901
- } finally {
1902
- await sql.exec(sql.engine === "postgres" ? `LOCK TABLE ${table} IN ACCESS EXCLUSIVE MODE; DROP TRIGGER conformance_refuse ON ${table}; DROP FUNCTION conformance_refuse();` : "DROP TRIGGER conformance_refuse;");
1903
- }
1904
- }
1905
- /** The log's fields in the order its format (`coffre.audit.v2`) encodes them. */
1906
- const ENTRY_FIELDS = [
1907
- "seq",
1908
- "author",
1909
- "key_id",
1910
- "occurred_at",
1911
- "actor",
1912
- "action",
1913
- "decision",
1914
- "code",
1915
- "subject_principal",
1916
- "project_id",
1917
- "environment_id",
1918
- "secret_id",
1919
- "secret_version_id",
1920
- "operation_id",
1921
- "request_id",
1922
- "source_ip",
1923
- "related_seq",
1924
- "metadata"
1925
- ];
1926
- /**
1927
- * An entry's fields as its MAC and hash cover them, written here from the
1928
- * format rather than taken from coffre, so a check does not trust the code
1929
- * it checks: each field length-prefixed in order, null as length -1.
1930
- */
1931
- function entryFields(row) {
1932
- return Buffer.concat(ENTRY_FIELDS.flatMap((field) => {
1933
- const header = Buffer.alloc(4);
1934
- const value = row[field];
1935
- if (value === null || value === void 0) {
1936
- header.writeInt32BE(-1);
1937
- return [header];
1938
- }
1939
- const bytes = Buffer.from(String(value));
1940
- header.writeInt32BE(bytes.length);
1941
- return [header, bytes];
1942
- }));
1943
- }
1944
- //#endregion
1945
1874
  //#region src/checks/members.ts
1946
1875
  async function member(deployment, { admin }, name) {
1947
1876
  const email = `${name}@conformance.example`;
@@ -1957,83 +1886,117 @@ async function member(deployment, { admin }, name) {
1957
1886
  api: browser.client()
1958
1887
  };
1959
1888
  }
1960
- /** 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
+ */
1961
1943
  async function memberTampering(deployment, people, kind) {
1962
1944
  const person = await member(deployment, people, `tamper-${kind}`);
1963
1945
  const { admin } = people;
1964
- const path = kind === "grant" ? PROD : DEV;
1965
- 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;
1966
1948
  await using(deployment.database(), async (sql) => {
1967
- const [before] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
1968
- 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]);
1969
1951
  expect(before !== void 0 && grants.length === 1, "the fixture has no member row and grant");
1970
- let restored = before;
1971
1952
  let held = grants;
1972
- if (kind === "stale") {
1973
- await admin.api.access.set(person.member, { [DEV]: "developer" });
1974
- [restored] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
1975
- held = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
1976
- await restoreRow(sql, "vault_members", before, "principal");
1977
- await query(sql, "UPDATE vault_grants SET role = $1, granted_at = $2, granted_by = $3 WHERE principal = $4", [
1978
- grants[0].role,
1979
- grants[0].granted_at,
1980
- grants[0].granted_by,
1981
- person.member
1982
- ]);
1983
- } else if (kind === "member") await query(sql, "UPDATE vault_members SET role = 'owner' WHERE principal = $1", [person.member]);
1984
- else {
1985
- const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
1986
- WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
1987
- expect(place !== void 0, "no prod environment to forge a grant on");
1988
- await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by) VALUES ($1, $2, 'viewer', $3, $4)`, [
1989
- person.member,
1990
- place.id,
1991
- Date.now(),
1992
- person.member
1993
- ]);
1994
- }
1953
+ let named;
1995
1954
  try {
1996
- const response = await person.browser.send("POST", "/api/reveals", { path });
1997
- const text = await response.text();
1998
- expect(response.status === 401 && JSON.parse(text).error === "unauthenticated", "the tampered credential was not refused", text);
1999
- expect(!protectedValues.some((value) => text.includes(value)), "the tampered refusal carried a value", text);
2000
- const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
2001
- AND subject_principal = $1 AND code = $2`, [person.member, kind === "stale" ? "stale" : "mac"]);
2002
- expect(reports.length > 0, "the tampering was not logged at use", reports);
2003
- expect((await admin.api.members.get(person.member)).status === "tampered", "the refused member is not marked tampered");
2004
- const verified = await admin.api.audit.verify();
2005
- 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);
2006
1973
  } finally {
2007
- await restoreRow(sql, "vault_members", restored, "principal");
2008
- await query(sql, "DELETE FROM vault_grants WHERE principal = $1", [person.member]);
2009
- 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);
2010
1977
  }
2011
1978
  });
2012
1979
  expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "restoring the member did not restore legitimate use");
2013
- expect((await admin.api.audit.verify()).ok, "the restored member did not verify");
2014
- 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`;
2015
1983
  }
2016
- /** 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. */
2017
1985
  async function accessAuthorship(deployment, people) {
2018
1986
  const person = await member(deployment, people, "access-actions");
2019
1987
  const { admin } = people;
2020
1988
  await admin.api.access.set(person.member, { [DEV]: null });
2021
1989
  await admin.api.members.remove(person.member);
2022
1990
  await admin.api.members.add(person.member);
2023
- await using(deployment.database(), async (sql) => {
2024
- const access = await sql.query(`SELECT author, action, subject_principal FROM audit_log
2025
- WHERE decision = 'allow' AND (action LIKE 'access.%' OR action LIKE 'member.%' OR action LIKE 'directory.%' OR action LIKE 'grant.%' OR action LIKE 'principal.%')`);
2026
- expect(access.length > 0 && access.every((entry) => entry.author === "vault"), "an access change was written by the app", access.filter((entry) => entry.author !== "vault"));
2027
- const actions = access.filter((entry) => entry.subject_principal === person.member).map((entry) => entry.action);
2028
- for (const action of [
2029
- "member.add",
2030
- "member.remove",
2031
- "member.restore",
2032
- "access.grant",
2033
- "access.revoke"
2034
- ]) expect(actions.includes(action), `${action} left no vault entry`, actions);
2035
- });
2036
- 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";
2037
2000
  }
2038
2001
  /** A failure to append may not commit an access change. */
2039
2002
  async function noAuditNoAccess(deployment, people) {
@@ -2041,8 +2004,8 @@ async function noAuditNoAccess(deployment, people) {
2041
2004
  const { admin } = people;
2042
2005
  await using(deployment.database(), async (sql) => {
2043
2006
  const snapshot = async () => ({
2044
- members: await sql.query("SELECT * FROM vault_members ORDER BY principal"),
2045
- 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")
2046
2009
  });
2047
2010
  const before = await snapshot();
2048
2011
  await logRefuses(sql, async () => {
@@ -2059,62 +2022,59 @@ async function noAuditNoAccess(deployment, people) {
2059
2022
  expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "legitimate reads did not recover");
2060
2023
  return "grant, removal and admission commit nothing while the log refuses their entries";
2061
2024
  }
2062
- /** 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
+ */
2063
2037
  async function sealingRace(deployment, people) {
2064
- if (deployment.vaultRuntime === null) throw new Skip("SQLite prevents a write beside a decision; the vault suite covers review R2 on Postgres");
2065
2038
  const [login] = await using(deployment.vaultRuntime(), (sql) => sql.query("SELECT current_user AS name"));
2066
2039
  const person = await member(deployment, people, "seal-race");
2067
2040
  const { admin } = people;
2041
+ const protectedValues = Object.values((await admin.api.secrets.reveal(PROD)).values);
2042
+ let forged;
2068
2043
  try {
2069
2044
  await using(deployment.database(), (holder) => using(deployment.database(), async (sql) => {
2070
- const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
2071
- WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
2072
- const [head] = await sql.query("SELECT next_seq FROM audit_chain_head");
2073
2045
  const [owner] = await holder.query("SELECT pg_backend_pid() AS pid");
2074
- expect(place !== void 0, "no prod environment for the sealing race");
2075
2046
  await holder.exec("BEGIN");
2076
2047
  let pending;
2048
+ let outcome;
2077
2049
  try {
2078
- await holder.query("SELECT 1 FROM audit_chain_head FOR UPDATE");
2050
+ await holder.query(`SELECT pg_advisory_xact_lock(${CHANGE_LOCK})`);
2079
2051
  pending = admin.api.access.set(person.member, { [DEV]: "developer" }).then((value) => ({ value }), (error) => ({ error }));
2080
- await until("the vault decision waiting on our head", async () => {
2052
+ await until("the vault's change waiting on the change lock", async () => {
2081
2053
  return (await query(sql, `SELECT pid FROM pg_stat_activity WHERE datname = current_database()
2082
2054
  AND usename = $1 AND $2 = ANY(pg_blocking_pids(pid))`, [login.name, owner.pid])).length > 0;
2083
2055
  }, 10);
2084
- await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by)
2085
- VALUES ($1, $2, 'viewer', $3, $4)`, [
2086
- person.member,
2087
- place.id,
2088
- Date.now(),
2089
- person.member
2090
- ]);
2056
+ forged = await forgeGrant(sql, person.member);
2091
2057
  } finally {
2092
2058
  await holder.exec("ROLLBACK");
2093
- if (pending !== void 0) await pending;
2059
+ if (pending !== void 0) outcome = await pending;
2094
2060
  }
2095
- expect(pending !== void 0, "the access decision was not started");
2096
- const result = await pending;
2097
- 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);
2098
- const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
2099
- AND subject_principal = $1 AND code = 'mac' AND seq >= $2`, [person.member, head.next_seq]);
2100
- expect(reports.length > 0, "the grant inserted during the decision was not reported", reports);
2101
- const response = await person.browser.send("POST", "/api/reveals", { path: PROD });
2102
- const body = await response.json();
2103
- 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);
2104
2068
  }));
2105
2069
  } finally {
2070
+ if (forged !== void 0) await using(deployment.database(), (sql) => query(sql, "DELETE FROM grants WHERE id = $1", [forged]));
2106
2071
  await admin.api.members.remove(person.member);
2107
2072
  }
2108
- expect((await admin.api.audit.verify()).ok, "the log did not verify after recovering the sealing-race member");
2109
- 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";
2110
2074
  }
2111
2075
  //#endregion
2112
2076
  //#region src/checks/signin.ts
2113
2077
  const hash = (value) => createHash("sha256").update(value).digest();
2114
- async function reported(deployment, id) {
2115
- const event = new RegExp(`auth_row_tampered[^}]*${id}`);
2116
- await until(`the sign-in row failure report for ${id}`, async () => event.test(deployment.output()), 10);
2117
- }
2118
2078
  /** Knowing the database password and a chosen token must not mint a credential. */
2119
2079
  async function forgedCredential(deployment, people) {
2120
2080
  const person = await member(deployment, people, "forged-credential");
@@ -2133,7 +2093,7 @@ async function forgedCredential(deployment, people) {
2133
2093
  try {
2134
2094
  const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${token}` } });
2135
2095
  expect(response.status === 401, "an owner-forged credential authenticated", await response.text());
2136
- await reported(deployment, forged.id);
2096
+ await alerted(deployment, "credentials", forged.id);
2137
2097
  } finally {
2138
2098
  await query(sql, "DELETE FROM credentials WHERE id = $1", [forged.id]);
2139
2099
  }
@@ -2158,7 +2118,7 @@ async function forgedIdentity(deployment, people) {
2158
2118
  try {
2159
2119
  await signIn(deployment, browser, email).catch(() => {});
2160
2120
  expect((await browser.fetch("/api/me")).status === 401, "an owner-forged identity minted a session");
2161
- await reported(deployment, forged.id);
2121
+ await alerted(deployment, "identities", forged.id);
2162
2122
  } finally {
2163
2123
  await query(sql, "DELETE FROM credentials WHERE identity_id = $1", [forged.id]);
2164
2124
  await query(sql, "DELETE FROM identities WHERE id = $1", [forged.id]);
@@ -2173,472 +2133,82 @@ async function startDevice(deployment) {
2173
2133
  headers: { "content-type": "application/json" },
2174
2134
  body: "{}"
2175
2135
  });
2176
- expect(response.ok, "the device fixture did not start", await response.clone().text());
2177
- return response.json();
2178
- }
2179
- function pollDevice(deployment, code) {
2180
- return fetch(`${deployment.origin}/api/auth/device/token`, {
2181
- method: "POST",
2182
- headers: { "content-type": "application/json" },
2183
- body: JSON.stringify({ device_code: code })
2184
- });
2185
- }
2186
- /** An authentic pending row, changed to approved around the app, grants nothing. */
2187
- async function forgedApproval(deployment, people) {
2188
- const person = await member(deployment, people, "forged-approval");
2189
- const device = await startDevice(deployment);
2190
- await using(deployment.database(), async (sql) => {
2191
- const [row] = await query(sql, "SELECT * FROM device_authorizations WHERE user_code = $1", [device.user_code]);
2192
- const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [person.member]);
2193
- expect(row !== void 0, "no device row to forge");
2194
- await query(sql, `UPDATE device_authorizations SET decision = 'approved', decided_at = $1, principal = $2, generation = $3 WHERE id = $4`, [
2195
- sql.engine === "sqlite" ? Date.now() : /* @__PURE__ */ new Date(),
2196
- person.member,
2197
- standing.generation,
2198
- row.id
2199
- ]);
2200
- try {
2201
- const response = await pollDevice(deployment, device.device_code);
2202
- const body = await response.text();
2203
- expect(!response.ok && !body.includes("access_token"), "an owner-forged approval minted a session", body);
2204
- await reported(deployment, String(row.id));
2205
- } finally {
2206
- await query(sql, "DELETE FROM device_authorizations WHERE id = $1", [row.id]);
2207
- }
2208
- });
2209
- const control = await startDevice(deployment);
2210
- await person.api.deviceLogins.decide(control.user_code, true);
2211
- expect((await pollDevice(deployment, control.device_code)).ok, "a genuine approval no longer works");
2212
- return "an owner-edited device approval grants nothing and is reported; a genuine approval works";
2213
- }
2214
- /** Changing an old credential's generation cannot bring its chosen token back. */
2215
- async function editedGeneration(deployment, people) {
2216
- const principal = "token:generation-mac";
2217
- const { admin } = people;
2218
- await admin.api.members.add(principal);
2219
- await admin.api.access.set(principal, { "conformance/dev": "viewer" });
2220
- const issued = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
2221
- await using(deployment.database(), async (sql) => {
2222
- const [old] = await query(sql, "SELECT * FROM credentials WHERE token_hash = $1", [hash(issued.token)]);
2223
- expect(old !== void 0, "no old token row to save");
2224
- await admin.api.members.remove(principal);
2225
- await admin.api.members.add(principal);
2226
- await admin.api.access.set(principal, { "conformance/dev": "viewer" });
2227
- const [current] = await query(sql, "SELECT * FROM credentials WHERE id = $1", [old.id]);
2228
- const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [principal]);
2229
- await restoreRow(sql, "credentials", {
2230
- ...old,
2231
- generation: standing.generation
2232
- }, "id");
2233
- try {
2234
- const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${issued.token}` } });
2235
- expect(response.status === 401, "editing the credential generation revived the token", await response.text());
2236
- await reported(deployment, String(old.id));
2237
- } finally {
2238
- await restoreRow(sql, "credentials", current, "id");
2239
- }
2240
- });
2241
- const fresh = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
2242
- expect((await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${fresh.token}` } })).ok, "a newly issued token does not work");
2243
- return "an old token with its generation edited to the current membership is refused and reported";
2244
- }
2245
- //#endregion
2246
- //#region src/checks/audit.ts
2247
- /**
2248
- * Every returned value read, committed by the vault under the response's
2249
- * operation: one entry for the place, which lists each secret of a read of
2250
- * several, and which a look for any one of them finds. With 0.15's log, each
2251
- * value read is its use (`usesAudited`).
2252
- */
2253
- async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
2254
- const path = purpose === "reveal" ? `${DEV}/API_KEY` : DEV;
2255
- const asked = Date.now();
2256
- const { operationId, values } = await reader.api.secrets.reveal(path);
2257
- expect(typeof operationId === "string" && operationId.length > 0, "the read returned no operation id");
2258
- const expected = purpose === "reveal" ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
2259
- expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), "the read returned different values", values);
2260
- if ((await admin.api.audit.verifyTrees()).kept) return usesAudited(deployment, {
2261
- admin,
2262
- reader
2263
- }, Object.keys(expected), purpose, asked);
2264
- const bundle = (await everyAuditEntry(admin.api)).filter((entry) => entry.operationId === operationId);
2265
- expect(bundle.length === 1, "the read of one place is not logged as one entry", bundle);
2266
- const [entry] = bundle;
2267
- const read = entry.secrets ?? [{
2268
- key: entry.key,
2269
- version: entry.version
2270
- }];
2271
- expect(JSON.stringify(read.map((secret) => secret.key).sort()) === JSON.stringify(Object.keys(expected).sort()), "the read does not list each value once", entry);
2272
- expect(entry.author === "vault" && entry.action === "secret.read" && entry.decision === "allow", "the read's entry is not the vault's allowed read", entry);
2273
- expect(entry.metadata.purpose === purpose && entry.requestId !== null, "the read does not name its request and its purpose", entry);
2274
- const { keys: listed } = await admin.api.secrets.list(DEV);
2275
- for (const secret of read) {
2276
- expect(secret.version === listed.find((key) => key.key === secret.key)?.version, `the entry for ${String(secret.key)} names another version`, entry);
2277
- const own = await admin.api.audit.list({
2278
- path: `${DEV}/${String(secret.key)}`,
2279
- limit: 200
2280
- });
2281
- expect(own.entries.some((found) => found.seq === entry.seq), `the read is not in ${String(secret.key)}'s log`, own.entries.slice(0, 5));
2282
- }
2283
- await using(deployment.database(), async (sql) => {
2284
- const rows = await query(sql, "SELECT author, action, decision, request_id, secret_id, metadata FROM audit_log WHERE operation_id = $1", [operationId]);
2285
- expect(rows.length === 1, "the committed read is not one entry", rows);
2286
- const [row] = rows;
2287
- expect(row.author === "vault" && row.action === "secret.read" && row.decision === "allow" && row.request_id !== null, "the committed read is not the vault release", row);
2288
- const metadata = JSON.parse(row.metadata);
2289
- const committed = metadata.secrets ?? [{
2290
- secretId: row.secret_id,
2291
- version: metadata.version
2292
- }];
2293
- expect(metadata.purpose === purpose && committed.length === Object.keys(expected).length, "the committed read does not name its purpose and each secret", row);
2294
- for (const secret of committed) {
2295
- const [current] = await query(sql, "SELECT current_version FROM secrets WHERE id = $1", [secret.secretId]);
2296
- expect(current?.current_version === secret.version, "the committed read names another version", row);
2297
- }
2298
- });
2299
- return `${read.length} values read by the vault for ${purpose}, in one entry under ${operationId}`;
2300
- }
2301
- /**
2302
- * How long before a read 0.15's log may hold the use it rode on: a repeat
2303
- * within a minute rides on the first one's entry (a cover, R53), and the
2304
- * deployment's clock may stand a little off the harness's.
2305
- */
2306
- const COVERED_MS = 9e4;
2307
- /** How long a read may wait to be listed: the checkpointer's tick seals it, then the projector's lists it, each a tick or so. */
2308
- const LISTED_WITHIN_MS = 2e4;
2309
- /**
2310
- * Whether `probe` holds within `LISTED_WITHIN_MS`, asked every quarter of a
2311
- * second: what a check of 0.15's reads waits for, as a read is listed once
2312
- * the log has sealed it and the vault's projector has gone that far (T166).
2313
- */
2314
- async function listed(probe) {
2315
- for (const deadline = Date.now() + LISTED_WITHIN_MS;;) {
2316
- if (await probe()) return true;
2317
- if (Date.now() >= deadline) return false;
2318
- await new Promise((resolve) => setTimeout(resolve, 250));
2319
- }
2320
- }
2321
- /**
2322
- * Every value read, with 0.15's log, where a read is a use of each key,
2323
- * which names no operation (R53, T166): the reader's newest read of each,
2324
- * for the purpose, made by this read or by one within the minute before it
2325
- * that this rode on, listed within `LISTED_WITHIN_MS`, the vault's and
2326
- * vouched for, of the version the key holds, in the key's own log, and the
2327
- * use the vault wrote, as its projector listed it.
2328
- */
2329
- async function usesAudited(deployment, { admin, reader }, keys, purpose, asked) {
2330
- const { keys: held } = await admin.api.secrets.list(DEV);
2331
- const readOf = (entries, key) => entries.find((found) => found.log === "0.15" && found.action === "secret.read" && found.actorId === reader.email && found.key === key && `${found.project}/${found.environment}` === DEV && found.metadata.via === void 0 && found.metadata.purpose === purpose && Date.parse(found.occurredAt) >= asked - COVERED_MS);
2332
- let entries = await everyAuditEntry(admin.api);
2333
- await listed(async () => keys.every((key) => readOf(entries, key) !== void 0) || (entries = await everyAuditEntry(admin.api), false));
2334
- const read = keys.map((key) => {
2335
- const entry = readOf(entries, key);
2336
- expect(entry !== void 0, `the read of ${key} is not listed in 0.15's log within ${LISTED_WITHIN_MS / 1e3} s`, entries.slice(0, 5));
2337
- expect(entry.author === "vault" && entry.decision === "allow" && entry.forged !== true, `the read of ${key} is not the vault's allowed read`, entry);
2338
- expect(entry.version === held.find((found) => found.key === key)?.version, `the entry for ${key} names another version`, entry);
2339
- return entry;
2340
- });
2341
- const theirs = await everyAuditEntry(admin.api, { actor: reader.member });
2342
- for (const entry of read) {
2343
- expect(theirs.some((found) => found.log === "0.15" && found.seq === entry.seq), `the read of ${String(entry.key)} is not on ${reader.email}'s page`, {
2344
- read: entry,
2345
- newest: theirs.slice(0, 5)
2346
- });
2347
- const page = await admin.api.audit.list({
2348
- path: `${DEV}/${String(entry.key)}`,
2349
- sealed: "1",
2350
- limit: 1
2351
- });
2352
- expect(page.readsOmitted === "path", `${String(entry.key)}'s page does not say where its reads are listed`, page.readsOmitted);
2353
- }
2354
- await using(deployment.database(), async (sql) => {
2355
- for (const entry of read) {
2356
- const rows = await query(sql, "SELECT secret FROM log_uses WHERE number = $1", [entry.seq]);
2357
- expect(rows.length === 1 && String(rows[0].secret) === entry.metadata.secretId, "the read is not the use the vault wrote", {
2358
- entry,
2359
- rows
2360
- });
2361
- const projected = await query(sql, "SELECT secret, principal FROM log_reads WHERE number = $1", [entry.seq]);
2362
- expect(projected.length === 1 && String(projected[0].secret) === entry.metadata.secretId && projected[0].principal === `user:${reader.email}`, "the read is not the use as the vault's projector listed it", {
2363
- entry,
2364
- projected
2365
- });
2366
- }
2367
- });
2368
- return `${read.length} values read by the vault for ${purpose}, each its use in 0.15's log`;
2369
- }
2370
- /**
2371
- * Verification must reach the stored head, not just some intact prefix:
2372
- * the page's, from the newest checkpoint, and the walk from the first
2373
- * entry, a stretch a request, which ends where it does. And the audit page
2374
- * is sent with its verdict, rendered by the server, never a badge to come.
2375
- */
2376
- async function verification(deployment, { admin }) {
2377
- const anchored = await admin.api.audit.verify();
2378
- expect(anchored.ok && anchored.remaining === 0, "the log does not verify from its newest checkpoint", anchored);
2379
- const result = await admin.api.audit.verifyWhole();
2380
- expect(result.ok && result.remaining === 0, "the full log does not verify to its end", result);
2381
- expect(result.through !== null && anchored.through !== null && result.through >= anchored.through, "the walk from the first entry stopped short", {
2382
- anchored,
2383
- result
2384
- });
2385
- await using(deployment.database(), async (sql) => {
2386
- const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
2387
- const [last] = await sql.query("SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1");
2388
- expect(head !== void 0 && last !== void 0, "the head or newest entry is missing");
2389
- expect(BigInt(head.next_seq) === BigInt(last.seq) + 1n, "the head does not name the newest entry");
2390
- expect(Buffer.from(head.head_hash).equals(Buffer.from(last.hash)), "the head has another hash");
2391
- expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), "verification stopped before the head", {
2392
- result,
2393
- head: head.next_seq
2394
- });
2395
- expect(BigInt(result.entries) === BigInt(head.next_seq), "verification did not count every entry", result);
2396
- });
2397
- const trees = await admin.api.audit.verifyTrees();
2398
- expect(!trees.kept || trees.problems.length === 0, "0.15's log does not verify from its newest checkpoint", trees);
2399
- expect(!trees.kept || !trees.capped, "0.15's log is not checked to its head: more entries wait above its newest checkpoint than a check reads, the checkpointer behind", trees);
2400
- const reads = await admin.api.audit.verifyReads();
2401
- expect(!reads.kept || reads.ok, "the reads listed are not the uses 0.15's log sealed", reads);
2402
- const page = await admin.browser.fetch("/audit");
2403
- const html = await page.text();
2404
- expect(page.status === 200 && html.includes("chain-seal") && !html.includes("Verifying"), "the audit page was not sent with its verification result", { status: page.status });
2405
- const sealed = !trees.kept ? "" : trees.checkpoint === null ? "; 0.15's log, nothing sealed yet" : `; 0.15's log from its checkpoint up to ${trees.checkpoint.upto}`;
2406
- return `${result.entries} entries verified through the head at ${result.through}${sealed}, and the audit page sent with its result`;
2407
- }
2408
- /**
2409
- * Each heartbeat has the vault sign the log up to its last entry, in an
2410
- * entry of its own, after checking that the prefix it signed last is still
2411
- * there. Two, so the second covers the first; then the log is verified from
2412
- * its first entry, every checkpoint with it.
2413
- */
2414
- async function checkpoints(deployment, { admin }) {
2415
- const before = await admin.api.audit.verify();
2416
- expect(before.ok, "the log does not verify", before);
2417
- await deployment.scheduled();
2418
- await deployment.scheduled();
2419
- const after = await admin.api.audit.verify();
2420
- expect(after.ok && after.checkpoint !== null, "the audit log is not verified and checkpointed", after);
2421
- expect(after.checkpoint.seq > (before.through ?? -1), "the checkpoint does not cover what was written before it", {
2422
- before,
2423
- after
2424
- });
2425
- const { entries } = await admin.api.audit.list({
2426
- detail: "1",
2427
- limit: 20
2428
- });
2429
- const signed = entries.filter((entry) => entry.action === "audit.checkpoint" && entry.decision === "allow");
2430
- expect(signed.length >= 2 && signed.every((entry) => entry.author === "vault" && entry.detail), "the checkpoints are not the vault's entries, hidden as detail", signed);
2431
- const [newest, previous] = signed;
2432
- expect(newest.metadata.seq === after.checkpoint.seq, "the newest checkpoint entry is not the one verified", {
2433
- signed,
2434
- after
2435
- });
2436
- expect(previous.seq <= after.checkpoint.seq, "the newest checkpoint does not cover the one before it", signed);
2437
- return `${after.entries} entries verified through ${after.through}, checkpointed at ${after.checkpoint.seq}`;
2438
- }
2439
- /**
2440
- * Every version the app says it stored names the vault's entry for the key
2441
- * it was sealed with, `key.wrap` for a write and `key.rewrap` for a restore,
2442
- * a mark and a loosening:
2443
- * the same member, request and operation, the same secret and version. And
2444
- * every value read is the vault's to log: the app keeps no reads of its own.
2445
- */
2446
- async function writesAgree(deployment) {
2447
- return using(deployment.database(), async (sql) => {
2448
- const entries = await sql.query("SELECT * FROM audit_log ORDER BY seq");
2449
- const bySeq = new Map(entries.map((entry) => [String(entry.seq), entry]));
2450
- const writes = entries.filter((entry) => entry.author === "app" && entry.decision === "allow" && (entry.action === "secret.write" || entry.action === "secret.restore" || entry.action === "secret.loosen"));
2451
- 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);
2452
- for (const entry of writes) {
2453
- const seal = entry.action === "secret.write" && JSON.parse(entry.metadata).kept === void 0 ? "key.wrap" : "key.rewrap";
2454
- const key = entry.related_seq === null ? void 0 : bySeq.get(String(entry.related_seq));
2455
- expect(key !== void 0 && key.author === "vault" && key.action === seal && key.decision === "allow", `a ${entry.action} names no ${seal} of the vault's`, {
2456
- entry,
2457
- key
2458
- });
2459
- for (const field of [
2460
- "actor",
2461
- "request_id",
2462
- "operation_id",
2463
- "project_id",
2464
- "environment_id"
2465
- ]) expect(entry[field] === key[field], `the write and its seal have different ${field}`, {
2466
- entry,
2467
- key
2468
- });
2469
- expect(entry.operation_id !== null && entry.request_id !== null, "the committed write has no operation or request", entry);
2470
- const metadata = JSON.parse(entry.metadata);
2471
- const sealed = JSON.parse(key.metadata);
2472
- 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", {
2473
- entry,
2474
- key
2475
- });
2476
- }
2477
- const versions = await query(sql, `SELECT v.secret_id, v.version FROM secret_versions v JOIN secrets s ON s.id = v.secret_id
2478
- JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT$1]);
2479
- expect(versions.length > 0, "no stored versions were inspected");
2480
- for (const version of versions) {
2481
- const matching = writes.filter((entry) => entry.secret_id === version.secret_id && JSON.parse(entry.metadata).version === version.version);
2482
- expect(matching.length === 1, "a stored version has no unique app write entry", {
2483
- version,
2484
- matching
2485
- });
2486
- }
2487
- return `${versions.length} stored versions, each with one app write naming the vault's seal in the same operation`;
2488
- });
2489
- }
2490
- /**
2491
- * No value leaves without its entry: with 0.15's log refusing uses (a
2492
- * trigger, as a full disk or a lost connection would), the vault's use,
2493
- * which writes its row in the statement that decides it, fails, so a
2494
- * reveal fails and carries nothing. Of a key no one has read yet: a key's
2495
- * use within a minute of the last is that one's, its isolate's cover, and
2496
- * writes nothing again.
2497
- */
2498
- async function noAuditNoValue(deployment, { admin }, canaries) {
2499
- let status = 0;
2500
- const key = `UNLOGGED_${randomBytes(4).toString("hex").toUpperCase()}`;
2501
- const value = `coffre-canary-${randomBytes(10).toString("hex")}`;
2502
- await admin.api.secrets.set(DEV, { [key]: value });
2503
- await using(deployment.database(), (sql) => logRefuses(sql, async () => {
2504
- const before = deployment.output().split(LOG_REFUSAL).length;
2505
- const response = await admin.browser.send("POST", "/api/reveals", { path: `${DEV}/${key}` });
2506
- const text = await response.text();
2507
- status = response.status;
2508
- const body = JSON.parse(text);
2509
- expect(status === 500 && body.error === "internal_error" && body.reason === void 0, "the unlogged reveal did not fail as internal_error", {
2510
- status,
2511
- body
2512
- });
2513
- expect(body.message === "something went wrong; see the server log", "the audit failure exposed another reason", body);
2514
- expect(![value, ...Object.values(canaries)].some((canary) => text.includes(canary)), "a reveal that could not be logged carried a value", text);
2515
- await until("the injected audit refusal in the processes' output", async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
2516
- }, void 0, "log_uses"));
2517
- expect((await admin.api.secrets.reveal(`${DEV}/${key}`)).values[key] === value, "reveals did not come back once the log took writes again");
2518
- 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");
2519
- return `a reveal the log would not take: ${status} internal_error, its injected cause in the log, and no value`;
2520
- }
2521
- /** A publicly chained entry still needs the vault's MAC. */
2522
- async function forgedVaultEntry(deployment, { admin }) {
2523
- const verify = () => admin.api.audit.verify();
2524
- expect((await verify()).ok, "the log does not verify before the forged entry");
2136
+ expect(response.ok, "the device fixture did not start", await response.clone().text());
2137
+ return response.json();
2138
+ }
2139
+ function pollDevice(deployment, code) {
2140
+ return fetch(`${deployment.origin}/api/auth/device/token`, {
2141
+ method: "POST",
2142
+ headers: { "content-type": "application/json" },
2143
+ body: JSON.stringify({ device_code: code })
2144
+ });
2145
+ }
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
+ */
2153
+ async function forgedApproval(deployment, people) {
2154
+ const person = await member(deployment, people, "forged-approval");
2155
+ const device = await startDevice(deployment);
2525
2156
  await using(deployment.database(), async (sql) => {
2526
- const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
2527
- const [writer] = await sql.query("SELECT key_id FROM audit_log WHERE author = 'vault' ORDER BY seq DESC LIMIT 1");
2528
- expect(writer !== void 0, "no vault key id to copy");
2529
- const seq = BigInt(head.next_seq);
2530
- const forged = {
2531
- seq,
2532
- author: "vault",
2533
- key_id: writer.key_id,
2534
- occurred_at: Date.now(),
2535
- actor: "user:forger@conformance.example",
2536
- action: "secret.read",
2537
- decision: "allow",
2538
- metadata: "{}"
2539
- };
2540
- const prevHash = Buffer.from(head.head_hash);
2541
- const mac = Buffer.alloc(32, 65);
2542
- const hash = createHash("sha256").update("coffre.audit.chain.v2").update(prevHash).update(entryFields(forged)).update(mac).digest();
2543
- await insertRow(sql, "audit_log", {
2544
- ...forged,
2545
- prev_hash: prevHash,
2546
- mac,
2547
- hash
2548
- });
2549
- await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq + 1n, hash]);
2550
- let inserted;
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]);
2159
+ expect(row !== void 0, "no device row to forge");
2160
+ await query(sql, `UPDATE device_logins SET decision = 'approved', decided_at = $1, principal = $2, generation = $3 WHERE id = $4`, [
2161
+ /* @__PURE__ */ new Date(),
2162
+ person.member,
2163
+ standing.generation,
2164
+ row.id
2165
+ ]);
2551
2166
  try {
2552
- inserted = await verify();
2167
+ const response = await pollDevice(deployment, device.device_code);
2168
+ const body = await response.text();
2169
+ expect(!response.ok && !body.includes("access_token"), "an owner-forged approval minted a session", body);
2553
2170
  } finally {
2554
- await appendOnlyLifted(sql, () => query(sql, "DELETE FROM audit_log WHERE seq = $1", [seq]));
2555
- await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq, prevHash]);
2171
+ await query(sql, "DELETE FROM device_logins WHERE id = $1", [row.id]);
2556
2172
  }
2557
- 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);
2558
- const removed = await verify();
2559
- expect(removed.ok, "the log did not verify once the entry was gone", removed);
2560
2173
  });
2561
- return "a publicly chained vault entry with a made-up MAC was refused at its sequence";
2174
+ const control = await startDevice(deployment);
2175
+ await person.api.deviceLogins.decide(control.user_code, true);
2176
+ expect((await pollDevice(deployment, control.device_code)).ok, "a genuine approval no longer works");
2177
+ return "an owner-edited device approval grants nothing; a genuine approval works";
2562
2178
  }
2563
2179
  /**
2564
- * Rewrite either author's first entry in place and require a fault at that
2565
- * exact sequence: from the walk from the first entry, since the page's
2566
- * check goes on from the newest checkpoint and reads nothing before it.
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).
2567
2183
  */
2568
- async function rewrittenEntry(deployment, { admin }, author) {
2569
- expect((await admin.api.audit.verifyWhole()).ok, "the log does not verify before the rewrite");
2570
- let seq = 0;
2571
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2572
- const [row] = await query(sql, "SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1", [author]);
2573
- expect(row !== void 0, `no ${author} entry to rewrite`);
2574
- seq = Number(row.seq);
2575
- await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", ["user:nobody@conformance.example", row.seq]);
2576
- try {
2577
- const result = await admin.api.audit.verifyWhole();
2578
- expect(!result.ok && result.failedAtSeq === Number(row.seq), "a rewritten entry was not caught at its sequence", result);
2579
- } finally {
2580
- await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", [row.actor, row.seq]);
2581
- }
2582
- }));
2583
- expect((await admin.api.audit.verifyWhole()).ok, "the restored entry did not verify");
2584
- return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
2585
- }
2586
- /** Remove an unreferenced entry, then put its exact bytes back. */
2587
- async function missingEntry(deployment, { admin }, position) {
2588
- expect((await admin.api.audit.verify()).ok, "the log does not verify before deletion");
2589
- let seq = 0;
2590
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2591
- 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`);
2592
- expect(row !== void 0, "no unreferenced entry to delete");
2593
- seq = Number(row.seq);
2594
- const linked = await query(sql, "SELECT * FROM audit_log WHERE related_seq = $1", [row.seq]);
2595
- await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE related_seq = $1", [row.seq]);
2596
- await query(sql, "DELETE FROM audit_log WHERE seq = $1", [row.seq]);
2184
+ async function editedGeneration(deployment, people) {
2185
+ const principal = "token:generation-mac";
2186
+ const { admin } = people;
2187
+ await admin.api.members.add(principal);
2188
+ await admin.api.access.set(principal, { "conformance/dev": "viewer" });
2189
+ const issued = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
2190
+ await using(deployment.database(), async (sql) => {
2191
+ const [old] = await query(sql, "SELECT * FROM credentials WHERE token_hash = $1", [hash(issued.token)]);
2192
+ expect(old !== void 0, "no old token row to save");
2193
+ await admin.api.members.remove(principal);
2194
+ await admin.api.members.add(principal);
2195
+ await admin.api.access.set(principal, { "conformance/dev": "viewer" });
2196
+ const [current] = await query(sql, "SELECT * FROM credentials WHERE id = $1", [old.id]);
2197
+ const [standing] = await query(sql, "SELECT generation FROM principals WHERE id = $1", [principal]);
2198
+ await restoreRow(sql, "credentials", {
2199
+ ...old,
2200
+ generation: standing.generation
2201
+ }, "id");
2597
2202
  try {
2598
- const result = await admin.api.audit.verify();
2599
- expect(!result.ok, "a log with an entry missing verifies", result);
2600
- if (position !== "middle") expect(result.reason.includes(`sequence gap: expected seq ${position === "first" ? 0 : 1e3}`), "the sequence gap was not reported", result);
2203
+ const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${issued.token}` } });
2204
+ expect(response.status === 401, "editing the credential generation revived the token", await response.text());
2601
2205
  } finally {
2602
- await insertRow(sql, "audit_log", row);
2603
- for (const entry of linked) await query(sql, "UPDATE audit_log SET related_seq = $1 WHERE seq = $2", [entry.related_seq, entry.seq]);
2206
+ await restoreRow(sql, "credentials", current, "id");
2604
2207
  }
2605
- }));
2606
- expect((await admin.api.audit.verify()).ok, "the restored log did not verify");
2607
- const which = {
2608
- first: "the first",
2609
- batch: "the first past a page of 1,000",
2610
- middle: "a heartbeat"
2611
- }[position];
2612
- return `entry ${seq}, ${which}, deleted: caught, then put back`;
2613
- }
2614
- /** Last: keep the head, delete its newest entries, and require verification to notice. */
2615
- async function deletedTail(deployment, { admin }) {
2616
- const intact = await admin.api.audit.verify();
2617
- expect(intact.ok && intact.checkpoint !== null, "the log is not intact and checkpointed");
2618
- const signed = intact.checkpoint.seq;
2619
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2620
- await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE seq >= $1", [signed]);
2621
- await query(sql, "DELETE FROM audit_log WHERE seq >= $1", [signed]);
2622
- }));
2623
- const result = await admin.api.audit.verify();
2624
- expect(!result.ok && result.author === "app" && result.reason.includes("removed from the end"), "the log verifies with its newest entries deleted", result);
2625
- return "the newest entries deleted, with the head left alone, were caught";
2626
- }
2627
- /** The whole log, detail included, newest first: 0.14's entries, and the sign-ins 0.15's records. */
2628
- async function everyAuditEntry(api, query = {}) {
2629
- const all = [];
2630
- for (let before;;) {
2631
- const { entries, next } = await api.audit.list({
2632
- before,
2633
- limit: 500,
2634
- detail: "1",
2635
- sealed: "1",
2636
- ...query
2637
- });
2638
- all.push(...entries);
2639
- if (next === null) return all;
2640
- before = next;
2641
- }
2208
+ });
2209
+ const fresh = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
2210
+ expect((await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${fresh.token}` } })).ok, "a newly issued token does not work");
2211
+ return "an old token with its generation edited to the current membership is refused";
2642
2212
  }
2643
2213
  //#endregion
2644
2214
  //#region src/checks/access.ts
@@ -2757,24 +2327,24 @@ async function offboarding(deployment, { admin, leaver, service }, canaries) {
2757
2327
  const device = await startDevice(deployment);
2758
2328
  await leaver.api.deviceLogins.decide(device.user_code, true);
2759
2329
  await leaver.cli.secrets.reveal(DEV);
2760
- const expected = Object.keys(valuesIn(canaries, DEV)).map((key) => `${DEV}/${key}`).sort();
2761
- const seen = (report) => report.exposed.map((entry) => `${entry.project}/${entry.environment}/${entry.key}`).sort();
2762
- await listed(async () => same(seen(await admin.api.members.get(leaver.member)), expected));
2763
2330
  const app = await connectedApp(deployment, leaver);
2764
2331
  const before = await admin.api.members.get(leaver.member);
2765
- 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);
2766
2333
  const { revoked, report } = await admin.api.members.remove(leaver.member);
2767
2334
  expect(report !== null, "an owner's removal does not answer with the leaver's report");
2768
- 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", {
2769
2336
  revoked,
2770
2337
  apps: report.apps
2771
2338
  });
2772
2339
  expect(!await discovers(deployment, app), "a removed member's app still reaches /mcp");
2773
- expect(same(seen(report), expected), "the offboarding report does not name what they read", report.exposed);
2774
- expect((await leaver.browser.fetch("/api/me")).status === 401, "a removed member's browser session still works");
2340
+ const exposed = report.exposed.map((entry) => `${entry.project}/${entry.environment}/${entry.key}`).sort();
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);
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);
2775
2344
  await refused$1("a removed member's CLI session read secrets", leaver.cli.secrets.list(DEV));
2776
2345
  expect(!(await signIn(deployment, leaver.browser, leaver.email)).ok, "a removed member signed in again");
2777
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);
2778
2348
  await refused$1("a removed service's token read secrets", service.api.secrets.reveal(DEV));
2779
2349
  await admin.api.members.add(leaver.member);
2780
2350
  await admin.api.access.set(leaver.member, { [DEV]: "developer" });
@@ -2787,7 +2357,7 @@ async function offboarding(deployment, { admin, leaver, service }, canaries) {
2787
2357
  expect((await signIn(deployment, leaver.browser, leaver.email)).ok, "a fresh provider callback could not sign the re-admitted member in");
2788
2358
  await admin.api.members.remove(leaver.member);
2789
2359
  await admin.api.members.remove(service.member);
2790
- return `the report names ${report.exposed.length} values and their connected app, which removal disconnects; re-admission revives no old session, token or approval; a fresh provider callback works`;
2360
+ return `the report names ${exposed.length} values and their connected app, which removal disconnects; re-admission revives no old session, token or approval; a fresh provider callback works`;
2791
2361
  }
2792
2362
  /**
2793
2363
  * Instance roles: a Developer scoped to dev reads and writes the dev of a
@@ -2867,67 +2437,286 @@ function sorted(value) {
2867
2437
  return Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : 1));
2868
2438
  }
2869
2439
  //#endregion
2870
- //#region src/checks/checkpoints.ts
2871
- /** Leave one old signature invalid, reseal the chain, and make later checkpoints valid. */
2872
- async function earlierCheckpoint(deployment, { admin }) {
2873
- expect((await admin.api.audit.verifyWhole()).ok, "the log does not verify before checkpoint tampering");
2874
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2875
- const rows = await sql.query("SELECT * FROM audit_log ORDER BY seq");
2876
- const signed = rows.filter((row) => row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow");
2877
- expect(signed.length >= 2, "two checkpoints are needed");
2878
- const [head] = await sql.query("SELECT * FROM audit_chain_head");
2879
- const seed = Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.VAULT_KEY, "base64"), Buffer.alloc(0), "coffre.vault.signing-key.v1", 32));
2880
- const signingKey = createPrivateKey({
2881
- key: Buffer.concat([Buffer.from("302e020100300506032b657004220420", "hex"), seed]),
2882
- format: "der",
2883
- type: "pkcs8"
2440
+ //#region src/checks/audit.ts
2441
+ /**
2442
+ * Every returned value read, committed by the vault under the response's
2443
+ * operation: one entry for the place, which lists each secret of a read of
2444
+ * several, and which a look for any one of them finds.
2445
+ */
2446
+ async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
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);
2451
+ const { operationId, values } = await reader.api.secrets.reveal(path);
2452
+ expect(typeof operationId === "string" && operationId.length > 0, "the read returned no operation id");
2453
+ const expected = purpose === "reveal" ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
2454
+ expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), "the read returned different values", values);
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);
2460
+ expect(bundle.length === 1, "the read of one place is not logged as one entry", bundle);
2461
+ const [entry] = bundle;
2462
+ const read = entry.secrets ?? [{
2463
+ key: entry.key,
2464
+ version: entry.version
2465
+ }];
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);
2467
+ expect(entry.author === "vault" && entry.action === "secret.read" && entry.decision === "allow", "the read's entry is not the vault's allowed read", entry);
2468
+ expect(entry.metadata.purpose === purpose, "the read does not name its purpose", entry);
2469
+ const { keys: listed } = await admin.api.secrets.list(DEV);
2470
+ for (const secret of read) {
2471
+ expect(secret.version === listed.find((key) => key.key === secret.key)?.version, `the entry for ${String(secret.key)} names another version`, entry);
2472
+ const own = await admin.api.audit.list({
2473
+ path: `${DEV}/${String(secret.key)}`,
2474
+ limit: 200
2884
2475
  });
2885
- const keys = {
2886
- app: Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.APP_KEY, "base64"), Buffer.alloc(0), "coffre.audit.app.v2", 32)),
2887
- vault: Buffer.from(hkdfSync("sha256", seed, Buffer.alloc(0), "coffre.audit.vault.v2", 32))
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));
2477
+ }
2478
+ await using(deployment.database(), async (sql) => {
2479
+ const rows = (await logBodies(sql)).filter((body) => body.request === operationId);
2480
+ expect(rows.length === 1, "the committed read is not one entry", rows);
2481
+ const [row] = rows;
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);
2485
+ for (const secret of committed) {
2486
+ const [current] = await query(sql, "SELECT current_version FROM secrets WHERE id = $1", [secret.secret]);
2487
+ expect(current?.current_version === secret.version, "the committed read names another version", row);
2488
+ }
2489
+ });
2490
+ return `${read.length} values read by the vault for ${purpose}, in one entry under ${operationId}`;
2491
+ }
2492
+ /**
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.
2500
+ */
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;
2511
+ });
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)
2522
+ });
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`;
2525
+ }
2526
+ /**
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.
2532
+ */
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)
2549
+ });
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);
2888
2603
  };
2889
- const hashes = /* @__PURE__ */ new Map();
2890
- let previous = Buffer.alloc(32);
2891
2604
  try {
2892
- await transaction(sql, async () => {
2893
- for (const original of rows) {
2894
- const row = { ...original };
2895
- if (row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow") {
2896
- const checkpoint = JSON.parse(String(row.metadata));
2897
- if (row.seq === signed[0].seq) checkpoint.signature = Buffer.alloc(64).toString("base64");
2898
- else {
2899
- checkpoint.hash = hashes.get(BigInt(checkpoint.seq));
2900
- checkpoint.signature = sign(null, Buffer.from(`coffre.checkpoint.v3|${checkpoint.seq}|${checkpoint.hash}|${checkpoint.signedAt}`), signingKey).toString("base64");
2901
- }
2902
- row.metadata = JSON.stringify(checkpoint);
2903
- }
2904
- const fields = entryFields(row);
2905
- row.prev_hash = previous;
2906
- row.mac = createHmac("sha256", keys[row.author]).update("coffre.audit.mac.v2").update(previous).update(fields).digest();
2907
- previous = createHash("sha256").update("coffre.audit.chain.v2").update(previous).update(fields).update(row.mac).digest();
2908
- row.hash = previous;
2909
- hashes.set(BigInt(String(row.seq)), previous.toString("hex"));
2910
- await restoreRow(sql, "audit_log", row, "seq");
2911
- }
2912
- await restoreRow(sql, "audit_chain_head", {
2913
- ...head,
2914
- head_hash: previous
2915
- }, "only_row");
2916
- });
2917
- const result = await admin.api.audit.verifyWhole();
2918
- 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);
2605
+ await putBack(await cut(true));
2606
+ await putBack(await cut(false));
2919
2607
  } finally {
2920
- await transaction(sql, async () => {
2921
- for (const row of rows) await restoreRow(sql, "audit_log", row, "seq");
2922
- await restoreRow(sql, "audit_chain_head", head, "only_row");
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;
2622
+ });
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
2630
+ });
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";
2632
+ }
2633
+ /**
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").
2641
+ */
2642
+ const WRITES = /* @__PURE__ */ new Set([
2643
+ "secret.write",
2644
+ "secret.loosen",
2645
+ "environment.create"
2646
+ ]);
2647
+ async function writesAgree(deployment) {
2648
+ return using(deployment.database(), async (sql) => {
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
+ }
2659
+ }
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
2661
+ JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT$1]);
2662
+ expect(versions.length > 0, "no stored versions were inspected");
2663
+ for (const version of versions) {
2664
+ const entries = written.get(`${version.secret_id} ${version.version}`) ?? 0;
2665
+ expect(entries === 1, "a stored version has no unique write entry", {
2666
+ version,
2667
+ entries
2923
2668
  });
2924
- keys.app.fill(0);
2925
- keys.vault.fill(0);
2926
- seed.fill(0);
2927
2669
  }
2928
- }));
2929
- expect((await admin.api.audit.verifyWhole()).ok, "restoring the signed prefixes did not restore verification");
2930
- return "an invalid earlier checkpoint was caught despite valid MACs and a valid later checkpoint";
2670
+ return `${versions.length} stored versions, each written by one entry of the vault's, naming its actor and request`;
2671
+ });
2672
+ }
2673
+ /**
2674
+ * No value leaves without its entry: with the audit log refusing writes (a
2675
+ * trigger, as a full disk or a lost connection would), the vault cannot log
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.
2680
+ */
2681
+ const UNLOGGED = "NO_AUDIT_NO_VALUE";
2682
+ async function noAuditNoValue(deployment, { admin }, canaries) {
2683
+ let status = 0;
2684
+ const value = canary();
2685
+ canaries[`unlogged ${DEV}/${UNLOGGED}`] = value;
2686
+ await admin.api.secrets.set(DEV, { [UNLOGGED]: value });
2687
+ await using(deployment.database(), (sql) => logRefuses(sql, async () => {
2688
+ const before = deployment.output().split(LOG_REFUSAL).length;
2689
+ const response = await admin.browser.send("POST", "/api/reveals", { path: DEV });
2690
+ const text = await response.text();
2691
+ status = response.status;
2692
+ const body = JSON.parse(text);
2693
+ expect(status === 500 && body.error === "internal_error" && body.reason === void 0, "the unlogged reveal did not fail as internal_error", {
2694
+ status,
2695
+ body
2696
+ });
2697
+ expect(body.message === "something went wrong; see the server log", "the audit failure exposed another reason", body);
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);
2700
+ await until("the injected audit refusal in the processes' output", async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
2701
+ }, "secret.read"));
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 });
2705
+ return `a reveal the audit log would not take: ${status} internal_error, its injected cause in the log, and no value`;
2706
+ }
2707
+ /** The whole log, detail included, newest first. */
2708
+ async function everyAuditEntry(api) {
2709
+ const all = [];
2710
+ for (let before;;) {
2711
+ const { entries, more } = await api.audit.list({
2712
+ before,
2713
+ limit: 500,
2714
+ detail: "1"
2715
+ });
2716
+ all.push(...entries);
2717
+ if (!more || entries.length === 0) return all;
2718
+ before = entries.at(-1).seq;
2719
+ }
2931
2720
  }
2932
2721
  //#endregion
2933
2722
  //#region src/checks/deletion.ts
@@ -2999,18 +2788,24 @@ async function deletion(deployment, cli, admin) {
2999
2788
  expect((await admin.api.projects.list()).projects.every((project) => !project.slug.startsWith(PROJECT)), "a deleted project is still listed");
3000
2789
  expect((await admin.api.members.get(SERVICE$1)).live.grants === 0, "a grant on the deleted project is still live");
3001
2790
  const revealed = await admin.api.secrets.reveal(`${tombstone.slug}/prod`).then(() => null, (error) => error);
3002
- 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);
3003
2792
  const fresh = value();
3004
2793
  await admin.api.projects.create(PROJECT, { name: "Gone, again" });
3005
2794
  await admin.api.environments.create(`${PROJECT}/prod`, { name: "prod" });
3006
2795
  await admin.api.secrets.set(`${PROJECT}/prod`, { API_KEY: fresh });
3007
2796
  expect(await projectId(deployment, PROJECT) !== id, "the new project took the deleted one's row");
3008
2797
  expect((await admin.api.secrets.reveal(`${PROJECT}/prod/API_KEY`)).values.API_KEY === fresh, "the new project does not read its own value");
3009
- const { entries } = await admin.api.audit.list({
3010
- path: tombstone.slug,
3011
- limit: 500
3012
- });
3013
- 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));
3014
2809
  await admin.api.environments.update(`${PROJECT}/prod`, { archived: true });
3015
2810
  await run([
3016
2811
  "environments",
@@ -3018,18 +2813,14 @@ async function deletion(deployment, cli, admin) {
3018
2813
  `${PROJECT}/prod`,
3019
2814
  "--apply"
3020
2815
  ]);
3021
- await admin.api.environments.create(`${PROJECT}/prod`, { name: "prod, again" });
2816
+ await admin.api.environments.create(`${PROJECT}/prod`, {});
3022
2817
  const environments = (await admin.api.projects.list()).projects.find((project) => project.slug === PROJECT)?.environments ?? [];
3023
- 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);
3024
2819
  expect((await sealed(deployment, await projectId(deployment, PROJECT))).sealed === 0, "a deleted environment still holds a value");
3025
- const verified = await admin.api.audit.verify();
3026
- expect(verified.ok, "the log does not verify after the deletions", verified);
3027
- 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`;
3028
2821
  }
3029
2822
  //#endregion
3030
2823
  //#region src/checks/environments.ts
3031
- /** A read of one key refused, whatever the reason (D153): `outcome`'s status and code. */
3032
- const NO = "404 not_available";
3033
2824
  /** Where the fork check copies dev to; archived once checked. */
3034
2825
  const FORKED = `${PROJECT$1}/forked`;
3035
2826
  /**
@@ -3154,8 +2945,7 @@ async function personalConfigs(deployment, { admin, reader }) {
3154
2945
  }
3155
2946
  /** The key in dev the reference check makes, archived once checked. */
3156
2947
  const HELD = "CONFORMANCE_REFERENCE";
3157
- /** The key a forged reference row is held by, a new one each run: it is archived after, and its name stays taken (`references`). */
3158
- const FORGED = `CONFORMANCE_FORGED_${randomUUID().slice(0, 8).toUpperCase()}`;
2948
+ const FORGED = "CONFORMANCE_FORGED";
3159
2949
  /** The status a call failed with and its reason, the vault's or the app's, or that it went through. */
3160
2950
  async function outcome(call) {
3161
2951
  try {
@@ -3180,81 +2970,78 @@ async function references(deployment, { admin, reader, leaver }, canaries) {
3180
2970
  expect(sourceKey !== void 0, "prod holds no secret to refer to");
3181
2971
  const source = `${PROD}/${sourceKey}`;
3182
2972
  await refused$1("a viewer on dev read prod directly", reader.api.secrets.reveal(source));
3183
- 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);
3184
2975
  await admin.api.secrets.set(DEV, { [HELD]: { ref: source } });
3185
2976
  try {
3186
- 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");
3187
- const through = (entries) => entries.filter((entry) => entry.action === "secret.read" && entry.actorId === reader.email && `${entry.project}/${entry.environment}/${entry.key}` === source && entry.metadata.via?.path === `${DEV}/${HELD}`);
3188
- expect(await listed(async () => through((await admin.api.audit.list({
3189
- limit: 200,
3190
- sealed: "1"
3191
- })).entries).length >= 1), "the read through the reference is not on the whole log, filed at its source with the holder beside it");
3192
- for (const path of [PROD, DEV]) {
3193
- const page = await admin.api.audit.list({
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({
3194
2988
  path,
3195
- limit: 200,
3196
- sealed: "1"
3197
- });
3198
- expect(page.readsOmitted === "path" || through(page.entries).length >= 1, `${path}'s log neither lists the read through the reference nor says where it is`, page.readsOmitted);
3199
- }
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
+ });
3200
2997
  const archives = [[source, (archived) => admin.api.secrets.update(source, { archived })], [PROD, (archived) => admin.api.environments.update(PROD, { archived })]];
3201
2998
  for (const [what, archive] of archives) {
3202
2999
  const refusal = await archive(true).then(() => null, (error) => error);
3203
3000
  if (refusal === null) await archive(false);
3204
- 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);
3205
3004
  }
3206
3005
  await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
3207
3006
  const underArchived = await outcome(admin.api.secrets.update(source, { archived: true })).finally(() => admin.api.secrets.update(`${DEV}/${HELD}`, { archived: false }));
3208
3007
  try {
3209
3008
  expect(underArchived === "let through", `${source} could not be archived while only an archived key referred to it: ${underArchived}`);
3210
3009
  const restored = await outcome(reader.api.secrets.reveal(`${DEV}/${HELD}`));
3211
- expect(restored === NO, `a reference read a source archived while its holder was: ${restored}`);
3010
+ expect(restored.startsWith("409 "), `a reference read a source archived while its holder was: ${restored}`);
3212
3011
  } finally {
3213
3012
  if (underArchived === "let through") await admin.api.secrets.update(source, { archived: false });
3214
3013
  }
3215
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");
3216
3015
  const forged = await using(deployment.database(), async (sql) => {
3217
- const [holder] = await query(sql, "SELECT id, project_id, environment_id FROM secrets WHERE key = $1", [HELD]);
3218
- 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]]);
3219
- 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]]);
3220
3018
  const id = randomUUID();
3221
- 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)`, [
3222
3022
  id,
3223
3023
  holder.project_id,
3224
3024
  holder.environment_id,
3225
- FORGED
3226
- ]);
3227
- 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)
3228
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [
3229
- randomUUID(),
3230
- holder.project_id,
3231
- holder.environment_id,
3232
- id,
3233
- target.project_id,
3234
- target.environment_id,
3025
+ FORGED,
3026
+ now,
3235
3027
  target.id,
3236
- String(entry.seq),
3237
3028
  admin.member
3238
3029
  ]);
3239
3030
  return id;
3240
3031
  });
3241
3032
  try {
3242
3033
  const forgedRead = await outcome(reader.api.secrets.reveal(`${DEV}/${FORGED}`));
3243
- expect([
3244
- "404 no_value",
3245
- "404 not_found",
3246
- "403 bad_claim",
3247
- NO
3248
- ].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}`);
3249
3035
  } finally {
3250
- await using(deployment.database(), async (sql) => {
3251
- await query(sql, "DELETE FROM secret_references WHERE secret_id = $1", [forged]);
3252
- await query(sql, sql.engine === "postgres" ? "UPDATE secrets SET archived_at = now() WHERE id = $1" : "UPDATE secrets SET archived_at = CAST(unixepoch('subsec') * 1000 AS INTEGER) WHERE id = $1", [forged]);
3253
- });
3036
+ await using(deployment.database(), (sql) => query(sql, "DELETE FROM secrets WHERE id = $1", [forged]));
3254
3037
  }
3255
3038
  await admin.api.references.break(`${DEV}/${HELD}`);
3256
- const after = await outcome(reader.api.secrets.reveal(`${DEV}/${HELD}`));
3257
- expect(after === NO, `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);
3258
3045
  } finally {
3259
3046
  await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
3260
3047
  }
@@ -3281,10 +3068,14 @@ async function loosening({ admin, reader, leaver }) {
3281
3068
  try {
3282
3069
  await refused$1("a viewer read a sensitive value", reader.api.secrets.reveal(path));
3283
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");
3284
- 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());
3285
3073
  expect((await admin.api.secrets.list(DEV)).loosenable === true, "the admin, an Owner of the whole instance, is told they do not loosen");
3286
- const marked = await admin.api.secrets.set(DEV, { [LOOSE]: { sensitivity: "secret" } });
3287
- 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);
3288
3079
  expect(JSON.stringify(marked.keys[LOOSE]) === JSON.stringify({
3289
3080
  version: 2,
3290
3081
  sensitivity: "secret"
@@ -3292,17 +3083,29 @@ async function loosening({ admin, reader, leaver }) {
3292
3083
  expect((await reader.api.secrets.reveal(path)).values[LOOSE] === value, "the reader did not read the loosened value as dev's secret");
3293
3084
  const versions = (await admin.api.secrets.history(path)).versions.map((version) => version.sensitivity);
3294
3085
  expect(versions.join() === "secret,sensitive", "the loosening changed the old version too, or made none", versions);
3295
- const logged = (await admin.api.audit.list({
3296
- path,
3297
- limit: 50
3298
- })).entries.filter((entry) => entry.action === "secret.loosen");
3299
- 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);
3300
3095
  } finally {
3301
3096
  await admin.api.secrets.set(DEV, { [LOOSE]: null });
3302
3097
  }
3303
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";
3304
3099
  }
3305
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
+ /**
3306
3109
  * Within a project, key names are not secret; values are. What dev is
3307
3110
  * missing is compared with every sibling: the reader, a viewer on dev who
3308
3111
  * cannot read prod, is told prod has the key, as the admin is, and nothing
@@ -3331,35 +3134,17 @@ async function missingKeys({ admin, reader }) {
3331
3134
  }
3332
3135
  //#endregion
3333
3136
  //#region src/checks/logins.ts
3334
- const ZEROS = "decode(repeat('00', 32), 'hex')";
3335
- const appendAs = (author) => `INSERT INTO audit_log
3336
- (seq, author, key_id, occurred_at, actor, action, decision, prev_hash, mac, hash, log015)
3337
- VALUES (9000000000, '${author}', '${author}:0', 0, 'system:conformance', 'probe', 'allow', ${ZEROS}, ${ZEROS}, ${ZEROS}, true)`;
3338
- const IMMUTABLE = [
3339
- "UPDATE audit_log SET action = 'rewritten' WHERE author = 'app'",
3340
- "UPDATE audit_log SET action = 'rewritten' WHERE author = 'vault'",
3341
- "DELETE FROM audit_log",
3342
- "TRUNCATE audit_log",
3343
- "DROP TABLE audit_log",
3344
- "UPDATE secret_versions SET id = id",
3345
- "DELETE FROM secret_versions",
3346
- "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 = [
3347
3139
  "CREATE TABLE conformance_probe (id integer)",
3348
3140
  "CREATE TEMP TABLE conformance_probe (id integer)",
3349
- "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",
3350
3145
  "SET session_replication_role = 'replica'",
3351
3146
  "SET ROLE coffre_owner"
3352
3147
  ];
3353
- /** A column of each app table, for an update that would change nothing, if it were allowed. */
3354
- const APP_COLUMNS = {
3355
- projects: "id",
3356
- environments: "id",
3357
- secrets: "id",
3358
- secret_versions: "id",
3359
- identities: "id",
3360
- credentials: "id",
3361
- device_authorizations: "id"
3362
- };
3363
3148
  async function refuses(sql, statements) {
3364
3149
  for (const statement of statements) {
3365
3150
  await sql.exec("BEGIN");
@@ -3376,7 +3161,7 @@ async function refuses(sql, statements) {
3376
3161
  }
3377
3162
  return statements.length;
3378
3163
  }
3379
- /** 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. */
3380
3165
  async function noWrites(sql, names) {
3381
3166
  const columns = await sql.query(`SELECT table_name, column_name,
3382
3167
  has_column_privilege(current_user, quote_ident(table_name), column_name, 'INSERT') AS insertable,
@@ -3392,163 +3177,42 @@ async function noWrites(sql, names) {
3392
3177
  expect(inspected.every((column) => !column.insertable && !column.updatable && !column.deletable && !column.truncatable), `the login has a column write privilege on ${name}`, inspected);
3393
3178
  }
3394
3179
  }
3180
+ /** The app's login writes no table: every row is the vault's. */
3395
3181
  async function appLogin(deployment) {
3396
- if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
3397
- expect(deployment.runtime !== null, "the app runtime login is missing");
3398
- const protectedTables = (await using(deployment.database(), tables)).filter((name) => name.startsWith("vault_"));
3182
+ const all = await using(deployment.database(), tables);
3399
3183
  return using(deployment.runtime(), async (sql) => {
3400
- await noWrites(sql, protectedTables);
3184
+ await noWrites(sql, all);
3401
3185
  const statements = [
3402
- ...IMMUTABLE,
3403
- appendAs("vault"),
3186
+ ...BYPASSES,
3404
3187
  "SET ROLE coffre_vault",
3405
- "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"
3406
3196
  ];
3407
- 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}`);
3408
- 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`;
3409
3199
  });
3410
3200
  }
3201
+ /** The vault's login writes coffre's rows, never 0.14's archived log, and empties no table. */
3411
3202
  async function vaultLogin(deployment) {
3412
- if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
3413
- expect(deployment.vaultRuntime !== null, "the vault runtime login is missing");
3414
- const appTables = await vaultReadOnly(deployment);
3415
- return using(deployment.vaultRuntime(), (sql) => vaultRefuses(sql, appTables));
3416
- }
3417
- /**
3418
- * The hot path's login, which every use runs as:
3419
- * the one the harness was given logs in, as coffre_vault_hot, with the
3420
- * timeouts and generic plans 0022 gives it, and holds what the vault's
3421
- * login holds, no more.
3422
- */
3423
- async function hotLogin(deployment) {
3424
- if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
3425
- expect(deployment.hotRuntime !== null, "the hot path login is missing");
3426
- const appTables = await vaultReadOnly(deployment);
3427
- return using(deployment.hotRuntime(), async (sql) => {
3428
- const [login] = await sql.query(`SELECT current_user AS name,
3429
- current_setting('lock_timeout') AS lock, current_setting('statement_timeout') AS statement, current_setting('plan_cache_mode') AS plans,
3430
- has_table_privilege(current_user, 'vault_members', 'INSERT') AS members`);
3431
- expect(login.name === "coffre_vault_hot", `the hot path's URL logs in as ${login.name}, not coffre_vault_hot`);
3432
- expect(login.lock === "1s" && login.statement === "2s" && login.plans === "force_generic_plan", "the hot path's login lacks its timeouts or generic plans", login);
3433
- expect(login.members, "the hot path's login cannot write members, as the vault's can");
3434
- return `as coffre_vault_hot, lock 1s, statement 2s, generic plans; ${await vaultRefuses(sql, appTables)}`;
3435
- });
3436
- }
3437
- /** The tables only the app writes: every one but the vault's and the log's. */
3438
- async function vaultReadOnly(deployment) {
3439
- return (await using(deployment.database(), tables)).filter((name) => ![
3440
- "vault_members",
3441
- "vault_grants",
3442
- "audit_log",
3443
- "audit_chain_head"
3444
- ].includes(name));
3445
- }
3446
- /** What a vault login may not do: write the app's tables, replace the log's head, rewrite the log or act as the app. */
3447
- async function vaultRefuses(sql, appTables) {
3448
- await noWrites(sql, appTables);
3449
- const [head] = await sql.query(`SELECT
3450
- has_table_privilege(current_user, 'audit_chain_head', 'INSERT') AS insertable,
3451
- has_table_privilege(current_user, 'audit_chain_head', 'DELETE') AS deletable,
3452
- has_table_privilege(current_user, 'audit_chain_head', 'TRUNCATE') AS truncatable,
3453
- has_column_privilege(current_user, 'audit_chain_head', 'only_row', 'UPDATE') AS identity_updatable`);
3454
- expect(!head.insertable && !head.deletable && !head.truncatable && !head.identity_updatable, "the vault can replace the head row", head);
3455
- const statements = [
3456
- ...IMMUTABLE,
3457
- appendAs("app"),
3458
- "SET ROLE coffre_app",
3459
- "UPDATE audit_chain_head SET only_row = only_row",
3460
- "DELETE FROM audit_chain_head",
3461
- "TRUNCATE audit_chain_head",
3462
- "UPDATE vault_members SET principal = principal",
3463
- "UPDATE vault_grants SET role = role"
3464
- ];
3465
- for (const table of appTables) {
3466
- const column = APP_COLUMNS[table];
3467
- if (column !== void 0) statements.push(`UPDATE ${quoted(table)} SET ${column} = ${column}`);
3468
- }
3469
- return `${await refuses(sql, statements)} writes and bypasses refused; app tables have no write privileges`;
3470
- }
3471
- //#endregion
3472
- //#region src/checks/readiness.ts
3473
- async function red(deployment) {
3474
- await until("readiness red", async () => (await fetch(`${deployment.origin}/readyz`)).status === 503, 10);
3475
- const body = await (await fetch(`${deployment.origin}/readyz`)).json();
3476
- expect(body.ok === false && body.checkpointed === false, "readiness did not report the missing checkpoint", body);
3477
- }
3478
- async function refusedCheckpoint(deployment) {
3479
- expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before the checkpoint refusal");
3480
- await using(deployment.database(), (sql) => logRefuses(sql, async () => {
3481
- const [before] = await sql.query("SELECT seq FROM audit_log WHERE action = 'audit.heartbeat' ORDER BY seq DESC LIMIT 1");
3482
- await deployment.scheduled({ allowFailure: true });
3483
- await until("the new heartbeat", async () => {
3484
- const [after] = await sql.query("SELECT seq FROM audit_log WHERE action = 'audit.heartbeat' ORDER BY seq DESC LIMIT 1");
3485
- return BigInt(after.seq) > BigInt(before.seq);
3486
- }, 10);
3487
- await red(deployment);
3488
- }, "audit.checkpoint"));
3489
- await deployment.scheduled();
3490
- await until("readiness recovered", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
3491
- return "a committed heartbeat with a refused checkpoint turns readiness red, then recovers";
3492
- }
3493
- async function missingCheckpoint(deployment) {
3494
- expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before removing checkpoints");
3495
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
3496
- const rows = await sql.query("SELECT * FROM audit_log WHERE action = 'audit.checkpoint' ORDER BY seq");
3497
- expect(rows.length > 0, "no checkpoints to remove");
3498
- await sql.exec("DELETE FROM audit_log WHERE action = 'audit.checkpoint'");
3499
- try {
3500
- await deployment.scheduled({ allowFailure: true });
3501
- await red(deployment);
3502
- } finally {
3503
- for (const row of rows) await insertRow(sql, "audit_log", row);
3504
- }
3505
- }));
3506
- await deployment.scheduled();
3507
- await until("readiness recovered", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
3508
- return "checkpoints deleted: the next beat finds them gone and readiness is red; restored, the next beat recovers";
3509
- }
3510
- /** A hole before the latest signed prefix must stop the next scheduled checkpoint. */
3511
- async function middleCut(deployment, { admin }) {
3512
- expect((await fetch(`${deployment.origin}/readyz`)).ok, "readiness is not green before the middle cut");
3513
- let seq = 0;
3514
- await using(deployment.database(), async (sql) => {
3515
- const [head] = await sql.query("SELECT next_seq FROM audit_chain_head");
3516
- 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");
3517
- expect(latest !== void 0, "no signed prefix to cut from");
3518
- const [heartbeat] = await query(sql, `SELECT h.seq FROM audit_log h JOIN audit_log c ON c.seq = h.seq + 1
3519
- WHERE h.action = 'audit.heartbeat' AND h.seq > 0 AND c.author = 'vault' AND c.action = 'audit.checkpoint'
3520
- AND c.decision = 'allow' AND c.seq < $1 AND NOT EXISTS
3521
- (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]);
3522
- expect(heartbeat !== void 0, "no older heartbeat and checkpoint to cut");
3523
- seq = Number(heartbeat.seq);
3524
- const kept = await query(sql, "SELECT * FROM audit_log WHERE seq >= $1 AND seq <= $2 ORDER BY seq", [seq, seq + 1]);
3525
- expect(kept.length === 2, "the middle cut needs two adjacent entries");
3526
- await appendOnlyLifted(sql, async () => {
3527
- await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE seq = $1", [seq + 1]);
3528
- await query(sql, "DELETE FROM audit_log WHERE seq >= $1 AND seq <= $2", [seq, seq + 1]);
3529
- try {
3530
- await deployment.scheduled({ allowFailure: true });
3531
- let checkpoint;
3532
- await until("the checkpoint after the cut", async () => {
3533
- [checkpoint] = await query(sql, `SELECT decision, code, metadata FROM audit_log
3534
- WHERE author = 'vault' AND action = 'audit.checkpoint' AND seq >= $1 ORDER BY seq LIMIT 1`, [head.next_seq]);
3535
- return checkpoint !== void 0;
3536
- }, 10);
3537
- expect(checkpoint?.decision === "deny" && checkpoint.code === "log_broken", "the checkpoint signed over a middle cut", checkpoint);
3538
- const detail = JSON.parse(checkpoint.metadata);
3539
- expect(detail.reason.includes(`sequence gap: expected seq ${seq}`), "the checkpoint did not name the middle gap", detail);
3540
- await red(deployment);
3541
- } finally {
3542
- await transaction(sql, async () => {
3543
- for (const row of kept) await insertRow(sql, "audit_log", row);
3544
- });
3545
- }
3546
- });
3203
+ const all = await using(deployment.database(), tables);
3204
+ return using(deployment.vaultRuntime(), async (sql) => {
3205
+ await noWrites(sql, ["audit_archive"]);
3206
+ const statements = [
3207
+ ...BYPASSES,
3208
+ "SET ROLE coffre_app",
3209
+ "UPDATE audit_archive SET action = action",
3210
+ "DELETE FROM audit_archive",
3211
+ "INSERT INTO audit_archive SELECT * FROM audit_archive LIMIT 1"
3212
+ ];
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`;
3547
3215
  });
3548
- await deployment.scheduled();
3549
- await until("readiness recovered after the middle cut", async () => (await fetch(`${deployment.origin}/readyz`)).ok, 20);
3550
- expect((await admin.api.audit.verify()).ok, "the restored middle cut did not verify");
3551
- return `entries ${seq} and ${seq + 1}, before the latest signed prefix, cut: checkpoint log_broken, readiness red; restored and recovered`;
3552
3216
  }
3553
3217
  //#endregion
3554
3218
  //#region ../client/dist/routes.js
@@ -3611,12 +3275,6 @@ function getCalls({ places, secrets, members, services }) {
3611
3275
  detail: "1"
3612
3276
  }
3613
3277
  }],
3614
- "GET /audit/verification": [{ params: {} }],
3615
- "GET /audit/trees": [{ params: {} }],
3616
- "GET /audit/reads": [{
3617
- params: {},
3618
- input: { from: "start" }
3619
- }],
3620
3278
  "GET /audit/keys": [{ params: {} }]
3621
3279
  };
3622
3280
  }
@@ -3759,9 +3417,9 @@ async function canaryScan(deployment, people, canaries) {
3759
3417
  await using(deployment.database(), async (sql) => {
3760
3418
  const names = await tables(sql);
3761
3419
  const control = canary();
3762
- 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)");
3763
3421
  try {
3764
- 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)]);
3765
3423
  let found = false;
3766
3424
  await scanTables(sql, ["conformance_canary_probe"], (_, bytes) => {
3767
3425
  found ||= Buffer.from(bytes).includes(control);
@@ -3773,20 +3431,11 @@ async function canaryScan(deployment, people, canaries) {
3773
3431
  await scanTables(sql, names, look);
3774
3432
  stored.push(`${names.length} tables, with a binary positive control`);
3775
3433
  });
3776
- if (deployment.databaseFile !== null) {
3777
- look("the database's file", files(deployment.databaseFile));
3778
- stored.push("the database file and WAL");
3779
- }
3780
3434
  look("the processes' output", deployment.output());
3781
3435
  stored.push("the processes' output");
3782
3436
  expect(leaks.length === 0, "a value was found outside a reveal", leaks);
3783
3437
  return `${answers} answers from ${Object.keys(routes).length} GET routes and ${paths.length} pages as ${callers.length} callers, and ${stored.join(", ")}: no value`;
3784
3438
  }
3785
- /** A SQLite file and its write-ahead log, as bytes: what anyone with the disk would read. */
3786
- function files(path) {
3787
- expect(existsSync(path), `the store file could not be found: ${path}`);
3788
- return Buffer.concat([path, `${path}-wal`].filter((file) => existsSync(file)).map((file) => readFileSync(file)));
3789
- }
3790
3439
  //#endregion
3791
3440
  //#region src/checks/cli.ts
3792
3441
  /** What `coffre verify instance` makes and finds on an instance, as an owner: its own place, service and key. */
@@ -3890,7 +3539,7 @@ function marks(output) {
3890
3539
  const lines = [...output.matchAll(/^ {2}([✓✗–]) (\S.{18}) /gm)];
3891
3540
  return Object.fromEntries(lines.map(([, mark, name]) => [name.trim(), mark]));
3892
3541
  }
3893
- /** 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. */
3894
3543
  async function verifyWithToken(cli, live) {
3895
3544
  const { canary } = live;
3896
3545
  const login = await cli.run([
@@ -3908,9 +3557,9 @@ async function verifyWithToken(cli, live) {
3908
3557
  ], {}, `${canary.value}\n`);
3909
3558
  const seen = marks(run.output);
3910
3559
  expect(run.code === 0, `coffre verify instance with a token exited ${run.code}`, run.output);
3911
- 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);
3912
3561
  expect(!run.output.includes(live.token) && !run.output.includes(canary.value), "the run printed its token or its canary", run.output);
3913
- 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`;
3914
3563
  }
3915
3564
  /** What a run as an owner must leave as it was: every member and their grants, but its service; every project and environment. */
3916
3565
  async function everythingElse(api) {
@@ -3946,6 +3595,7 @@ async function verifyAsOwner(cli) {
3946
3595
  const run = await cli.run(["verify", "instance"]);
3947
3596
  const seen = marks(run.output);
3948
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);
3949
3599
  const skipped = Object.entries(seen).filter(([, mark]) => mark !== "✓").map(([name]) => name);
3950
3600
  expect(skipped.length === 0, `the run skipped ${skipped.join(", ")}`, run.output);
3951
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]);
@@ -3999,65 +3649,61 @@ async function verifyLeftovers(cli, admin, setups, before, kept) {
3999
3649
  expect(await everythingElse(admin.api) === before, "the runs changed a member, a grant, a project or an environment besides their own");
4000
3650
  const { entries } = await admin.api.audit.list({
4001
3651
  actor: PROBE.service,
4002
- limit: 50,
4003
- sealed: "1"
3652
+ limit: 50
4004
3653
  });
4005
3654
  expect(entries.length > 0, `the audit log holds no entry by ${PROBE.service}`);
4006
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`;
4007
3656
  }
4008
3657
  /**
4009
- * `coffre verify keys`, with the admin's session, the keys piped in: this
4010
- * deployment's pass; a wrong vault key fails and the right app key passes;
4011
- * 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.
4012
3661
  */
4013
3662
  async function verifyKeys(cli) {
4014
3663
  const wrong = Buffer.alloc(32, 7).toString("base64");
4015
3664
  const runs = [
4016
3665
  {
4017
3666
  args: ["--vault-id", KEYS.VAULT_KEY_ID],
4018
- input: `${KEYS.VAULT_KEY}\n${KEYS.APP_KEY}\n`,
3667
+ input: `${KEYS.VAULT_KEY}\n`,
4019
3668
  code: 0,
4020
- vault: "✓",
4021
- app: "✓"
3669
+ key: "✓"
4022
3670
  },
4023
3671
  {
4024
3672
  args: [],
4025
- input: `${wrong}\n${KEYS.APP_KEY}\n`,
3673
+ input: `${wrong}\n`,
4026
3674
  code: 1,
4027
- vault: "✗",
4028
- app: "✓",
4029
- said: /✗ vault key +not this instance's vault key/
3675
+ key: "✗",
3676
+ said: /✗ key +not this instance's/
4030
3677
  },
4031
3678
  {
4032
3679
  args: [],
4033
- input: `${KEYS.VAULT_KEY}\nnot-a-key\n`,
3680
+ input: "not-a-key\n",
4034
3681
  code: 1,
4035
- vault: "✓",
4036
- app: "✗",
4037
- said: /✗ app key +not an app key/
3682
+ key: "✗",
3683
+ said: /✗ key +not a key/
4038
3684
  }
4039
3685
  ];
4040
- for (const { args, input, code, vault, app, said } of runs) {
3686
+ for (const { args, input, code, key, said } of runs) {
4041
3687
  const run = await cli.run([
4042
3688
  "verify",
4043
3689
  "keys",
4044
3690
  ...args
4045
3691
  ], {}, input);
4046
- const seen = marks(run.output);
4047
- expect(run.code === code && seen["vault key"] === vault && seen["app key"] === app, `coffre verify keys exited ${run.code}`, run.output);
4048
- if (said !== void 0) expect(said.test(run.output), "it did not say which key is not right", run.output);
4049
- 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 [
4050
3695
  KEYS.VAULT_KEY,
4051
3696
  KEYS.APP_KEY,
4052
3697
  wrong,
4053
3698
  "not-a-key"
4054
- ]) expect(!run.output.includes(key), "a key was printed", run.output);
3699
+ ]) expect(!run.output.includes(shown), "a key was printed", run.output);
4055
3700
  }
4056
- 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";
4057
3702
  }
4058
3703
  /**
4059
3704
  * Everything the API does, the CLI does: with the admin's `coffre login`
4060
- * 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
4061
3707
  * service admitted and granted, a token issued into a 0600 file, then
4062
3708
  * piped to `coffre login --token` in a CLI of the service's own, read
4063
3709
  * with, revoked after its preview, and refused from then on; the grant
@@ -4081,6 +3727,17 @@ async function manageByCli(cli) {
4081
3727
  as(ci, ", as the service"),
4082
3728
  as(helper, ", with a personal token")
4083
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
+ };
4084
3741
  try {
4085
3742
  const value = `cli-value-${randomBytes(8).toString("hex")}`;
4086
3743
  await coffre([
@@ -4095,6 +3752,16 @@ async function manageByCli(cli) {
4095
3752
  "create",
4096
3753
  "conformance-cli/ci"
4097
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);
4098
3765
  await coffre(["set", "conformance-cli/ci/API_KEY"], value);
4099
3766
  await coffre([
4100
3767
  "rename",
@@ -4172,8 +3839,7 @@ async function manageByCli(cli) {
4172
3839
  listed.id,
4173
3840
  "--apply"
4174
3841
  ]);
4175
- const gone = await service(["get", "conformance-cli/ci/API_TOKEN"], void 0, 1);
4176
- 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"]);
4177
3843
  const personal = join(cli.home, "agent-token");
4178
3844
  await coffre([
4179
3845
  "tokens",
@@ -4215,7 +3881,7 @@ async function manageByCli(cli) {
4215
3881
  mine.id,
4216
3882
  "--apply"
4217
3883
  ]);
4218
- 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"]);
4219
3885
  await coffre([
4220
3886
  "revoke",
4221
3887
  "conformance-cli",
@@ -4557,27 +4223,7 @@ async function pagesInBrowser(deployment, admin, browser) {
4557
4223
  [
4558
4224
  "/settings",
4559
4225
  "Settings",
4560
- "General"
4561
- ],
4562
- [
4563
- "/settings?tab=roles",
4564
- "Settings",
4565
- "Roles"
4566
- ],
4567
- [
4568
- "/settings?tab=roles&roles=global",
4569
- "Settings",
4570
- "Roles"
4571
- ],
4572
- [
4573
- "/settings?tab=instance-roles",
4574
- "Settings",
4575
- "Roles"
4576
- ],
4577
- [
4578
- "/settings?tab=keys",
4579
- "Settings",
4580
- "Keys"
4226
+ null
4581
4227
  ],
4582
4228
  [
4583
4229
  person,
@@ -4644,7 +4290,7 @@ async function pagesInBrowser(deployment, admin, browser) {
4644
4290
  expect(loaded.cards.filter((title) => title === "Sign in with OIDC" || title === "Bearer tokens").join(" then ") === "Sign in with OIDC then Bearer tokens", `${path} does not show OIDC, then bearer tokens`, loaded.cards);
4645
4291
  expect(loaded.dialog?.includes("Removing revokes 0 grants, 0 bearer tokens") === true, `${path}: Remove… does not preview what it would revoke`, loaded.dialog);
4646
4292
  }
4647
- if (path === "/settings?tab=keys") expect(loaded.cards.includes("Keys"), `${path} does not show what the keys are checked against`, loaded.cards);
4293
+ if (path === "/settings") expect(loaded.cards.includes("Keys"), "/settings does not show what the keys are checked against", loaded.cards);
4648
4294
  if (path === `${person}/apps`) expect(loaded.cards.includes("Connected apps"), `${path} does not show an owner the person's connected apps`, loaded.cards);
4649
4295
  if (path === "/account/apps") {
4650
4296
  expect(loaded.cards.join(" then ") === "Connect an app then Connected apps", `${path} does not show Connect an app, then the connected apps`, loaded.cards);
@@ -4698,8 +4344,7 @@ function pem() {
4698
4344
  /**
4699
4345
  * A row's value field, opened by the button `open` names, and `text`
4700
4346
  * pasted into it as the clipboard would, over the characters `select`
4701
- * picks; then saved by the button `save` names, under the table. A new
4702
- * secret is named first.
4347
+ * picks; then the row saved by its own button. A new secret is named first.
4703
4348
  * Answers which field has the focus after the paste, and where its caret is.
4704
4349
  */
4705
4350
  const pasteInto = (steps) => `(async () => {
@@ -4736,7 +4381,7 @@ const pasteInto = (steps) => `(async () => {
4736
4381
  })()`;
4737
4382
  /**
4738
4383
  * A new secret's value typed as a person would: `first`, Shift+Enter while composing (which does nothing), Shift+Enter, then
4739
- * `second` into whichever field the page has by then; then saved, under the table.
4384
+ * `second` into whichever field the page has by then; then the row saved.
4740
4385
  * Answers which field has the focus after the line break, and where its caret is.
4741
4386
  */
4742
4387
  const typeLines = (steps) => `(async () => {
@@ -4801,7 +4446,7 @@ async function pastedValues(deployment, admin, browser) {
4801
4446
  name: "PASTED_KEY",
4802
4447
  field: "Value for PASTED_KEY",
4803
4448
  text: added,
4804
- save: "Save"
4449
+ save: "Save PASTED_KEY now"
4805
4450
  }));
4806
4451
  expect(adding.errors.length === 0, `${page} reported errors in the browser`, adding.errors.join("\n"));
4807
4452
  expect(JSON.stringify(adding.answer) === JSON.stringify({
@@ -4815,7 +4460,7 @@ async function pastedValues(deployment, admin, browser) {
4815
4460
  field: "New value for EDITED_KEY",
4816
4461
  select: [...at],
4817
4462
  text: edit.replaceAll("\n", "\r\n"),
4818
- save: "Save"
4463
+ save: "Save EDITED_KEY now"
4819
4464
  }));
4820
4465
  expect(editing.errors.length === 0, `${page} reported errors in the browser`, editing.errors.join("\n"));
4821
4466
  const after = at[0] + edit.length;
@@ -4829,7 +4474,7 @@ async function pastedValues(deployment, admin, browser) {
4829
4474
  field: "Value for TYPED_KEY",
4830
4475
  first: "abc",
4831
4476
  second: "def",
4832
- save: "Save"
4477
+ save: "Save TYPED_KEY now"
4833
4478
  }));
4834
4479
  expect(typing.errors.length === 0, `${page} reported errors in the browser`, typing.errors.join("\n"));
4835
4480
  expect(JSON.stringify(typing.answer) === JSON.stringify({
@@ -4852,8 +4497,7 @@ async function pastedValues(deployment, admin, browser) {
4852
4497
  * A refused sign-in, said once: the page coffre sends the browser back to
4853
4498
  * shows why, rendered by the server, then takes it out of the address, so a
4854
4499
  * reload is a clean retry. The refusal is a real one: a member's address
4855
- * that GitHub now gives another account, which names the provider the
4856
- * address already signs in with.
4500
+ * that GitHub now gives another account, said bare: 0.16 names no provider.
4857
4501
  */
4858
4502
  async function signinErrorOnce(deployment, admin, browser) {
4859
4503
  const executable = chromeOrSkip(browser);
@@ -4866,8 +4510,8 @@ async function signinErrorOnce(deployment, admin, browser) {
4866
4510
  const refused = await signIn(deployment, visitor, email);
4867
4511
  expect(!refused.ok && refused.error === "account_mismatch", "another GitHub account with a member's address was not refused as account_mismatch", refused);
4868
4512
  const back = new URL(refused.location, deployment.origin);
4869
- expect(back.searchParams.get("with") === "github", `the refusal does not name the provider the address signs in with: ${refused.location}`);
4870
- 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";
4871
4515
  const html = await visitor.fetch(refused.location);
4872
4516
  expect(html.status === 200 && (await html.text()).includes(said), `${refused.location}, without scripts, does not say the address ${said}`, html.status);
4873
4517
  const chrome = await Chrome.open(executable);
@@ -4898,31 +4542,18 @@ const MARKERS = [
4898
4542
  pattern: /drizzle:[A-Z]/
4899
4543
  },
4900
4544
  ...[
4901
- "audit_chain_head",
4902
- "audit_log",
4903
- "capped_alerts",
4904
- "consumed_tokens",
4905
- "device_authorizations",
4545
+ "audit_archive",
4546
+ "device_logins",
4906
4547
  "dismissed_keys",
4907
- "log_bulk",
4908
- "log_changes",
4909
- "log_checkpoints",
4910
- "log_config",
4911
- "log_fence",
4912
- "log_read_days",
4913
- "log_reads",
4914
- "log_reads_cursor",
4548
+ "enrollment_codes",
4549
+ "log_head",
4550
+ "log_index",
4551
+ "log_pending",
4915
4552
  "log_tiles",
4916
- "log_uses",
4917
4553
  "mcp_approvals",
4918
- "mcp_connections",
4919
4554
  "oauth_clients",
4920
- "secret_references",
4921
4555
  "secret_versions",
4922
- "service_bindings",
4923
- "vault_grants",
4924
- "vault_members",
4925
- "vault_releases"
4556
+ "spent_id_tokens"
4926
4557
  ].map((name) => ({
4927
4558
  label: `the table ${name}`,
4928
4559
  pattern: new RegExp(`\\b${name}\\b`)
@@ -4931,10 +4562,6 @@ const MARKERS = [
4931
4562
  label: "pg",
4932
4563
  pattern: /cloudflare:sockets|pg-protocol|pgpass/
4933
4564
  },
4934
- {
4935
- label: "libsql",
4936
- pattern: /@libsql|libsql/
4937
- },
4938
4565
  {
4939
4566
  label: "a COFFRE_ variable read",
4940
4567
  pattern: /env\s*(\.|\[\s*['"`])COFFRE_/
@@ -4964,19 +4591,28 @@ function clientFiles(dir) {
4964
4591
  }
4965
4592
  //#endregion
4966
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
+ }
4967
4610
  async function health(deployment) {
4968
4611
  const { origin } = deployment;
4969
4612
  const live = await fetch(`${origin}/livez`);
4970
4613
  expect(live.ok, `/livez answered ${live.status}`);
4971
- if (deployment.beforeFirstBeat) {
4972
- const early = await fetch(`${origin}/readyz`);
4973
- expect(early.status === 503, `/readyz answered ${early.status} before any heartbeat`, await early.text());
4974
- await deployment.scheduled();
4975
- }
4976
4614
  await until("/readyz", async () => (await fetch(`${origin}/readyz`)).ok, 20);
4977
- const ready = await (await fetch(`${origin}/readyz`)).json();
4978
- expect(ready.checkpointed === true, "/readyz passed without a checkpoint covering the heartbeat", ready);
4979
- return deployment.beforeFirstBeat ? "/livez, and /readyz only once the heartbeat ran and was checkpointed" : "/livez and /readyz";
4615
+ return "/livez and /readyz";
4980
4616
  }
4981
4617
  /** The headers coffre sets on everything it answers, but the policy, whose nonce is each response's own. */
4982
4618
  const SECURED = {
@@ -5136,18 +4772,18 @@ async function runSignsIn(deployment, admin, canaries) {
5136
4772
  const { values } = await bearer(deployment.origin, token).secrets.reveal(`${DEV}/API_KEY`);
5137
4773
  expect(values.API_KEY === canaries[`${DEV}/API_KEY`], "the credential did not read the value as the service");
5138
4774
  let entries = [];
4775
+ const exchangedIn = () => entries.find((entry) => entry.action === "token.exchange");
5139
4776
  const readIn = () => entries.find((entry) => entry.author === "vault" && entry.action === "secret.read" && entry.key === "API_KEY");
5140
- await listed(async () => (entries = (await admin.api.audit.list({
5141
- actor: RUNNER,
5142
- detail: "1",
5143
- sealed: "1"
5144
- })).entries, readIn() !== void 0));
5145
- const exchanged = entries.find((entry) => entry.action === "token.exchange");
5146
- const read = readIn();
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()];
5147
4785
  expect(exchanged !== void 0 && read !== void 0, "the exchange or the read is not in the log", entries.map((entry) => entry.action));
5148
- const credentialId = exchanged.metadata.credentialId;
5149
- expect(typeof credentialId === "string", "the exchange did not log its credential", exchanged.metadata);
5150
- 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);
5151
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);
5152
4788
  return {
5153
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`,
@@ -5214,11 +4850,7 @@ async function runsRefused(deployment, admin) {
5214
4850
  "no_match"
5215
4851
  ]
5216
4852
  ];
5217
- const before = (await admin.api.audit.list({
5218
- actor: RUNNER,
5219
- detail: "1",
5220
- sealed: "1"
5221
- })).entries.filter((entry) => entry.action === "token.exchange").length;
4853
+ const from = Date.now();
5222
4854
  for (const [what, token, reason] of cases) {
5223
4855
  const answer = await exchange(deployment, token);
5224
4856
  expect(answer.status === 401 && answer.reason === reason, `a token ${what} was not refused as ${reason}`, shown(answer));
@@ -5226,12 +4858,18 @@ async function runsRefused(deployment, admin) {
5226
4858
  }
5227
4859
  const stranger = await exchange(deployment, await mint(deployment), "token:conformance-nobody");
5228
4860
  expect(stranger.status === 401 && stranger.reason === "no_match", "a token was taken for a service no binding names", shown(stranger));
5229
- const after = (await admin.api.audit.list({
5230
- actor: RUNNER,
5231
- detail: "1",
5232
- sealed: "1"
5233
- })).entries.filter((entry) => entry.action === "token.exchange").length;
5234
- 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`);
5235
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`;
5236
4874
  }
5237
4875
  /**
@@ -5309,7 +4947,11 @@ async function keysUploaded(deployment, admin, canaries) {
5309
4947
  jwks: { keys },
5310
4948
  label: "conformance pod"
5311
4949
  });
5312
- 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
+ });
5313
4955
  const now = Math.floor(Date.now() / 1e3);
5314
4956
  const pod = (alg, uid = POD_ACCOUNT.uid, times = {}) => mint(deployment, {
5315
4957
  iss: CLUSTER,
@@ -5334,7 +4976,7 @@ async function keysUploaded(deployment, admin, canaries) {
5334
4976
  nbf: now,
5335
4977
  exp: now + 3601
5336
4978
  }), POD);
5337
- 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));
5338
4980
  await admin.api.bindings.replaceKeys(POD, {
5339
4981
  issuer: CLUSTER,
5340
4982
  jwks: { keys: keys.filter((key) => key.alg === "ES256") }
@@ -5345,17 +4987,21 @@ async function keysUploaded(deployment, admin, canaries) {
5345
4987
  expect(kept.status === 200, "a token signed by the key kept was refused", shown(kept));
5346
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`;
5347
4989
  }
5348
- /** 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
+ */
5349
4995
  async function runUnbound(deployment, admin, bindingId, credential) {
5350
4996
  const me = (token) => fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${token}` } }).then((response) => response.status);
5351
4997
  expect(await me(credential) === 200, "the credential did not work before its binding was removed");
5352
4998
  await admin.api.bindings.remove(RUNNER, bindingId);
5353
- 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);
5354
5000
  const next = await exchange(deployment, await mint(deployment));
5355
5001
  expect(next.status === 401 && next.reason === "no_match", "a run was let in once its binding was removed", shown(next));
5356
5002
  const { bindings } = await admin.api.bindings.list(RUNNER);
5357
5003
  expect(bindings.length === 0, "the removed binding is still listed", bindings);
5358
- 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";
5359
5005
  }
5360
5006
  /**
5361
5007
  * One address may exchange only so often: tokens that would be refused
@@ -5386,7 +5032,8 @@ async function tokensUnlogged(deployment) {
5386
5032
  //#region src/suite.ts
5387
5033
  /** The names of the checks that failed. */
5388
5034
  async function conform(deployment, options) {
5389
- const report = new Report();
5035
+ const report = new Report(void 0, UNSERVED);
5036
+ await report.check("ready once adopted", {}, () => readyOnceAdopted(deployment));
5390
5037
  await report.check("health", {}, () => health(deployment));
5391
5038
  await report.check("browser bundle", {}, () => browserBundle(deployment));
5392
5039
  const admin = await report.check("sign-in", {}, () => signInAdmin(deployment));
@@ -5501,17 +5148,14 @@ async function conform(deployment, options) {
5501
5148
  }, ({ admin, binding, credential }) => runUnbound(deployment, admin, binding, credential));
5502
5149
  await report.check("exchanges limited", { binding }, () => exchangesLimited(deployment));
5503
5150
  await report.check("tokens unlogged", { binding }, () => tokensUnlogged(deployment));
5504
- await report.check("checkpoints", { people }, ({ people }) => checkpoints(deployment, people));
5505
5151
  await report.check("keys behind writes", { people }, ({ people }) => writesAgree(deployment));
5506
5152
  await report.check("no audit, no value", all, ({ people, canaries }) => noAuditNoValue(deployment, people, canaries));
5507
5153
  await report.check("access authorship", { people }, ({ people }) => accessAuthorship(deployment, people));
5508
5154
  await report.check("no audit, no access", { people }, ({ people }) => noAuditNoAccess(deployment, people));
5509
- await report.check("full verification", { people }, ({ people }) => verification(deployment, people));
5510
- await report.check("checkpoint refused", {}, () => refusedCheckpoint(deployment));
5511
- 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));
5512
5157
  await report.check("app login", {}, () => appLogin(deployment));
5513
5158
  await report.check("vault login", {}, () => vaultLogin(deployment));
5514
- await report.check("hot login", {}, () => hotLogin(deployment));
5515
5159
  await report.check("forged grant", { people }, ({ people }) => memberTampering(deployment, people, "grant"));
5516
5160
  await report.check("forged member", { people }, ({ people }) => memberTampering(deployment, people, "member"));
5517
5161
  await report.check("stale member", { people }, ({ people }) => memberTampering(deployment, people, "stale"));
@@ -5520,35 +5164,25 @@ async function conform(deployment, options) {
5520
5164
  await report.check("forged identity", { people }, ({ people }) => forgedIdentity(deployment, people));
5521
5165
  await report.check("forged approval", { people }, ({ people }) => forgedApproval(deployment, people));
5522
5166
  await report.check("edited generation", { people }, ({ people }) => editedGeneration(deployment, people));
5167
+ await report.check("log cut", { people }, ({ people }) => logCut(deployment, people));
5523
5168
  await report.check("canary scan", all, ({ people, canaries }) => canaryScan(deployment, people, canaries));
5524
- for (const author of ["app", "vault"]) await report.check(`${author} rewritten`, { people }, ({ people }) => rewrittenEntry(deployment, people, author));
5525
- await report.check("vault forged", { people }, ({ people }) => forgedVaultEntry(deployment, people));
5526
- await report.check("middle gap", { people }, ({ people }) => missingEntry(deployment, people, "middle"));
5527
- await report.check("middle cut", { people }, ({ people }) => middleCut(deployment, people));
5528
- await report.check("first gap", { people }, ({ people }) => missingEntry(deployment, people, "first"));
5529
- await report.check("batch gap", { people }, ({ people }) => missingEntry(deployment, people, "batch"));
5530
- await report.check("earlier checkpoint", { people }, ({ people }) => earlierCheckpoint(deployment, people));
5531
- await report.check("tail deleted", { people }, ({ people }) => deletedTail(deployment, people));
5532
5169
  return report.failed;
5533
5170
  }
5534
5171
  //#endregion
5535
5172
  //#region src/main.ts
5536
5173
  const USAGE = `usage:
5537
- coffre-conformance workers [<dir>] --postgres <owner URL> --runtime <coffre_runtime URL>
5538
- --vault-runtime <coffre_vault_runtime URL> --hot-runtime <coffre_vault_hot URL> [options]
5539
- coffre-conformance node [<dir>] [options]
5174
+ coffre-conformance workers|node [<dir>] --postgres <owner URL> --runtime <coffre_runtime URL>
5175
+ --vault-runtime <coffre_vault_runtime URL> [options]
5540
5176
 
5541
5177
  <dir> the deployment, as \`coffre init\` wrote it (default: here)
5542
5178
  --port <n> coffre's port; the IdP gets the next, wrangler's inspector the one after (3082)
5543
5179
  --bulk-limit <n> the vault's bulkLimit count, when not the default (1000)
5544
5180
  --browser <path> Chrome or Chromium, to load the pages in (default: one found here;
5545
5181
  without one, that check is skipped)
5546
- --postgres <url> workers: a Postgres login that may create databases; one is made for the run
5547
- --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
5548
5184
  --vault-runtime <url>
5549
- workers: the same server as coffre_vault_runtime, the login the vault runs as
5550
- --hot-runtime <url>
5551
- workers: the same server as coffre_vault_hot, the login 0.15's uses run as
5185
+ the same server as coffre_vault_runtime, the login the vault runs as
5552
5186
 
5553
5187
  To check an instance that is running already, from outside, use the CLI:
5554
5188
  \`coffre verify instance\`.`;
@@ -5567,7 +5201,6 @@ const { values, positionals } = parseArgs({
5567
5201
  postgres: { type: "string" },
5568
5202
  runtime: { type: "string" },
5569
5203
  "vault-runtime": { type: "string" },
5570
- "hot-runtime": { type: "string" },
5571
5204
  help: {
5572
5205
  type: "boolean",
5573
5206
  short: "h"
@@ -5590,8 +5223,7 @@ try {
5590
5223
  port,
5591
5224
  postgres: values.postgres,
5592
5225
  runtime: values.runtime,
5593
- vaultRuntime: values["vault-runtime"],
5594
- hotRuntime: values["hot-runtime"]
5226
+ vaultRuntime: values["vault-runtime"]
5595
5227
  });
5596
5228
  } catch (error) {
5597
5229
  const detail = error instanceof Failure && error.detail !== void 0 ? `\n${String(error.detail)}` : "";