@coffre/conformance 0.4.0 → 0.4.2

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
@@ -85,6 +85,52 @@ function drainingConfig(server) {
85
85
  if (import.meta.main) console.log(drainingConfig(resolve(process.argv[2])));
86
86
  //#endregion
87
87
  //#region ../client/dist/index.js
88
+ /** Two colons or more, in hex: an IPv6 address, as `::1` or `2001:db8::1:443` with its port. */
89
+ const IPV6 = /[0-9a-f]*:[0-9a-f]*:[0-9a-f:]*/i;
90
+ const TIMEOUT_OR_REFUSAL = /* @__PURE__ */ new Set([
91
+ "ECONNREFUSED",
92
+ "ETIMEDOUT",
93
+ "UND_ERR_CONNECT_TIMEOUT",
94
+ "ENETUNREACH",
95
+ "EHOSTUNREACH"
96
+ ]);
97
+ /** What to try for the common causes; null for the others. */
98
+ function hint(cause) {
99
+ const code = cause.code ?? "";
100
+ if (code === "ENOTFOUND" || code === "EAI_AGAIN") return "the name does not resolve here: check the address, or flush this machine's DNS cache";
101
+ const tried = [cause, ...cause.errors ?? []].flatMap((each) => [each.address ?? "", each.message]);
102
+ if (TIMEOUT_OR_REFUSAL.has(code) && tried.some((each) => IPV6.test(each))) return "it was tried over IPv6, which this network may not carry: try NODE_OPTIONS=--dns-result-order=ipv4first";
103
+ if (/CERT|UNABLE_TO_VERIFY|SELF_SIGNED/.test(code)) return "something between you and the instance, such as a proxy or a network filter, presents its own certificate";
104
+ return null;
105
+ }
106
+ /** The cause's own words, with its code and address when they don't already say them. */
107
+ function described(cause) {
108
+ const said = cause.message || (cause.errors ?? []).map((each) => each.message).join(", ");
109
+ const address = cause.address === void 0 ? void 0 : cause.port === void 0 ? cause.address : `${cause.address}:${cause.port}`;
110
+ const more = [cause.code, address].filter((each) => each !== void 0 && !said.includes(each));
111
+ if (said === "") return more.join(" ") || cause.name;
112
+ return more.length === 0 ? said : `${said} (${more.join(", ")})`;
113
+ }
114
+ /**
115
+ * `could not reach <origin>: <why>`, and what to try when the cause is a
116
+ * common one: `could not reach https://x: getaddrinfo ENOTFOUND x; the name
117
+ * does not resolve here: …`.
118
+ */
119
+ function unreachable(origin, error) {
120
+ const cause = error instanceof Error && error.cause instanceof Error ? error.cause : null;
121
+ if (cause === null) return `could not reach ${origin}: ${error instanceof Error ? error.message : String(error)}`;
122
+ const then = hint(cause);
123
+ return `could not reach ${origin}: ${described(cause)}${then === null ? "" : `; ${then}`}`;
124
+ }
125
+ /** A request that never reached the instance: its message says why, `cause` is what `fetch` threw. */
126
+ var Unreachable = class extends Error {
127
+ origin;
128
+ constructor(origin, error) {
129
+ super(unreachable(origin, error), { cause: error });
130
+ this.name = "Unreachable";
131
+ this.origin = origin;
132
+ }
133
+ };
88
134
  /** The API's error shape, `{ error, message }`, with the HTTP status. */
89
135
  var CoffreError = class extends Error {
90
136
  status;
@@ -110,7 +156,13 @@ function place(path) {
110
156
  }
111
157
  function createClient(options) {
112
158
  const origin = options.url.replace(/\/+$/, "");
113
- const transport = options.transport ?? ((request) => fetch(request));
159
+ const transport = options.transport ?? (async (request) => {
160
+ try {
161
+ return await fetch(request);
162
+ } catch (error) {
163
+ throw new Unreachable(origin, error);
164
+ }
165
+ });
114
166
  async function send(method, path, input) {
115
167
  const url = new URL(`${origin}/api${path}`);
116
168
  const headers = new Headers(await options.headers?.());
@@ -924,1945 +976,1966 @@ async function pageLoad(person) {
924
976
  return `${paths.length} calls at once, each answered`;
925
977
  }
926
978
  //#endregion
927
- //#region src/checks/storage.ts
928
- const TABLES = [
929
- "projects",
930
- "environments",
931
- "secrets",
932
- "secret_versions",
933
- "vault_members",
934
- "vault_grants",
935
- "audit_log",
936
- "audit_chain_head",
937
- "identities",
938
- "credentials",
939
- "device_authorizations"
940
- ];
941
- /** Inspect every table, including any a deployment added. Missing shared tables fail. */
942
- async function tables(sql) {
943
- 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);
944
- const missing = TABLES.filter((name) => !names.includes(name));
945
- expect(missing.length === 0, "shared tables could not be inspected", missing);
946
- return names;
947
- }
948
- function quoted(name) {
949
- return `"${name.replaceAll("\"", "\"\"")}"`;
950
- }
951
- /** Both drivers return binary columns as bytes; casting records to text hides bytea. */
952
- async function scanTables(sql, names, look) {
953
- 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) ?? "");
954
- }
955
- /** Parameters use one spelling in the checks, whatever the database. */
956
- function query(sql, statement, params = []) {
957
- return sql.query(sql.engine === "sqlite" ? statement.replace(/\$\d+/g, "?") : statement, params);
958
- }
959
- /** Insert a saved or forged row as it is, but for its generated columns. */
960
- async function insertRow(sql, table, row) {
961
- const fields = Object.keys(row).filter((field) => field !== "active_subject");
962
- await query(sql, `INSERT INTO ${quoted(table)} (${fields.map(quoted).join(", ")}) VALUES (${fields.map((_, i) => `$${i + 1}`).join(", ")})`, fields.map((field) => row[field]));
963
- }
964
- /** Put a saved row back without changing its bytes or its generated columns. */
965
- async function restoreRow(sql, table, row, key) {
966
- const fields = Object.keys(row).filter((field) => field !== key && field !== "active_subject");
967
- 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]]);
968
- }
969
- /** Keep a deliberate rewrite, and its restoration, invisible until each is complete. */
970
- async function transaction(sql, work) {
971
- await sql.exec("BEGIN");
979
+ //#region src/checks/mcp.ts
980
+ const NAME = "Conformance MCP client";
981
+ /** A native client's redirect, on loopback. A custom scheme is left out of a registration (D37). */
982
+ const REDIRECT = "http://127.0.0.1:33418/callback";
983
+ /** A `state` the router would read as a number: it must come back as sent. */
984
+ const STATE = "1e5";
985
+ /** What every 2026-07-28 request carries: its revision and the client's capabilities. */
986
+ const ENVELOPE = {
987
+ "io.modelcontextprotocol/protocolVersion": "2026-07-28",
988
+ "io.modelcontextprotocol/clientCapabilities": {}
989
+ };
990
+ const base64url = (bytes) => bytes.toString("base64url");
991
+ async function json(response) {
992
+ const text = await response.text();
972
993
  try {
973
- const result = await work();
974
- await sql.exec("COMMIT");
975
- return result;
976
- } catch (error) {
977
- await sql.exec("ROLLBACK");
978
- throw error;
994
+ return JSON.parse(text);
995
+ } catch {
996
+ return { text };
979
997
  }
980
998
  }
981
- /**
982
- * `work` with the audit log's append-only triggers lifted, as only its owner
983
- * can: Postgres disables them for the session, SQLite drops them and makes
984
- * them again after.
985
- */
986
- async function appendOnlyLifted(sql, work) {
987
- if (sql.engine === "postgres") {
988
- await sql.exec("ALTER TABLE audit_log DISABLE TRIGGER USER");
989
- try {
990
- return await work();
991
- } finally {
992
- await sql.exec("ALTER TABLE audit_log ENABLE TRIGGER USER");
993
- }
994
- }
995
- const triggers = await sql.query(`SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = 'audit_log'`);
996
- for (const trigger of triggers) await sql.exec(`DROP TRIGGER ${trigger.name}`);
997
- try {
998
- return await work();
999
- } finally {
1000
- for (const trigger of triggers) await sql.exec(trigger.sql);
1001
- }
999
+ /** A JSON-RPC message to /mcp, with the headers a client sends. */
1000
+ function mcp(deployment, headers, method = "server/discover", params = {}) {
1001
+ return fetch(`${deployment.origin}/mcp`, {
1002
+ method: "POST",
1003
+ headers: {
1004
+ "content-type": "application/json",
1005
+ accept: "application/json, text/event-stream",
1006
+ ...headers
1007
+ },
1008
+ body: JSON.stringify({
1009
+ jsonrpc: "2.0",
1010
+ id: 1,
1011
+ method,
1012
+ params
1013
+ })
1014
+ });
1002
1015
  }
1003
- const LOG_REFUSAL = "coffre-conformance: the log refuses writes";
1004
- /**
1005
- * `work` with the audit log refusing appends, of one action or all, as a
1006
- * full disk or a lost connection would, without changing the deployment's
1007
- * code: a trigger, dropped after.
1008
- */
1009
- async function logRefuses(sql, work, action) {
1010
- const when = action === void 0 ? "" : ` WHEN (NEW.action = '${action}')`;
1011
- const create = sql.engine === "postgres" ? `CREATE FUNCTION conformance_refuse() RETURNS trigger LANGUAGE plpgsql AS $$
1012
- BEGIN RAISE EXCEPTION '${LOG_REFUSAL}'; END $$;
1013
- CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log FOR EACH ROW${when} EXECUTE FUNCTION conformance_refuse();` : `CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log${when}
1014
- BEGIN SELECT RAISE(ABORT, '${LOG_REFUSAL}'); END;`;
1015
- await sql.exec(create);
1016
- try {
1017
- return await work();
1018
- } finally {
1019
- await sql.exec(sql.engine === "postgres" ? "DROP TRIGGER conformance_refuse ON audit_log; DROP FUNCTION conformance_refuse();" : "DROP TRIGGER conformance_refuse;");
1020
- }
1016
+ function token(deployment, fields) {
1017
+ return fetch(`${deployment.origin}/api/oauth/token`, {
1018
+ method: "POST",
1019
+ headers: { "content-type": "application/x-www-form-urlencoded" },
1020
+ body: new URLSearchParams(fields)
1021
+ });
1021
1022
  }
1022
- /** The log's fields in the order its format (`coffre.audit.v2`) encodes them. */
1023
- const ENTRY_FIELDS = [
1024
- "seq",
1025
- "author",
1026
- "key_id",
1027
- "occurred_at",
1028
- "actor",
1029
- "action",
1030
- "decision",
1031
- "code",
1032
- "subject_principal",
1033
- "project_id",
1034
- "environment_id",
1035
- "secret_id",
1036
- "secret_version_id",
1037
- "operation_id",
1038
- "request_id",
1039
- "source_ip",
1040
- "related_seq",
1041
- "metadata"
1042
- ];
1043
1023
  /**
1044
- * An entry's fields as its MAC and hash cover them, written here from the
1045
- * format rather than taken from coffre, so a check does not trust the code
1046
- * it checks: each field length-prefixed in order, null as length -1.
1024
+ * The person approves on the consent page, as its Approve does, with what
1025
+ * they ticked: by default what the client asked for. The code comes back on
1026
+ * the redirect.
1047
1027
  */
1048
- function entryFields(row) {
1049
- return Buffer.concat(ENTRY_FIELDS.flatMap((field) => {
1050
- const header = Buffer.alloc(4);
1051
- const value = row[field];
1052
- if (value === null || value === void 0) {
1053
- header.writeInt32BE(-1);
1054
- return [header];
1055
- }
1056
- const bytes = Buffer.from(String(value));
1057
- header.writeInt32BE(bytes.length);
1058
- return [header, bytes];
1059
- }));
1060
- }
1061
- //#endregion
1062
- //#region src/checks/members.ts
1063
- async function member(deployment, { admin }, name) {
1064
- const email = `${name}@conformance.example`;
1065
- const principal = `user:${email}`;
1066
- await admin.api.members.add(principal);
1067
- await admin.api.access.set(principal, { [DEV]: "viewer" });
1068
- const browser = new Browser(deployment.origin);
1069
- expect((await signIn(deployment, browser, email)).ok, "the fixture member could not sign in");
1028
+ async function connect(deployment, person, clientId, scope = "read", ticked = scope.split(" ")) {
1029
+ const verifier = base64url(randomBytes(32));
1030
+ const request = {
1031
+ client_id: clientId,
1032
+ redirect_uri: REDIRECT,
1033
+ response_type: "code",
1034
+ code_challenge: base64url(createHash("sha256").update(verifier).digest()),
1035
+ code_challenge_method: "S256",
1036
+ state: STATE,
1037
+ scope,
1038
+ resource: `${deployment.origin}/mcp`
1039
+ };
1040
+ const view = await person.api.oauth.describe(request);
1041
+ expect(view.status === "ready", "the consent page refused a good request", view);
1042
+ expect(view.client.registration === "dcr" && view.client.name === NAME && view.scopes.join(" ") === scope && view.loopbackOnly, "the consent page does not show the registered client as it is", view);
1043
+ const { redirect } = await person.api.oauth.decide({
1044
+ request,
1045
+ approve: true,
1046
+ scopes: ticked
1047
+ });
1048
+ const back = new URL(redirect);
1049
+ expect(`${back.origin}${back.pathname}` === REDIRECT, `the answer went to ${back.origin}${back.pathname}, not the redirect`, redirect);
1050
+ expect(back.searchParams.get("state") === STATE && back.searchParams.get("iss") === deployment.origin, "the answer does not carry the state as sent and the issuer", redirect);
1051
+ const code = back.searchParams.get("code");
1052
+ expect(code !== null, "an approval carried no code", redirect);
1070
1053
  return {
1071
- email,
1072
- member: principal,
1073
- browser,
1074
- api: browser.client()
1054
+ code,
1055
+ verifier
1075
1056
  };
1076
1057
  }
1077
- /** The row is refused at use, not only when someone explicitly verifies the log. */
1078
- async function memberTampering(deployment, people, kind) {
1079
- const person = await member(deployment, people, `tamper-${kind}`);
1080
- const { admin } = people;
1081
- const path = kind === "grant" ? PROD : DEV;
1082
- const protectedValues = Object.values((await admin.api.secrets.reveal(path)).values);
1083
- await using(deployment.database(), async (sql) => {
1084
- const [before] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
1085
- const grants = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
1086
- expect(before !== void 0 && grants.length === 1, "the fixture has no member row and grant");
1087
- let restored = before;
1088
- let held = grants;
1089
- if (kind === "stale") {
1090
- await admin.api.access.set(person.member, { [DEV]: "developer" });
1091
- [restored] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
1092
- held = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
1093
- await restoreRow(sql, "vault_members", before, "principal");
1094
- await query(sql, "UPDATE vault_grants SET role = $1, granted_at = $2, granted_by = $3 WHERE principal = $4", [
1095
- grants[0].role,
1096
- grants[0].granted_at,
1097
- grants[0].granted_by,
1098
- person.member
1099
- ]);
1100
- } else if (kind === "member") await query(sql, "UPDATE vault_members SET owner = $1 WHERE principal = $2", [sql.engine === "sqlite" ? 1 : true, person.member]);
1101
- else {
1102
- const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
1103
- WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
1104
- expect(place !== void 0, "no prod environment to forge a grant on");
1105
- await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by) VALUES ($1, $2, 'viewer', $3, $4)`, [
1106
- person.member,
1107
- place.id,
1108
- Date.now(),
1109
- person.member
1110
- ]);
1111
- }
1112
- try {
1113
- const response = await person.browser.send("POST", "/api/reveals", { path });
1114
- const text = await response.text();
1115
- expect(response.status === 401 && JSON.parse(text).error === "unauthenticated", "the tampered credential was not refused", text);
1116
- expect(!protectedValues.some((value) => text.includes(value)), "the tampered refusal carried a value", text);
1117
- const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
1118
- AND subject_principal = $1 AND code = $2`, [person.member, kind === "stale" ? "stale" : "mac"]);
1119
- expect(reports.length > 0, "the tampering was not logged at use", reports);
1120
- expect((await admin.api.members.get(person.member)).status === "tampered", "the refused member is not marked tampered");
1121
- const verified = await admin.api.audit.verify();
1122
- expect(!verified.ok && verified.author === "vault" && verified.failedAtSeq === null, "the tampered member verifies", verified);
1123
- } finally {
1124
- await restoreRow(sql, "vault_members", restored, "principal");
1125
- await query(sql, "DELETE FROM vault_grants WHERE principal = $1", [person.member]);
1126
- for (const grant of held) await insertRow(sql, "vault_grants", grant);
1127
- }
1128
- });
1129
- expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "restoring the member did not restore legitimate use");
1130
- expect((await admin.api.audit.verify()).ok, "the restored member did not verify");
1131
- return `${kind === "stale" ? "a genuine older member row, put back," : `a forged ${kind}`} refused at use as tampered, logged, then restored`;
1132
- }
1133
- /** Access changes have one author, the vault that commits the changed rows. */
1134
- async function accessAuthorship(deployment, people) {
1135
- const person = await member(deployment, people, "access-actions");
1136
- const { admin } = people;
1137
- await admin.api.access.set(person.member, { [DEV]: null });
1138
- await admin.api.members.remove(person.member);
1139
- await admin.api.members.add(person.member);
1140
- await using(deployment.database(), async (sql) => {
1141
- const access = await sql.query(`SELECT author, action, subject_principal FROM audit_log
1142
- WHERE decision = 'allow' AND (action LIKE 'access.%' OR action LIKE 'member.%' OR action LIKE 'directory.%' OR action LIKE 'grant.%' OR action LIKE 'principal.%')`);
1143
- expect(access.length > 0 && access.every((entry) => entry.author === "vault"), "an access change was written by the app", access.filter((entry) => entry.author !== "vault"));
1144
- const actions = access.filter((entry) => entry.subject_principal === person.member).map((entry) => entry.action);
1145
- for (const action of [
1146
- "member.add",
1147
- "member.remove",
1148
- "member.restore",
1149
- "access.grant",
1150
- "access.revoke"
1151
- ]) expect(actions.includes(action), `${action} left no vault entry`, actions);
1152
- });
1153
- return "admission, grant, revoke, removal and re-admission are only vault entries";
1154
- }
1155
- /** A failure to append may not commit an access change. */
1156
- async function noAuditNoAccess(deployment, people) {
1157
- const person = await member(deployment, people, "no-audit");
1158
- const { admin } = people;
1159
- await using(deployment.database(), async (sql) => {
1160
- const snapshot = async () => ({
1161
- members: await sql.query("SELECT * FROM vault_members ORDER BY principal"),
1162
- grants: await sql.query("SELECT * FROM vault_grants ORDER BY principal, project_id, environment_id")
1163
- });
1164
- const before = await snapshot();
1165
- await logRefuses(sql, async () => {
1166
- for (const change of [
1167
- () => admin.api.access.set(person.member, { [DEV]: "developer" }),
1168
- () => admin.api.members.remove(person.member),
1169
- () => admin.api.members.add("user:no-audit-new@conformance.example")
1170
- ]) {
1171
- expect(!await change().then(() => true, () => false), "an access change succeeded while the log refused writes");
1172
- expect(JSON.stringify(await snapshot()) === JSON.stringify(before), "an access change committed without its entry");
1173
- }
1174
- });
1058
+ async function redeem(deployment, clientId, { code, verifier }) {
1059
+ const answer = await token(deployment, {
1060
+ grant_type: "authorization_code",
1061
+ code,
1062
+ code_verifier: verifier,
1063
+ redirect_uri: REDIRECT,
1064
+ client_id: clientId,
1065
+ resource: `${deployment.origin}/mcp`
1175
1066
  });
1176
- expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "legitimate reads did not recover");
1177
- return "grant, removal and admission commit nothing while the log refuses their entries";
1178
- }
1179
- /** Hold the head after the vault checked a member, then forge a grant before it seals them. */
1180
- async function sealingRace(deployment, people) {
1181
- if (deployment.vaultRuntime === null) throw new Skip("SQLite prevents a write beside a decision; the vault suite covers review R2 on Postgres");
1182
- const [login] = await using(deployment.vaultRuntime(), (sql) => sql.query("SELECT current_user AS name"));
1183
- const person = await member(deployment, people, "seal-race");
1184
- const { admin } = people;
1185
- try {
1186
- await using(deployment.database(), (holder) => using(deployment.database(), async (sql) => {
1187
- const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
1188
- WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
1189
- const [head] = await sql.query("SELECT next_seq FROM audit_chain_head");
1190
- const [owner] = await holder.query("SELECT pg_backend_pid() AS pid");
1191
- expect(place !== void 0, "no prod environment for the sealing race");
1192
- await holder.exec("BEGIN");
1193
- let pending;
1194
- try {
1195
- await holder.query("SELECT 1 FROM audit_chain_head FOR UPDATE");
1196
- pending = admin.api.access.set(person.member, { [DEV]: "developer" }).then((value) => ({ value }), (error) => ({ error }));
1197
- await until("the vault decision waiting on our head", async () => {
1198
- return (await query(sql, `SELECT pid FROM pg_stat_activity WHERE datname = current_database()
1199
- AND usename = $1 AND $2 = ANY(pg_blocking_pids(pid))`, [login.name, owner.pid])).length > 0;
1200
- }, 10);
1201
- await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by)
1202
- VALUES ($1, $2, 'viewer', $3, $4)`, [
1203
- person.member,
1204
- place.id,
1205
- Date.now(),
1206
- person.member
1207
- ]);
1208
- } finally {
1209
- await holder.exec("ROLLBACK");
1210
- if (pending !== void 0) await pending;
1211
- }
1212
- expect(pending !== void 0, "the access decision was not started");
1213
- const result = await pending;
1214
- 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);
1215
- const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
1216
- AND subject_principal = $1 AND code = 'mac' AND seq >= $2`, [person.member, head.next_seq]);
1217
- expect(reports.length > 0, "the grant inserted during the decision was not reported", reports);
1218
- const response = await person.browser.send("POST", "/api/reveals", { path: PROD });
1219
- const body = await response.json();
1220
- expect(response.status === 401 && body.error === "unauthenticated", "the forged grant let its member read", body);
1221
- }));
1222
- } finally {
1223
- await admin.api.members.remove(person.member);
1224
- }
1225
- expect((await admin.api.audit.verify()).ok, "the log did not verify after recovering the sealing-race member");
1226
- return "a grant inserted while the vault waited on the head was refused as tampered and logged; removal recovered the member";
1227
- }
1228
- //#endregion
1229
- //#region src/checks/signin.ts
1230
- const hash = (value) => createHash("sha256").update(value).digest();
1231
- async function reported(deployment, id) {
1232
- const event = new RegExp(`auth_row_tampered[^}]*${id}`);
1233
- await until(`the sign-in row failure report for ${id}`, async () => event.test(deployment.output()), 10);
1067
+ const body = await json(answer);
1068
+ expect(answer.status === 200 && typeof body.access_token === "string" && typeof body.refresh_token === "string", `redeeming the code answered ${answer.status}`, body);
1069
+ expect(answer.headers.get("cache-control") === "no-store", "tokens were answered without cache-control: no-store");
1070
+ return body;
1234
1071
  }
1235
- /** Knowing the database password and a chosen token must not mint a credential. */
1236
- async function forgedCredential(deployment, people) {
1237
- const person = await member(deployment, people, "forged-credential");
1238
- const token = `coffre_cli_${randomBytes(32).toString("base64url")}`;
1239
- await using(deployment.database(), async (sql) => {
1240
- const [row] = await query(sql, "SELECT * FROM credentials WHERE principal = $1 AND revoked_at IS NULL LIMIT 1", [person.member]);
1241
- expect(row !== void 0, "no genuine credential to copy");
1242
- const forged = {
1243
- ...row,
1244
- id: randomUUID(),
1245
- kind: "cli",
1246
- token_hash: hash(token),
1247
- identity_id: null
1248
- };
1249
- await insertRow(sql, "credentials", forged);
1250
- try {
1251
- const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${token}` } });
1252
- expect(response.status === 401, "an owner-forged credential authenticated", await response.text());
1253
- await reported(deployment, forged.id);
1254
- } finally {
1255
- await query(sql, "DELETE FROM credentials WHERE id = $1", [forged.id]);
1256
- }
1257
- });
1258
- expect((await person.api.me()).principal.id === person.email, "the genuine credential stopped working");
1259
- return "a chosen token in an owner-inserted credential cannot authenticate, and is reported";
1072
+ async function refused(deployment, what, fields) {
1073
+ const answer = await token(deployment, fields);
1074
+ const body = await json(answer);
1075
+ expect(answer.status === 400 && body.error === "invalid_grant", `${what} answered ${answer.status}, not invalid_grant`, body);
1260
1076
  }
1261
- /** An account binding inserted by the owner must not sign someone in as a member. */
1262
- async function forgedIdentity(deployment, people) {
1263
- const person = await member(deployment, people, "forged-identity");
1264
- const email = "identity-forger@conformance.example";
1265
- const browser = new Browser(deployment.origin);
1266
- await using(deployment.database(), async (sql) => {
1267
- const [row] = await query(sql, "SELECT * FROM identities WHERE principal = $1 AND revoked_at IS NULL LIMIT 1", [person.member]);
1268
- expect(row !== void 0, "no genuine identity to copy");
1269
- const forged = {
1270
- ...row,
1271
- id: randomUUID(),
1272
- subject: String(defaultGitHubAccount(email).id)
1273
- };
1274
- await insertRow(sql, "identities", forged);
1275
- try {
1276
- await signIn(deployment, browser, email).catch(() => {});
1277
- expect((await browser.fetch("/api/me")).status === 401, "an owner-forged identity minted a session");
1278
- await reported(deployment, forged.id);
1279
- } finally {
1280
- await query(sql, "DELETE FROM credentials WHERE identity_id = $1", [forged.id]);
1281
- await query(sql, "DELETE FROM identities WHERE id = $1", [forged.id]);
1282
- }
1283
- });
1284
- expect((await person.api.me()).principal.id === person.email, "the genuine identity stopped working");
1285
- return "an owner-inserted account binding cannot mint a session, and is reported";
1077
+ /** Whether a connection's access token still reaches /mcp. */
1078
+ async function discovers(deployment, access) {
1079
+ const answer = await mcp(deployment, {
1080
+ authorization: `Bearer ${access}`,
1081
+ "mcp-protocol-version": "2026-07-28",
1082
+ "mcp-method": "server/discover"
1083
+ }, "server/discover", { _meta: ENVELOPE });
1084
+ if (answer.status === 401) return false;
1085
+ const body = await json(answer);
1086
+ expect(answer.status === 200 && Array.isArray(body.result?.supportedVersions), `server/discover answered ${answer.status}`, body);
1087
+ return true;
1286
1088
  }
1287
- async function startDevice(deployment) {
1288
- const response = await fetch(`${deployment.origin}/api/auth/device`, {
1089
+ async function mcpConnect(deployment, people) {
1090
+ const { origin } = deployment;
1091
+ const anonymous = await mcp(deployment, {});
1092
+ const challenge = anonymous.headers.get("www-authenticate") ?? "";
1093
+ expect(anonymous.status === 401, `/mcp answered ${anonymous.status} without a token, not 401`);
1094
+ expect(challenge.includes(`resource_metadata="${origin}/.well-known/oauth-protected-resource/mcp"`) && challenge.includes("scope=\"read\""), "the 401's challenge does not name the resource metadata and Read", challenge);
1095
+ const resource = await json(await fetch(`${origin}/.well-known/oauth-protected-resource/mcp`));
1096
+ expect(resource.resource === `${origin}/mcp` && resource.authorization_servers?.[0] === origin, "the protected resource metadata does not name /mcp and coffre as its issuer", resource);
1097
+ const server = await json(await fetch(`${origin}/.well-known/oauth-authorization-server`));
1098
+ expect(server.issuer === origin && server.authorization_endpoint === `${origin}/oauth/authorize`, "the authorization server metadata is wrong", server);
1099
+ for (const [field, value] of [
1100
+ ["code_challenge_methods_supported", "S256"],
1101
+ ["token_endpoint_auth_methods_supported", "none"],
1102
+ ["grant_types_supported", "refresh_token"]
1103
+ ]) expect(server[field]?.includes(value), `the authorization server metadata leaves ${value} out of ${field}`, server);
1104
+ expect(server.client_id_metadata_document_supported === true, "the authorization server metadata does not offer metadata documents", server);
1105
+ const registered = await fetch(`${origin}/api/oauth/register`, {
1289
1106
  method: "POST",
1290
1107
  headers: { "content-type": "application/json" },
1291
- body: "{}"
1108
+ body: JSON.stringify({
1109
+ client_name: NAME,
1110
+ redirect_uris: [REDIRECT, "conformance-app://callback"],
1111
+ token_endpoint_auth_method: "none"
1112
+ })
1292
1113
  });
1293
- expect(response.ok, "the device fixture did not start", await response.clone().text());
1294
- return response.json();
1295
- }
1296
- function pollDevice(deployment, code) {
1297
- return fetch(`${deployment.origin}/api/auth/device/token`, {
1114
+ const client = await json(registered);
1115
+ expect(registered.status === 201 && typeof client.client_id === "string", `registering answered ${registered.status}`, client);
1116
+ expect(JSON.stringify(client.redirect_uris) === JSON.stringify([REDIRECT]), "the registration kept a redirect it should have left out", client);
1117
+ const clientId = client.client_id;
1118
+ const page = await people.reader.browser.fetch(`/oauth/authorize?${new URLSearchParams({
1119
+ client_id: clientId,
1120
+ redirect_uri: REDIRECT
1121
+ })}`);
1122
+ expect(page.status === 200, `the consent page answered ${page.status}`);
1123
+ const forged = await people.reader.browser.fetch("/api/oauth/authorizations", {
1298
1124
  method: "POST",
1299
- headers: { "content-type": "application/json" },
1300
- body: JSON.stringify({ device_code: code })
1125
+ headers: {
1126
+ origin: "https://attacker.example",
1127
+ "sec-fetch-site": "cross-site",
1128
+ "content-type": "application/json"
1129
+ },
1130
+ body: JSON.stringify({
1131
+ request: {
1132
+ client_id: clientId,
1133
+ redirect_uri: REDIRECT
1134
+ },
1135
+ approve: true
1136
+ })
1301
1137
  });
1302
- }
1303
- /** An authentic pending row, changed to approved around the app, grants nothing. */
1304
- async function forgedApproval(deployment, people) {
1305
- const person = await member(deployment, people, "forged-approval");
1306
- const device = await startDevice(deployment);
1307
- await using(deployment.database(), async (sql) => {
1308
- const [row] = await query(sql, "SELECT * FROM device_authorizations WHERE user_code = $1", [device.user_code]);
1309
- const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [person.member]);
1310
- expect(row !== void 0, "no device row to forge");
1311
- await query(sql, `UPDATE device_authorizations SET decision = 'approved', decided_at = $1, principal = $2, generation = $3 WHERE id = $4`, [
1312
- sql.engine === "sqlite" ? Date.now() : /* @__PURE__ */ new Date(),
1313
- person.member,
1314
- standing.generation,
1315
- row.id
1316
- ]);
1317
- try {
1318
- const response = await pollDevice(deployment, device.device_code);
1319
- const body = await response.text();
1320
- expect(!response.ok && !body.includes("access_token"), "an owner-forged approval minted a session", body);
1321
- await reported(deployment, String(row.id));
1322
- } finally {
1323
- await query(sql, "DELETE FROM device_authorizations WHERE id = $1", [row.id]);
1324
- }
1138
+ expect(forged.status === 403, `another site's approval answered ${forged.status}, not 403`);
1139
+ const first = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
1140
+ expect(first.access_token.startsWith("coffre_mcp_") && first.refresh_token.startsWith("coffre_mcr_") && first.scope === "read", "the tokens are not the shapes and scope coffre issues", { scope: first.scope });
1141
+ expect(await discovers(deployment, first.access_token), "a fresh access token was refused at /mcp");
1142
+ const atApi = await fetch(`${origin}/api/me`, { headers: { authorization: `Bearer ${first.access_token}` } });
1143
+ expect(atApi.status === 401, `an MCP token at /api answered ${atApi.status}, not 401`);
1144
+ const cli = await mcp(deployment, { authorization: `Bearer ${people.leaver.cliToken}` });
1145
+ expect(cli.status === 401, `a CLI session at /mcp answered ${cli.status}, not 401`);
1146
+ const browser = await people.reader.browser.fetch("/mcp", {
1147
+ method: "POST",
1148
+ headers: {
1149
+ "content-type": "application/json",
1150
+ origin
1151
+ },
1152
+ body: JSON.stringify({
1153
+ jsonrpc: "2.0",
1154
+ id: 1,
1155
+ method: "server/discover"
1156
+ })
1325
1157
  });
1326
- const control = await startDevice(deployment);
1327
- await person.api.deviceLogins.decide(control.user_code, true);
1328
- expect((await pollDevice(deployment, control.device_code)).ok, "a genuine approval no longer works");
1329
- return "an owner-edited device approval grants nothing and is reported; a genuine approval works";
1158
+ expect(browser.status === 401, `a browser's cookie at /mcp answered ${browser.status}, not 401`);
1159
+ const foreign = await mcp(deployment, {
1160
+ authorization: `Bearer ${first.access_token}`,
1161
+ origin: "https://attacker.example"
1162
+ });
1163
+ expect(foreign.status === 403, `/mcp answered ${foreign.status} to another site's page, not 403`);
1164
+ const rotate = (refresh) => token(deployment, {
1165
+ grant_type: "refresh_token",
1166
+ refresh_token: refresh,
1167
+ client_id: clientId
1168
+ });
1169
+ const rotated = await rotate(first.refresh_token);
1170
+ const next = await json(rotated);
1171
+ expect(rotated.status === 200 && next.refresh_token !== first.refresh_token, `refreshing answered ${rotated.status}, or the same refresh token`);
1172
+ expect(await discovers(deployment, next.access_token), "a refreshed access token was refused");
1173
+ await refused(deployment, "a replaced refresh token", {
1174
+ grant_type: "refresh_token",
1175
+ refresh_token: first.refresh_token,
1176
+ client_id: clientId
1177
+ });
1178
+ await refused(deployment, "the current refresh token, after a reuse", {
1179
+ grant_type: "refresh_token",
1180
+ refresh_token: next.refresh_token,
1181
+ client_id: clientId
1182
+ });
1183
+ expect(!await discovers(deployment, next.access_token), "an access token outlived its connection, ended by a reused refresh token");
1184
+ const asked = await connect(deployment, people.reader, clientId);
1185
+ const second = await redeem(deployment, clientId, asked);
1186
+ await refused(deployment, "a code redeemed already", {
1187
+ grant_type: "authorization_code",
1188
+ code: asked.code,
1189
+ code_verifier: asked.verifier,
1190
+ redirect_uri: REDIRECT,
1191
+ client_id: clientId
1192
+ });
1193
+ expect(!await discovers(deployment, second.access_token), "an access token outlived its connection, ended by a reused code");
1194
+ const third = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
1195
+ const listed = (await people.reader.api.apps.list()).apps;
1196
+ expect(listed.length === 1 && listed[0].name === NAME && listed[0].registration === "dcr", "Connected apps does not list the one live connection", listed);
1197
+ await people.reader.api.apps.disconnect(listed[0].id);
1198
+ expect(!await discovers(deployment, third.access_token), "an access token outlived its disconnection");
1199
+ expect((await people.reader.api.apps.list()).apps.length === 0, "a disconnected app is still listed");
1200
+ const { entries } = await people.admin.api.audit.list({
1201
+ actor: people.reader.member,
1202
+ detail: "1",
1203
+ limit: 500
1204
+ });
1205
+ const mine = entries.filter((entry) => entry.metadata.clientId === clientId);
1206
+ const count = (action) => mine.filter((entry) => entry.action === action && entry.decision === "allow").length;
1207
+ const counts = {
1208
+ connect: count("mcp.connect"),
1209
+ token: count("mcp.token"),
1210
+ disconnect: count("mcp.disconnect")
1211
+ };
1212
+ expect(counts.connect === 3 && counts.token === 4 && counts.disconnect === 3, "the log does not hold each connection, token and disconnection", counts);
1213
+ const reasons = mine.filter((entry) => entry.action === "mcp.disconnect").map((entry) => entry.metadata.reason).sort();
1214
+ expect(JSON.stringify(reasons) === JSON.stringify([
1215
+ "code_reused",
1216
+ "person",
1217
+ "refresh_reused"
1218
+ ]), "the disconnections do not say why", reasons);
1219
+ 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";
1330
1220
  }
1331
- /** Changing an old credential's generation cannot bring its chosen token back. */
1332
- async function editedGeneration(deployment, people) {
1333
- const principal = "token:generation-mac";
1334
- const { admin } = people;
1335
- await admin.api.members.add(principal);
1336
- await admin.api.access.set(principal, { "conformance/dev": "viewer" });
1337
- const issued = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
1338
- await using(deployment.database(), async (sql) => {
1339
- const [old] = await query(sql, "SELECT * FROM credentials WHERE token_hash = $1", [hash(issued.token)]);
1340
- expect(old !== void 0, "no old token row to save");
1341
- await admin.api.members.remove(principal);
1342
- await admin.api.members.add(principal);
1343
- await admin.api.access.set(principal, { "conformance/dev": "viewer" });
1344
- const [current] = await query(sql, "SELECT * FROM credentials WHERE id = $1", [old.id]);
1345
- const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [principal]);
1346
- await restoreRow(sql, "credentials", {
1347
- ...old,
1348
- generation: standing.generation
1349
- }, "id");
1350
- try {
1351
- const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${issued.token}` } });
1352
- expect(response.status === 401, "editing the credential generation revived the token", await response.text());
1353
- await reported(deployment, String(old.id));
1354
- } finally {
1355
- await restoreRow(sql, "credentials", current, "id");
1221
+ /** A 2026-07-28 request as a client sends it: its headers say what its body does; `capabilities` are the client's. */
1222
+ async function modern(deployment, access, method, params = {}, name, capabilities = {}) {
1223
+ const response = await mcp(deployment, {
1224
+ authorization: `Bearer ${access}`,
1225
+ "mcp-protocol-version": "2026-07-28",
1226
+ "mcp-method": method,
1227
+ ...name === void 0 ? {} : { "mcp-name": name }
1228
+ }, method, {
1229
+ ...params,
1230
+ _meta: {
1231
+ ...ENVELOPE,
1232
+ "io.modelcontextprotocol/clientCapabilities": capabilities
1356
1233
  }
1357
1234
  });
1358
- const fresh = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
1359
- expect((await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${fresh.token}` } })).ok, "a newly issued token does not work");
1360
- return "an old token with its generation edited to the current membership is refused and reported";
1235
+ return {
1236
+ response,
1237
+ body: await json(response)
1238
+ };
1361
1239
  }
1362
- //#endregion
1363
- //#region src/checks/access.ts
1364
- async function membersOnly(deployment, { stranger }) {
1365
- const attempt = await signIn(deployment, stranger.browser, stranger.email);
1366
- expect(!attempt.ok, "someone never admitted signed in");
1367
- expect(!stranger.browser.hasCookies, "the refused sign-in left the browser a cookie");
1368
- expect((await stranger.browser.fetch("/api/me")).status === 401, "the refused sign-in left a session");
1369
- await refused$1("a made-up token read secrets", bearer(deployment.origin, "coffre_cli_conformanceforgedtokenconformanceforged").secrets.list(DEV));
1370
- for (const [method, path, body] of [
1371
- [
1372
- "GET",
1373
- `/api/secrets/${DEV}`,
1374
- void 0
1375
- ],
1376
- [
1377
- "POST",
1378
- "/api/reveals",
1379
- { path: DEV }
1380
- ],
1381
- [
1382
- "PATCH",
1383
- `/api/secrets/${DEV}`,
1384
- { API_KEY: "from nobody" }
1385
- ],
1386
- [
1387
- "GET",
1388
- "/api/audit",
1389
- void 0
1390
- ]
1391
- ]) {
1392
- const response = await fetch(`${deployment.origin}${path}`, {
1393
- method,
1394
- headers: {
1395
- origin: deployment.origin,
1396
- "content-type": "application/json"
1397
- },
1398
- body: body === void 0 ? void 0 : JSON.stringify(body)
1399
- });
1400
- expect(response.status === 401, `${method} ${path} answered ${response.status} to no one`);
1401
- }
1402
- return `a stranger's sign-in is ${attempt.ok ? "let" : attempt.error}; no one and a made-up token get 401`;
1240
+ /** A client registered for one check, by the name every check's client has. */
1241
+ async function register(deployment) {
1242
+ return (await json(await fetch(`${deployment.origin}/api/oauth/register`, {
1243
+ method: "POST",
1244
+ headers: { "content-type": "application/json" },
1245
+ body: JSON.stringify({
1246
+ client_name: NAME,
1247
+ redirect_uris: [REDIRECT]
1248
+ })
1249
+ }))).client_id;
1403
1250
  }
1404
- async function grantScoping({ reader, service, bulk }, canaries) {
1405
- const read = await reader.api.secrets.reveal(DEV);
1406
- expect(same(read.values, valuesIn(canaries, DEV)), "a viewer on dev did not get dev's values", read.values);
1407
- await refused$1("a viewer on dev revealed prod", reader.api.secrets.reveal(PROD));
1408
- await refused$1("a viewer on dev listed prod", reader.api.secrets.list(PROD));
1409
- await refused$1("a viewer wrote a value", reader.api.secrets.set(DEV, { API_KEY: "from the reader" }));
1410
- await refused$1("a viewer granted themselves more", reader.api.access.set(reader.member, { [PROD]: "owner" }));
1411
- await refused$1("a viewer added a member", reader.api.members.add("user:friend@conformance.example"));
1412
- await refused$1("a viewer issued a service token", reader.api.tokens.issue(service.member, { expiresInDays: 1 }));
1413
- const places = (await reader.api.me()).environments.map((place) => `${place.project}/${place.environment}`);
1414
- expect(places.length === 1 && places[0] === DEV, "a viewer on dev is told of other places", places);
1415
- const served = await service.api.secrets.reveal(DEV);
1416
- expect(same(served.values, valuesIn(canaries, DEV)), "a service viewer on dev did not get dev's values", served.values);
1417
- await refused$1("a service viewer on dev revealed prod", service.api.secrets.reveal(PROD));
1418
- await refused$1("a viewer on bulk revealed dev", bulk.api.secrets.reveal(DEV));
1419
- return "a viewer on dev reads dev and nothing else, in a browser or with a token, and changes nothing";
1251
+ /** A client registered and connected as the person, with Read, for a check of another's: its access token. */
1252
+ async function connectedApp(deployment, person) {
1253
+ const clientId = await register(deployment);
1254
+ return (await redeem(deployment, clientId, await connect(deployment, person, clientId))).access_token;
1420
1255
  }
1421
1256
  /**
1422
- * A change sent with the session cookie from another site's page, or from
1423
- * no page coffre can vouch for, is refused (CSRF), and so is a reveal.
1257
+ * The Read tools, as a client calls them (design section 14, checks 1, 4
1258
+ * and 5): on 2026-07-28 and through a 2025-11-25 `initialize`, as the
1259
+ * reader, who sees dev and not prod. No value appears in any answer, the
1260
+ * headers must agree with the body, and every call is in the log under the
1261
+ * client, the API's own entries naming the connection.
1424
1262
  */
1425
- async function crossSite({ admin }, canaries) {
1426
- for (const headers of [
1427
- {
1428
- origin: "https://attacker.example",
1429
- "sec-fetch-site": "cross-site"
1430
- },
1431
- { origin: "https://attacker.example" },
1432
- { "sec-fetch-site": "same-site" },
1433
- {}
1434
- ]) for (const [method, path, body] of [
1435
- [
1436
- "PATCH",
1437
- `/api/secrets/${DEV}`,
1438
- { API_KEY: "from another site" }
1439
- ],
1440
- [
1441
- "POST",
1442
- "/api/reveals",
1443
- { path: DEV }
1444
- ],
1445
- [
1446
- "PUT",
1447
- "/api/members/user:mallory@attacker.example",
1448
- {}
1449
- ],
1450
- [
1451
- "POST",
1452
- "/auth/signout",
1453
- void 0
1454
- ]
1455
- ]) {
1456
- const response = await admin.browser.fetch(path, {
1457
- method,
1458
- headers: {
1459
- ...headers,
1460
- "content-type": "application/json"
1461
- },
1462
- body: body === void 0 ? void 0 : JSON.stringify(body)
1463
- });
1464
- const text = await response.text();
1465
- expect(response.status === 403, `${method} ${path} with ${JSON.stringify(headers)} answered ${response.status}`, text);
1466
- expect(!Object.values(canaries).some((value) => text.includes(value)), `${method} ${path} from another site carried a value`);
1467
- }
1468
- const after = await admin.api.secrets.reveal(DEV);
1469
- expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`], "a value changed from another site", after.values);
1470
- expect(!(await admin.api.members.list()).members.some((entry) => entry.member.includes("mallory")), "a member was added from another site");
1471
- return "writes, reveals and sign-out with the cookie, from another site or no page at all: 403";
1472
- }
1473
- async function offboarding(deployment, { admin, leaver, service }, canaries) {
1474
- const device = await startDevice(deployment);
1475
- await leaver.api.deviceLogins.decide(device.user_code, true);
1476
- await leaver.cli.secrets.reveal(DEV);
1477
- const { report } = await admin.api.members.remove(leaver.member);
1478
- const exposed = report.exposed.map((entry) => `${entry.project}/${entry.environment}/${entry.key}`).sort();
1479
- expect(same(exposed, Object.keys(valuesIn(canaries, DEV)).map((key) => `${DEV}/${key}`).sort()), "the offboarding report does not name what they read", report.exposed);
1480
- expect((await leaver.browser.fetch("/api/me")).status === 401, "a removed member's browser session still works");
1481
- await refused$1("a removed member's CLI session read secrets", leaver.cli.secrets.list(DEV));
1482
- expect(!(await signIn(deployment, leaver.browser, leaver.email)).ok, "a removed member signed in again");
1483
- await admin.api.members.remove(service.member);
1484
- await refused$1("a removed service's token read secrets", service.api.secrets.reveal(DEV));
1485
- await admin.api.members.add(leaver.member);
1486
- await admin.api.access.set(leaver.member, { [DEV]: "developer" });
1487
- await admin.api.members.add(service.member);
1488
- await admin.api.access.set(service.member, { [DEV]: "viewer" });
1489
- expect((await leaver.browser.fetch("/api/me")).status === 401, "re-admission revived the browser session");
1490
- await refused$1("re-admission revived the CLI session", leaver.cli.secrets.reveal(DEV));
1491
- await refused$1("re-admission revived the service token", service.api.secrets.reveal(DEV));
1492
- expect(!(await pollDevice(deployment, device.device_code)).ok, "re-admission revived the old device approval");
1493
- expect((await signIn(deployment, leaver.browser, leaver.email)).ok, "a fresh provider callback could not sign the re-admitted member in");
1494
- await admin.api.members.remove(leaver.member);
1495
- await admin.api.members.remove(service.member);
1496
- return `the report names ${exposed.length} values; re-admission revives no old session, token or approval; a fresh provider callback works`;
1497
- }
1498
- /**
1499
- * Grants on every project: one on `*` reaches a project made after it; one
1500
- * on `*` and a slug reaches that environment in every project and never
1501
- * another; nobody but an owner gives one; and removing a member takes
1502
- * them. Its services are removed, and its project archived, as it ends.
1503
- */
1504
- async function everyProject(deployment, { admin, reader }, canaries) {
1505
- const services = {
1506
- everything: "token:conformance-everywhere",
1507
- dev: "token:conformance-every-dev",
1508
- manager: "token:conformance-manager"
1263
+ async function mcpRead(deployment, people, canaries) {
1264
+ const clientId = await register(deployment);
1265
+ const { access_token: access } = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
1266
+ const answers = [];
1267
+ const discovered = await modern(deployment, access, "server/discover");
1268
+ answers.push(JSON.stringify(discovered.body));
1269
+ expect(discovered.response.status === 200, `server/discover answered ${discovered.response.status}`, discovered.body);
1270
+ const listed = await modern(deployment, access, "tools/list");
1271
+ const result = listed.body.result;
1272
+ const names = result?.tools?.map((tool) => tool.name) ?? [];
1273
+ expect(names.includes("list_secrets") && names.includes("run_with_secrets") && result?.cacheScope === "public" && typeof result.ttlMs === "number", "tools/list does not list the Read tools, cacheable", listed.body);
1274
+ const callTool = async (tool, args) => {
1275
+ const { response, body } = await modern(deployment, access, "tools/call", {
1276
+ name: tool,
1277
+ arguments: args
1278
+ }, tool);
1279
+ answers.push(JSON.stringify(body));
1280
+ expect(response.status === 200, `tools/call ${tool} answered ${response.status}`, body);
1281
+ return body.result;
1509
1282
  };
1510
- const client = async (member) => bearer(deployment.origin, (await admin.api.tokens.issue(member, { expiresInDays: 1 })).token);
1511
- for (const member of Object.values(services)) await admin.api.members.add(member);
1512
- await admin.api.access.set(services.manager, { [PROJECT$1]: "access-manager" });
1513
- await refused$1("a project's access manager granted on every project", (await client(services.manager)).access.set(services.everything, { "*": "viewer" }));
1514
- await refused$1("a viewer granted themselves every project", reader.api.access.set(reader.member, { "*/prod": "viewer" }));
1515
- await admin.api.access.set(services.everything, { "*": "viewer" });
1516
- await admin.api.access.set(services.dev, { "*/dev": "viewer" });
1517
- const [everything, dev] = [await client(services.everything), await client(services.dev)];
1518
- const later = `${PROJECT$1}-later`;
1519
- const made = await admin.api.projects.create(later, { name: "Made later" });
1520
- expect(made.inherited.some((grant) => grant.member === services.everything && grant.place === "*"), "making a project did not say who reaches it", made.inherited);
1521
- for (const environment of ["dev", "prod"]) {
1522
- const path = `${later}/${environment}`;
1523
- const { inherited } = await admin.api.environments.create(path, { name: environment });
1524
- const reaches = inherited.filter((grant) => grant.member === services.dev).length;
1525
- expect(reaches === (environment === "dev" ? 1 : 0), `making ${path} misnamed who reaches it through dev in every project`, inherited);
1526
- canaries[`${path}/API_KEY`] = canary();
1527
- await admin.api.secrets.set(path, valuesIn(canaries, path));
1528
- }
1529
- for (const path of [
1530
- `${later}/prod`,
1531
- `${later}/dev`,
1532
- PROD
1533
- ]) {
1534
- const read = await everything.secrets.reveal(path);
1535
- expect(read.values.API_KEY === canaries[`${path}/API_KEY`], `viewer on every project could not read ${path}`, read.values);
1536
- }
1537
- await refused$1("viewer on every project wrote a value", everything.secrets.set(`${later}/prod`, { API_KEY: "from everywhere" }));
1538
- const devRead = await dev.secrets.reveal(`${later}/dev`);
1539
- expect(devRead.values.API_KEY === canaries[`${later}/dev/API_KEY`], "viewer on dev in every project could not read a dev made later", devRead.values);
1540
- await refused$1("viewer on dev in every project read a prod made later", dev.secrets.reveal(`${later}/prod`));
1541
- await refused$1("viewer on dev in every project read prod", dev.secrets.reveal(PROD));
1542
- for (const member of Object.values(services)) await admin.api.members.remove(member);
1543
- await refused$1("a removed member read through a grant on every project", everything.secrets.reveal(`${later}/prod`));
1544
- await refused$1("a removed member read through a grant on dev in every project", dev.secrets.reveal(`${later}/dev`));
1545
- const left = (await admin.api.members.list("*")).members.filter((member) => Object.values(services).includes(member.member));
1546
- expect(left.length === 0, "grants on every project outlived their members", left);
1547
- await admin.api.projects.update(later, { archived: true });
1548
- return "a grant on every project reaches a project made after it; one on dev, every dev and nothing else; refused to non-owners; gone with its member";
1549
- }
1550
- /** Reading more values at once than the vault allows in its window is refused. */
1551
- async function bulkLimit$1({ admin, bulk }, limit) {
1552
- const values = {};
1553
- for (let i = 0; i <= limit; i++) values[`BULK_${i}`] = `bulk-${i}`;
1554
- await admin.api.secrets.set(BULK, values);
1555
- const refusal = await refused$1(`${limit + 1} values were revealed at once`, bulk.api.secrets.reveal(BULK));
1556
- expect(refusal.status === 403 && refusal.code === "bulk_limit" && refusal.reason === "bulk_limit", "the bulk read was not refused as bulk_limit", refusal);
1557
- const one = await bulk.api.secrets.reveal(`${BULK}/BULK_0`);
1558
- expect(one.values.BULK_0 === "bulk-0", "a viewer on bulk could not read one of its values", one.values);
1559
- return `${limit + 1} values at once: ${refusal.status} ${refusal.reason ?? refusal.code}; one still opens`;
1560
- }
1561
- function same(a, b) {
1562
- return JSON.stringify(sorted(a)) === JSON.stringify(sorted(b));
1563
- }
1564
- function sorted(value) {
1565
- if (Array.isArray(value) || value === null || typeof value !== "object") return value;
1566
- return Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : 1));
1567
- }
1568
- //#endregion
1569
- //#region src/checks/audit.ts
1570
- /** One read per returned value, committed by the vault under the response's operation. */
1571
- async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
1572
- const path = purpose === "reveal" ? `${DEV}/API_KEY` : DEV;
1573
- const { operationId, values } = await reader.api.secrets.reveal(path);
1574
- expect(typeof operationId === "string" && operationId.length > 0, "the read returned no operation id");
1575
- const expected = purpose === "reveal" ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
1576
- expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), "the read returned different values", values);
1577
- const bundle = (await everyAuditEntry(admin.api)).filter((entry) => entry.operationId === operationId);
1578
- const keys = bundle.map((entry) => entry.key).sort();
1579
- expect(JSON.stringify(keys) === JSON.stringify(Object.keys(expected).sort()), "the read is not logged once per value", bundle);
1580
- expect(bundle.every((entry) => entry.author === "vault" && entry.action === "secret.read" && entry.decision === "allow"), "the read's entries are not the vault's allowed reads", bundle);
1581
- expect(bundle.every((entry) => entry.metadata.purpose === purpose && entry.requestId !== null && entry.requestId === bundle[0].requestId), "the reads do not name one request and its purpose", bundle);
1582
- const { keys: listed } = await admin.api.secrets.list(DEV);
1583
- for (const entry of bundle) expect(entry.version === listed.find((key) => key.key === entry.key)?.version, `the entry for ${String(entry.key)} names another version`, entry);
1584
- await using(deployment.database(), async (sql) => {
1585
- const rows = await query(sql, `SELECT l.author, l.action, l.decision, l.request_id, l.metadata, s.key, s.current_version
1586
- FROM audit_log l LEFT JOIN secrets s ON s.id = l.secret_id WHERE l.operation_id = $1`, [operationId]);
1587
- expect(JSON.stringify(rows.map((row) => row.key).sort()) === JSON.stringify(Object.keys(expected).sort()), "the committed read is not once per value", rows);
1588
- for (const row of rows) {
1589
- const metadata = JSON.parse(row.metadata);
1590
- expect(row.author === "vault" && row.action === "secret.read" && row.decision === "allow", "the committed read is not the vault release", row);
1591
- expect(row.request_id !== null && row.request_id === rows[0].request_id && metadata.purpose === purpose, "the committed reads have different requests or purposes", rows);
1592
- expect(metadata.version === row.current_version, "the committed read names another version", row);
1593
- }
1283
+ const keys = ((await callTool("list_secrets", { environment: DEV })).structuredContent?.keys ?? []).map((key) => key.key);
1284
+ expect(["API_KEY", "DATABASE_URL"].every((key) => keys.includes(key)), `list_secrets on ${DEV} does not list its keys`, keys);
1285
+ const prod = await callTool("list_secrets", { environment: PROD });
1286
+ expect(prod.isError === true, `the reader listed ${PROD}'s keys through MCP, which they cannot`, prod);
1287
+ const run = await callTool("run_with_secrets", {
1288
+ environment: DEV,
1289
+ command: "npm test"
1594
1290
  });
1595
- return `${bundle.length} values, each read once by the vault for ${purpose}, under ${operationId}`;
1596
- }
1597
- /** Full verification must reach the stored head, not just some intact prefix. */
1598
- async function verification(deployment, { admin }) {
1599
- const result = await admin.api.audit.verify();
1600
- expect(result.ok, "the full log does not verify", result);
1601
- await using(deployment.database(), async (sql) => {
1602
- const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
1603
- const [last] = await sql.query("SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1");
1604
- expect(head !== void 0 && last !== void 0, "the head or newest entry is missing");
1605
- expect(BigInt(head.next_seq) === BigInt(last.seq) + 1n, "the head does not name the newest entry");
1606
- expect(Buffer.from(head.head_hash).equals(Buffer.from(last.hash)), "the head has another hash");
1607
- expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), "verification stopped before the head", {
1608
- result,
1609
- head: head.next_seq
1610
- });
1611
- expect(BigInt(result.entries) === BigInt(head.next_seq), "verification did not count every entry", result);
1291
+ expect(run.content?.[0]?.text?.includes(`coffre run ${DEV} -- npm test`) === true, "run_with_secrets does not say how to run the command", run);
1292
+ for (const tool of ["whoami", "list_projects"]) await callTool(tool, {});
1293
+ await callTool("secret_history", { secret: `${DEV}/API_KEY` });
1294
+ const mismatched = await mcp(deployment, {
1295
+ authorization: `Bearer ${access}`,
1296
+ "mcp-protocol-version": "2026-07-28",
1297
+ "mcp-method": "tools/call",
1298
+ "mcp-name": "list_projects"
1299
+ }, "tools/call", {
1300
+ name: "whoami",
1301
+ arguments: {},
1302
+ _meta: ENVELOPE
1612
1303
  });
1613
- return `${result.entries} entries verified through the head at ${result.through}`;
1614
- }
1615
- /**
1616
- * Each heartbeat has the vault sign the log up to its last entry, in an
1617
- * entry of its own, after checking that the prefix it signed last is still
1618
- * there. Two, so the second covers the first; then the log is verified from
1619
- * its first entry, every checkpoint with it.
1620
- */
1621
- async function checkpoints(deployment, { admin }) {
1622
- const before = await admin.api.audit.verify();
1623
- expect(before.ok, "the log does not verify", before);
1624
- await deployment.scheduled();
1625
- await deployment.scheduled();
1626
- const after = await admin.api.audit.verify();
1627
- expect(after.ok && after.checkpoint !== null, "the audit log is not verified and checkpointed", after);
1628
- expect(after.checkpoint.seq > (before.through ?? -1), "the checkpoint does not cover what was written before it", {
1629
- before,
1630
- after
1304
+ const mismatch = await json(mismatched);
1305
+ expect(mismatched.status === 400 && mismatch.error?.code === -32020, `a Mcp-Name naming another tool answered ${mismatched.status}`, mismatch);
1306
+ const initialized = await mcp(deployment, { authorization: `Bearer ${access}` }, "initialize", {
1307
+ protocolVersion: "2025-11-25",
1308
+ capabilities: {},
1309
+ clientInfo: {
1310
+ name: "coffre-conformance",
1311
+ version: "1"
1312
+ }
1631
1313
  });
1632
- const { entries } = await admin.api.audit.list({
1314
+ const init = await json(initialized);
1315
+ expect(initialized.status === 200 && init.result?.protocolVersion === "2025-11-25", `initialize for 2025-11-25 answered ${initialized.status}`, init);
1316
+ expect(initialized.headers.get("mcp-session-id") === null, "initialize minted a session: coffre keeps none");
1317
+ const legacy = await mcp(deployment, {
1318
+ authorization: `Bearer ${access}`,
1319
+ "mcp-protocol-version": "2025-11-25"
1320
+ }, "tools/call", {
1321
+ name: "list_secrets",
1322
+ arguments: { environment: DEV }
1323
+ });
1324
+ const legacyBody = await json(legacy);
1325
+ answers.push(JSON.stringify(legacyBody));
1326
+ expect(legacy.status === 200 && legacyBody.result?.isError !== true, `a 2025-11-25 tools/call answered ${legacy.status}`, legacyBody);
1327
+ const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1328
+ expect(leaked.length === 0, "a Read tool answered with a secret value", leaked);
1329
+ const { entries } = await people.admin.api.audit.list({
1330
+ actor: people.reader.member,
1633
1331
  detail: "1",
1634
- limit: 20
1332
+ limit: 500
1635
1333
  });
1636
- const signed = entries.filter((entry) => entry.action === "audit.checkpoint" && entry.decision === "allow");
1637
- expect(signed.length >= 2 && signed.every((entry) => entry.author === "vault" && entry.detail), "the checkpoints are not the vault's entries, hidden as detail", signed);
1638
- const [newest, previous] = signed;
1639
- expect(newest.metadata.seq === after.checkpoint.seq, "the newest checkpoint entry is not the one verified", {
1640
- signed,
1641
- after
1334
+ const mine = entries.filter((entry) => entry.metadata.via?.clientId === clientId);
1335
+ const reads = mine.filter((entry) => entry.action === "mcp.read").length;
1336
+ const refused = mine.filter((entry) => entry.action === "mcp.call" && entry.decision === "deny").length;
1337
+ expect(reads === 6 && refused === 1, "the log does not hold each call under the client", {
1338
+ reads,
1339
+ refused
1642
1340
  });
1643
- expect(previous.seq <= after.checkpoint.seq, "the newest checkpoint does not cover the one before it", signed);
1644
- return `${after.entries} entries verified through ${after.through}, checkpointed at ${after.checkpoint.seq}`;
1341
+ 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));
1342
+ const shown = (await people.admin.api.audit.list({
1343
+ actor: people.reader.member,
1344
+ limit: 500
1345
+ })).entries;
1346
+ expect(!shown.some((entry) => entry.action === "mcp.read"), "read-only calls are not detail");
1347
+ 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`;
1645
1348
  }
1646
1349
  /**
1647
- * Every version the app says it stored names the vault's entry for the key
1648
- * it was sealed with, `key.wrap` for a write and `key.rewrap` for a restore:
1649
- * the same member, request and operation, the same secret and version. And
1650
- * every value read is the vault's to log: the app keeps no reads of its own.
1350
+ * Changes, as a client asks for them (design section 14, checks 2 and 3):
1351
+ * Read cannot ask at all, and with Write nothing changes until the person
1352
+ * approves on coffre's page. A client without URL elicitation gets the link
1353
+ * and calls again; one with it is asked to open the page, and retries with
1354
+ * its requestState, which no other call may use. Only the person decides,
1355
+ * with the digest the page showed; the page makes the change, once, and its
1356
+ * entries name the client and the approval. A value comes from the page.
1651
1357
  */
1652
- async function writesAgree(deployment) {
1653
- return using(deployment.database(), async (sql) => {
1654
- const entries = await sql.query("SELECT * FROM audit_log ORDER BY seq");
1655
- const bySeq = new Map(entries.map((entry) => [String(entry.seq), entry]));
1656
- const writes = entries.filter((entry) => entry.author === "app" && entry.decision === "allow" && (entry.action === "secret.write" || entry.action === "secret.restore"));
1657
- 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);
1658
- for (const entry of writes) {
1659
- const seal = entry.action === "secret.write" ? "key.wrap" : "key.rewrap";
1660
- const key = entry.related_seq === null ? void 0 : bySeq.get(String(entry.related_seq));
1661
- expect(key !== void 0 && key.author === "vault" && key.action === seal && key.decision === "allow", `a ${entry.action} names no ${seal} of the vault's`, {
1662
- entry,
1663
- key
1664
- });
1665
- for (const field of [
1666
- "actor",
1667
- "request_id",
1668
- "operation_id",
1669
- "project_id",
1670
- "environment_id"
1671
- ]) expect(entry[field] === key[field], `the write and its seal have different ${field}`, {
1672
- entry,
1673
- key
1674
- });
1675
- expect(entry.operation_id !== null && entry.request_id !== null, "the committed write has no operation or request", entry);
1676
- const metadata = JSON.parse(entry.metadata);
1677
- const sealed = JSON.parse(key.metadata);
1678
- 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", {
1679
- entry,
1680
- key
1681
- });
1682
- }
1683
- const versions = await query(sql, `SELECT v.secret_id, v.version FROM secret_versions v JOIN secrets s ON s.id = v.secret_id
1684
- JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT$1]);
1685
- expect(versions.length > 0, "no stored versions were inspected");
1686
- for (const version of versions) {
1687
- const matching = writes.filter((entry) => entry.secret_id === version.secret_id && JSON.parse(entry.metadata).version === version.version);
1688
- expect(matching.length === 1, "a stored version has no unique app write entry", {
1689
- version,
1690
- matching
1691
- });
1692
- }
1693
- return `${versions.length} stored versions, each with one app write naming the vault's seal in the same operation`;
1694
- });
1695
- }
1696
- /**
1697
- * No value leaves without its entry: with the audit log refusing writes (a
1698
- * trigger, as a full disk or a lost connection would), the vault cannot log
1699
- * the read, so a reveal fails and carries nothing.
1700
- */
1701
- async function noAuditNoValue(deployment, { admin }, canaries) {
1702
- let status = 0;
1703
- await using(deployment.database(), (sql) => logRefuses(sql, async () => {
1704
- const before = deployment.output().split(LOG_REFUSAL).length;
1705
- const response = await admin.browser.send("POST", "/api/reveals", { path: DEV });
1706
- const text = await response.text();
1707
- status = response.status;
1708
- const body = JSON.parse(text);
1709
- expect(status === 500 && body.error === "internal_error" && body.reason === void 0, "the unlogged reveal did not fail as internal_error", {
1710
- status,
1711
- body
1712
- });
1713
- expect(body.message === "something went wrong; see the server log", "the audit failure exposed another reason", body);
1714
- expect(!Object.values(canaries).some((value) => text.includes(value)), "a reveal that could not be logged carried a value", text);
1715
- await until("the injected audit refusal in the processes' output", async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
1716
- }, "secret.read"));
1717
- 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");
1718
- return `a reveal the audit log would not take: ${status} internal_error, its injected cause in the log, and no value`;
1719
- }
1720
- /** A publicly chained entry still needs the vault's MAC. */
1721
- async function forgedVaultEntry(deployment, { admin }) {
1722
- const verify = () => admin.api.audit.verify();
1723
- expect((await verify()).ok, "the log does not verify before the forged entry");
1724
- await using(deployment.database(), async (sql) => {
1725
- const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
1726
- const [writer] = await sql.query("SELECT key_id FROM audit_log WHERE author = 'vault' ORDER BY seq DESC LIMIT 1");
1727
- expect(writer !== void 0, "no vault key id to copy");
1728
- const seq = BigInt(head.next_seq);
1729
- const forged = {
1730
- seq,
1731
- author: "vault",
1732
- key_id: writer.key_id,
1733
- occurred_at: Date.now(),
1734
- actor: "user:forger@conformance.example",
1735
- action: "secret.read",
1736
- decision: "allow",
1737
- metadata: "{}"
1358
+ async function mcpChanges(deployment, people, canaries) {
1359
+ const { admin } = people;
1360
+ const place = `${PROJECT$1}-mcp/dev`;
1361
+ await admin.api.projects.create(`${PROJECT$1}-mcp`, { name: "MCP changes" });
1362
+ await admin.api.environments.create(place, { name: "Development" });
1363
+ const values = {
1364
+ KEEP: canary(),
1365
+ OLD: canary()
1366
+ };
1367
+ for (const [key, value] of Object.entries(values)) canaries[`${place}/${key}`] = value;
1368
+ await admin.api.secrets.set(place, values);
1369
+ const changer = await personaOn(deployment, admin, "changer", { [`${PROJECT$1}-mcp`]: "maintainer" });
1370
+ const clientId = await register(deployment);
1371
+ const answers = [];
1372
+ const call = async (access, tool, args, more = {}, capabilities = {}) => {
1373
+ const { response, body } = await modern(deployment, access, "tools/call", {
1374
+ name: tool,
1375
+ arguments: args,
1376
+ ...more
1377
+ }, tool, capabilities);
1378
+ answers.push(JSON.stringify(body));
1379
+ return {
1380
+ status: response.status,
1381
+ challenge: response.headers.get("www-authenticate") ?? "",
1382
+ result: body.result,
1383
+ error: body.error
1738
1384
  };
1739
- const prevHash = Buffer.from(head.head_hash);
1740
- const mac = Buffer.alloc(32, 65);
1741
- const hash = createHash("sha256").update("coffre.audit.chain.v2").update(prevHash).update(entryFields(forged)).update(mac).digest();
1742
- await insertRow(sql, "audit_log", {
1743
- ...forged,
1744
- prev_hash: prevHash,
1745
- mac,
1746
- hash
1747
- });
1748
- await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq + 1n, hash]);
1749
- let inserted;
1750
- try {
1751
- inserted = await verify();
1752
- } finally {
1753
- await appendOnlyLifted(sql, () => query(sql, "DELETE FROM audit_log WHERE seq = $1", [seq]));
1754
- await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq, prevHash]);
1755
- }
1756
- 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);
1757
- const removed = await verify();
1758
- expect(removed.ok, "the log did not verify once the entry was gone", removed);
1385
+ };
1386
+ const archived = async (key) => (await admin.api.secrets.history(`${place}/${key}`)).archived;
1387
+ const reading = (await redeem(deployment, clientId, await connect(deployment, changer, clientId))).access_token;
1388
+ const stepUp = await call(reading, "archive_secret", { secret: `${place}/OLD` });
1389
+ expect(stepUp.status === 403 && stepUp.challenge.includes("error=\"insufficient_scope\"") && stepUp.challenge.includes("scope=\"read write\""), `archive_secret with Read answered ${stepUp.status}, not a step-up to Write`, stepUp.challenge);
1390
+ expect(stepUp.result?.isError === true && stepUp.result.content?.[0]?.text?.includes("ticking Write on coffre's consent page") === true, "archive_secret with Read does not say, as its result, how to grant Write", stepUp.result);
1391
+ expect(!await archived("OLD"), "archive_secret with Read archived the secret");
1392
+ const granted = await redeem(deployment, clientId, await connect(deployment, changer, clientId, "read", ["write"]));
1393
+ expect(granted.scope === "read write", `a connection ticked Write answered scope "${granted.scope}", not "read write"`, granted.scope);
1394
+ const write = granted.access_token;
1395
+ const linked = await call(write, "archive_secret", { secret: `${place}/OLD` });
1396
+ const approval = linked.result?.structuredContent?.approval;
1397
+ expect(linked.status === 200 && linked.result?.structuredContent?.status === "pending" && approval?.url === `${deployment.origin}/approvals/${approval?.id}`, "a change without URL elicitation was not answered with its approval link", linked.result);
1398
+ expect(linked.result?.content?.[0]?.text?.includes(approval.url) === true, "the link is not in the text the model reads", linked.result);
1399
+ expect(!await archived("OLD"), "a change was made before its approval");
1400
+ const theirs = await people.reader.api.approvals.get(approval.id).then(() => null, (error) => error);
1401
+ expect(theirs instanceof CoffreError && theirs.status === 403, "another person opened someone else's approval", theirs);
1402
+ const page = await changer.browser.fetch(`/approvals/${approval.id}`);
1403
+ expect(page.status === 200, `the approval page answered ${page.status}`);
1404
+ const shown = (await changer.api.approvals.get(approval.id)).approval;
1405
+ expect(shown.status === "pending" && shown.summary === `archive ${place}/OLD` && shown.client.name === NAME, "the approval page does not show the change asked for", shown);
1406
+ const forgedDigest = await changer.api.approvals.decide(approval.id, {
1407
+ approve: true,
1408
+ digest: "0".repeat(64)
1409
+ }).then(() => null, (error) => error);
1410
+ expect(forgedDigest instanceof CoffreError && forgedDigest.status === 409, "a decision on a change the page did not show was taken", forgedDigest);
1411
+ const forgedSite = await changer.browser.fetch(`/api/approvals/${approval.id}`, {
1412
+ method: "POST",
1413
+ headers: {
1414
+ origin: "https://attacker.example",
1415
+ "sec-fetch-site": "cross-site",
1416
+ "content-type": "application/json"
1417
+ },
1418
+ body: JSON.stringify({
1419
+ approve: true,
1420
+ digest: shown.digest
1421
+ })
1759
1422
  });
1760
- return "a publicly chained vault entry with a made-up MAC was refused at its sequence";
1761
- }
1762
- /** Rewrite either author's entry and require a fault at that exact sequence. */
1763
- async function rewrittenEntry(deployment, { admin }, author) {
1764
- expect((await admin.api.audit.verify()).ok, "the log does not verify before the rewrite");
1765
- let seq = 0;
1766
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
1767
- const [row] = await query(sql, "SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1", [author]);
1768
- expect(row !== void 0, `no ${author} entry to rewrite`);
1769
- seq = Number(row.seq);
1770
- await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", ["user:nobody@conformance.example", row.seq]);
1771
- try {
1772
- const result = await admin.api.audit.verify();
1773
- expect(!result.ok && result.failedAtSeq === Number(row.seq), "a rewritten entry was not caught at its sequence", result);
1774
- } finally {
1775
- await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", [row.actor, row.seq]);
1776
- }
1777
- }));
1778
- expect((await admin.api.audit.verify()).ok, "the restored entry did not verify");
1779
- return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
1780
- }
1781
- /** Remove an unreferenced entry, then put its exact bytes back. */
1782
- async function missingEntry(deployment, { admin }, position) {
1783
- expect((await admin.api.audit.verify()).ok, "the log does not verify before deletion");
1784
- let seq = 0;
1785
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
1786
- 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`);
1787
- expect(row !== void 0, "no unreferenced entry to delete");
1788
- seq = Number(row.seq);
1789
- const linked = await query(sql, "SELECT * FROM audit_log WHERE related_seq = $1", [row.seq]);
1790
- await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE related_seq = $1", [row.seq]);
1791
- await query(sql, "DELETE FROM audit_log WHERE seq = $1", [row.seq]);
1792
- try {
1793
- const result = await admin.api.audit.verify();
1794
- expect(!result.ok, "a log with an entry missing verifies", result);
1795
- if (position !== "middle") expect(result.reason.includes(`sequence gap: expected seq ${position === "first" ? 0 : 1e3}`), "the sequence gap was not reported", result);
1796
- } finally {
1797
- await insertRow(sql, "audit_log", row);
1798
- for (const entry of linked) await query(sql, "UPDATE audit_log SET related_seq = $1 WHERE seq = $2", [entry.related_seq, entry.seq]);
1799
- }
1800
- }));
1801
- expect((await admin.api.audit.verify()).ok, "the restored log did not verify");
1802
- const which = {
1803
- first: "the first",
1804
- batch: "the first past a page of 1,000",
1805
- middle: "a heartbeat"
1806
- }[position];
1807
- return `entry ${seq}, ${which}, deleted: caught, then put back`;
1808
- }
1809
- /** Last: keep the head, delete its newest entries, and require verification to notice. */
1810
- async function deletedTail(deployment, { admin }) {
1811
- const intact = await admin.api.audit.verify();
1812
- expect(intact.ok && intact.checkpoint !== null, "the log is not intact and checkpointed");
1813
- const signed = intact.checkpoint.seq;
1814
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
1815
- await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE seq >= $1", [signed]);
1816
- await query(sql, "DELETE FROM audit_log WHERE seq >= $1", [signed]);
1817
- }));
1818
- const result = await admin.api.audit.verify();
1819
- expect(!result.ok && result.author === "app" && result.reason.includes("removed from the end"), "the log verifies with its newest entries deleted", result);
1820
- return "the newest entries deleted, with the head left alone, were caught";
1821
- }
1822
- /** The whole log, detail included, newest first. */
1823
- async function everyAuditEntry(api) {
1824
- const all = [];
1825
- for (let before;;) {
1826
- const { entries } = await api.audit.list({
1827
- before,
1828
- limit: 500,
1829
- detail: "1"
1830
- });
1831
- all.push(...entries);
1832
- if (entries.length < 500) return all;
1833
- before = entries.at(-1).seq;
1834
- }
1423
+ expect(forgedSite.status === 403, `another site's approval answered ${forgedSite.status}, not 403`);
1424
+ expect(!await archived("OLD"), "a refused decision changed something");
1425
+ const decided = await changer.api.approvals.decide(approval.id, {
1426
+ approve: true,
1427
+ digest: shown.digest
1428
+ });
1429
+ expect(decided.status === "approved" && await archived("OLD"), "Approve did not make the change", decided);
1430
+ const again = await changer.api.approvals.decide(approval.id, {
1431
+ approve: true,
1432
+ digest: shown.digest
1433
+ }).then(() => null, (error) => error);
1434
+ expect(again instanceof CoffreError && again.status === 409, "an approval was decided twice", again);
1435
+ const reported = await call(write, "archive_secret", { secret: `${place}/OLD` });
1436
+ expect(reported.result?.structuredContent?.status === "approved" && reported.result.isError !== true, "calling again does not report the approved change", reported.result);
1437
+ const elicits = { elicitation: { url: {} } };
1438
+ const asked = await call(write, "rename_secret", {
1439
+ secret: `${place}/KEEP`,
1440
+ newKey: "KEPT"
1441
+ }, {}, elicits);
1442
+ const request = asked.result?.inputRequests?.approve;
1443
+ expect(asked.result?.resultType === "input_required" && request?.method === "elicitation/create" && request.params.mode === "url" && typeof asked.result.requestState === "string", "a change with URL elicitation was not answered with an input_required URL elicitation", asked.result);
1444
+ const id = request.params.url.split("/").at(-1);
1445
+ const replayed = await call(write, "rename_secret", {
1446
+ secret: `${place}/KEEP`,
1447
+ newKey: "OTHER"
1448
+ }, {
1449
+ requestState: asked.result.requestState,
1450
+ inputResponses: { approve: { action: "accept" } }
1451
+ }, elicits);
1452
+ expect(replayed.error?.code === -32602, "a requestState was accepted for another call", replayed);
1453
+ const renamedEarly = await admin.api.secrets.list(place);
1454
+ expect(renamedEarly.keys.some((key) => key.key === "KEEP"), "a change was made before its approval", renamedEarly.keys);
1455
+ const second = (await changer.api.approvals.get(id)).approval;
1456
+ expect((await changer.api.approvals.decide(id, {
1457
+ approve: true,
1458
+ digest: second.digest
1459
+ })).status === "approved", "the second approval was not made");
1460
+ const retried = await call(write, "rename_secret", {
1461
+ secret: `${place}/KEEP`,
1462
+ newKey: "KEPT"
1463
+ }, {
1464
+ requestState: asked.result.requestState,
1465
+ inputResponses: { approve: { action: "accept" } }
1466
+ }, elicits);
1467
+ expect(retried.result?.structuredContent?.status === "approved", "the retry does not report the approved change", retried.result);
1468
+ const typed = canary();
1469
+ canaries[`${place}/TYPED`] = typed;
1470
+ const valueId = (await call(write, "request_secret_value", { secret: `${place}/TYPED` })).result.structuredContent.approval.id;
1471
+ const valuePage = (await changer.api.approvals.get(valueId)).approval;
1472
+ expect(valuePage.asks?.value !== void 0, "the approval page does not ask for the value");
1473
+ await changer.api.approvals.decide(valueId, {
1474
+ approve: true,
1475
+ digest: valuePage.digest,
1476
+ value: typed
1477
+ });
1478
+ expect((await admin.api.secrets.reveal(`${place}/TYPED`)).values.TYPED === typed, "the value typed on the page was not written");
1479
+ await call(write, "request_secret_value", { secret: `${place}/TYPED` });
1480
+ const generatedId = (await call(write, "generate_secret_value", {
1481
+ secret: `${place}/GENERATED`,
1482
+ alphabet: "hex"
1483
+ })).result.structuredContent.approval.id;
1484
+ const generatedPage = (await changer.api.approvals.get(generatedId)).approval;
1485
+ const generated = await changer.api.approvals.decide(generatedId, {
1486
+ approve: true,
1487
+ digest: generatedPage.digest
1488
+ });
1489
+ const made = (await admin.api.secrets.reveal(`${place}/GENERATED`)).values.GENERATED ?? "";
1490
+ canaries[`${place}/GENERATED`] = made;
1491
+ expect(/^[0-9a-f]{64}$/.test(made) && generated.shown.length === 0 && !JSON.stringify(generated).includes(made), "generate_secret_value did not make a 64-character hex value, shown to no one");
1492
+ await call(write, "generate_secret_value", {
1493
+ secret: `${place}/GENERATED`,
1494
+ alphabet: "hex"
1495
+ });
1496
+ const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1497
+ expect(leaked.length === 0, "a change tool answered with a secret value", leaked);
1498
+ const { entries } = await admin.api.audit.list({
1499
+ actor: changer.member,
1500
+ limit: 500
1501
+ });
1502
+ const viaApproval = (action) => entries.filter((entry) => entry.action === action && entry.decision === "allow" && typeof entry.metadata.via?.approvalId === "string");
1503
+ const once = {
1504
+ archive: viaApproval("secret.archive").length,
1505
+ rename: viaApproval("secret.rename").length,
1506
+ write: viaApproval("secret.write").length
1507
+ };
1508
+ expect(once.archive === 1 && once.rename === 1 && once.write === 2, "the changes are not each made once, via the client and its approval", once);
1509
+ const decisions = entries.filter((entry) => entry.action === "mcp.approve").map((entry) => `${entry.decision} ${entry.reason ?? ""}`.trim()).sort();
1510
+ expect(JSON.stringify(decisions) === JSON.stringify([
1511
+ "allow",
1512
+ "allow",
1513
+ "allow",
1514
+ "allow",
1515
+ "deny approved",
1516
+ "deny changed"
1517
+ ]), "the decisions, and the two refused, are not in the log", decisions);
1518
+ 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 and another site refused, a replayed requestState refused; each change made once, via the client and its approval; a value typed on the page only, and one coffre made reaching no one`;
1835
1519
  }
1836
- //#endregion
1837
- //#region src/checks/checkpoints.ts
1838
- /** Leave one old signature invalid, reseal the chain, and make later checkpoints valid. */
1839
- async function earlierCheckpoint(deployment, { admin }) {
1840
- expect((await admin.api.audit.verify()).ok, "the log does not verify before checkpoint tampering");
1841
- await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
1842
- const rows = await sql.query("SELECT * FROM audit_log ORDER BY seq");
1843
- const signed = rows.filter((row) => row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow");
1844
- expect(signed.length >= 2, "two checkpoints are needed");
1845
- const [head] = await sql.query("SELECT * FROM audit_chain_head");
1846
- const seed = Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.VAULT_KEY, "base64"), Buffer.alloc(0), "coffre.vault.signing-key.v1", 32));
1847
- const signingKey = createPrivateKey({
1848
- key: Buffer.concat([Buffer.from("302e020100300506032b657004220420", "hex"), seed]),
1849
- format: "der",
1850
- type: "pkcs8"
1851
- });
1852
- const keys = {
1853
- app: Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.APP_KEY, "base64"), Buffer.alloc(0), "coffre.audit.app.v2", 32)),
1854
- vault: Buffer.from(hkdfSync("sha256", seed, Buffer.alloc(0), "coffre.audit.vault.v2", 32))
1520
+ /**
1521
+ * Values (design section 14, checks 2 and 4): Read cannot get one, Reveal
1522
+ * values gets what the person may, a value shown on coffre's page goes to
1523
+ * the person and not the client, and one a client asks coffre to make
1524
+ * reaches no one.
1525
+ */
1526
+ async function mcpValues(deployment, people, canaries) {
1527
+ const { reader } = people;
1528
+ const clientId = await register(deployment);
1529
+ const answers = [];
1530
+ const call = async (access, tool, args) => {
1531
+ const { response, body } = await modern(deployment, access, "tools/call", {
1532
+ name: tool,
1533
+ arguments: args
1534
+ }, tool);
1535
+ answers.push(JSON.stringify(body));
1536
+ return {
1537
+ status: response.status,
1538
+ challenge: response.headers.get("www-authenticate") ?? "",
1539
+ result: body.result
1855
1540
  };
1856
- const hashes = /* @__PURE__ */ new Map();
1857
- let previous = Buffer.alloc(32);
1858
- try {
1859
- await transaction(sql, async () => {
1860
- for (const original of rows) {
1861
- const row = { ...original };
1862
- if (row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow") {
1863
- const checkpoint = JSON.parse(String(row.metadata));
1864
- if (row.seq === signed[0].seq) checkpoint.signature = Buffer.alloc(64).toString("base64");
1865
- else {
1866
- checkpoint.hash = hashes.get(BigInt(checkpoint.seq));
1867
- checkpoint.signature = sign(null, Buffer.from(`coffre.checkpoint.v3|${checkpoint.seq}|${checkpoint.hash}|${checkpoint.signedAt}`), signingKey).toString("base64");
1868
- }
1869
- row.metadata = JSON.stringify(checkpoint);
1870
- }
1871
- const fields = entryFields(row);
1872
- row.prev_hash = previous;
1873
- row.mac = createHmac("sha256", keys[row.author]).update("coffre.audit.mac.v2").update(previous).update(fields).digest();
1874
- previous = createHash("sha256").update("coffre.audit.chain.v2").update(previous).update(fields).update(row.mac).digest();
1875
- row.hash = previous;
1876
- hashes.set(BigInt(String(row.seq)), previous.toString("hex"));
1877
- await restoreRow(sql, "audit_log", row, "seq");
1878
- }
1879
- await restoreRow(sql, "audit_chain_head", {
1880
- ...head,
1881
- head_hash: previous
1882
- }, "only_row");
1883
- });
1884
- const result = await admin.api.audit.verify();
1885
- 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);
1886
- } finally {
1887
- await transaction(sql, async () => {
1888
- for (const row of rows) await restoreRow(sql, "audit_log", row, "seq");
1889
- await restoreRow(sql, "audit_chain_head", head, "only_row");
1890
- });
1891
- keys.app.fill(0);
1892
- keys.vault.fill(0);
1893
- seed.fill(0);
1894
- }
1895
- }));
1896
- expect((await admin.api.audit.verify()).ok, "restoring the signed prefixes did not restore verification");
1897
- return "an invalid earlier checkpoint was caught despite valid MACs and a valid later checkpoint";
1541
+ };
1542
+ const dev = valuesIn(canaries, DEV);
1543
+ const reading = (await redeem(deployment, clientId, await connect(deployment, reader, clientId))).access_token;
1544
+ const stepUp = await call(reading, "read_secret_values", { path: DEV });
1545
+ expect(stepUp.status === 403 && stepUp.challenge.includes("scope=\"read reveal\""), `read_secret_values with Read answered ${stepUp.status}, not a step-up`, stepUp.challenge);
1546
+ const show = await call(reading, "show_secret_value", { secret: `${DEV}/API_KEY` });
1547
+ const id = show.result?.structuredContent?.approval?.id;
1548
+ expect(show.result?.structuredContent?.status === "pending" && id !== void 0, "show_secret_value did not open an approval", show.result);
1549
+ const page = (await reader.api.approvals.get(id)).approval;
1550
+ expect(page.kind === "reveal", "the approval page does not say it shows a value", page);
1551
+ expect((await reader.api.approvals.decide(id, {
1552
+ approve: true,
1553
+ digest: page.digest
1554
+ })).shown.some((line) => line.value === dev.API_KEY), "Reveal did not show the person the value");
1555
+ const told = await call(reading, "show_secret_value", { secret: `${DEV}/API_KEY` });
1556
+ expect(told.result?.structuredContent?.status === "approved", "show_secret_value does not report the value as shown", told.result);
1557
+ const readLeaks = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
1558
+ expect(readLeaks.length === 0, "a Read answer held a value", readLeaks);
1559
+ const revealing = (await redeem(deployment, clientId, await connect(deployment, reader, clientId, "read reveal"))).access_token;
1560
+ const read = await call(revealing, "read_secret_values", { path: DEV });
1561
+ expect(JSON.stringify(read.result?.structuredContent?.values) === JSON.stringify(dev), `read_secret_values on ${DEV} did not answer its values`);
1562
+ expect(read.result?.content?.[0]?.text?.startsWith("These values are now part of this conversation") === true, "read_secret_values does not warn first", read.result?.content);
1563
+ const prod = await call(revealing, "read_secret_values", { path: PROD });
1564
+ expect(prod.result?.isError === true && !JSON.stringify(prod.result).includes(canaries[`${PROD}/API_KEY`]), `the reader read ${PROD} through MCP`, prod.result);
1565
+ 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}`;
1898
1566
  }
1899
1567
  //#endregion
1900
- //#region src/checks/deletion.ts
1901
- const PROJECT = "conformance-gone";
1902
- const SERVICE$1 = "token:conformance-gone-ci";
1903
- /** Each version under a project, by whether it still holds a ciphertext or a wrapped key. */
1904
- async function sealed(deployment, projectId) {
1905
- const rows = await using(deployment.database(), (sql) => query(sql, `SELECT length(v.ciphertext) AS ciphertext, length(v.wrapped_dek) AS wrapped
1906
- FROM secret_versions v JOIN secrets s ON s.id = v.secret_id WHERE s.project_id = $1`, [projectId]));
1907
- return {
1908
- versions: rows.length,
1909
- sealed: rows.filter((row) => Number(row.ciphertext) > 0 || Number(row.wrapped) > 0).length
1910
- };
1568
+ //#region src/checks/storage.ts
1569
+ const TABLES = [
1570
+ "projects",
1571
+ "environments",
1572
+ "secrets",
1573
+ "secret_versions",
1574
+ "vault_members",
1575
+ "vault_grants",
1576
+ "audit_log",
1577
+ "audit_chain_head",
1578
+ "identities",
1579
+ "credentials",
1580
+ "device_authorizations"
1581
+ ];
1582
+ /** Inspect every table, including any a deployment added. Missing shared tables fail. */
1583
+ async function tables(sql) {
1584
+ 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);
1585
+ const missing = TABLES.filter((name) => !names.includes(name));
1586
+ expect(missing.length === 0, "shared tables could not be inspected", missing);
1587
+ return names;
1911
1588
  }
1912
- async function projectId(deployment, slug) {
1913
- const [row] = await using(deployment.database(), (sql) => query(sql, "SELECT id FROM projects WHERE slug = $1", [slug]));
1914
- return row?.id;
1589
+ function quoted(name) {
1590
+ return `"${name.replaceAll("\"", "\"\"")}"`;
1915
1591
  }
1916
- async function deletion(deployment, cli, admin) {
1917
- const run = async (args, code = 0) => {
1918
- const result = await cli.run(args);
1919
- expect(result.code === code, `coffre ${args.join(" ")} exited ${result.code}, not ${code}`, result.output);
1920
- return result.output;
1921
- };
1922
- const value = () => `deleted-value-${randomBytes(8).toString("hex")}`;
1923
- await admin.api.projects.create(PROJECT, { name: "Gone" });
1924
- for (const environment of ["prod", "dev"]) {
1925
- await admin.api.environments.create(`${PROJECT}/${environment}`, { name: environment });
1926
- await admin.api.secrets.set(`${PROJECT}/${environment}`, {
1927
- API_KEY: value(),
1928
- DATABASE_URL: value()
1929
- });
1930
- }
1931
- await admin.api.secrets.set(`${PROJECT}/prod`, { API_KEY: value() });
1932
- await admin.api.members.add(SERVICE$1);
1933
- await admin.api.access.set(SERVICE$1, { [`${PROJECT}/prod`]: "viewer" });
1934
- const id = await projectId(deployment, PROJECT);
1935
- const live = await run([
1936
- "projects",
1937
- "delete",
1938
- PROJECT,
1939
- "--apply"
1940
- ], 1);
1941
- expect(/not archived/.test(live), "deleting a live project was not refused as not archived", live);
1942
- await run([
1943
- "projects",
1944
- "archive",
1945
- PROJECT
1946
- ]);
1947
- const preview = await run([
1948
- "projects",
1949
- "delete",
1950
- PROJECT
1951
- ]);
1952
- expect(/erased: 5 versions/.test(preview) && /revoked: 1 grant/.test(preview) && preview.includes("service:conformance-gone-ci would hold nothing anywhere"), "the preview does not say what the deletion takes", preview);
1953
- expect(/Backups taken before the deletion still hold the encrypted values/.test(preview) && /Nothing changed/.test(preview), "the preview does not warn of backups, or changed something", preview);
1954
- expect((await sealed(deployment, id)).sealed === 5, "the preview erased something");
1955
- const done = await run([
1956
- "projects",
1957
- "delete",
1958
- PROJECT,
1959
- "--apply"
1960
- ]);
1961
- expect(/^deleted conformance-gone, for good/.test(done), "the deletion did not say it was done", done);
1962
- const after = await sealed(deployment, id);
1963
- expect(after.versions === 5 && after.sealed === 0, "a deleted project still holds a ciphertext or a wrapped key", after);
1964
- const [tombstone] = await using(deployment.database(), (sql) => query(sql, "SELECT slug FROM projects WHERE id = $1", [id]));
1965
- expect(/^conformance-gone~deleted-\d{4}-\d{2}-\d{2}$/.test(tombstone?.slug ?? ""), "the tombstone is not named for the day", tombstone);
1966
- expect((await admin.api.projects.list()).projects.every((project) => !project.slug.startsWith(PROJECT)), "a deleted project is still listed");
1967
- expect((await admin.api.members.get(SERVICE$1)).live.grants === 0, "a grant on the deleted project is still live");
1968
- const revealed = await admin.api.secrets.reveal(`${tombstone.slug}/prod`).then(() => null, (error) => error);
1969
- expect(revealed instanceof CoffreError && revealed.status === 404, "a deleted project is still revealed by its tombstone", revealed);
1970
- const fresh = value();
1971
- await admin.api.projects.create(PROJECT, { name: "Gone, again" });
1972
- await admin.api.environments.create(`${PROJECT}/prod`, { name: "prod" });
1973
- await admin.api.secrets.set(`${PROJECT}/prod`, { API_KEY: fresh });
1974
- expect(await projectId(deployment, PROJECT) !== id, "the new project took the deleted one's row");
1975
- expect((await admin.api.secrets.reveal(`${PROJECT}/prod/API_KEY`)).values.API_KEY === fresh, "the new project does not read its own value");
1976
- const { entries } = await admin.api.audit.list({
1977
- path: tombstone.slug,
1978
- limit: 500
1979
- });
1980
- 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));
1981
- await admin.api.environments.update(`${PROJECT}/prod`, { archived: true });
1982
- await run([
1983
- "environments",
1984
- "delete",
1985
- `${PROJECT}/prod`,
1986
- "--apply"
1987
- ]);
1988
- await admin.api.environments.create(`${PROJECT}/prod`, { name: "prod, again" });
1989
- const environments = (await admin.api.projects.list()).projects.find((project) => project.slug === PROJECT)?.environments ?? [];
1990
- expect(environments.length === 1 && environments[0].name === "prod, again", "a deleted environment is still listed, or its slug is not free", environments);
1991
- expect((await sealed(deployment, await projectId(deployment, PROJECT))).sealed === 0, "a deleted environment still holds a value");
1992
- const verified = await admin.api.audit.verify();
1993
- expect(verified.ok, "the log does not verify after the deletions", verified);
1994
- 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`;
1592
+ /** Both drivers return binary columns as bytes; casting records to text hides bytea. */
1593
+ async function scanTables(sql, names, look) {
1594
+ 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) ?? "");
1595
+ }
1596
+ /** Parameters use one spelling in the checks, whatever the database. */
1597
+ function query(sql, statement, params = []) {
1598
+ return sql.query(sql.engine === "sqlite" ? statement.replace(/\$\d+/g, "?") : statement, params);
1599
+ }
1600
+ /** Insert a saved or forged row as it is, but for its generated columns. */
1601
+ async function insertRow(sql, table, row) {
1602
+ const fields = Object.keys(row).filter((field) => field !== "active_subject");
1603
+ await query(sql, `INSERT INTO ${quoted(table)} (${fields.map(quoted).join(", ")}) VALUES (${fields.map((_, i) => `$${i + 1}`).join(", ")})`, fields.map((field) => row[field]));
1604
+ }
1605
+ /** Put a saved row back without changing its bytes or its generated columns. */
1606
+ async function restoreRow(sql, table, row, key) {
1607
+ const fields = Object.keys(row).filter((field) => field !== key && field !== "active_subject");
1608
+ 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]]);
1609
+ }
1610
+ /** Keep a deliberate rewrite, and its restoration, invisible until each is complete. */
1611
+ async function transaction(sql, work) {
1612
+ await sql.exec("BEGIN");
1613
+ try {
1614
+ const result = await work();
1615
+ await sql.exec("COMMIT");
1616
+ return result;
1617
+ } catch (error) {
1618
+ await sql.exec("ROLLBACK");
1619
+ throw error;
1620
+ }
1995
1621
  }
1996
- //#endregion
1997
- //#region src/checks/environments.ts
1998
- /** Where the fork check copies dev to; archived once checked. */
1999
- const FORKED = `${PROJECT$1}/forked`;
2000
1622
  /**
2001
- * A folder grants, hides and renames nothing: a secret filed in one is
2002
- * still read, run and exported by its own name, by the same people, and
2003
- * only those who may write may file it.
1623
+ * `work` with the audit log's append-only triggers lifted, as only its owner
1624
+ * can: Postgres disables them for the session, SQLite drops them and makes
1625
+ * them again after.
2004
1626
  */
2005
- async function folders({ admin, reader }, canaries) {
2006
- const keys = Object.keys(valuesIn(canaries, DEV));
2007
- const key = keys[0];
2008
- expect(key !== void 0, "dev holds no secret to file");
2009
- await admin.api.projects.update(PROJECT$1, { folder: "Conformance" });
2010
- await admin.api.secrets.update(`${DEV}/${key}`, { folder: "filed" });
1627
+ async function appendOnlyLifted(sql, work) {
1628
+ if (sql.engine === "postgres") {
1629
+ await sql.exec("ALTER TABLE audit_log DISABLE TRIGGER USER");
1630
+ try {
1631
+ return await work();
1632
+ } finally {
1633
+ await sql.exec("ALTER TABLE audit_log ENABLE TRIGGER USER");
1634
+ }
1635
+ }
1636
+ const triggers = await sql.query(`SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = 'audit_log'`);
1637
+ for (const trigger of triggers) await sql.exec(`DROP TRIGGER ${trigger.name}`);
2011
1638
  try {
2012
- const project = (await reader.api.projects.list()).projects.find((each) => each.slug === PROJECT$1);
2013
- expect(project?.folder === "Conformance", "a project filed in a folder is not listed in it", project);
2014
- const listed = (await reader.api.secrets.list(DEV)).keys.find((each) => each.key === key);
2015
- expect(listed?.folder === "filed", "a secret filed in a folder is not listed in it", listed);
2016
- const read = await reader.api.secrets.reveal(DEV);
2017
- expect(JSON.stringify(Object.keys(read.values).sort()) === JSON.stringify([...keys].sort()), "filing a secret changed the names a run injects", Object.keys(read.values));
2018
- expect(read.values[key] === canaries[`${DEV}/${key}`], "filing a secret changed its value");
2019
- await refused$1("a viewer filed a secret", reader.api.secrets.update(`${DEV}/${key}`, { folder: "elsewhere" }));
2020
- await refused$1("a viewer filed a project", reader.api.projects.update(PROJECT$1, { folder: "Elsewhere" }));
2021
- await refused$1("a folder name with a slash was taken", admin.api.secrets.update(`${DEV}/${key}`, { folder: "a/b" }));
1639
+ return await work();
2022
1640
  } finally {
2023
- await admin.api.secrets.update(`${DEV}/${key}`, { folder: null });
2024
- await admin.api.projects.update(PROJECT$1, { folder: null });
1641
+ for (const trigger of triggers) await sql.exec(trigger.sql);
2025
1642
  }
2026
- return "a filed secret is read and run by its own name, by the same people; only writers file it";
2027
1643
  }
1644
+ const LOG_REFUSAL = "coffre-conformance: the log refuses writes";
2028
1645
  /**
2029
- * A fork copies dev's values into a new environment, and nothing of who
2030
- * may read dev: a viewer on dev reads neither the fork nor makes one.
1646
+ * `work` with the audit log refusing appends, of one action or all, as a
1647
+ * full disk or a lost connection would, without changing the deployment's
1648
+ * code: a trigger, dropped after.
2031
1649
  */
2032
- async function forks({ admin, reader }, canaries) {
2033
- const dev = valuesIn(canaries, DEV);
2034
- await refused$1("a viewer forked an environment", reader.api.environments.create(FORKED, {
2035
- name: "Forked",
2036
- from: DEV.split("/")[1]
1650
+ async function logRefuses(sql, work, action) {
1651
+ const when = action === void 0 ? "" : ` WHEN (NEW.action = '${action}')`;
1652
+ const create = sql.engine === "postgres" ? `CREATE FUNCTION conformance_refuse() RETURNS trigger LANGUAGE plpgsql AS $$
1653
+ BEGIN RAISE EXCEPTION '${LOG_REFUSAL}'; END $$;
1654
+ CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log FOR EACH ROW${when} EXECUTE FUNCTION conformance_refuse();` : `CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log${when}
1655
+ BEGIN SELECT RAISE(ABORT, '${LOG_REFUSAL}'); END;`;
1656
+ await sql.exec(create);
1657
+ try {
1658
+ return await work();
1659
+ } finally {
1660
+ await sql.exec(sql.engine === "postgres" ? "DROP TRIGGER conformance_refuse ON audit_log; DROP FUNCTION conformance_refuse();" : "DROP TRIGGER conformance_refuse;");
1661
+ }
1662
+ }
1663
+ /** The log's fields in the order its format (`coffre.audit.v2`) encodes them. */
1664
+ const ENTRY_FIELDS = [
1665
+ "seq",
1666
+ "author",
1667
+ "key_id",
1668
+ "occurred_at",
1669
+ "actor",
1670
+ "action",
1671
+ "decision",
1672
+ "code",
1673
+ "subject_principal",
1674
+ "project_id",
1675
+ "environment_id",
1676
+ "secret_id",
1677
+ "secret_version_id",
1678
+ "operation_id",
1679
+ "request_id",
1680
+ "source_ip",
1681
+ "related_seq",
1682
+ "metadata"
1683
+ ];
1684
+ /**
1685
+ * An entry's fields as its MAC and hash cover them, written here from the
1686
+ * format rather than taken from coffre, so a check does not trust the code
1687
+ * it checks: each field length-prefixed in order, null as length -1.
1688
+ */
1689
+ function entryFields(row) {
1690
+ return Buffer.concat(ENTRY_FIELDS.flatMap((field) => {
1691
+ const header = Buffer.alloc(4);
1692
+ const value = row[field];
1693
+ if (value === null || value === void 0) {
1694
+ header.writeInt32BE(-1);
1695
+ return [header];
1696
+ }
1697
+ const bytes = Buffer.from(String(value));
1698
+ header.writeInt32BE(bytes.length);
1699
+ return [header, bytes];
2037
1700
  }));
2038
- const made = await admin.api.environments.create(FORKED, {
2039
- name: "Forked",
2040
- from: DEV.split("/")[1]
1701
+ }
1702
+ //#endregion
1703
+ //#region src/checks/members.ts
1704
+ async function member(deployment, { admin }, name) {
1705
+ const email = `${name}@conformance.example`;
1706
+ const principal = `user:${email}`;
1707
+ await admin.api.members.add(principal);
1708
+ await admin.api.access.set(principal, { [DEV]: "viewer" });
1709
+ const browser = new Browser(deployment.origin);
1710
+ expect((await signIn(deployment, browser, email)).ok, "the fixture member could not sign in");
1711
+ return {
1712
+ email,
1713
+ member: principal,
1714
+ browser,
1715
+ api: browser.client()
1716
+ };
1717
+ }
1718
+ /** The row is refused at use, not only when someone explicitly verifies the log. */
1719
+ async function memberTampering(deployment, people, kind) {
1720
+ const person = await member(deployment, people, `tamper-${kind}`);
1721
+ const { admin } = people;
1722
+ const path = kind === "grant" ? PROD : DEV;
1723
+ const protectedValues = Object.values((await admin.api.secrets.reveal(path)).values);
1724
+ await using(deployment.database(), async (sql) => {
1725
+ const [before] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
1726
+ const grants = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
1727
+ expect(before !== void 0 && grants.length === 1, "the fixture has no member row and grant");
1728
+ let restored = before;
1729
+ let held = grants;
1730
+ if (kind === "stale") {
1731
+ await admin.api.access.set(person.member, { [DEV]: "developer" });
1732
+ [restored] = await query(sql, "SELECT * FROM vault_members WHERE principal = $1", [person.member]);
1733
+ held = await query(sql, "SELECT * FROM vault_grants WHERE principal = $1", [person.member]);
1734
+ await restoreRow(sql, "vault_members", before, "principal");
1735
+ await query(sql, "UPDATE vault_grants SET role = $1, granted_at = $2, granted_by = $3 WHERE principal = $4", [
1736
+ grants[0].role,
1737
+ grants[0].granted_at,
1738
+ grants[0].granted_by,
1739
+ person.member
1740
+ ]);
1741
+ } else if (kind === "member") await query(sql, "UPDATE vault_members SET owner = $1 WHERE principal = $2", [sql.engine === "sqlite" ? 1 : true, person.member]);
1742
+ else {
1743
+ const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
1744
+ WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
1745
+ expect(place !== void 0, "no prod environment to forge a grant on");
1746
+ await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by) VALUES ($1, $2, 'viewer', $3, $4)`, [
1747
+ person.member,
1748
+ place.id,
1749
+ Date.now(),
1750
+ person.member
1751
+ ]);
1752
+ }
1753
+ try {
1754
+ const response = await person.browser.send("POST", "/api/reveals", { path });
1755
+ const text = await response.text();
1756
+ expect(response.status === 401 && JSON.parse(text).error === "unauthenticated", "the tampered credential was not refused", text);
1757
+ expect(!protectedValues.some((value) => text.includes(value)), "the tampered refusal carried a value", text);
1758
+ const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
1759
+ AND subject_principal = $1 AND code = $2`, [person.member, kind === "stale" ? "stale" : "mac"]);
1760
+ expect(reports.length > 0, "the tampering was not logged at use", reports);
1761
+ expect((await admin.api.members.get(person.member)).status === "tampered", "the refused member is not marked tampered");
1762
+ const verified = await admin.api.audit.verify();
1763
+ expect(!verified.ok && verified.author === "vault" && verified.failedAtSeq === null, "the tampered member verifies", verified);
1764
+ } finally {
1765
+ await restoreRow(sql, "vault_members", restored, "principal");
1766
+ await query(sql, "DELETE FROM vault_grants WHERE principal = $1", [person.member]);
1767
+ for (const grant of held) await insertRow(sql, "vault_grants", grant);
1768
+ }
2041
1769
  });
2042
- try {
2043
- expect(made.forked?.keys === Object.keys(dev).length, "the fork did not copy each of dev's keys", made);
2044
- const read = await admin.api.secrets.reveal(FORKED);
2045
- expect(JSON.stringify(Object.entries(read.values).sort()) === JSON.stringify(Object.entries(dev).sort()), "the fork's values are not dev's");
2046
- const history = await admin.api.secrets.history(`${FORKED}/${Object.keys(dev)[0]}`);
2047
- expect(history.versions.length === 1, "the fork carried history", history.versions);
2048
- await refused$1("a viewer on dev read dev's fork", reader.api.secrets.reveal(FORKED));
1770
+ expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "restoring the member did not restore legitimate use");
1771
+ expect((await admin.api.audit.verify()).ok, "the restored member did not verify");
1772
+ return `${kind === "stale" ? "a genuine older member row, put back," : `a forged ${kind}`} refused at use as tampered, logged, then restored`;
1773
+ }
1774
+ /** Access changes have one author, the vault that commits the changed rows. */
1775
+ async function accessAuthorship(deployment, people) {
1776
+ const person = await member(deployment, people, "access-actions");
1777
+ const { admin } = people;
1778
+ await admin.api.access.set(person.member, { [DEV]: null });
1779
+ await admin.api.members.remove(person.member);
1780
+ await admin.api.members.add(person.member);
1781
+ await using(deployment.database(), async (sql) => {
1782
+ const access = await sql.query(`SELECT author, action, subject_principal FROM audit_log
1783
+ WHERE decision = 'allow' AND (action LIKE 'access.%' OR action LIKE 'member.%' OR action LIKE 'directory.%' OR action LIKE 'grant.%' OR action LIKE 'principal.%')`);
1784
+ expect(access.length > 0 && access.every((entry) => entry.author === "vault"), "an access change was written by the app", access.filter((entry) => entry.author !== "vault"));
1785
+ const actions = access.filter((entry) => entry.subject_principal === person.member).map((entry) => entry.action);
1786
+ for (const action of [
1787
+ "member.add",
1788
+ "member.remove",
1789
+ "member.restore",
1790
+ "access.grant",
1791
+ "access.revoke"
1792
+ ]) expect(actions.includes(action), `${action} left no vault entry`, actions);
1793
+ });
1794
+ return "admission, grant, revoke, removal and re-admission are only vault entries";
1795
+ }
1796
+ /** A failure to append may not commit an access change. */
1797
+ async function noAuditNoAccess(deployment, people) {
1798
+ const person = await member(deployment, people, "no-audit");
1799
+ const { admin } = people;
1800
+ await using(deployment.database(), async (sql) => {
1801
+ const snapshot = async () => ({
1802
+ members: await sql.query("SELECT * FROM vault_members ORDER BY principal"),
1803
+ grants: await sql.query("SELECT * FROM vault_grants ORDER BY principal, project_id, environment_id")
1804
+ });
1805
+ const before = await snapshot();
1806
+ await logRefuses(sql, async () => {
1807
+ for (const change of [
1808
+ () => admin.api.access.set(person.member, { [DEV]: "developer" }),
1809
+ () => admin.api.members.remove(person.member),
1810
+ () => admin.api.members.add("user:no-audit-new@conformance.example")
1811
+ ]) {
1812
+ expect(!await change().then(() => true, () => false), "an access change succeeded while the log refused writes");
1813
+ expect(JSON.stringify(await snapshot()) === JSON.stringify(before), "an access change committed without its entry");
1814
+ }
1815
+ });
1816
+ });
1817
+ expect((await person.api.secrets.reveal(DEV)).values.API_KEY !== void 0, "legitimate reads did not recover");
1818
+ return "grant, removal and admission commit nothing while the log refuses their entries";
1819
+ }
1820
+ /** Hold the head after the vault checked a member, then forge a grant before it seals them. */
1821
+ async function sealingRace(deployment, people) {
1822
+ if (deployment.vaultRuntime === null) throw new Skip("SQLite prevents a write beside a decision; the vault suite covers review R2 on Postgres");
1823
+ const [login] = await using(deployment.vaultRuntime(), (sql) => sql.query("SELECT current_user AS name"));
1824
+ const person = await member(deployment, people, "seal-race");
1825
+ const { admin } = people;
1826
+ try {
1827
+ await using(deployment.database(), (holder) => using(deployment.database(), async (sql) => {
1828
+ const [place] = await query(sql, `SELECT e.id FROM environments e JOIN projects p ON p.id = e.project_id
1829
+ WHERE p.slug = 'conformance' AND e.slug = 'prod'`);
1830
+ const [head] = await sql.query("SELECT next_seq FROM audit_chain_head");
1831
+ const [owner] = await holder.query("SELECT pg_backend_pid() AS pid");
1832
+ expect(place !== void 0, "no prod environment for the sealing race");
1833
+ await holder.exec("BEGIN");
1834
+ let pending;
1835
+ try {
1836
+ await holder.query("SELECT 1 FROM audit_chain_head FOR UPDATE");
1837
+ pending = admin.api.access.set(person.member, { [DEV]: "developer" }).then((value) => ({ value }), (error) => ({ error }));
1838
+ await until("the vault decision waiting on our head", async () => {
1839
+ return (await query(sql, `SELECT pid FROM pg_stat_activity WHERE datname = current_database()
1840
+ AND usename = $1 AND $2 = ANY(pg_blocking_pids(pid))`, [login.name, owner.pid])).length > 0;
1841
+ }, 10);
1842
+ await query(sql, `INSERT INTO vault_grants (principal, environment_id, role, granted_at, granted_by)
1843
+ VALUES ($1, $2, 'viewer', $3, $4)`, [
1844
+ person.member,
1845
+ place.id,
1846
+ Date.now(),
1847
+ person.member
1848
+ ]);
1849
+ } finally {
1850
+ await holder.exec("ROLLBACK");
1851
+ if (pending !== void 0) await pending;
1852
+ }
1853
+ expect(pending !== void 0, "the access decision was not started");
1854
+ const result = await pending;
1855
+ 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);
1856
+ const reports = await query(sql, `SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'vault.tampered'
1857
+ AND subject_principal = $1 AND code = 'mac' AND seq >= $2`, [person.member, head.next_seq]);
1858
+ expect(reports.length > 0, "the grant inserted during the decision was not reported", reports);
1859
+ const response = await person.browser.send("POST", "/api/reveals", { path: PROD });
1860
+ const body = await response.json();
1861
+ expect(response.status === 401 && body.error === "unauthenticated", "the forged grant let its member read", body);
1862
+ }));
2049
1863
  } finally {
2050
- await admin.api.environments.update(FORKED, { archived: true });
1864
+ await admin.api.members.remove(person.member);
2051
1865
  }
2052
- return "a fork holds dev's values and no history, and a viewer on dev neither makes one nor reads it";
1866
+ expect((await admin.api.audit.verify()).ok, "the log did not verify after recovering the sealing-race member");
1867
+ return "a grant inserted while the vault waited on the head was refused as tampered and logged; removal recovered the member";
2053
1868
  }
2054
- /** The key in dev the reference check makes, archived once checked. */
2055
- const HELD = "CONFORMANCE_REFERENCE";
2056
- const FORGED = "CONFORMANCE_FORGED";
2057
- /** The status a call failed with and its reason, the vault's or the app's, or that it went through. */
2058
- async function outcome(call) {
2059
- try {
2060
- await call;
2061
- return "let through";
2062
- } catch (error) {
2063
- if (!(error instanceof CoffreError)) throw error;
2064
- return `${error.status} ${error.reason ?? error.code}`;
2065
- }
1869
+ //#endregion
1870
+ //#region src/checks/signin.ts
1871
+ const hash = (value) => createHash("sha256").update(value).digest();
1872
+ async function reported(deployment, id) {
1873
+ const event = new RegExp(`auth_row_tampered[^}]*${id}`);
1874
+ await until(`the sign-in row failure report for ${id}`, async () => event.test(deployment.output()), 10);
2066
1875
  }
2067
- /**
2068
- * A reference lets whoever reads its holder read its source, so only the
2069
- * vault makes one, and a read through one is checked against the vault's
2070
- * own entry: the reader, a viewer on dev with no grant on prod, reads
2071
- * prod's value through dev's reference, and the read is in prod's log and
2072
- * dev's; a reference row the vault never made, written straight into the
2073
- * database, reads nothing; a developer on dev who cannot read prod makes
2074
- * none; and once broken, it reads no more.
2075
- */
2076
- async function references(deployment, { admin, reader, leaver }, canaries) {
2077
- const [sourceKey] = Object.keys(valuesIn(canaries, PROD));
2078
- expect(sourceKey !== void 0, "prod holds no secret to refer to");
2079
- const source = `${PROD}/${sourceKey}`;
2080
- await refused$1("a viewer on dev read prod directly", reader.api.secrets.reveal(source));
2081
- 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");
2082
- await admin.api.secrets.set(DEV, { [HELD]: { ref: source } });
2083
- try {
2084
- 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");
2085
- const logged = async (path) => (await admin.api.audit.list({
2086
- path,
2087
- limit: 200
2088
- })).entries.filter((entry) => entry.action === "secret.read" && entry.actorId === reader.email && entry.metadata.via !== void 0).length;
2089
- expect(await logged(PROD) >= 1 && await logged(DEV) >= 1, "the read through the reference is not in both the source's log and the holder's");
2090
- const archives = [[source, (archived) => admin.api.secrets.update(source, { archived })], [PROD, (archived) => admin.api.environments.update(PROD, { archived })]];
2091
- for (const [what, archive] of archives) {
2092
- const refusal = await archive(true).then(() => null, (error) => error);
2093
- if (refusal === null) await archive(false);
2094
- 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);
2095
- }
2096
- await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
2097
- const underArchived = await outcome(admin.api.secrets.update(source, { archived: true })).finally(() => admin.api.secrets.update(`${DEV}/${HELD}`, { archived: false }));
1876
+ /** Knowing the database password and a chosen token must not mint a credential. */
1877
+ async function forgedCredential(deployment, people) {
1878
+ const person = await member(deployment, people, "forged-credential");
1879
+ const token = `coffre_cli_${randomBytes(32).toString("base64url")}`;
1880
+ await using(deployment.database(), async (sql) => {
1881
+ const [row] = await query(sql, "SELECT * FROM credentials WHERE principal = $1 AND revoked_at IS NULL LIMIT 1", [person.member]);
1882
+ expect(row !== void 0, "no genuine credential to copy");
1883
+ const forged = {
1884
+ ...row,
1885
+ id: randomUUID(),
1886
+ kind: "cli",
1887
+ token_hash: hash(token),
1888
+ identity_id: null
1889
+ };
1890
+ await insertRow(sql, "credentials", forged);
2098
1891
  try {
2099
- expect(underArchived === "let through", `${source} could not be archived while only an archived key referred to it: ${underArchived}`);
2100
- const restored = await outcome(reader.api.secrets.reveal(`${DEV}/${HELD}`));
2101
- expect(restored.startsWith("409 "), `a reference read a source archived while its holder was: ${restored}`);
1892
+ const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${token}` } });
1893
+ expect(response.status === 401, "an owner-forged credential authenticated", await response.text());
1894
+ await reported(deployment, forged.id);
2102
1895
  } finally {
2103
- if (underArchived === "let through") await admin.api.secrets.update(source, { archived: false });
1896
+ await query(sql, "DELETE FROM credentials WHERE id = $1", [forged.id]);
2104
1897
  }
2105
- expect((await reader.api.secrets.reveal(`${DEV}/${HELD}`)).values[HELD] === canaries[source], "a reference did not read its source again once both were restored");
2106
- const forged = await using(deployment.database(), async (sql) => {
2107
- const [holder] = await query(sql, "SELECT id, project_id, environment_id FROM secrets WHERE key = $1", [HELD]);
2108
- 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]]);
2109
- 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");
2110
- const id = randomUUID();
2111
- await query(sql, "INSERT INTO secrets (id, project_id, environment_id, key, current_version) VALUES ($1, $2, $3, $4, 0)", [
2112
- id,
2113
- holder.project_id,
2114
- holder.environment_id,
2115
- FORGED
2116
- ]);
2117
- 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)
2118
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [
2119
- randomUUID(),
2120
- holder.project_id,
2121
- holder.environment_id,
2122
- id,
2123
- target.project_id,
2124
- target.environment_id,
2125
- target.id,
2126
- String(entry.seq),
2127
- admin.member
2128
- ]);
2129
- return id;
2130
- });
1898
+ });
1899
+ expect((await person.api.me()).principal.id === person.email, "the genuine credential stopped working");
1900
+ return "a chosen token in an owner-inserted credential cannot authenticate, and is reported";
1901
+ }
1902
+ /** An account binding inserted by the owner must not sign someone in as a member. */
1903
+ async function forgedIdentity(deployment, people) {
1904
+ const person = await member(deployment, people, "forged-identity");
1905
+ const email = "identity-forger@conformance.example";
1906
+ const browser = new Browser(deployment.origin);
1907
+ await using(deployment.database(), async (sql) => {
1908
+ const [row] = await query(sql, "SELECT * FROM identities WHERE principal = $1 AND revoked_at IS NULL LIMIT 1", [person.member]);
1909
+ expect(row !== void 0, "no genuine identity to copy");
1910
+ const forged = {
1911
+ ...row,
1912
+ id: randomUUID(),
1913
+ subject: String(defaultGitHubAccount(email).id)
1914
+ };
1915
+ await insertRow(sql, "identities", forged);
2131
1916
  try {
2132
- const forgedRead = await outcome(reader.api.secrets.reveal(`${DEV}/${FORGED}`));
2133
- expect([
2134
- "404 no_value",
2135
- "404 not_found",
2136
- "403 bad_claim"
2137
- ].includes(forgedRead), `a reference row the vault never made was read: ${forgedRead}`);
1917
+ await signIn(deployment, browser, email).catch(() => {});
1918
+ expect((await browser.fetch("/api/me")).status === 401, "an owner-forged identity minted a session");
1919
+ await reported(deployment, forged.id);
2138
1920
  } finally {
2139
- await using(deployment.database(), async (sql) => {
2140
- await query(sql, "DELETE FROM secret_references WHERE secret_id = $1", [forged]);
2141
- await query(sql, "DELETE FROM secrets WHERE id = $1", [forged]);
2142
- });
1921
+ await query(sql, "DELETE FROM credentials WHERE identity_id = $1", [forged.id]);
1922
+ await query(sql, "DELETE FROM identities WHERE id = $1", [forged.id]);
2143
1923
  }
2144
- await admin.api.references.break(`${DEV}/${HELD}`);
2145
- const after = await outcome(reader.api.secrets.reveal(`${DEV}/${HELD}`));
2146
- expect(after.startsWith("409 "), `a broken reference still read: ${after}`);
2147
- } finally {
2148
- await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
2149
- }
2150
- return "a viewer on dev reads prod's value through a reference, logged in both; prod and its key cannot be archived while it reads them, but can once its holder is archived, which then reads nothing until they are back; a forged row is refused, a broken one reads no more, and none is made without read on the source";
1924
+ });
1925
+ expect((await person.api.me()).principal.id === person.email, "the genuine identity stopped working");
1926
+ return "an owner-inserted account binding cannot mint a session, and is reported";
2151
1927
  }
2152
- /** A key prod has and dev does not, for the missing-keys check; archived once checked. */
2153
- const PROD_ONLY = "CONFORMANCE_PROD_ONLY";
2154
- /**
2155
- * Key names are metadata: what dev is missing is compared only with the
2156
- * environments the viewer reads. The admin sees prod's key missing from dev;
2157
- * the reader, a viewer on dev alone, sees nothing of prod, and dismisses
2158
- * nothing. A dismissal is the team's, and restoring undoes it.
2159
- */
2160
- async function missingKeys({ admin, reader }) {
2161
- await admin.api.secrets.set(PROD, { [PROD_ONLY]: canary() });
2162
- try {
2163
- const seen = await admin.api.environments.missing(DEV);
2164
- expect(seen.missing.some((key) => key.key === PROD_ONLY), "the admin was not told dev lacks prod's key", seen);
2165
- const hidden = await reader.api.environments.missing(DEV);
2166
- expect(![...hidden.missing, ...hidden.dismissed].some((key) => key.key === PROD_ONLY), "a viewer on dev learned a key name from prod, which they cannot read", hidden);
2167
- await refused$1("a viewer dismissed a missing key", reader.api.environments.dismiss(DEV, { [PROD_ONLY]: true }));
2168
- await admin.api.environments.dismiss(DEV, { [PROD_ONLY]: true });
2169
- const dismissed = await admin.api.environments.missing(DEV);
2170
- expect(dismissed.dismissed.some((key) => key.key === PROD_ONLY) && !dismissed.missing.some((key) => key.key === PROD_ONLY), "a dismissed key was still listed as missing", dismissed);
2171
- await admin.api.environments.dismiss(DEV, { [PROD_ONLY]: null });
2172
- expect((await admin.api.environments.missing(DEV)).missing.some((key) => key.key === PROD_ONLY), "a restored key was not missing again");
2173
- } finally {
2174
- await admin.api.secrets.update(`${PROD}/${PROD_ONLY}`, { archived: true });
2175
- }
2176
- return "only who reads prod learns what dev lacks of it; a viewer dismisses nothing; a dismissal is listed, and restored";
1928
+ async function startDevice(deployment) {
1929
+ const response = await fetch(`${deployment.origin}/api/auth/device`, {
1930
+ method: "POST",
1931
+ headers: { "content-type": "application/json" },
1932
+ body: "{}"
1933
+ });
1934
+ expect(response.ok, "the device fixture did not start", await response.clone().text());
1935
+ return response.json();
2177
1936
  }
2178
- //#endregion
2179
- //#region src/checks/logins.ts
2180
- const ZEROS = "decode(repeat('00', 32), 'hex')";
2181
- const appendAs = (author) => `INSERT INTO audit_log
2182
- (seq, author, key_id, occurred_at, actor, action, decision, prev_hash, mac, hash)
2183
- VALUES (9000000000, '${author}', '${author}:0', 0, 'system:conformance', 'probe', 'allow', ${ZEROS}, ${ZEROS}, ${ZEROS})`;
2184
- const IMMUTABLE = [
2185
- "UPDATE audit_log SET action = 'rewritten' WHERE author = 'app'",
2186
- "UPDATE audit_log SET action = 'rewritten' WHERE author = 'vault'",
2187
- "DELETE FROM audit_log",
2188
- "TRUNCATE audit_log",
2189
- "DROP TABLE audit_log",
2190
- "UPDATE secret_versions SET id = id",
2191
- "DELETE FROM secret_versions",
2192
- "DELETE FROM vault_members",
2193
- "CREATE TABLE conformance_probe (id integer)",
2194
- "CREATE TEMP TABLE conformance_probe (id integer)",
2195
- "ALTER TABLE audit_log DISABLE TRIGGER USER",
2196
- "SET session_replication_role = 'replica'",
2197
- "SET ROLE coffre_owner"
2198
- ];
2199
- /** A column of each app table, for an update that would change nothing, if it were allowed. */
2200
- const APP_COLUMNS = {
2201
- projects: "id",
2202
- environments: "id",
2203
- secrets: "id",
2204
- secret_versions: "id",
2205
- identities: "id",
2206
- credentials: "id",
2207
- device_authorizations: "id"
2208
- };
2209
- async function refuses(sql, statements) {
2210
- for (const statement of statements) {
2211
- await sql.exec("BEGIN");
2212
- let error;
1937
+ function pollDevice(deployment, code) {
1938
+ return fetch(`${deployment.origin}/api/auth/device/token`, {
1939
+ method: "POST",
1940
+ headers: { "content-type": "application/json" },
1941
+ body: JSON.stringify({ device_code: code })
1942
+ });
1943
+ }
1944
+ /** An authentic pending row, changed to approved around the app, grants nothing. */
1945
+ async function forgedApproval(deployment, people) {
1946
+ const person = await member(deployment, people, "forged-approval");
1947
+ const device = await startDevice(deployment);
1948
+ await using(deployment.database(), async (sql) => {
1949
+ const [row] = await query(sql, "SELECT * FROM device_authorizations WHERE user_code = $1", [device.user_code]);
1950
+ const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [person.member]);
1951
+ expect(row !== void 0, "no device row to forge");
1952
+ await query(sql, `UPDATE device_authorizations SET decision = 'approved', decided_at = $1, principal = $2, generation = $3 WHERE id = $4`, [
1953
+ sql.engine === "sqlite" ? Date.now() : /* @__PURE__ */ new Date(),
1954
+ person.member,
1955
+ standing.generation,
1956
+ row.id
1957
+ ]);
2213
1958
  try {
2214
- await sql.exec(statement);
2215
- } catch (failure) {
2216
- error = failure;
1959
+ const response = await pollDevice(deployment, device.device_code);
1960
+ const body = await response.text();
1961
+ expect(!response.ok && !body.includes("access_token"), "an owner-forged approval minted a session", body);
1962
+ await reported(deployment, String(row.id));
2217
1963
  } finally {
2218
- await sql.exec("ROLLBACK");
1964
+ await query(sql, "DELETE FROM device_authorizations WHERE id = $1", [row.id]);
2219
1965
  }
2220
- expect(error !== void 0, `the login could: ${statement}`);
2221
- expect(error.code === "42501", `${statement} failed for a reason other than privilege`, error);
2222
- }
2223
- return statements.length;
2224
- }
2225
- /** Neither table nor column grants may let a login write outside its component. */
2226
- async function noWrites(sql, names) {
2227
- const columns = await sql.query(`SELECT table_name, column_name,
2228
- has_column_privilege(current_user, quote_ident(table_name), column_name, 'INSERT') AS insertable,
2229
- has_column_privilege(current_user, quote_ident(table_name), column_name, 'UPDATE') AS updatable,
2230
- has_table_privilege(current_user, quote_ident(table_name), 'DELETE') AS deletable,
2231
- has_table_privilege(current_user, quote_ident(table_name), 'TRUNCATE') AS truncatable
2232
- FROM information_schema.columns WHERE table_schema = 'public'`);
2233
- for (const name of names) {
2234
- const inspected = columns.filter((column) => column.table_name === name);
2235
- const [table] = await sql.query(`SELECT
2236
- has_table_privilege(current_user, $1, 'INSERT, UPDATE, DELETE, TRUNCATE') AS writable`, [name]);
2237
- expect(!table.writable, `the login has a table write privilege on ${name}`);
2238
- expect(inspected.every((column) => !column.insertable && !column.updatable && !column.deletable && !column.truncatable), `the login has a column write privilege on ${name}`, inspected);
2239
- }
2240
- }
2241
- async function appLogin(deployment) {
2242
- if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
2243
- expect(deployment.runtime !== null, "the app runtime login is missing");
2244
- const protectedTables = (await using(deployment.database(), tables)).filter((name) => name.startsWith("vault_"));
2245
- return using(deployment.runtime(), async (sql) => {
2246
- await noWrites(sql, protectedTables);
2247
- const statements = [
2248
- ...IMMUTABLE,
2249
- appendAs("vault"),
2250
- "SET ROLE coffre_vault",
2251
- "DELETE FROM secrets"
2252
- ];
2253
- 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}`);
2254
- return `${await refuses(sql, statements)} writes and bypasses refused; no member or grant write privileges`;
2255
1966
  });
1967
+ const control = await startDevice(deployment);
1968
+ await person.api.deviceLogins.decide(control.user_code, true);
1969
+ expect((await pollDevice(deployment, control.device_code)).ok, "a genuine approval no longer works");
1970
+ return "an owner-edited device approval grants nothing and is reported; a genuine approval works";
2256
1971
  }
2257
- async function vaultLogin(deployment) {
2258
- if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
2259
- expect(deployment.vaultRuntime !== null, "the vault runtime login is missing");
2260
- const appTables = (await using(deployment.database(), tables)).filter((name) => ![
2261
- "vault_members",
2262
- "vault_grants",
2263
- "audit_log",
2264
- "audit_chain_head"
2265
- ].includes(name));
2266
- return using(deployment.vaultRuntime(), async (sql) => {
2267
- await noWrites(sql, appTables);
2268
- const [head] = await sql.query(`SELECT
2269
- has_table_privilege(current_user, 'audit_chain_head', 'INSERT') AS insertable,
2270
- has_table_privilege(current_user, 'audit_chain_head', 'DELETE') AS deletable,
2271
- has_table_privilege(current_user, 'audit_chain_head', 'TRUNCATE') AS truncatable,
2272
- has_column_privilege(current_user, 'audit_chain_head', 'only_row', 'UPDATE') AS identity_updatable`);
2273
- expect(!head.insertable && !head.deletable && !head.truncatable && !head.identity_updatable, "the vault can replace the head row", head);
2274
- const statements = [
2275
- ...IMMUTABLE,
2276
- appendAs("app"),
2277
- "SET ROLE coffre_app",
2278
- "UPDATE audit_chain_head SET only_row = only_row",
2279
- "DELETE FROM audit_chain_head",
2280
- "TRUNCATE audit_chain_head",
2281
- "UPDATE vault_members SET principal = principal",
2282
- "UPDATE vault_grants SET role = role"
2283
- ];
2284
- for (const table of appTables) {
2285
- const column = APP_COLUMNS[table];
2286
- if (column !== void 0) statements.push(`UPDATE ${quoted(table)} SET ${column} = ${column}`);
1972
+ /** Changing an old credential's generation cannot bring its chosen token back. */
1973
+ async function editedGeneration(deployment, people) {
1974
+ const principal = "token:generation-mac";
1975
+ const { admin } = people;
1976
+ await admin.api.members.add(principal);
1977
+ await admin.api.access.set(principal, { "conformance/dev": "viewer" });
1978
+ const issued = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
1979
+ await using(deployment.database(), async (sql) => {
1980
+ const [old] = await query(sql, "SELECT * FROM credentials WHERE token_hash = $1", [hash(issued.token)]);
1981
+ expect(old !== void 0, "no old token row to save");
1982
+ await admin.api.members.remove(principal);
1983
+ await admin.api.members.add(principal);
1984
+ await admin.api.access.set(principal, { "conformance/dev": "viewer" });
1985
+ const [current] = await query(sql, "SELECT * FROM credentials WHERE id = $1", [old.id]);
1986
+ const [standing] = await query(sql, "SELECT generation FROM vault_members WHERE principal = $1", [principal]);
1987
+ await restoreRow(sql, "credentials", {
1988
+ ...old,
1989
+ generation: standing.generation
1990
+ }, "id");
1991
+ try {
1992
+ const response = await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${issued.token}` } });
1993
+ expect(response.status === 401, "editing the credential generation revived the token", await response.text());
1994
+ await reported(deployment, String(old.id));
1995
+ } finally {
1996
+ await restoreRow(sql, "credentials", current, "id");
2287
1997
  }
2288
- return `${await refuses(sql, statements)} writes and bypasses refused; app tables have no write privileges`;
2289
1998
  });
1999
+ const fresh = await admin.api.tokens.issue(principal, { expiresInDays: 1 });
2000
+ expect((await fetch(`${deployment.origin}/api/me`, { headers: { authorization: `Bearer ${fresh.token}` } })).ok, "a newly issued token does not work");
2001
+ return "an old token with its generation edited to the current membership is refused and reported";
2290
2002
  }
2291
2003
  //#endregion
2292
- //#region src/checks/mcp.ts
2293
- const NAME = "Conformance MCP client";
2294
- /** A native client's redirect, on loopback. A custom scheme is left out of a registration (D37). */
2295
- const REDIRECT = "http://127.0.0.1:33418/callback";
2296
- /** A `state` the router would read as a number: it must come back as sent. */
2297
- const STATE = "1e5";
2298
- /** What every 2026-07-28 request carries: its revision and the client's capabilities. */
2299
- const ENVELOPE = {
2300
- "io.modelcontextprotocol/protocolVersion": "2026-07-28",
2301
- "io.modelcontextprotocol/clientCapabilities": {}
2302
- };
2303
- const base64url = (bytes) => bytes.toString("base64url");
2304
- async function json(response) {
2305
- const text = await response.text();
2306
- try {
2307
- return JSON.parse(text);
2308
- } catch {
2309
- return { text };
2004
+ //#region src/checks/access.ts
2005
+ async function membersOnly(deployment, { stranger }) {
2006
+ const attempt = await signIn(deployment, stranger.browser, stranger.email);
2007
+ expect(!attempt.ok, "someone never admitted signed in");
2008
+ expect(!stranger.browser.hasCookies, "the refused sign-in left the browser a cookie");
2009
+ expect((await stranger.browser.fetch("/api/me")).status === 401, "the refused sign-in left a session");
2010
+ await refused$1("a made-up token read secrets", bearer(deployment.origin, "coffre_cli_conformanceforgedtokenconformanceforged").secrets.list(DEV));
2011
+ for (const [method, path, body] of [
2012
+ [
2013
+ "GET",
2014
+ `/api/secrets/${DEV}`,
2015
+ void 0
2016
+ ],
2017
+ [
2018
+ "POST",
2019
+ "/api/reveals",
2020
+ { path: DEV }
2021
+ ],
2022
+ [
2023
+ "PATCH",
2024
+ `/api/secrets/${DEV}`,
2025
+ { API_KEY: "from nobody" }
2026
+ ],
2027
+ [
2028
+ "GET",
2029
+ "/api/audit",
2030
+ void 0
2031
+ ]
2032
+ ]) {
2033
+ const response = await fetch(`${deployment.origin}${path}`, {
2034
+ method,
2035
+ headers: {
2036
+ origin: deployment.origin,
2037
+ "content-type": "application/json"
2038
+ },
2039
+ body: body === void 0 ? void 0 : JSON.stringify(body)
2040
+ });
2041
+ expect(response.status === 401, `${method} ${path} answered ${response.status} to no one`);
2310
2042
  }
2043
+ return `a stranger's sign-in is ${attempt.ok ? "let" : attempt.error}; no one and a made-up token get 401`;
2311
2044
  }
2312
- /** A JSON-RPC message to /mcp, with the headers a client sends. */
2313
- function mcp(deployment, headers, method = "server/discover", params = {}) {
2314
- return fetch(`${deployment.origin}/mcp`, {
2315
- method: "POST",
2316
- headers: {
2317
- "content-type": "application/json",
2318
- accept: "application/json, text/event-stream",
2319
- ...headers
2045
+ async function grantScoping({ reader, service, bulk }, canaries) {
2046
+ const read = await reader.api.secrets.reveal(DEV);
2047
+ expect(same(read.values, valuesIn(canaries, DEV)), "a viewer on dev did not get dev's values", read.values);
2048
+ await refused$1("a viewer on dev revealed prod", reader.api.secrets.reveal(PROD));
2049
+ await refused$1("a viewer on dev listed prod", reader.api.secrets.list(PROD));
2050
+ await refused$1("a viewer wrote a value", reader.api.secrets.set(DEV, { API_KEY: "from the reader" }));
2051
+ await refused$1("a viewer granted themselves more", reader.api.access.set(reader.member, { [PROD]: "owner" }));
2052
+ await refused$1("a viewer added a member", reader.api.members.add("user:friend@conformance.example"));
2053
+ await refused$1("a viewer issued a service token", reader.api.tokens.issue(service.member, { expiresInDays: 1 }));
2054
+ const places = (await reader.api.me()).environments.map((place) => `${place.project}/${place.environment}`);
2055
+ expect(places.length === 1 && places[0] === DEV, "a viewer on dev is told of other places", places);
2056
+ const served = await service.api.secrets.reveal(DEV);
2057
+ expect(same(served.values, valuesIn(canaries, DEV)), "a service viewer on dev did not get dev's values", served.values);
2058
+ await refused$1("a service viewer on dev revealed prod", service.api.secrets.reveal(PROD));
2059
+ await refused$1("a viewer on bulk revealed dev", bulk.api.secrets.reveal(DEV));
2060
+ return "a viewer on dev reads dev and nothing else, in a browser or with a token, and changes nothing";
2061
+ }
2062
+ /**
2063
+ * A change sent with the session cookie from another site's page, or from
2064
+ * no page coffre can vouch for, is refused (CSRF), and so is a reveal.
2065
+ */
2066
+ async function crossSite({ admin }, canaries) {
2067
+ for (const headers of [
2068
+ {
2069
+ origin: "https://attacker.example",
2070
+ "sec-fetch-site": "cross-site"
2320
2071
  },
2321
- body: JSON.stringify({
2322
- jsonrpc: "2.0",
2323
- id: 1,
2072
+ { origin: "https://attacker.example" },
2073
+ { "sec-fetch-site": "same-site" },
2074
+ {}
2075
+ ]) for (const [method, path, body] of [
2076
+ [
2077
+ "PATCH",
2078
+ `/api/secrets/${DEV}`,
2079
+ { API_KEY: "from another site" }
2080
+ ],
2081
+ [
2082
+ "POST",
2083
+ "/api/reveals",
2084
+ { path: DEV }
2085
+ ],
2086
+ [
2087
+ "PUT",
2088
+ "/api/members/user:mallory@attacker.example",
2089
+ {}
2090
+ ],
2091
+ [
2092
+ "POST",
2093
+ "/auth/signout",
2094
+ void 0
2095
+ ]
2096
+ ]) {
2097
+ const response = await admin.browser.fetch(path, {
2324
2098
  method,
2325
- params
2326
- })
2327
- });
2099
+ headers: {
2100
+ ...headers,
2101
+ "content-type": "application/json"
2102
+ },
2103
+ body: body === void 0 ? void 0 : JSON.stringify(body)
2104
+ });
2105
+ const text = await response.text();
2106
+ expect(response.status === 403, `${method} ${path} with ${JSON.stringify(headers)} answered ${response.status}`, text);
2107
+ expect(!Object.values(canaries).some((value) => text.includes(value)), `${method} ${path} from another site carried a value`);
2108
+ }
2109
+ const after = await admin.api.secrets.reveal(DEV);
2110
+ expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`], "a value changed from another site", after.values);
2111
+ expect(!(await admin.api.members.list()).members.some((entry) => entry.member.includes("mallory")), "a member was added from another site");
2112
+ return "writes, reveals and sign-out with the cookie, from another site or no page at all: 403";
2328
2113
  }
2329
- function token(deployment, fields) {
2330
- return fetch(`${deployment.origin}/api/oauth/token`, {
2331
- method: "POST",
2332
- headers: { "content-type": "application/x-www-form-urlencoded" },
2333
- body: new URLSearchParams(fields)
2114
+ async function offboarding(deployment, { admin, leaver, service }, canaries) {
2115
+ const device = await startDevice(deployment);
2116
+ await leaver.api.deviceLogins.decide(device.user_code, true);
2117
+ await leaver.cli.secrets.reveal(DEV);
2118
+ const app = await connectedApp(deployment, leaver);
2119
+ const before = await admin.api.members.get(leaver.member);
2120
+ expect(before.live.apps === 1 && before.apps.length === 1, "the owner's report does not list the app the leaver connected", before.apps);
2121
+ const { revoked, report } = await admin.api.members.remove(leaver.member);
2122
+ expect(revoked.apps === 1 && report.live.apps === 0 && report.apps.length === 0, "removal does not say it disconnected their app", {
2123
+ revoked,
2124
+ apps: report.apps
2334
2125
  });
2126
+ expect(!await discovers(deployment, app), "a removed member's app still reaches /mcp");
2127
+ const exposed = report.exposed.map((entry) => `${entry.project}/${entry.environment}/${entry.key}`).sort();
2128
+ expect(same(exposed, Object.keys(valuesIn(canaries, DEV)).map((key) => `${DEV}/${key}`).sort()), "the offboarding report does not name what they read", report.exposed);
2129
+ expect((await leaver.browser.fetch("/api/me")).status === 401, "a removed member's browser session still works");
2130
+ await refused$1("a removed member's CLI session read secrets", leaver.cli.secrets.list(DEV));
2131
+ expect(!(await signIn(deployment, leaver.browser, leaver.email)).ok, "a removed member signed in again");
2132
+ await admin.api.members.remove(service.member);
2133
+ await refused$1("a removed service's token read secrets", service.api.secrets.reveal(DEV));
2134
+ await admin.api.members.add(leaver.member);
2135
+ await admin.api.access.set(leaver.member, { [DEV]: "developer" });
2136
+ await admin.api.members.add(service.member);
2137
+ await admin.api.access.set(service.member, { [DEV]: "viewer" });
2138
+ expect((await leaver.browser.fetch("/api/me")).status === 401, "re-admission revived the browser session");
2139
+ await refused$1("re-admission revived the CLI session", leaver.cli.secrets.reveal(DEV));
2140
+ await refused$1("re-admission revived the service token", service.api.secrets.reveal(DEV));
2141
+ expect(!(await pollDevice(deployment, device.device_code)).ok, "re-admission revived the old device approval");
2142
+ expect((await signIn(deployment, leaver.browser, leaver.email)).ok, "a fresh provider callback could not sign the re-admitted member in");
2143
+ await admin.api.members.remove(leaver.member);
2144
+ await admin.api.members.remove(service.member);
2145
+ 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`;
2335
2146
  }
2336
- /** The person approves on the consent page, as its Approve does; the code comes back on the redirect. */
2337
- async function connect(deployment, person, clientId, scope = "browse") {
2338
- const verifier = base64url(randomBytes(32));
2339
- const request = {
2340
- client_id: clientId,
2341
- redirect_uri: REDIRECT,
2342
- response_type: "code",
2343
- code_challenge: base64url(createHash("sha256").update(verifier).digest()),
2344
- code_challenge_method: "S256",
2345
- state: STATE,
2346
- scope,
2347
- resource: `${deployment.origin}/mcp`
2348
- };
2349
- const view = await person.api.oauth.describe(request);
2350
- expect(view.status === "ready", "the consent page refused a good request", view);
2351
- expect(view.client.registration === "dcr" && view.client.name === NAME && view.scopes.join(" ") === scope && view.loopbackOnly, "the consent page does not show the registered client as it is", view);
2352
- const { redirect } = await person.api.oauth.decide({
2353
- request,
2354
- approve: true,
2355
- scopes: scope.split(" ")
2356
- });
2357
- const back = new URL(redirect);
2358
- expect(`${back.origin}${back.pathname}` === REDIRECT, `the answer went to ${back.origin}${back.pathname}, not the redirect`, redirect);
2359
- expect(back.searchParams.get("state") === STATE && back.searchParams.get("iss") === deployment.origin, "the answer does not carry the state as sent and the issuer", redirect);
2360
- const code = back.searchParams.get("code");
2361
- expect(code !== null, "an approval carried no code", redirect);
2362
- return {
2363
- code,
2364
- verifier
2147
+ /**
2148
+ * Grants on every project: one on `*` reaches a project made after it; one
2149
+ * on `*` and a slug reaches that environment in every project and never
2150
+ * another; nobody but an owner gives one; and removing a member takes
2151
+ * them. Its services are removed, and its project archived, as it ends.
2152
+ */
2153
+ async function everyProject(deployment, { admin, reader }, canaries) {
2154
+ const services = {
2155
+ everything: "token:conformance-everywhere",
2156
+ dev: "token:conformance-every-dev",
2157
+ manager: "token:conformance-manager"
2365
2158
  };
2159
+ const client = async (member) => bearer(deployment.origin, (await admin.api.tokens.issue(member, { expiresInDays: 1 })).token);
2160
+ for (const member of Object.values(services)) await admin.api.members.add(member);
2161
+ await admin.api.access.set(services.manager, { [PROJECT$1]: "access-manager" });
2162
+ await refused$1("a project's access manager granted on every project", (await client(services.manager)).access.set(services.everything, { "*": "viewer" }));
2163
+ await refused$1("a viewer granted themselves every project", reader.api.access.set(reader.member, { "*/prod": "viewer" }));
2164
+ await admin.api.access.set(services.everything, { "*": "viewer" });
2165
+ await admin.api.access.set(services.dev, { "*/dev": "viewer" });
2166
+ const [everything, dev] = [await client(services.everything), await client(services.dev)];
2167
+ const later = `${PROJECT$1}-later`;
2168
+ const made = await admin.api.projects.create(later, { name: "Made later" });
2169
+ expect(made.inherited.some((grant) => grant.member === services.everything && grant.place === "*"), "making a project did not say who reaches it", made.inherited);
2170
+ for (const environment of ["dev", "prod"]) {
2171
+ const path = `${later}/${environment}`;
2172
+ const { inherited } = await admin.api.environments.create(path, { name: environment });
2173
+ const reaches = inherited.filter((grant) => grant.member === services.dev).length;
2174
+ expect(reaches === (environment === "dev" ? 1 : 0), `making ${path} misnamed who reaches it through dev in every project`, inherited);
2175
+ canaries[`${path}/API_KEY`] = canary();
2176
+ await admin.api.secrets.set(path, valuesIn(canaries, path));
2177
+ }
2178
+ for (const path of [
2179
+ `${later}/prod`,
2180
+ `${later}/dev`,
2181
+ PROD
2182
+ ]) {
2183
+ const read = await everything.secrets.reveal(path);
2184
+ expect(read.values.API_KEY === canaries[`${path}/API_KEY`], `viewer on every project could not read ${path}`, read.values);
2185
+ }
2186
+ await refused$1("viewer on every project wrote a value", everything.secrets.set(`${later}/prod`, { API_KEY: "from everywhere" }));
2187
+ const devRead = await dev.secrets.reveal(`${later}/dev`);
2188
+ expect(devRead.values.API_KEY === canaries[`${later}/dev/API_KEY`], "viewer on dev in every project could not read a dev made later", devRead.values);
2189
+ await refused$1("viewer on dev in every project read a prod made later", dev.secrets.reveal(`${later}/prod`));
2190
+ await refused$1("viewer on dev in every project read prod", dev.secrets.reveal(PROD));
2191
+ for (const member of Object.values(services)) await admin.api.members.remove(member);
2192
+ await refused$1("a removed member read through a grant on every project", everything.secrets.reveal(`${later}/prod`));
2193
+ await refused$1("a removed member read through a grant on dev in every project", dev.secrets.reveal(`${later}/dev`));
2194
+ const left = (await admin.api.members.list("*")).members.filter((member) => Object.values(services).includes(member.member));
2195
+ expect(left.length === 0, "grants on every project outlived their members", left);
2196
+ await admin.api.projects.update(later, { archived: true });
2197
+ return "a grant on every project reaches a project made after it; one on dev, every dev and nothing else; refused to non-owners; gone with its member";
2366
2198
  }
2367
- async function redeem(deployment, clientId, { code, verifier }) {
2368
- const answer = await token(deployment, {
2369
- grant_type: "authorization_code",
2370
- code,
2371
- code_verifier: verifier,
2372
- redirect_uri: REDIRECT,
2373
- client_id: clientId,
2374
- resource: `${deployment.origin}/mcp`
2375
- });
2376
- const body = await json(answer);
2377
- expect(answer.status === 200 && typeof body.access_token === "string" && typeof body.refresh_token === "string", `redeeming the code answered ${answer.status}`, body);
2378
- expect(answer.headers.get("cache-control") === "no-store", "tokens were answered without cache-control: no-store");
2379
- return body;
2199
+ /** Reading more values at once than the vault allows in its window is refused. */
2200
+ async function bulkLimit$1({ admin, bulk }, limit) {
2201
+ const values = {};
2202
+ for (let i = 0; i <= limit; i++) values[`BULK_${i}`] = `bulk-${i}`;
2203
+ await admin.api.secrets.set(BULK, values);
2204
+ const refusal = await refused$1(`${limit + 1} values were revealed at once`, bulk.api.secrets.reveal(BULK));
2205
+ expect(refusal.status === 403 && refusal.code === "bulk_limit" && refusal.reason === "bulk_limit", "the bulk read was not refused as bulk_limit", refusal);
2206
+ const one = await bulk.api.secrets.reveal(`${BULK}/BULK_0`);
2207
+ expect(one.values.BULK_0 === "bulk-0", "a viewer on bulk could not read one of its values", one.values);
2208
+ return `${limit + 1} values at once: ${refusal.status} ${refusal.reason ?? refusal.code}; one still opens`;
2380
2209
  }
2381
- async function refused(deployment, what, fields) {
2382
- const answer = await token(deployment, fields);
2383
- const body = await json(answer);
2384
- expect(answer.status === 400 && body.error === "invalid_grant", `${what} answered ${answer.status}, not invalid_grant`, body);
2210
+ function same(a, b) {
2211
+ return JSON.stringify(sorted(a)) === JSON.stringify(sorted(b));
2385
2212
  }
2386
- async function discovers(deployment, access) {
2387
- const answer = await mcp(deployment, {
2388
- authorization: `Bearer ${access}`,
2389
- "mcp-protocol-version": "2026-07-28",
2390
- "mcp-method": "server/discover"
2391
- }, "server/discover", { _meta: ENVELOPE });
2392
- if (answer.status === 401) return false;
2393
- const body = await json(answer);
2394
- expect(answer.status === 200 && Array.isArray(body.result?.supportedVersions), `server/discover answered ${answer.status}`, body);
2395
- return true;
2213
+ function sorted(value) {
2214
+ if (Array.isArray(value) || value === null || typeof value !== "object") return value;
2215
+ return Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : 1));
2396
2216
  }
2397
- async function mcpConnect(deployment, people) {
2398
- const { origin } = deployment;
2399
- const anonymous = await mcp(deployment, {});
2400
- const challenge = anonymous.headers.get("www-authenticate") ?? "";
2401
- expect(anonymous.status === 401, `/mcp answered ${anonymous.status} without a token, not 401`);
2402
- expect(challenge.includes(`resource_metadata="${origin}/.well-known/oauth-protected-resource/mcp"`) && challenge.includes("scope=\"browse\""), "the 401's challenge does not name the resource metadata and Browse", challenge);
2403
- const resource = await json(await fetch(`${origin}/.well-known/oauth-protected-resource/mcp`));
2404
- expect(resource.resource === `${origin}/mcp` && resource.authorization_servers?.[0] === origin, "the protected resource metadata does not name /mcp and coffre as its issuer", resource);
2405
- const server = await json(await fetch(`${origin}/.well-known/oauth-authorization-server`));
2406
- expect(server.issuer === origin && server.authorization_endpoint === `${origin}/oauth/authorize`, "the authorization server metadata is wrong", server);
2407
- for (const [field, value] of [
2408
- ["code_challenge_methods_supported", "S256"],
2409
- ["token_endpoint_auth_methods_supported", "none"],
2410
- ["grant_types_supported", "refresh_token"]
2411
- ]) expect(server[field]?.includes(value), `the authorization server metadata leaves ${value} out of ${field}`, server);
2412
- expect(server.client_id_metadata_document_supported === true, "the authorization server metadata does not offer metadata documents", server);
2413
- const registered = await fetch(`${origin}/api/oauth/register`, {
2414
- method: "POST",
2415
- headers: { "content-type": "application/json" },
2416
- body: JSON.stringify({
2417
- client_name: NAME,
2418
- redirect_uris: [REDIRECT, "conformance-app://callback"],
2419
- token_endpoint_auth_method: "none"
2420
- })
2421
- });
2422
- const client = await json(registered);
2423
- expect(registered.status === 201 && typeof client.client_id === "string", `registering answered ${registered.status}`, client);
2424
- expect(JSON.stringify(client.redirect_uris) === JSON.stringify([REDIRECT]), "the registration kept a redirect it should have left out", client);
2425
- const clientId = client.client_id;
2426
- const page = await people.reader.browser.fetch(`/oauth/authorize?${new URLSearchParams({
2427
- client_id: clientId,
2428
- redirect_uri: REDIRECT
2429
- })}`);
2430
- expect(page.status === 200, `the consent page answered ${page.status}`);
2431
- const forged = await people.reader.browser.fetch("/api/oauth/authorizations", {
2432
- method: "POST",
2433
- headers: {
2434
- origin: "https://attacker.example",
2435
- "sec-fetch-site": "cross-site",
2436
- "content-type": "application/json"
2437
- },
2438
- body: JSON.stringify({
2439
- request: {
2440
- client_id: clientId,
2441
- redirect_uri: REDIRECT
2442
- },
2443
- approve: true
2444
- })
2445
- });
2446
- expect(forged.status === 403, `another site's approval answered ${forged.status}, not 403`);
2447
- const first = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
2448
- expect(first.access_token.startsWith("coffre_mcp_") && first.refresh_token.startsWith("coffre_mcr_") && first.scope === "browse", "the tokens are not the shapes and scope coffre issues", { scope: first.scope });
2449
- expect(await discovers(deployment, first.access_token), "a fresh access token was refused at /mcp");
2450
- const atApi = await fetch(`${origin}/api/me`, { headers: { authorization: `Bearer ${first.access_token}` } });
2451
- expect(atApi.status === 401, `an MCP token at /api answered ${atApi.status}, not 401`);
2452
- const cli = await mcp(deployment, { authorization: `Bearer ${people.leaver.cliToken}` });
2453
- expect(cli.status === 401, `a CLI session at /mcp answered ${cli.status}, not 401`);
2454
- const browser = await people.reader.browser.fetch("/mcp", {
2455
- method: "POST",
2456
- headers: {
2457
- "content-type": "application/json",
2458
- origin
2459
- },
2460
- body: JSON.stringify({
2461
- jsonrpc: "2.0",
2462
- id: 1,
2463
- method: "server/discover"
2464
- })
2217
+ //#endregion
2218
+ //#region src/checks/audit.ts
2219
+ /** One read per returned value, committed by the vault under the response's operation. */
2220
+ async function revealAudited(deployment, { admin, reader }, canaries, purpose) {
2221
+ const path = purpose === "reveal" ? `${DEV}/API_KEY` : DEV;
2222
+ const { operationId, values } = await reader.api.secrets.reveal(path);
2223
+ expect(typeof operationId === "string" && operationId.length > 0, "the read returned no operation id");
2224
+ const expected = purpose === "reveal" ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
2225
+ expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), "the read returned different values", values);
2226
+ const bundle = (await everyAuditEntry(admin.api)).filter((entry) => entry.operationId === operationId);
2227
+ const keys = bundle.map((entry) => entry.key).sort();
2228
+ expect(JSON.stringify(keys) === JSON.stringify(Object.keys(expected).sort()), "the read is not logged once per value", bundle);
2229
+ expect(bundle.every((entry) => entry.author === "vault" && entry.action === "secret.read" && entry.decision === "allow"), "the read's entries are not the vault's allowed reads", bundle);
2230
+ expect(bundle.every((entry) => entry.metadata.purpose === purpose && entry.requestId !== null && entry.requestId === bundle[0].requestId), "the reads do not name one request and its purpose", bundle);
2231
+ const { keys: listed } = await admin.api.secrets.list(DEV);
2232
+ for (const entry of bundle) expect(entry.version === listed.find((key) => key.key === entry.key)?.version, `the entry for ${String(entry.key)} names another version`, entry);
2233
+ await using(deployment.database(), async (sql) => {
2234
+ const rows = await query(sql, `SELECT l.author, l.action, l.decision, l.request_id, l.metadata, s.key, s.current_version
2235
+ FROM audit_log l LEFT JOIN secrets s ON s.id = l.secret_id WHERE l.operation_id = $1`, [operationId]);
2236
+ expect(JSON.stringify(rows.map((row) => row.key).sort()) === JSON.stringify(Object.keys(expected).sort()), "the committed read is not once per value", rows);
2237
+ for (const row of rows) {
2238
+ const metadata = JSON.parse(row.metadata);
2239
+ expect(row.author === "vault" && row.action === "secret.read" && row.decision === "allow", "the committed read is not the vault release", row);
2240
+ expect(row.request_id !== null && row.request_id === rows[0].request_id && metadata.purpose === purpose, "the committed reads have different requests or purposes", rows);
2241
+ expect(metadata.version === row.current_version, "the committed read names another version", row);
2242
+ }
2465
2243
  });
2466
- expect(browser.status === 401, `a browser's cookie at /mcp answered ${browser.status}, not 401`);
2467
- const foreign = await mcp(deployment, {
2468
- authorization: `Bearer ${first.access_token}`,
2469
- origin: "https://attacker.example"
2244
+ return `${bundle.length} values, each read once by the vault for ${purpose}, under ${operationId}`;
2245
+ }
2246
+ /** Full verification must reach the stored head, not just some intact prefix. */
2247
+ async function verification(deployment, { admin }) {
2248
+ const result = await admin.api.audit.verify();
2249
+ expect(result.ok, "the full log does not verify", result);
2250
+ await using(deployment.database(), async (sql) => {
2251
+ const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
2252
+ const [last] = await sql.query("SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1");
2253
+ expect(head !== void 0 && last !== void 0, "the head or newest entry is missing");
2254
+ expect(BigInt(head.next_seq) === BigInt(last.seq) + 1n, "the head does not name the newest entry");
2255
+ expect(Buffer.from(head.head_hash).equals(Buffer.from(last.hash)), "the head has another hash");
2256
+ expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), "verification stopped before the head", {
2257
+ result,
2258
+ head: head.next_seq
2259
+ });
2260
+ expect(BigInt(result.entries) === BigInt(head.next_seq), "verification did not count every entry", result);
2470
2261
  });
2471
- expect(foreign.status === 403, `/mcp answered ${foreign.status} to another site's page, not 403`);
2472
- const rotate = (refresh) => token(deployment, {
2473
- grant_type: "refresh_token",
2474
- refresh_token: refresh,
2475
- client_id: clientId
2262
+ return `${result.entries} entries verified through the head at ${result.through}`;
2263
+ }
2264
+ /**
2265
+ * Each heartbeat has the vault sign the log up to its last entry, in an
2266
+ * entry of its own, after checking that the prefix it signed last is still
2267
+ * there. Two, so the second covers the first; then the log is verified from
2268
+ * its first entry, every checkpoint with it.
2269
+ */
2270
+ async function checkpoints(deployment, { admin }) {
2271
+ const before = await admin.api.audit.verify();
2272
+ expect(before.ok, "the log does not verify", before);
2273
+ await deployment.scheduled();
2274
+ await deployment.scheduled();
2275
+ const after = await admin.api.audit.verify();
2276
+ expect(after.ok && after.checkpoint !== null, "the audit log is not verified and checkpointed", after);
2277
+ expect(after.checkpoint.seq > (before.through ?? -1), "the checkpoint does not cover what was written before it", {
2278
+ before,
2279
+ after
2476
2280
  });
2477
- const rotated = await rotate(first.refresh_token);
2478
- const next = await json(rotated);
2479
- expect(rotated.status === 200 && next.refresh_token !== first.refresh_token, `refreshing answered ${rotated.status}, or the same refresh token`);
2480
- expect(await discovers(deployment, next.access_token), "a refreshed access token was refused");
2481
- await refused(deployment, "a replaced refresh token", {
2482
- grant_type: "refresh_token",
2483
- refresh_token: first.refresh_token,
2484
- client_id: clientId
2281
+ const { entries } = await admin.api.audit.list({
2282
+ detail: "1",
2283
+ limit: 20
2485
2284
  });
2486
- await refused(deployment, "the current refresh token, after a reuse", {
2487
- grant_type: "refresh_token",
2488
- refresh_token: next.refresh_token,
2489
- client_id: clientId
2285
+ const signed = entries.filter((entry) => entry.action === "audit.checkpoint" && entry.decision === "allow");
2286
+ expect(signed.length >= 2 && signed.every((entry) => entry.author === "vault" && entry.detail), "the checkpoints are not the vault's entries, hidden as detail", signed);
2287
+ const [newest, previous] = signed;
2288
+ expect(newest.metadata.seq === after.checkpoint.seq, "the newest checkpoint entry is not the one verified", {
2289
+ signed,
2290
+ after
2490
2291
  });
2491
- expect(!await discovers(deployment, next.access_token), "an access token outlived its connection, ended by a reused refresh token");
2492
- const asked = await connect(deployment, people.reader, clientId);
2493
- const second = await redeem(deployment, clientId, asked);
2494
- await refused(deployment, "a code redeemed already", {
2495
- grant_type: "authorization_code",
2496
- code: asked.code,
2497
- code_verifier: asked.verifier,
2498
- redirect_uri: REDIRECT,
2499
- client_id: clientId
2292
+ expect(previous.seq <= after.checkpoint.seq, "the newest checkpoint does not cover the one before it", signed);
2293
+ return `${after.entries} entries verified through ${after.through}, checkpointed at ${after.checkpoint.seq}`;
2294
+ }
2295
+ /**
2296
+ * Every version the app says it stored names the vault's entry for the key
2297
+ * it was sealed with, `key.wrap` for a write and `key.rewrap` for a restore:
2298
+ * the same member, request and operation, the same secret and version. And
2299
+ * every value read is the vault's to log: the app keeps no reads of its own.
2300
+ */
2301
+ async function writesAgree(deployment) {
2302
+ return using(deployment.database(), async (sql) => {
2303
+ const entries = await sql.query("SELECT * FROM audit_log ORDER BY seq");
2304
+ const bySeq = new Map(entries.map((entry) => [String(entry.seq), entry]));
2305
+ const writes = entries.filter((entry) => entry.author === "app" && entry.decision === "allow" && (entry.action === "secret.write" || entry.action === "secret.restore"));
2306
+ 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);
2307
+ for (const entry of writes) {
2308
+ const seal = entry.action === "secret.write" ? "key.wrap" : "key.rewrap";
2309
+ const key = entry.related_seq === null ? void 0 : bySeq.get(String(entry.related_seq));
2310
+ expect(key !== void 0 && key.author === "vault" && key.action === seal && key.decision === "allow", `a ${entry.action} names no ${seal} of the vault's`, {
2311
+ entry,
2312
+ key
2313
+ });
2314
+ for (const field of [
2315
+ "actor",
2316
+ "request_id",
2317
+ "operation_id",
2318
+ "project_id",
2319
+ "environment_id"
2320
+ ]) expect(entry[field] === key[field], `the write and its seal have different ${field}`, {
2321
+ entry,
2322
+ key
2323
+ });
2324
+ expect(entry.operation_id !== null && entry.request_id !== null, "the committed write has no operation or request", entry);
2325
+ const metadata = JSON.parse(entry.metadata);
2326
+ const sealed = JSON.parse(key.metadata);
2327
+ 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", {
2328
+ entry,
2329
+ key
2330
+ });
2331
+ }
2332
+ const versions = await query(sql, `SELECT v.secret_id, v.version FROM secret_versions v JOIN secrets s ON s.id = v.secret_id
2333
+ JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT$1]);
2334
+ expect(versions.length > 0, "no stored versions were inspected");
2335
+ for (const version of versions) {
2336
+ const matching = writes.filter((entry) => entry.secret_id === version.secret_id && JSON.parse(entry.metadata).version === version.version);
2337
+ expect(matching.length === 1, "a stored version has no unique app write entry", {
2338
+ version,
2339
+ matching
2340
+ });
2341
+ }
2342
+ return `${versions.length} stored versions, each with one app write naming the vault's seal in the same operation`;
2500
2343
  });
2501
- expect(!await discovers(deployment, second.access_token), "an access token outlived its connection, ended by a reused code");
2502
- const third = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
2503
- const listed = (await people.reader.api.apps.list()).apps;
2504
- expect(listed.length === 1 && listed[0].name === NAME && listed[0].registration === "dcr", "Connected apps does not list the one live connection", listed);
2505
- await people.reader.api.apps.disconnect(listed[0].id);
2506
- expect(!await discovers(deployment, third.access_token), "an access token outlived its disconnection");
2507
- expect((await people.reader.api.apps.list()).apps.length === 0, "a disconnected app is still listed");
2508
- const { entries } = await people.admin.api.audit.list({
2509
- actor: people.reader.member,
2510
- detail: "1",
2511
- limit: 500
2344
+ }
2345
+ /**
2346
+ * No value leaves without its entry: with the audit log refusing writes (a
2347
+ * trigger, as a full disk or a lost connection would), the vault cannot log
2348
+ * the read, so a reveal fails and carries nothing.
2349
+ */
2350
+ async function noAuditNoValue(deployment, { admin }, canaries) {
2351
+ let status = 0;
2352
+ await using(deployment.database(), (sql) => logRefuses(sql, async () => {
2353
+ const before = deployment.output().split(LOG_REFUSAL).length;
2354
+ const response = await admin.browser.send("POST", "/api/reveals", { path: DEV });
2355
+ const text = await response.text();
2356
+ status = response.status;
2357
+ const body = JSON.parse(text);
2358
+ expect(status === 500 && body.error === "internal_error" && body.reason === void 0, "the unlogged reveal did not fail as internal_error", {
2359
+ status,
2360
+ body
2361
+ });
2362
+ expect(body.message === "something went wrong; see the server log", "the audit failure exposed another reason", body);
2363
+ expect(!Object.values(canaries).some((value) => text.includes(value)), "a reveal that could not be logged carried a value", text);
2364
+ await until("the injected audit refusal in the processes' output", async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
2365
+ }, "secret.read"));
2366
+ 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");
2367
+ return `a reveal the audit log would not take: ${status} internal_error, its injected cause in the log, and no value`;
2368
+ }
2369
+ /** A publicly chained entry still needs the vault's MAC. */
2370
+ async function forgedVaultEntry(deployment, { admin }) {
2371
+ const verify = () => admin.api.audit.verify();
2372
+ expect((await verify()).ok, "the log does not verify before the forged entry");
2373
+ await using(deployment.database(), async (sql) => {
2374
+ const [head] = await sql.query("SELECT next_seq, head_hash FROM audit_chain_head");
2375
+ const [writer] = await sql.query("SELECT key_id FROM audit_log WHERE author = 'vault' ORDER BY seq DESC LIMIT 1");
2376
+ expect(writer !== void 0, "no vault key id to copy");
2377
+ const seq = BigInt(head.next_seq);
2378
+ const forged = {
2379
+ seq,
2380
+ author: "vault",
2381
+ key_id: writer.key_id,
2382
+ occurred_at: Date.now(),
2383
+ actor: "user:forger@conformance.example",
2384
+ action: "secret.read",
2385
+ decision: "allow",
2386
+ metadata: "{}"
2387
+ };
2388
+ const prevHash = Buffer.from(head.head_hash);
2389
+ const mac = Buffer.alloc(32, 65);
2390
+ const hash = createHash("sha256").update("coffre.audit.chain.v2").update(prevHash).update(entryFields(forged)).update(mac).digest();
2391
+ await insertRow(sql, "audit_log", {
2392
+ ...forged,
2393
+ prev_hash: prevHash,
2394
+ mac,
2395
+ hash
2396
+ });
2397
+ await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq + 1n, hash]);
2398
+ let inserted;
2399
+ try {
2400
+ inserted = await verify();
2401
+ } finally {
2402
+ await appendOnlyLifted(sql, () => query(sql, "DELETE FROM audit_log WHERE seq = $1", [seq]));
2403
+ await query(sql, "UPDATE audit_chain_head SET next_seq = $1, head_hash = $2", [seq, prevHash]);
2404
+ }
2405
+ 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);
2406
+ const removed = await verify();
2407
+ expect(removed.ok, "the log did not verify once the entry was gone", removed);
2512
2408
  });
2513
- const mine = entries.filter((entry) => entry.metadata.clientId === clientId);
2514
- const count = (action) => mine.filter((entry) => entry.action === action && entry.decision === "allow").length;
2515
- const counts = {
2516
- connect: count("mcp.connect"),
2517
- token: count("mcp.token"),
2518
- disconnect: count("mcp.disconnect")
2519
- };
2520
- expect(counts.connect === 3 && counts.token === 4 && counts.disconnect === 3, "the log does not hold each connection, token and disconnection", counts);
2521
- const reasons = mine.filter((entry) => entry.action === "mcp.disconnect").map((entry) => entry.metadata.reason).sort();
2522
- expect(JSON.stringify(reasons) === JSON.stringify([
2523
- "code_reused",
2524
- "person",
2525
- "refresh_reused"
2526
- ]), "the disconnections do not say why", reasons);
2527
- 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";
2409
+ return "a publicly chained vault entry with a made-up MAC was refused at its sequence";
2410
+ }
2411
+ /** Rewrite either author's entry and require a fault at that exact sequence. */
2412
+ async function rewrittenEntry(deployment, { admin }, author) {
2413
+ expect((await admin.api.audit.verify()).ok, "the log does not verify before the rewrite");
2414
+ let seq = 0;
2415
+ await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2416
+ const [row] = await query(sql, "SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1", [author]);
2417
+ expect(row !== void 0, `no ${author} entry to rewrite`);
2418
+ seq = Number(row.seq);
2419
+ await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", ["user:nobody@conformance.example", row.seq]);
2420
+ try {
2421
+ const result = await admin.api.audit.verify();
2422
+ expect(!result.ok && result.failedAtSeq === Number(row.seq), "a rewritten entry was not caught at its sequence", result);
2423
+ } finally {
2424
+ await query(sql, "UPDATE audit_log SET actor = $1 WHERE seq = $2", [row.actor, row.seq]);
2425
+ }
2426
+ }));
2427
+ expect((await admin.api.audit.verify()).ok, "the restored entry did not verify");
2428
+ return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
2429
+ }
2430
+ /** Remove an unreferenced entry, then put its exact bytes back. */
2431
+ async function missingEntry(deployment, { admin }, position) {
2432
+ expect((await admin.api.audit.verify()).ok, "the log does not verify before deletion");
2433
+ let seq = 0;
2434
+ await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2435
+ 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`);
2436
+ expect(row !== void 0, "no unreferenced entry to delete");
2437
+ seq = Number(row.seq);
2438
+ const linked = await query(sql, "SELECT * FROM audit_log WHERE related_seq = $1", [row.seq]);
2439
+ await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE related_seq = $1", [row.seq]);
2440
+ await query(sql, "DELETE FROM audit_log WHERE seq = $1", [row.seq]);
2441
+ try {
2442
+ const result = await admin.api.audit.verify();
2443
+ expect(!result.ok, "a log with an entry missing verifies", result);
2444
+ if (position !== "middle") expect(result.reason.includes(`sequence gap: expected seq ${position === "first" ? 0 : 1e3}`), "the sequence gap was not reported", result);
2445
+ } finally {
2446
+ await insertRow(sql, "audit_log", row);
2447
+ for (const entry of linked) await query(sql, "UPDATE audit_log SET related_seq = $1 WHERE seq = $2", [entry.related_seq, entry.seq]);
2448
+ }
2449
+ }));
2450
+ expect((await admin.api.audit.verify()).ok, "the restored log did not verify");
2451
+ const which = {
2452
+ first: "the first",
2453
+ batch: "the first past a page of 1,000",
2454
+ middle: "a heartbeat"
2455
+ }[position];
2456
+ return `entry ${seq}, ${which}, deleted: caught, then put back`;
2457
+ }
2458
+ /** Last: keep the head, delete its newest entries, and require verification to notice. */
2459
+ async function deletedTail(deployment, { admin }) {
2460
+ const intact = await admin.api.audit.verify();
2461
+ expect(intact.ok && intact.checkpoint !== null, "the log is not intact and checkpointed");
2462
+ const signed = intact.checkpoint.seq;
2463
+ await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2464
+ await query(sql, "UPDATE audit_log SET related_seq = NULL WHERE seq >= $1", [signed]);
2465
+ await query(sql, "DELETE FROM audit_log WHERE seq >= $1", [signed]);
2466
+ }));
2467
+ const result = await admin.api.audit.verify();
2468
+ expect(!result.ok && result.author === "app" && result.reason.includes("removed from the end"), "the log verifies with its newest entries deleted", result);
2469
+ return "the newest entries deleted, with the head left alone, were caught";
2528
2470
  }
2529
- /** A 2026-07-28 request as a client sends it: its headers say what its body does; `capabilities` are the client's. */
2530
- async function modern(deployment, access, method, params = {}, name, capabilities = {}) {
2531
- const response = await mcp(deployment, {
2532
- authorization: `Bearer ${access}`,
2533
- "mcp-protocol-version": "2026-07-28",
2534
- "mcp-method": method,
2535
- ...name === void 0 ? {} : { "mcp-name": name }
2536
- }, method, {
2537
- ...params,
2538
- _meta: {
2539
- ...ENVELOPE,
2540
- "io.modelcontextprotocol/clientCapabilities": capabilities
2471
+ /** The whole log, detail included, newest first. */
2472
+ async function everyAuditEntry(api) {
2473
+ const all = [];
2474
+ for (let before;;) {
2475
+ const { entries } = await api.audit.list({
2476
+ before,
2477
+ limit: 500,
2478
+ detail: "1"
2479
+ });
2480
+ all.push(...entries);
2481
+ if (entries.length < 500) return all;
2482
+ before = entries.at(-1).seq;
2483
+ }
2484
+ }
2485
+ //#endregion
2486
+ //#region src/checks/checkpoints.ts
2487
+ /** Leave one old signature invalid, reseal the chain, and make later checkpoints valid. */
2488
+ async function earlierCheckpoint(deployment, { admin }) {
2489
+ expect((await admin.api.audit.verify()).ok, "the log does not verify before checkpoint tampering");
2490
+ await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
2491
+ const rows = await sql.query("SELECT * FROM audit_log ORDER BY seq");
2492
+ const signed = rows.filter((row) => row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow");
2493
+ expect(signed.length >= 2, "two checkpoints are needed");
2494
+ const [head] = await sql.query("SELECT * FROM audit_chain_head");
2495
+ const seed = Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.VAULT_KEY, "base64"), Buffer.alloc(0), "coffre.vault.signing-key.v1", 32));
2496
+ const signingKey = createPrivateKey({
2497
+ key: Buffer.concat([Buffer.from("302e020100300506032b657004220420", "hex"), seed]),
2498
+ format: "der",
2499
+ type: "pkcs8"
2500
+ });
2501
+ const keys = {
2502
+ app: Buffer.from(hkdfSync("sha256", Buffer.from(KEYS.APP_KEY, "base64"), Buffer.alloc(0), "coffre.audit.app.v2", 32)),
2503
+ vault: Buffer.from(hkdfSync("sha256", seed, Buffer.alloc(0), "coffre.audit.vault.v2", 32))
2504
+ };
2505
+ const hashes = /* @__PURE__ */ new Map();
2506
+ let previous = Buffer.alloc(32);
2507
+ try {
2508
+ await transaction(sql, async () => {
2509
+ for (const original of rows) {
2510
+ const row = { ...original };
2511
+ if (row.author === "vault" && row.action === "audit.checkpoint" && row.decision === "allow") {
2512
+ const checkpoint = JSON.parse(String(row.metadata));
2513
+ if (row.seq === signed[0].seq) checkpoint.signature = Buffer.alloc(64).toString("base64");
2514
+ else {
2515
+ checkpoint.hash = hashes.get(BigInt(checkpoint.seq));
2516
+ checkpoint.signature = sign(null, Buffer.from(`coffre.checkpoint.v3|${checkpoint.seq}|${checkpoint.hash}|${checkpoint.signedAt}`), signingKey).toString("base64");
2517
+ }
2518
+ row.metadata = JSON.stringify(checkpoint);
2519
+ }
2520
+ const fields = entryFields(row);
2521
+ row.prev_hash = previous;
2522
+ row.mac = createHmac("sha256", keys[row.author]).update("coffre.audit.mac.v2").update(previous).update(fields).digest();
2523
+ previous = createHash("sha256").update("coffre.audit.chain.v2").update(previous).update(fields).update(row.mac).digest();
2524
+ row.hash = previous;
2525
+ hashes.set(BigInt(String(row.seq)), previous.toString("hex"));
2526
+ await restoreRow(sql, "audit_log", row, "seq");
2527
+ }
2528
+ await restoreRow(sql, "audit_chain_head", {
2529
+ ...head,
2530
+ head_hash: previous
2531
+ }, "only_row");
2532
+ });
2533
+ const result = await admin.api.audit.verify();
2534
+ 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);
2535
+ } finally {
2536
+ await transaction(sql, async () => {
2537
+ for (const row of rows) await restoreRow(sql, "audit_log", row, "seq");
2538
+ await restoreRow(sql, "audit_chain_head", head, "only_row");
2539
+ });
2540
+ keys.app.fill(0);
2541
+ keys.vault.fill(0);
2542
+ seed.fill(0);
2541
2543
  }
2542
- });
2544
+ }));
2545
+ expect((await admin.api.audit.verify()).ok, "restoring the signed prefixes did not restore verification");
2546
+ return "an invalid earlier checkpoint was caught despite valid MACs and a valid later checkpoint";
2547
+ }
2548
+ //#endregion
2549
+ //#region src/checks/deletion.ts
2550
+ const PROJECT = "conformance-gone";
2551
+ const SERVICE$1 = "token:conformance-gone-ci";
2552
+ /** Each version under a project, by whether it still holds a ciphertext or a wrapped key. */
2553
+ async function sealed(deployment, projectId) {
2554
+ const rows = await using(deployment.database(), (sql) => query(sql, `SELECT length(v.ciphertext) AS ciphertext, length(v.wrapped_dek) AS wrapped
2555
+ FROM secret_versions v JOIN secrets s ON s.id = v.secret_id WHERE s.project_id = $1`, [projectId]));
2543
2556
  return {
2544
- response,
2545
- body: await json(response)
2557
+ versions: rows.length,
2558
+ sealed: rows.filter((row) => Number(row.ciphertext) > 0 || Number(row.wrapped) > 0).length
2546
2559
  };
2547
2560
  }
2548
- /** A client registered for one check, by the name every check's client has. */
2549
- async function register(deployment) {
2550
- return (await json(await fetch(`${deployment.origin}/api/oauth/register`, {
2551
- method: "POST",
2552
- headers: { "content-type": "application/json" },
2553
- body: JSON.stringify({
2554
- client_name: NAME,
2555
- redirect_uris: [REDIRECT]
2556
- })
2557
- }))).client_id;
2561
+ async function projectId(deployment, slug) {
2562
+ const [row] = await using(deployment.database(), (sql) => query(sql, "SELECT id FROM projects WHERE slug = $1", [slug]));
2563
+ return row?.id;
2558
2564
  }
2559
- /**
2560
- * The Browse tools, as a client calls them (design section 14, checks 1, 4
2561
- * and 5): on 2026-07-28 and through a 2025-11-25 `initialize`, as the
2562
- * reader, who sees dev and not prod. No value appears in any answer, the
2563
- * headers must agree with the body, and every call is in the log under the
2564
- * client, the API's own entries naming the connection.
2565
- */
2566
- async function mcpBrowse(deployment, people, canaries) {
2567
- const clientId = await register(deployment);
2568
- const { access_token: access } = await redeem(deployment, clientId, await connect(deployment, people.reader, clientId));
2569
- const answers = [];
2570
- const discovered = await modern(deployment, access, "server/discover");
2571
- answers.push(JSON.stringify(discovered.body));
2572
- expect(discovered.response.status === 200, `server/discover answered ${discovered.response.status}`, discovered.body);
2573
- const listed = await modern(deployment, access, "tools/list");
2574
- const result = listed.body.result;
2575
- const names = result?.tools?.map((tool) => tool.name) ?? [];
2576
- expect(names.includes("list_secrets") && names.includes("run_with_secrets") && result?.cacheScope === "public" && typeof result.ttlMs === "number", "tools/list does not list the Browse tools, cacheable", listed.body);
2577
- const callTool = async (tool, args) => {
2578
- const { response, body } = await modern(deployment, access, "tools/call", {
2579
- name: tool,
2580
- arguments: args
2581
- }, tool);
2582
- answers.push(JSON.stringify(body));
2583
- expect(response.status === 200, `tools/call ${tool} answered ${response.status}`, body);
2584
- return body.result;
2565
+ async function deletion(deployment, cli, admin) {
2566
+ const run = async (args, code = 0) => {
2567
+ const result = await cli.run(args);
2568
+ expect(result.code === code, `coffre ${args.join(" ")} exited ${result.code}, not ${code}`, result.output);
2569
+ return result.output;
2585
2570
  };
2586
- const keys = ((await callTool("list_secrets", { environment: DEV })).structuredContent?.keys ?? []).map((key) => key.key);
2587
- expect(["API_KEY", "DATABASE_URL"].every((key) => keys.includes(key)), `list_secrets on ${DEV} does not list its keys`, keys);
2588
- const prod = await callTool("list_secrets", { environment: PROD });
2589
- expect(prod.isError === true, `the reader listed ${PROD}'s keys through MCP, which they cannot`, prod);
2590
- const run = await callTool("run_with_secrets", {
2591
- environment: DEV,
2592
- command: "npm test"
2593
- });
2594
- expect(run.content?.[0]?.text?.includes(`coffre run ${DEV} -- npm test`) === true, "run_with_secrets does not say how to run the command", run);
2595
- for (const tool of ["whoami", "list_projects"]) await callTool(tool, {});
2596
- await callTool("secret_history", { secret: `${DEV}/API_KEY` });
2597
- const mismatched = await mcp(deployment, {
2598
- authorization: `Bearer ${access}`,
2599
- "mcp-protocol-version": "2026-07-28",
2600
- "mcp-method": "tools/call",
2601
- "mcp-name": "list_projects"
2602
- }, "tools/call", {
2603
- name: "whoami",
2604
- arguments: {},
2605
- _meta: ENVELOPE
2606
- });
2607
- const mismatch = await json(mismatched);
2608
- expect(mismatched.status === 400 && mismatch.error?.code === -32020, `a Mcp-Name naming another tool answered ${mismatched.status}`, mismatch);
2609
- const initialized = await mcp(deployment, { authorization: `Bearer ${access}` }, "initialize", {
2610
- protocolVersion: "2025-11-25",
2611
- capabilities: {},
2612
- clientInfo: {
2613
- name: "coffre-conformance",
2614
- version: "1"
2615
- }
2616
- });
2617
- const init = await json(initialized);
2618
- expect(initialized.status === 200 && init.result?.protocolVersion === "2025-11-25", `initialize for 2025-11-25 answered ${initialized.status}`, init);
2619
- expect(initialized.headers.get("mcp-session-id") === null, "initialize minted a session: coffre keeps none");
2620
- const legacy = await mcp(deployment, {
2621
- authorization: `Bearer ${access}`,
2622
- "mcp-protocol-version": "2025-11-25"
2623
- }, "tools/call", {
2624
- name: "list_secrets",
2625
- arguments: { environment: DEV }
2626
- });
2627
- const legacyBody = await json(legacy);
2628
- answers.push(JSON.stringify(legacyBody));
2629
- expect(legacy.status === 200 && legacyBody.result?.isError !== true, `a 2025-11-25 tools/call answered ${legacy.status}`, legacyBody);
2630
- const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
2631
- expect(leaked.length === 0, "a Browse tool answered with a secret value", leaked);
2632
- const { entries } = await people.admin.api.audit.list({
2633
- actor: people.reader.member,
2634
- detail: "1",
2635
- limit: 500
2636
- });
2637
- const mine = entries.filter((entry) => entry.metadata.via?.clientId === clientId);
2638
- const reads = mine.filter((entry) => entry.action === "mcp.read").length;
2639
- const refused = mine.filter((entry) => entry.action === "mcp.call" && entry.decision === "deny").length;
2640
- expect(reads === 6 && refused === 1, "the log does not hold each call under the client", {
2641
- reads,
2642
- refused
2643
- });
2644
- 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));
2645
- const shown = (await people.admin.api.audit.list({
2646
- actor: people.reader.member,
2571
+ const value = () => `deleted-value-${randomBytes(8).toString("hex")}`;
2572
+ await admin.api.projects.create(PROJECT, { name: "Gone" });
2573
+ for (const environment of ["prod", "dev"]) {
2574
+ await admin.api.environments.create(`${PROJECT}/${environment}`, { name: environment });
2575
+ await admin.api.secrets.set(`${PROJECT}/${environment}`, {
2576
+ API_KEY: value(),
2577
+ DATABASE_URL: value()
2578
+ });
2579
+ }
2580
+ await admin.api.secrets.set(`${PROJECT}/prod`, { API_KEY: value() });
2581
+ await admin.api.members.add(SERVICE$1);
2582
+ await admin.api.access.set(SERVICE$1, { [`${PROJECT}/prod`]: "viewer" });
2583
+ const id = await projectId(deployment, PROJECT);
2584
+ const live = await run([
2585
+ "projects",
2586
+ "delete",
2587
+ PROJECT,
2588
+ "--apply"
2589
+ ], 1);
2590
+ expect(/not archived/.test(live), "deleting a live project was not refused as not archived", live);
2591
+ await run([
2592
+ "projects",
2593
+ "archive",
2594
+ PROJECT
2595
+ ]);
2596
+ const preview = await run([
2597
+ "projects",
2598
+ "delete",
2599
+ PROJECT
2600
+ ]);
2601
+ expect(/erased: 5 versions/.test(preview) && /revoked: 1 grant/.test(preview) && preview.includes("service:conformance-gone-ci would hold nothing anywhere"), "the preview does not say what the deletion takes", preview);
2602
+ expect(/Backups taken before the deletion still hold the encrypted values/.test(preview) && /Nothing changed/.test(preview), "the preview does not warn of backups, or changed something", preview);
2603
+ expect((await sealed(deployment, id)).sealed === 5, "the preview erased something");
2604
+ const done = await run([
2605
+ "projects",
2606
+ "delete",
2607
+ PROJECT,
2608
+ "--apply"
2609
+ ]);
2610
+ expect(/^deleted conformance-gone, for good/.test(done), "the deletion did not say it was done", done);
2611
+ const after = await sealed(deployment, id);
2612
+ expect(after.versions === 5 && after.sealed === 0, "a deleted project still holds a ciphertext or a wrapped key", after);
2613
+ const [tombstone] = await using(deployment.database(), (sql) => query(sql, "SELECT slug FROM projects WHERE id = $1", [id]));
2614
+ expect(/^conformance-gone~deleted-\d{4}-\d{2}-\d{2}$/.test(tombstone?.slug ?? ""), "the tombstone is not named for the day", tombstone);
2615
+ expect((await admin.api.projects.list()).projects.every((project) => !project.slug.startsWith(PROJECT)), "a deleted project is still listed");
2616
+ expect((await admin.api.members.get(SERVICE$1)).live.grants === 0, "a grant on the deleted project is still live");
2617
+ const revealed = await admin.api.secrets.reveal(`${tombstone.slug}/prod`).then(() => null, (error) => error);
2618
+ expect(revealed instanceof CoffreError && revealed.status === 404, "a deleted project is still revealed by its tombstone", revealed);
2619
+ const fresh = value();
2620
+ await admin.api.projects.create(PROJECT, { name: "Gone, again" });
2621
+ await admin.api.environments.create(`${PROJECT}/prod`, { name: "prod" });
2622
+ await admin.api.secrets.set(`${PROJECT}/prod`, { API_KEY: fresh });
2623
+ expect(await projectId(deployment, PROJECT) !== id, "the new project took the deleted one's row");
2624
+ expect((await admin.api.secrets.reveal(`${PROJECT}/prod/API_KEY`)).values.API_KEY === fresh, "the new project does not read its own value");
2625
+ const { entries } = await admin.api.audit.list({
2626
+ path: tombstone.slug,
2647
2627
  limit: 500
2648
- })).entries;
2649
- expect(!shown.some((entry) => entry.action === "mcp.read"), "read-only calls are not detail");
2650
- return `on 2026-07-28 and 2025-11-25, the Browse tools as the reader: ${DEV} listed, ${PROD} refused, no value in any answer; headers held to the body; ${reads} reads as detail and the refusal shown, each under the client`;
2628
+ });
2629
+ 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));
2630
+ await admin.api.environments.update(`${PROJECT}/prod`, { archived: true });
2631
+ await run([
2632
+ "environments",
2633
+ "delete",
2634
+ `${PROJECT}/prod`,
2635
+ "--apply"
2636
+ ]);
2637
+ await admin.api.environments.create(`${PROJECT}/prod`, { name: "prod, again" });
2638
+ const environments = (await admin.api.projects.list()).projects.find((project) => project.slug === PROJECT)?.environments ?? [];
2639
+ expect(environments.length === 1 && environments[0].name === "prod, again", "a deleted environment is still listed, or its slug is not free", environments);
2640
+ expect((await sealed(deployment, await projectId(deployment, PROJECT))).sealed === 0, "a deleted environment still holds a value");
2641
+ const verified = await admin.api.audit.verify();
2642
+ expect(verified.ok, "the log does not verify after the deletions", verified);
2643
+ 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`;
2651
2644
  }
2645
+ //#endregion
2646
+ //#region src/checks/environments.ts
2647
+ /** Where the fork check copies dev to; archived once checked. */
2648
+ const FORKED = `${PROJECT$1}/forked`;
2652
2649
  /**
2653
- * Changes, as a client asks for them (design section 14, checks 2 and 3):
2654
- * Browse cannot ask at all, and with Write nothing changes until the person
2655
- * approves on coffre's page. A client without URL elicitation gets the link
2656
- * and calls again; one with it is asked to open the page, and retries with
2657
- * its requestState, which no other call may use. Only the person decides,
2658
- * with the digest the page showed; the page makes the change, once, and its
2659
- * entries name the client and the approval. A value comes from the page.
2650
+ * A folder grants, hides and renames nothing: a secret filed in one is
2651
+ * still read, run and exported by its own name, by the same people, and
2652
+ * only those who may write may file it.
2660
2653
  */
2661
- async function mcpChanges(deployment, people, canaries) {
2662
- const { admin } = people;
2663
- const place = `${PROJECT$1}-mcp/dev`;
2664
- await admin.api.projects.create(`${PROJECT$1}-mcp`, { name: "MCP changes" });
2665
- await admin.api.environments.create(place, { name: "Development" });
2666
- const values = {
2667
- KEEP: canary(),
2668
- OLD: canary()
2669
- };
2670
- for (const [key, value] of Object.entries(values)) canaries[`${place}/${key}`] = value;
2671
- await admin.api.secrets.set(place, values);
2672
- const changer = await personaOn(deployment, admin, "changer", { [`${PROJECT$1}-mcp`]: "maintainer" });
2673
- const clientId = await register(deployment);
2674
- const answers = [];
2675
- const call = async (access, tool, args, more = {}, capabilities = {}) => {
2676
- const { response, body } = await modern(deployment, access, "tools/call", {
2677
- name: tool,
2678
- arguments: args,
2679
- ...more
2680
- }, tool, capabilities);
2681
- answers.push(JSON.stringify(body));
2682
- return {
2683
- status: response.status,
2684
- challenge: response.headers.get("www-authenticate") ?? "",
2685
- result: body.result,
2686
- error: body.error
2687
- };
2688
- };
2689
- const archived = async (key) => (await admin.api.secrets.history(`${place}/${key}`)).archived;
2690
- const browse = (await redeem(deployment, clientId, await connect(deployment, changer, clientId))).access_token;
2691
- const stepUp = await call(browse, "archive_secret", { secret: `${place}/OLD` });
2692
- expect(stepUp.status === 403 && stepUp.challenge.includes("error=\"insufficient_scope\"") && stepUp.challenge.includes("scope=\"browse write\""), `archive_secret with Browse answered ${stepUp.status}, not a step-up to Write`, stepUp.challenge);
2693
- expect(!await archived("OLD"), "archive_secret with Browse archived the secret");
2694
- const write = (await redeem(deployment, clientId, await connect(deployment, changer, clientId, "browse write"))).access_token;
2695
- const linked = await call(write, "archive_secret", { secret: `${place}/OLD` });
2696
- const approval = linked.result?.structuredContent?.approval;
2697
- expect(linked.status === 200 && linked.result?.structuredContent?.status === "pending" && approval?.url === `${deployment.origin}/approvals/${approval?.id}`, "a change without URL elicitation was not answered with its approval link", linked.result);
2698
- expect(linked.result?.content?.[0]?.text?.includes(approval.url) === true, "the link is not in the text the model reads", linked.result);
2699
- expect(!await archived("OLD"), "a change was made before its approval");
2700
- const theirs = await people.reader.api.approvals.get(approval.id).then(() => null, (error) => error);
2701
- expect(theirs instanceof CoffreError && theirs.status === 403, "another person opened someone else's approval", theirs);
2702
- const page = await changer.browser.fetch(`/approvals/${approval.id}`);
2703
- expect(page.status === 200, `the approval page answered ${page.status}`);
2704
- const shown = (await changer.api.approvals.get(approval.id)).approval;
2705
- expect(shown.status === "pending" && shown.summary === `archive ${place}/OLD` && shown.client.name === NAME, "the approval page does not show the change asked for", shown);
2706
- const forgedDigest = await changer.api.approvals.decide(approval.id, {
2707
- approve: true,
2708
- digest: "0".repeat(64)
2709
- }).then(() => null, (error) => error);
2710
- expect(forgedDigest instanceof CoffreError && forgedDigest.status === 409, "a decision on a change the page did not show was taken", forgedDigest);
2711
- const forgedSite = await changer.browser.fetch(`/api/approvals/${approval.id}`, {
2712
- method: "POST",
2713
- headers: {
2714
- origin: "https://attacker.example",
2715
- "sec-fetch-site": "cross-site",
2716
- "content-type": "application/json"
2717
- },
2718
- body: JSON.stringify({
2719
- approve: true,
2720
- digest: shown.digest
2721
- })
2722
- });
2723
- expect(forgedSite.status === 403, `another site's approval answered ${forgedSite.status}, not 403`);
2724
- expect(!await archived("OLD"), "a refused decision changed something");
2725
- const decided = await changer.api.approvals.decide(approval.id, {
2726
- approve: true,
2727
- digest: shown.digest
2728
- });
2729
- expect(decided.status === "approved" && await archived("OLD"), "Approve did not make the change", decided);
2730
- const again = await changer.api.approvals.decide(approval.id, {
2731
- approve: true,
2732
- digest: shown.digest
2733
- }).then(() => null, (error) => error);
2734
- expect(again instanceof CoffreError && again.status === 409, "an approval was decided twice", again);
2735
- const reported = await call(write, "archive_secret", { secret: `${place}/OLD` });
2736
- expect(reported.result?.structuredContent?.status === "approved" && reported.result.isError !== true, "calling again does not report the approved change", reported.result);
2737
- const elicits = { elicitation: { url: {} } };
2738
- const asked = await call(write, "rename_secret", {
2739
- secret: `${place}/KEEP`,
2740
- newKey: "KEPT"
2741
- }, {}, elicits);
2742
- const request = asked.result?.inputRequests?.approve;
2743
- expect(asked.result?.resultType === "input_required" && request?.method === "elicitation/create" && request.params.mode === "url" && typeof asked.result.requestState === "string", "a change with URL elicitation was not answered with an input_required URL elicitation", asked.result);
2744
- const id = request.params.url.split("/").at(-1);
2745
- const replayed = await call(write, "rename_secret", {
2746
- secret: `${place}/KEEP`,
2747
- newKey: "OTHER"
2748
- }, {
2749
- requestState: asked.result.requestState,
2750
- inputResponses: { approve: { action: "accept" } }
2751
- }, elicits);
2752
- expect(replayed.error?.code === -32602, "a requestState was accepted for another call", replayed);
2753
- const renamedEarly = await admin.api.secrets.list(place);
2754
- expect(renamedEarly.keys.some((key) => key.key === "KEEP"), "a change was made before its approval", renamedEarly.keys);
2755
- const second = (await changer.api.approvals.get(id)).approval;
2756
- expect((await changer.api.approvals.decide(id, {
2757
- approve: true,
2758
- digest: second.digest
2759
- })).status === "approved", "the second approval was not made");
2760
- const retried = await call(write, "rename_secret", {
2761
- secret: `${place}/KEEP`,
2762
- newKey: "KEPT"
2763
- }, {
2764
- requestState: asked.result.requestState,
2765
- inputResponses: { approve: { action: "accept" } }
2766
- }, elicits);
2767
- expect(retried.result?.structuredContent?.status === "approved", "the retry does not report the approved change", retried.result);
2768
- const typed = canary();
2769
- canaries[`${place}/TYPED`] = typed;
2770
- const valueId = (await call(write, "request_secret_value", { secret: `${place}/TYPED` })).result.structuredContent.approval.id;
2771
- const valuePage = (await changer.api.approvals.get(valueId)).approval;
2772
- expect(valuePage.asks?.value !== void 0, "the approval page does not ask for the value");
2773
- await changer.api.approvals.decide(valueId, {
2774
- approve: true,
2775
- digest: valuePage.digest,
2776
- value: typed
2777
- });
2778
- expect((await admin.api.secrets.reveal(`${place}/TYPED`)).values.TYPED === typed, "the value typed on the page was not written");
2779
- await call(write, "request_secret_value", { secret: `${place}/TYPED` });
2780
- const generatedId = (await call(write, "generate_secret_value", {
2781
- secret: `${place}/GENERATED`,
2782
- alphabet: "hex"
2783
- })).result.structuredContent.approval.id;
2784
- const generatedPage = (await changer.api.approvals.get(generatedId)).approval;
2785
- const generated = await changer.api.approvals.decide(generatedId, {
2786
- approve: true,
2787
- digest: generatedPage.digest
2788
- });
2789
- const made = (await admin.api.secrets.reveal(`${place}/GENERATED`)).values.GENERATED ?? "";
2790
- canaries[`${place}/GENERATED`] = made;
2791
- expect(/^[0-9a-f]{64}$/.test(made) && generated.shown.length === 0 && !JSON.stringify(generated).includes(made), "generate_secret_value did not make a 64-character hex value, shown to no one");
2792
- await call(write, "generate_secret_value", {
2793
- secret: `${place}/GENERATED`,
2794
- alphabet: "hex"
2795
- });
2796
- const leaked = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
2797
- expect(leaked.length === 0, "a change tool answered with a secret value", leaked);
2798
- const { entries } = await admin.api.audit.list({
2799
- actor: changer.member,
2800
- limit: 500
2654
+ async function folders({ admin, reader }, canaries) {
2655
+ const keys = Object.keys(valuesIn(canaries, DEV));
2656
+ const key = keys[0];
2657
+ expect(key !== void 0, "dev holds no secret to file");
2658
+ await admin.api.projects.update(PROJECT$1, { folder: "Conformance" });
2659
+ await admin.api.secrets.update(`${DEV}/${key}`, { folder: "filed" });
2660
+ try {
2661
+ const project = (await reader.api.projects.list()).projects.find((each) => each.slug === PROJECT$1);
2662
+ expect(project?.folder === "Conformance", "a project filed in a folder is not listed in it", project);
2663
+ const listed = (await reader.api.secrets.list(DEV)).keys.find((each) => each.key === key);
2664
+ expect(listed?.folder === "filed", "a secret filed in a folder is not listed in it", listed);
2665
+ const read = await reader.api.secrets.reveal(DEV);
2666
+ expect(JSON.stringify(Object.keys(read.values).sort()) === JSON.stringify([...keys].sort()), "filing a secret changed the names a run injects", Object.keys(read.values));
2667
+ expect(read.values[key] === canaries[`${DEV}/${key}`], "filing a secret changed its value");
2668
+ await refused$1("a viewer filed a secret", reader.api.secrets.update(`${DEV}/${key}`, { folder: "elsewhere" }));
2669
+ await refused$1("a viewer filed a project", reader.api.projects.update(PROJECT$1, { folder: "Elsewhere" }));
2670
+ await refused$1("a folder name with a slash was taken", admin.api.secrets.update(`${DEV}/${key}`, { folder: "a/b" }));
2671
+ } finally {
2672
+ await admin.api.secrets.update(`${DEV}/${key}`, { folder: null });
2673
+ await admin.api.projects.update(PROJECT$1, { folder: null });
2674
+ }
2675
+ return "a filed secret is read and run by its own name, by the same people; only writers file it";
2676
+ }
2677
+ /**
2678
+ * A fork copies dev's values into a new environment, and nothing of who
2679
+ * may read dev: a viewer on dev reads neither the fork nor makes one.
2680
+ */
2681
+ async function forks({ admin, reader }, canaries) {
2682
+ const dev = valuesIn(canaries, DEV);
2683
+ await refused$1("a viewer forked an environment", reader.api.environments.create(FORKED, {
2684
+ name: "Forked",
2685
+ from: DEV.split("/")[1]
2686
+ }));
2687
+ const made = await admin.api.environments.create(FORKED, {
2688
+ name: "Forked",
2689
+ from: DEV.split("/")[1]
2801
2690
  });
2802
- const viaApproval = (action) => entries.filter((entry) => entry.action === action && entry.decision === "allow" && typeof entry.metadata.via?.approvalId === "string");
2803
- const once = {
2804
- archive: viaApproval("secret.archive").length,
2805
- rename: viaApproval("secret.rename").length,
2806
- write: viaApproval("secret.write").length
2807
- };
2808
- expect(once.archive === 1 && once.rename === 1 && once.write === 2, "the changes are not each made once, via the client and its approval", once);
2809
- const decisions = entries.filter((entry) => entry.action === "mcp.approve").map((entry) => `${entry.decision} ${entry.reason ?? ""}`.trim()).sort();
2810
- expect(JSON.stringify(decisions) === JSON.stringify([
2811
- "allow",
2812
- "allow",
2813
- "allow",
2814
- "allow",
2815
- "deny approved",
2816
- "deny changed"
2817
- ]), "the decisions, and the two refused, are not in the log", decisions);
2818
- return `Browse stepped up to Write; with Write, the link without URL elicitation and an elicitation with it; nothing changed until ${changer.email} approved on coffre's page, another person and another site refused, a replayed requestState refused; each change made once, via the client and its approval; a value typed on the page only, and one coffre made reaching no one`;
2691
+ try {
2692
+ expect(made.forked?.keys === Object.keys(dev).length, "the fork did not copy each of dev's keys", made);
2693
+ const read = await admin.api.secrets.reveal(FORKED);
2694
+ expect(JSON.stringify(Object.entries(read.values).sort()) === JSON.stringify(Object.entries(dev).sort()), "the fork's values are not dev's");
2695
+ const history = await admin.api.secrets.history(`${FORKED}/${Object.keys(dev)[0]}`);
2696
+ expect(history.versions.length === 1, "the fork carried history", history.versions);
2697
+ await refused$1("a viewer on dev read dev's fork", reader.api.secrets.reveal(FORKED));
2698
+ } finally {
2699
+ await admin.api.environments.update(FORKED, { archived: true });
2700
+ }
2701
+ return "a fork holds dev's values and no history, and a viewer on dev neither makes one nor reads it";
2702
+ }
2703
+ /** The key in dev the reference check makes, archived once checked. */
2704
+ const HELD = "CONFORMANCE_REFERENCE";
2705
+ const FORGED = "CONFORMANCE_FORGED";
2706
+ /** The status a call failed with and its reason, the vault's or the app's, or that it went through. */
2707
+ async function outcome(call) {
2708
+ try {
2709
+ await call;
2710
+ return "let through";
2711
+ } catch (error) {
2712
+ if (!(error instanceof CoffreError)) throw error;
2713
+ return `${error.status} ${error.reason ?? error.code}`;
2714
+ }
2715
+ }
2716
+ /**
2717
+ * A reference lets whoever reads its holder read its source, so only the
2718
+ * vault makes one, and a read through one is checked against the vault's
2719
+ * own entry: the reader, a viewer on dev with no grant on prod, reads
2720
+ * prod's value through dev's reference, and the read is in prod's log and
2721
+ * dev's; a reference row the vault never made, written straight into the
2722
+ * database, reads nothing; a developer on dev who cannot read prod makes
2723
+ * none; and once broken, it reads no more.
2724
+ */
2725
+ async function references(deployment, { admin, reader, leaver }, canaries) {
2726
+ const [sourceKey] = Object.keys(valuesIn(canaries, PROD));
2727
+ expect(sourceKey !== void 0, "prod holds no secret to refer to");
2728
+ const source = `${PROD}/${sourceKey}`;
2729
+ await refused$1("a viewer on dev read prod directly", reader.api.secrets.reveal(source));
2730
+ 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");
2731
+ await admin.api.secrets.set(DEV, { [HELD]: { ref: source } });
2732
+ try {
2733
+ 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");
2734
+ const logged = async (path) => (await admin.api.audit.list({
2735
+ path,
2736
+ limit: 200
2737
+ })).entries.filter((entry) => entry.action === "secret.read" && entry.actorId === reader.email && entry.metadata.via !== void 0).length;
2738
+ expect(await logged(PROD) >= 1 && await logged(DEV) >= 1, "the read through the reference is not in both the source's log and the holder's");
2739
+ const archives = [[source, (archived) => admin.api.secrets.update(source, { archived })], [PROD, (archived) => admin.api.environments.update(PROD, { archived })]];
2740
+ for (const [what, archive] of archives) {
2741
+ const refusal = await archive(true).then(() => null, (error) => error);
2742
+ if (refusal === null) await archive(false);
2743
+ 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);
2744
+ }
2745
+ await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
2746
+ const underArchived = await outcome(admin.api.secrets.update(source, { archived: true })).finally(() => admin.api.secrets.update(`${DEV}/${HELD}`, { archived: false }));
2747
+ try {
2748
+ expect(underArchived === "let through", `${source} could not be archived while only an archived key referred to it: ${underArchived}`);
2749
+ const restored = await outcome(reader.api.secrets.reveal(`${DEV}/${HELD}`));
2750
+ expect(restored.startsWith("409 "), `a reference read a source archived while its holder was: ${restored}`);
2751
+ } finally {
2752
+ if (underArchived === "let through") await admin.api.secrets.update(source, { archived: false });
2753
+ }
2754
+ expect((await reader.api.secrets.reveal(`${DEV}/${HELD}`)).values[HELD] === canaries[source], "a reference did not read its source again once both were restored");
2755
+ const forged = await using(deployment.database(), async (sql) => {
2756
+ const [holder] = await query(sql, "SELECT id, project_id, environment_id FROM secrets WHERE key = $1", [HELD]);
2757
+ 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]]);
2758
+ 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");
2759
+ const id = randomUUID();
2760
+ await query(sql, "INSERT INTO secrets (id, project_id, environment_id, key, current_version) VALUES ($1, $2, $3, $4, 0)", [
2761
+ id,
2762
+ holder.project_id,
2763
+ holder.environment_id,
2764
+ FORGED
2765
+ ]);
2766
+ 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)
2767
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [
2768
+ randomUUID(),
2769
+ holder.project_id,
2770
+ holder.environment_id,
2771
+ id,
2772
+ target.project_id,
2773
+ target.environment_id,
2774
+ target.id,
2775
+ String(entry.seq),
2776
+ admin.member
2777
+ ]);
2778
+ return id;
2779
+ });
2780
+ try {
2781
+ const forgedRead = await outcome(reader.api.secrets.reveal(`${DEV}/${FORGED}`));
2782
+ expect([
2783
+ "404 no_value",
2784
+ "404 not_found",
2785
+ "403 bad_claim"
2786
+ ].includes(forgedRead), `a reference row the vault never made was read: ${forgedRead}`);
2787
+ } finally {
2788
+ await using(deployment.database(), async (sql) => {
2789
+ await query(sql, "DELETE FROM secret_references WHERE secret_id = $1", [forged]);
2790
+ await query(sql, "DELETE FROM secrets WHERE id = $1", [forged]);
2791
+ });
2792
+ }
2793
+ await admin.api.references.break(`${DEV}/${HELD}`);
2794
+ const after = await outcome(reader.api.secrets.reveal(`${DEV}/${HELD}`));
2795
+ expect(after.startsWith("409 "), `a broken reference still read: ${after}`);
2796
+ } finally {
2797
+ await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
2798
+ }
2799
+ return "a viewer on dev reads prod's value through a reference, logged in both; prod and its key cannot be archived while it reads them, but can once its holder is archived, which then reads nothing until they are back; a forged row is refused, a broken one reads no more, and none is made without read on the source";
2819
2800
  }
2801
+ /** A key prod has and dev does not, for the missing-keys check; archived once checked. */
2802
+ const PROD_ONLY = "CONFORMANCE_PROD_ONLY";
2820
2803
  /**
2821
- * Values (design section 14, checks 2 and 4): Browse cannot read one, Read
2822
- * values reads what the person may, a value shown on coffre's page goes to
2823
- * the person and not the client, and one a client asks coffre to make
2824
- * reaches no one.
2804
+ * Key names are metadata: what dev is missing is compared only with the
2805
+ * environments the viewer reads. The admin sees prod's key missing from dev;
2806
+ * the reader, a viewer on dev alone, sees nothing of prod, and dismisses
2807
+ * nothing. A dismissal is the team's, and restoring undoes it.
2825
2808
  */
2826
- async function mcpValues(deployment, people, canaries) {
2827
- const { reader } = people;
2828
- const clientId = await register(deployment);
2829
- const answers = [];
2830
- const call = async (access, tool, args) => {
2831
- const { response, body } = await modern(deployment, access, "tools/call", {
2832
- name: tool,
2833
- arguments: args
2834
- }, tool);
2835
- answers.push(JSON.stringify(body));
2836
- return {
2837
- status: response.status,
2838
- challenge: response.headers.get("www-authenticate") ?? "",
2839
- result: body.result
2840
- };
2841
- };
2842
- const dev = valuesIn(canaries, DEV);
2843
- const browse = (await redeem(deployment, clientId, await connect(deployment, reader, clientId))).access_token;
2844
- const stepUp = await call(browse, "read_secret_values", { path: DEV });
2845
- expect(stepUp.status === 403 && stepUp.challenge.includes("scope=\"browse read-values\""), `read_secret_values with Browse answered ${stepUp.status}, not a step-up`, stepUp.challenge);
2846
- const show = await call(browse, "show_secret_value", { secret: `${DEV}/API_KEY` });
2847
- const id = show.result?.structuredContent?.approval?.id;
2848
- expect(show.result?.structuredContent?.status === "pending" && id !== void 0, "show_secret_value did not open an approval", show.result);
2849
- const page = (await reader.api.approvals.get(id)).approval;
2850
- expect(page.kind === "reveal", "the approval page does not say it shows a value", page);
2851
- expect((await reader.api.approvals.decide(id, {
2852
- approve: true,
2853
- digest: page.digest
2854
- })).shown.some((line) => line.value === dev.API_KEY), "Reveal did not show the person the value");
2855
- const told = await call(browse, "show_secret_value", { secret: `${DEV}/API_KEY` });
2856
- expect(told.result?.structuredContent?.status === "approved", "show_secret_value does not report the value as shown", told.result);
2857
- const browseLeaks = Object.entries(canaries).filter(([, value]) => answers.some((answer) => answer.includes(value))).map(([path]) => path);
2858
- expect(browseLeaks.length === 0, "a Browse answer held a value", browseLeaks);
2859
- const reading = (await redeem(deployment, clientId, await connect(deployment, reader, clientId, "browse read-values"))).access_token;
2860
- const read = await call(reading, "read_secret_values", { path: DEV });
2861
- expect(JSON.stringify(read.result?.structuredContent?.values) === JSON.stringify(dev), `read_secret_values on ${DEV} did not answer its values`);
2862
- expect(read.result?.content?.[0]?.text?.startsWith("These values are now part of this conversation") === true, "read_secret_values does not warn first", read.result?.content);
2863
- const prod = await call(reading, "read_secret_values", { path: PROD });
2864
- expect(prod.result?.isError === true && !JSON.stringify(prod.result).includes(canaries[`${PROD}/API_KEY`]), `the reader read ${PROD} through MCP`, prod.result);
2865
- return `Browse stepped up for values and showed ${DEV}/API_KEY to ${reader.email} on coffre's page only; Read values answered ${DEV}'s values, warning first, and refused ${PROD}`;
2809
+ async function missingKeys({ admin, reader }) {
2810
+ await admin.api.secrets.set(PROD, { [PROD_ONLY]: canary() });
2811
+ try {
2812
+ const seen = await admin.api.environments.missing(DEV);
2813
+ expect(seen.missing.some((key) => key.key === PROD_ONLY), "the admin was not told dev lacks prod's key", seen);
2814
+ const hidden = await reader.api.environments.missing(DEV);
2815
+ expect(![...hidden.missing, ...hidden.dismissed].some((key) => key.key === PROD_ONLY), "a viewer on dev learned a key name from prod, which they cannot read", hidden);
2816
+ await refused$1("a viewer dismissed a missing key", reader.api.environments.dismiss(DEV, { [PROD_ONLY]: true }));
2817
+ await admin.api.environments.dismiss(DEV, { [PROD_ONLY]: true });
2818
+ const dismissed = await admin.api.environments.missing(DEV);
2819
+ expect(dismissed.dismissed.some((key) => key.key === PROD_ONLY) && !dismissed.missing.some((key) => key.key === PROD_ONLY), "a dismissed key was still listed as missing", dismissed);
2820
+ await admin.api.environments.dismiss(DEV, { [PROD_ONLY]: null });
2821
+ expect((await admin.api.environments.missing(DEV)).missing.some((key) => key.key === PROD_ONLY), "a restored key was not missing again");
2822
+ } finally {
2823
+ await admin.api.secrets.update(`${PROD}/${PROD_ONLY}`, { archived: true });
2824
+ }
2825
+ return "only who reads prod learns what dev lacks of it; a viewer dismisses nothing; a dismissal is listed, and restored";
2826
+ }
2827
+ //#endregion
2828
+ //#region src/checks/logins.ts
2829
+ const ZEROS = "decode(repeat('00', 32), 'hex')";
2830
+ const appendAs = (author) => `INSERT INTO audit_log
2831
+ (seq, author, key_id, occurred_at, actor, action, decision, prev_hash, mac, hash)
2832
+ VALUES (9000000000, '${author}', '${author}:0', 0, 'system:conformance', 'probe', 'allow', ${ZEROS}, ${ZEROS}, ${ZEROS})`;
2833
+ const IMMUTABLE = [
2834
+ "UPDATE audit_log SET action = 'rewritten' WHERE author = 'app'",
2835
+ "UPDATE audit_log SET action = 'rewritten' WHERE author = 'vault'",
2836
+ "DELETE FROM audit_log",
2837
+ "TRUNCATE audit_log",
2838
+ "DROP TABLE audit_log",
2839
+ "UPDATE secret_versions SET id = id",
2840
+ "DELETE FROM secret_versions",
2841
+ "DELETE FROM vault_members",
2842
+ "CREATE TABLE conformance_probe (id integer)",
2843
+ "CREATE TEMP TABLE conformance_probe (id integer)",
2844
+ "ALTER TABLE audit_log DISABLE TRIGGER USER",
2845
+ "SET session_replication_role = 'replica'",
2846
+ "SET ROLE coffre_owner"
2847
+ ];
2848
+ /** A column of each app table, for an update that would change nothing, if it were allowed. */
2849
+ const APP_COLUMNS = {
2850
+ projects: "id",
2851
+ environments: "id",
2852
+ secrets: "id",
2853
+ secret_versions: "id",
2854
+ identities: "id",
2855
+ credentials: "id",
2856
+ device_authorizations: "id"
2857
+ };
2858
+ async function refuses(sql, statements) {
2859
+ for (const statement of statements) {
2860
+ await sql.exec("BEGIN");
2861
+ let error;
2862
+ try {
2863
+ await sql.exec(statement);
2864
+ } catch (failure) {
2865
+ error = failure;
2866
+ } finally {
2867
+ await sql.exec("ROLLBACK");
2868
+ }
2869
+ expect(error !== void 0, `the login could: ${statement}`);
2870
+ expect(error.code === "42501", `${statement} failed for a reason other than privilege`, error);
2871
+ }
2872
+ return statements.length;
2873
+ }
2874
+ /** Neither table nor column grants may let a login write outside its component. */
2875
+ async function noWrites(sql, names) {
2876
+ const columns = await sql.query(`SELECT table_name, column_name,
2877
+ has_column_privilege(current_user, quote_ident(table_name), column_name, 'INSERT') AS insertable,
2878
+ has_column_privilege(current_user, quote_ident(table_name), column_name, 'UPDATE') AS updatable,
2879
+ has_table_privilege(current_user, quote_ident(table_name), 'DELETE') AS deletable,
2880
+ has_table_privilege(current_user, quote_ident(table_name), 'TRUNCATE') AS truncatable
2881
+ FROM information_schema.columns WHERE table_schema = 'public'`);
2882
+ for (const name of names) {
2883
+ const inspected = columns.filter((column) => column.table_name === name);
2884
+ const [table] = await sql.query(`SELECT
2885
+ has_table_privilege(current_user, $1, 'INSERT, UPDATE, DELETE, TRUNCATE') AS writable`, [name]);
2886
+ expect(!table.writable, `the login has a table write privilege on ${name}`);
2887
+ expect(inspected.every((column) => !column.insertable && !column.updatable && !column.deletable && !column.truncatable), `the login has a column write privilege on ${name}`, inspected);
2888
+ }
2889
+ }
2890
+ async function appLogin(deployment) {
2891
+ if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
2892
+ expect(deployment.runtime !== null, "the app runtime login is missing");
2893
+ const protectedTables = (await using(deployment.database(), tables)).filter((name) => name.startsWith("vault_"));
2894
+ return using(deployment.runtime(), async (sql) => {
2895
+ await noWrites(sql, protectedTables);
2896
+ const statements = [
2897
+ ...IMMUTABLE,
2898
+ appendAs("vault"),
2899
+ "SET ROLE coffre_vault",
2900
+ "DELETE FROM secrets"
2901
+ ];
2902
+ 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}`);
2903
+ return `${await refuses(sql, statements)} writes and bypasses refused; no member or grant write privileges`;
2904
+ });
2905
+ }
2906
+ async function vaultLogin(deployment) {
2907
+ if (deployment.kind === "node") throw new Skip("SQLite has no database logins");
2908
+ expect(deployment.vaultRuntime !== null, "the vault runtime login is missing");
2909
+ const appTables = (await using(deployment.database(), tables)).filter((name) => ![
2910
+ "vault_members",
2911
+ "vault_grants",
2912
+ "audit_log",
2913
+ "audit_chain_head"
2914
+ ].includes(name));
2915
+ return using(deployment.vaultRuntime(), async (sql) => {
2916
+ await noWrites(sql, appTables);
2917
+ const [head] = await sql.query(`SELECT
2918
+ has_table_privilege(current_user, 'audit_chain_head', 'INSERT') AS insertable,
2919
+ has_table_privilege(current_user, 'audit_chain_head', 'DELETE') AS deletable,
2920
+ has_table_privilege(current_user, 'audit_chain_head', 'TRUNCATE') AS truncatable,
2921
+ has_column_privilege(current_user, 'audit_chain_head', 'only_row', 'UPDATE') AS identity_updatable`);
2922
+ expect(!head.insertable && !head.deletable && !head.truncatable && !head.identity_updatable, "the vault can replace the head row", head);
2923
+ const statements = [
2924
+ ...IMMUTABLE,
2925
+ appendAs("app"),
2926
+ "SET ROLE coffre_app",
2927
+ "UPDATE audit_chain_head SET only_row = only_row",
2928
+ "DELETE FROM audit_chain_head",
2929
+ "TRUNCATE audit_chain_head",
2930
+ "UPDATE vault_members SET principal = principal",
2931
+ "UPDATE vault_grants SET role = role"
2932
+ ];
2933
+ for (const table of appTables) {
2934
+ const column = APP_COLUMNS[table];
2935
+ if (column !== void 0) statements.push(`UPDATE ${quoted(table)} SET ${column} = ${column}`);
2936
+ }
2937
+ return `${await refuses(sql, statements)} writes and bypasses refused; app tables have no write privileges`;
2938
+ });
2866
2939
  }
2867
2940
  //#endregion
2868
2941
  //#region src/checks/readiness.ts
@@ -3052,8 +3125,12 @@ function pages(people) {
3052
3125
  "/",
3053
3126
  "/login",
3054
3127
  "/unregistered",
3055
- "/account",
3056
- "/access",
3128
+ ...[
3129
+ "",
3130
+ "/sessions",
3131
+ "/apps",
3132
+ "/appearance"
3133
+ ].map((tab) => `/account${tab}`),
3057
3134
  "/audit",
3058
3135
  "/settings",
3059
3136
  "/auth/device",
@@ -3850,7 +3927,7 @@ async function consentUrl(deployment) {
3850
3927
  code_challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
3851
3928
  code_challenge_method: "S256",
3852
3929
  state: "pages",
3853
- scope: "browse write"
3930
+ scope: "read write"
3854
3931
  });
3855
3932
  return `${deployment.origin}/oauth/authorize?${request}`;
3856
3933
  }
@@ -3897,8 +3974,29 @@ async function pagesInBrowser(deployment, admin, browser) {
3897
3974
  `${account}/activity`,
3898
3975
  `service:${name}`,
3899
3976
  "Activity"
3977
+ ],
3978
+ [
3979
+ "/account",
3980
+ "Account",
3981
+ "Profile"
3982
+ ],
3983
+ [
3984
+ "/account/sessions",
3985
+ "Account",
3986
+ "Sessions"
3987
+ ],
3988
+ [
3989
+ "/account/apps",
3990
+ "Account",
3991
+ "Connected apps"
3992
+ ],
3993
+ [
3994
+ "/account/appearance",
3995
+ "Account",
3996
+ "Appearance"
3900
3997
  ]
3901
3998
  ];
3999
+ const mcp = `${deployment.origin}/mcp`;
3902
4000
  const chrome = await Chrome.open(executable);
3903
4001
  try {
3904
4002
  for (const [path, heading, tab] of pages) {
@@ -3913,6 +4011,15 @@ async function pagesInBrowser(deployment, admin, browser) {
3913
4011
  expect(loaded.dialog?.includes("Removing revokes 0 grants, 0 bearer tokens") === true, `${path}: Remove… does not preview what it would revoke`, loaded.dialog);
3914
4012
  }
3915
4013
  if (path === "/settings") expect(loaded.cards.includes("Keys"), "/settings does not show what the keys are checked against", loaded.cards);
4014
+ if (path === person) expect(loaded.cards.includes("Connected apps"), `${path} does not show an owner the person's connected apps`, loaded.cards);
4015
+ if (path === "/account/apps") {
4016
+ expect(loaded.cards.join(" then ") === "Connect an app then Connected apps", `${path} does not show Connect an app, then the connected apps`, loaded.cards);
4017
+ for (const shown of [
4018
+ mcp,
4019
+ `claude mcp add --transport http coffre ${mcp}`,
4020
+ "Add custom connector"
4021
+ ]) expect(loaded.text.includes(shown), `${path} does not show "${shown}"`, loaded.text);
4022
+ }
3916
4023
  }
3917
4024
  const project = new URL(`/projects/${PROJECT$1}`, deployment.origin).href;
3918
4025
  const walked = await chrome.load(project, admin.browser.cookies(), void 0, WALK_TABS);
@@ -3936,14 +4043,16 @@ async function pagesInBrowser(deployment, admin, browser) {
3936
4043
  "Unverified",
3937
4044
  "localhost",
3938
4045
  admin.email,
3939
- "Browse",
4046
+ "Read",
4047
+ "Reveal values",
4048
+ "Manage access",
3940
4049
  "Approve",
3941
4050
  "Deny"
3942
4051
  ]) expect(loaded.text.includes(shown), `/oauth/authorize does not show "${shown}"`, loaded.text);
3943
4052
  } finally {
3944
4053
  await chrome.close();
3945
4054
  }
3946
- return `${pages.map(([path]) => path).join(", ")}, signed in, in Chrome: each rendered, on its tab, no error from their scripts; a project's tabs by their links, back and forward; a service account shown as service:${name}, OIDC then bearer tokens, its removal previewed; the keys' checks in Settings; its grant on dev in every project with an owner's grant button; the MCP consent page, a registered client shown as unverified`;
4055
+ return `${pages.map(([path]) => path).join(", ")}, signed in, in Chrome: each rendered, on its tab, no error from their scripts; a project's tabs by their links, back and forward; a service account shown as service:${name}, OIDC then bearer tokens, its removal previewed; the keys' checks in Settings; its grant on dev in every project with an owner's grant button; the account's Connected apps tab, with this instance's MCP URL and Claude Code's command; the MCP consent page, a registered client shown as unverified`;
3947
4056
  }
3948
4057
  /**
3949
4058
  * A refused sign-in, said once: the page coffre sends the browser back to
@@ -4422,7 +4531,7 @@ async function conform(deployment, options) {
4422
4531
  await report.check("references", all, ({ people, canaries }) => references(deployment, people, canaries));
4423
4532
  await report.check("missing keys", { people }, ({ people }) => missingKeys(people));
4424
4533
  await report.check("MCP connect", { people }, ({ people }) => mcpConnect(deployment, people));
4425
- await report.check("MCP browse", all, ({ people, canaries }) => mcpBrowse(deployment, people, canaries));
4534
+ await report.check("MCP read", all, ({ people, canaries }) => mcpRead(deployment, people, canaries));
4426
4535
  await report.check("MCP changes", all, ({ people, canaries }) => mcpChanges(deployment, people, canaries));
4427
4536
  await report.check("MCP values", all, ({ people, canaries }) => mcpValues(deployment, people, canaries));
4428
4537
  await report.check("reveals audited", all, ({ people, canaries }) => revealAudited(deployment, people, canaries, "reveal"));