@coffre/conformance 0.14.7 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  // A value leaves coffre through a reveal and nowhere else: not in any
2
2
  // answer to a GET, whoever asks, not in a page, and not in what it stores or
3
3
  // prints. Every value set is a random canary, so finding one is a leak.
4
- import { existsSync, readFileSync } from 'node:fs';
5
4
 
6
5
  import { using } from '../database.ts';
7
6
  import type { Deployment } from '../harness.ts';
@@ -97,9 +96,9 @@ export async function canaryScan(deployment: Deployment, people: People, canarie
97
96
  const names = await tables(sql);
98
97
  // Prove the reader sees bytes before trusting an absence of plaintext.
99
98
  const control = canary();
100
- await sql.exec(`CREATE TABLE conformance_canary_probe (value ${sql.engine === 'postgres' ? 'bytea' : 'BLOB'})`);
99
+ await sql.exec('CREATE TABLE conformance_canary_probe (value bytea)');
101
100
  try {
102
- await sql.query(`INSERT INTO conformance_canary_probe VALUES (${sql.engine === 'postgres' ? '$1' : '?'})`, [Buffer.from(control)]);
101
+ await sql.query('INSERT INTO conformance_canary_probe VALUES ($1)', [Buffer.from(control)]);
103
102
  let found = false;
104
103
  await scanTables(sql, ['conformance_canary_probe'], (_, bytes) => { found ||= Buffer.from(bytes).includes(control); });
105
104
  expect(found, 'the database scanner missed its planted binary canary');
@@ -109,21 +108,9 @@ export async function canaryScan(deployment: Deployment, people: People, canarie
109
108
  await scanTables(sql, names, look);
110
109
  stored.push(`${names.length} tables, with a binary positive control`);
111
110
  });
112
- if (deployment.databaseFile !== null) {
113
- look("the database's file", files(deployment.databaseFile));
114
- stored.push('the database file and WAL');
115
- }
116
111
  look("the processes' output", deployment.output());
117
112
  stored.push("the processes' output");
118
113
 
119
114
  expect(leaks.length === 0, 'a value was found outside a reveal', leaks);
120
115
  return `${answers} answers from ${Object.keys(routes).length} GET routes and ${paths.length} pages as ${callers.length} callers, and ${stored.join(', ')}: no value`;
121
116
  }
122
-
123
- /** A SQLite file and its write-ahead log, as bytes: what anyone with the disk would read. */
124
- function files(path: string): Buffer {
125
- expect(existsSync(path), `the store file could not be found: ${path}`);
126
- return Buffer.concat([path, `${path}-wal`]
127
- .filter((file) => existsSync(file))
128
- .map((file) => readFileSync(file)));
129
- }
package/src/checks/cli.ts CHANGED
@@ -115,7 +115,7 @@ function marks(output: string): Record<string, string> {
115
115
  return Object.fromEntries(lines.map(([, mark, name]) => [name!.trim(), mark!]));
116
116
  }
117
117
 
118
- /** The token's run, from CI: as no one, then as the token, which is no owner and skips verification. */
118
+ /** The token's run, from CI: as no one, then as the token, which reads no audit log but its project's, so holds none to its head. */
119
119
  export async function verifyWithToken(cli: Cli, live: { token: string; canary: Canary }): Promise<string> {
120
120
  const { canary } = live;
121
121
  // As a CI step does: the token piped to `coffre login --token`, then the canary's value to the check.
@@ -124,10 +124,10 @@ export async function verifyWithToken(cli: Cli, live: { token: string; canary: C
124
124
  const run = await cli.run(['verify', 'instance', cli.origin, '--canary', `${canary.project}/${canary.environment}/${canary.key}`], {}, `${canary.value}\n`);
125
125
  const seen = marks(run.output);
126
126
  expect(run.code === 0, `coffre verify instance with a token exited ${run.code}`, run.output);
127
- expect(seen['token verification'] === '–', 'a token that is no owner did not skip verification', run.output);
127
+ expect(!('owner log proofs' in seen), "a token's run held the log to its head, which only an owner's reads whole", run.output);
128
128
  expect(!run.output.includes(live.token) && !run.output.includes(canary.value), 'the run printed its token or its canary', run.output);
129
129
  const ok = Object.values(seen).filter((mark) => mark === '✓').length;
130
- return `${ok} checks ok as no one and as the token, its verification skipped; neither the token nor the canary shown`;
130
+ return `${ok} checks ok as no one and as the token; neither the token nor the canary shown`;
131
131
  }
132
132
 
133
133
  /** What a run as an owner must leave as it was: every member and their grants, but its service; every project and environment. */
@@ -163,7 +163,8 @@ export async function verifyAsOwner(cli: Cli): Promise<{ detail: string; value:
163
163
  const run = await cli.run(['verify', 'instance']);
164
164
  const seen = marks(run.output);
165
165
  expect(run.code === 0, `coffre verify instance as the admin exited ${run.code}`, run.output);
166
- // Signed in, nothing is out of reach: a check skipped, such as the token's verification, would only be noise.
166
+ // Signed in, nothing is out of reach: a check skipped would only be noise.
167
+ expect(seen['owner log proofs'] === '✓', "the run did not hold the log's newest entries to the head the vault signed", run.output);
167
168
  const skipped = Object.entries(seen).filter(([, mark]) => mark !== '✓').map(([name]) => name);
168
169
  expect(skipped.length === 0, `the run skipped ${skipped.join(', ')}`, run.output);
169
170
  expect(!SHOWN.test(run.output) && !run.output.includes(session), 'the run printed a credential, the session or its canary', SHOWN.exec(run.output)?.[0]);
@@ -221,31 +222,30 @@ export async function verifyLeftovers(cli: Cli, admin: Person, setups: string[],
221
222
  }
222
223
 
223
224
  /**
224
- * `coffre verify keys`, with the admin's session, the keys piped in: this
225
- * deployment's pass; a wrong vault key fails and the right app key passes;
226
- * a malformed app key fails. No key is ever printed.
225
+ * `coffre verify keys`, with the admin's session, the key piped in: this
226
+ * deployment's vault key passes, its vault ID as given; a wrong key and a
227
+ * malformed one each fail, named. No key is ever printed.
227
228
  */
228
229
  export async function verifyKeys(cli: Cli): Promise<string> {
229
230
  const wrong = Buffer.alloc(32, 7).toString('base64');
230
- // Piped in, as a script does: the vault key's line, then the app key's.
231
- const runs: { args: string[]; input: string; code: number; vault: string; app: string; said?: RegExp }[] = [
232
- { args: ['--vault-id', KEYS.VAULT_KEY_ID], input: `${KEYS.VAULT_KEY}\n${KEYS.APP_KEY}\n`, code: 0, vault: '✓', app: '✓' },
233
- { args: [], input: `${wrong}\n${KEYS.APP_KEY}\n`, code: 1, vault: '✗', app: '✓', said: /✗ vault key +not this instance's vault key/ },
234
- { args: [], input: `${KEYS.VAULT_KEY}\nnot-a-key\n`, code: 1, vault: '✓', app: '✗', said: /✗ app key +not an app key/ },
231
+ const runs: { args: string[]; input: string; code: number; key: string; said?: RegExp }[] = [
232
+ { args: ['--vault-id', KEYS.VAULT_KEY_ID], input: `${KEYS.VAULT_KEY}\n`, code: 0, key: '✓' },
233
+ { args: [], input: `${wrong}\n`, code: 1, key: '✗', said: /✗ key +not this instance's/ },
234
+ { args: [], input: 'not-a-key\n', code: 1, key: '✗', said: /✗ key +not a key/ },
235
235
  ];
236
- for (const { args, input, code, vault, app, said } of runs) {
236
+ for (const { args, input, code, key, said } of runs) {
237
237
  const run = await cli.run(['verify', 'keys', ...args], {}, input);
238
- const seen = marks(run.output);
239
- expect(run.code === code && seen['vault key'] === vault && seen['app key'] === app, `coffre verify keys exited ${run.code}`, run.output);
240
- if (said !== undefined) expect(said.test(run.output), 'it did not say which key is not right', run.output);
241
- for (const key of [KEYS.VAULT_KEY, KEYS.APP_KEY, wrong, 'not-a-key']) expect(!run.output.includes(key), 'a key was printed', run.output);
238
+ expect(run.code === code && marks(run.output)['key'] === key, `coffre verify keys exited ${run.code}`, run.output);
239
+ if (said !== undefined) expect(said.test(run.output), 'it did not say the key is not right', run.output);
240
+ for (const shown of [KEYS.VAULT_KEY, KEYS.APP_KEY, wrong, 'not-a-key']) expect(!run.output.includes(shown), 'a key was printed', run.output);
242
241
  }
243
- return "this deployment's keys pass, vault ID included; a wrong vault key and a malformed app key are each named; no key shown";
242
+ return "this deployment's vault key passes, vault ID included; a wrong key and a malformed one are each named; no key shown";
244
243
  }
245
244
 
246
245
  /**
247
246
  * Everything the API does, the CLI does: with the admin's `coffre login`
248
- * session, a project and its environment, a secret set and renamed, a
247
+ * session, a project and its environment, made twice, the second time
248
+ * changing nothing, a secret set and renamed, a
249
249
  * service admitted and granted, a token issued into a 0600 file, then
250
250
  * piped to `coffre login --token` in a CLI of the service's own, read
251
251
  * with, revoked after its preview, and refused from then on; the grant
@@ -265,10 +265,27 @@ export async function manageByCli(cli: Cli): Promise<string> {
265
265
  return run.output;
266
266
  };
267
267
  const [coffre, service, agent] = [as(cli, ''), as(ci, ', as the service'), as(helper, ', with a personal token')];
268
+ // A revocation takes up to the vault's memory, 2 seconds (D218): the token was asked about a moment ago. Asked every
269
+ // 250 ms until refused, for that and a second more.
270
+ const revoked = async (who: Cli, label: string, args: string[]): Promise<void> => {
271
+ for (const started = Date.now(); ; await sleep(250)) {
272
+ const run = await who.run(args, {});
273
+ const waited = Date.now() - started;
274
+ if (run.code === 0 && waited < 3000) continue;
275
+ expect(run.code === 1, `coffre ${args.join(' ')}${label} exited ${run.code} ${waited} ms after its token was revoked, not 1`, run.output);
276
+ expect(/does not know this bearer token/.test(run.output), `a revoked token${label} was not refused plainly`, run.output);
277
+ steps.push(`${args.slice(0, 2).join(' ')}${label}`);
278
+ return;
279
+ }
280
+ };
268
281
  try {
269
282
  const value = `cli-value-${randomBytes(8).toString('hex')}`;
270
283
  await coffre(['projects', 'create', 'conformance-cli', '--name', 'CLI']);
271
284
  await coffre(['environments', 'create', 'conformance-cli/ci']);
285
+ // Made again, as a script that makes what it needs does: the server refuses it, `exists`, and the CLI says nothing changed.
286
+ const again = (await coffre(['projects', 'create', 'conformance-cli'])) + (await coffre(['environments', 'create', 'conformance-cli/ci']));
287
+ expect(again.includes('conformance-cli exists already: nothing changed') && again.includes('conformance-cli/ci exists already: nothing changed'),
288
+ 'making a project or an environment that is there already did not say nothing changed', again);
272
289
  await coffre(['set', 'conformance-cli/ci/API_KEY'], value);
273
290
  await coffre(['rename', 'conformance-cli/ci/API_KEY', 'API_TOKEN']);
274
291
  const refused = await coffre(['grant', 'conformance-cli', 'conformance-deploy', '--role', 'viewer', '--env', 'ci', '--service'], undefined, 1);
@@ -295,8 +312,7 @@ export async function manageByCli(cli: Cli): Promise<string> {
295
312
  expect(/Nothing changed\. Re-run with --apply/.test(preview), 'tokens revoke without --apply did not preview', preview);
296
313
  await service(['get', 'conformance-cli/ci/API_TOKEN']);
297
314
  await coffre(['tokens', 'revoke', 'conformance-deploy', listed!.id, '--apply']);
298
- const gone = await service(['get', 'conformance-cli/ci/API_TOKEN'], undefined, 1);
299
- expect(/does not know this bearer token/.test(gone), 'a revoked token was not refused plainly', gone);
315
+ await revoked(ci, ', as the service', ['get', 'conformance-cli/ci/API_TOKEN']);
300
316
 
301
317
  // A personal token, for an agent with a CLI of its own: it reads as the admin, within its scope, and makes no token.
302
318
  const personal = join(cli.home, 'agent-token');
@@ -314,7 +330,7 @@ export async function manageByCli(cli: Cli): Promise<string> {
314
330
  const [mine] = JSON.parse(await coffre(['tokens', '--json'])) as { id: string; label: string }[];
315
331
  expect(mine?.label === 'conformance-agent', 'tokens --json does not list the personal token', mine);
316
332
  await coffre(['tokens', 'revoke', mine!.id, '--apply']);
317
- await agent(['get', 'conformance-cli/ci/API_TOKEN'], undefined, 1);
333
+ await revoked(helper, ', with a personal token', ['get', 'conformance-cli/ci/API_TOKEN']);
318
334
 
319
335
  await coffre(['revoke', 'conformance-cli', 'conformance-deploy', '--env', 'ci', '--service']);
320
336
  await coffre(['archive', 'conformance-cli/ci/API_TOKEN']);
@@ -8,7 +8,7 @@ import { CoffreError } from '@coffre/client';
8
8
 
9
9
  import { using } from '../database.ts';
10
10
  import type { Deployment } from '../harness.ts';
11
- import { expect } from '../report.ts';
11
+ import { expect, until, PUBLISHED_SECONDS } from '../report.ts';
12
12
  import type { Cli } from './cli.ts';
13
13
  import type { Person } from './people.ts';
14
14
  import { query } from './storage.ts';
@@ -72,7 +72,8 @@ export async function deletion(deployment: Deployment, cli: Cli, admin: Person):
72
72
  expect((await admin.api.projects.list()).projects.every((project) => !project.slug.startsWith(PROJECT)), 'a deleted project is still listed');
73
73
  expect((await admin.api.members.get(SERVICE)).live.grants === 0, 'a grant on the deleted project is still live');
74
74
  const revealed = await admin.api.secrets.reveal(`${tombstone!.slug}/prod`).then(() => null, (error: unknown) => error);
75
- expect(revealed instanceof CoffreError && revealed.status === 404, 'a deleted project is still revealed by its tombstone', revealed);
75
+ // 0.16 refuses the tombstone's slug as no path at all (400), where 0.14 found nothing there (404).
76
+ expect(revealed instanceof CoffreError && (revealed.status === 404 || revealed.status === 400), 'a deleted project is still revealed by its tombstone', revealed);
76
77
 
77
78
  // Its slug is another project's now; the log tells the two apart.
78
79
  const fresh = value();
@@ -81,18 +82,25 @@ export async function deletion(deployment: Deployment, cli: Cli, admin: Person):
81
82
  await admin.api.secrets.set(`${PROJECT}/prod`, { API_KEY: fresh });
82
83
  expect((await projectId(deployment, PROJECT)) !== id, 'the new project took the deleted one\'s row');
83
84
  expect((await admin.api.secrets.reveal(`${PROJECT}/prod/API_KEY`)).values.API_KEY === fresh, 'the new project does not read its own value');
84
- const { entries } = await admin.api.audit.list({ path: tombstone!.slug, limit: 500 });
85
- 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));
85
+ // The page lists the deletion once the signer has published it, within its minute.
86
+ type Page = Awaited<ReturnType<typeof admin.api.audit.list>>;
87
+ let page: Page | Error | undefined;
88
+ const deleted = () => page !== undefined && !(page instanceof Error) && page.entries.some((entry) => entry.action === 'project.delete' && entry.decision === 'allow');
89
+ await until('the deletion on the log under its tombstone', async () => {
90
+ page = await admin.api.audit.list({ path: tombstone!.slug, limit: 500 }).catch((error: unknown) => error instanceof Error ? error : new Error(String(error)));
91
+ return page instanceof Error || deleted();
92
+ }, PUBLISHED_SECONDS).catch(() => undefined);
93
+ expect(!(page instanceof Error), 'the log cannot be read under the tombstone\'s slug', page instanceof Error ? page.message : page);
94
+ expect(deleted(), 'the deletion is not in the log under its tombstone', (page as Page | undefined)?.entries.slice(0, 5));
86
95
 
87
96
  // An environment alone, the same way.
88
97
  await admin.api.environments.update(`${PROJECT}/prod`, { archived: true });
89
98
  await run(['environments', 'delete', `${PROJECT}/prod`, '--apply']);
90
- await admin.api.environments.create(`${PROJECT}/prod`, { name: 'prod, again' });
99
+ await admin.api.environments.create(`${PROJECT}/prod`, {});
100
+ // 0.16 names a place by its slug alone (0.14.7 kept a display name): the new prod is told from the deleted one by holding no key.
91
101
  const environments = (await admin.api.projects.list()).projects.find((project) => project.slug === PROJECT)?.environments ?? [];
92
- expect(environments.length === 1 && environments[0]!.name === 'prod, again', 'a deleted environment is still listed, or its slug is not free', environments);
102
+ expect(environments.length === 1 && environments[0]!.slug === 'prod' && environments[0]!.details?.secretCount === 0,
103
+ 'a deleted environment is still listed, or its slug is not free', environments);
93
104
  expect((await sealed(deployment, (await projectId(deployment, PROJECT))!)).sealed === 0, 'a deleted environment still holds a value');
94
-
95
- const verified = await admin.api.audit.verify();
96
- expect(verified.ok, 'the log does not verify after the deletions', verified);
97
- 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`;
105
+ return `${PROJECT} and ${PROJECT}/prod deleted by the CLI: 5 versions erased, a grant revoked, both unlisted and their slugs reused`;
98
106
  }
@@ -3,12 +3,12 @@
3
3
  // (docs/design/environments.md).
4
4
  import { randomUUID } from 'node:crypto';
5
5
 
6
- import { CoffreError } from '@coffre/client';
6
+ import { CoffreError, type AuditEntryView } from '@coffre/client';
7
7
 
8
8
  import { using } from '../database.ts';
9
9
  import type { Deployment } from '../harness.ts';
10
- import { expect, refused } from '../report.ts';
11
- import { canary, DEV, PROD, PROJECT, valuesIn, type Canaries, type People } from './people.ts';
10
+ import { expect, refused, until, PUBLISHED_SECONDS } from '../report.ts';
11
+ import { canary, DEV, PROD, PROJECT, valuesIn, type Canaries, type People, type Person } from './people.ts';
12
12
  import { query } from './storage.ts';
13
13
 
14
14
  /** Where the fork check copies dev to; archived once checked. */
@@ -164,19 +164,33 @@ export async function references(deployment: Deployment, { admin, reader, leaver
164
164
  expect(sourceKey !== undefined, 'prod holds no secret to refer to');
165
165
  const source = `${PROD}/${sourceKey}`;
166
166
  await refused('a viewer on dev read prod directly', reader.api.secrets.reveal(source));
167
- expect(await outcome(leaver.api.secrets.set(DEV, { [HELD]: { ref: source } })) === '403 missing_secret_read',
168
- "a developer on dev who cannot read prod made a reference to prod's secret");
167
+ // 0.14 refused it `missing_secret_read`; 0.16's vault, `no_access`, as any place the maker does not read.
168
+ const made = await outcome(leaver.api.secrets.set(DEV, { [HELD]: { ref: source } }));
169
+ expect(made === '403 no_access', "a developer on dev who cannot read prod made a reference to prod's secret", made);
169
170
 
170
171
  await admin.api.secrets.set(DEV, { [HELD]: { ref: source } });
171
172
  try {
172
173
  const read = await reader.api.secrets.reveal(`${DEV}/${HELD}`);
173
174
  expect(read.values[HELD] === canaries[source], "a viewer on dev did not read prod's value through dev's reference");
174
- const logged = async (path: string) => (await admin.api.audit.list({ path, limit: 200 })).entries
175
- .filter((entry) => entry.action === 'secret.read' && entry.actorId === reader.email && entry.metadata.via !== undefined).length;
176
- 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");
175
+ // 0.16's entry names the read by its reader and its request, and each key read through a reference by its holder, `through`;
176
+ // the page lists it once the signer has published it, within its minute. The source key's own log is looked at
177
+ // too, to tell a read missing from prod's log from one missing from the log altogether.
178
+ const through = (entry: AuditEntryView) => entry.requestId === read.operationId && entry.action === 'secret.read'
179
+ && entry.actorType === 'user' && entry.actorId === reader.email
180
+ && Array.isArray(entry.metadata.keys) && entry.metadata.keys.some((key: unknown) => typeof key === 'object' && key !== null && 'through' in key);
181
+ const seen: Record<string, boolean> = { [PROD]: false, [DEV]: false, [source]: false };
182
+ const shown: Record<string, AuditEntryView[]> = {};
183
+ await until('the read through the reference on the logs', async () => {
184
+ for (const path of Object.keys(seen)) seen[path] ||= (shown[path] = (await admin.api.audit.list({ path, limit: 200 })).entries).some(through);
185
+ return Object.values(seen).every(Boolean);
186
+ }, PUBLISHED_SECONDS).catch(() => undefined);
187
+ expect(seen[PROD] && seen[DEV], "the reader's read through the reference is not in both the source's log and the holder's",
188
+ { seen, shown: Object.fromEntries(Object.entries(shown).map(([path, entries]) => [path, entries.slice(0, 5).map((entry) => `${entry.seq} ${entry.action} ${entry.actorId} ${entry.requestId}`)])) });
177
189
 
178
190
  // What it reads cannot be archived while it reads it (D41): not the key, nor its environment. Its
179
191
  // project could be, since the reference is held there too, and archiving it stops no one else's read.
192
+ // 0.16's refusal does not name the holder, as 0.14.7's did: the source's references list does, to whoever
193
+ // lists or manages access there, and the refusal says so.
180
194
  const archives: [string, (archived: boolean) => Promise<unknown>][] = [
181
195
  [source, (archived) => admin.api.secrets.update(source, { archived })],
182
196
  [PROD, (archived) => admin.api.environments.update(PROD, { archived })],
@@ -184,8 +198,10 @@ export async function references(deployment: Deployment, { admin, reader, leaver
184
198
  for (const [what, archive] of archives) {
185
199
  const refusal = await archive(true).then(() => null, (error: unknown) => error);
186
200
  if (refusal === null) await archive(false);
187
- expect(refusal instanceof CoffreError && refusal.status === 409 && refusal.message.includes(`${DEV}/${HELD} reads ${source}`),
188
- `${what} was archived, or refused without naming the reference, while a reference read it`, refusal instanceof Error ? refusal.message : refusal);
201
+ expect(refusal instanceof CoffreError && refusal.status === 409 && refusal.reason === 'referenced',
202
+ `${what} was archived, or refused other than as referenced, while a reference read it`, refusal instanceof Error ? refusal.message : refusal);
203
+ const holders = (await admin.api.references.list(what)).references.filter((reference) => reference.source === source).map((reference) => reference.holder);
204
+ expect(holders.includes(`${DEV}/${HELD}`), `${what}'s references do not name ${DEV}/${HELD}, the reference that blocks its archive`, holders);
189
205
  }
190
206
  // Unless what holds it is archived (D58): nobody reads through it then, so its source archives. Restored,
191
207
  // the holder reads nothing through it until its source is back, and says so.
@@ -201,34 +217,32 @@ export async function references(deployment: Deployment, { admin, reader, leaver
201
217
  const back = await reader.api.secrets.reveal(`${DEV}/${HELD}`);
202
218
  expect(back.values[HELD] === canaries[source], 'a reference did not read its source again once both were restored');
203
219
 
204
- // A row the vault never sealed: a reference to prod, held in dev, pointing at a vault entry about something else.
220
+ // A row the vault never sealed: a key in dev whose row says it reads prod's secret, its MAC none of the vault's.
205
221
  const forged = await using(deployment.database(), async (sql) => {
206
- const [holder] = await query<{ id: string; project_id: string; environment_id: string }>(sql,
207
- 'SELECT id, project_id, environment_id FROM secrets WHERE key = $1', [HELD]);
208
- const [target] = await query<{ id: string; project_id: string; environment_id: string }>(sql,
209
- '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]]);
210
- const [entry] = await query<{ seq: number | string | bigint }>(sql,
211
- "SELECT seq FROM audit_log WHERE author = 'vault' AND action = 'access.grant' AND decision = 'allow' ORDER BY seq DESC LIMIT 1");
222
+ const [holder] = await query<{ project_id: string; environment_id: string }>(sql,
223
+ 'SELECT project_id, environment_id FROM secrets WHERE key = $1', [HELD]);
224
+ const [target] = await query<{ id: string }>(sql,
225
+ 'SELECT s.id FROM secrets s JOIN environments e ON e.id = s.environment_id WHERE s.key = $1 AND e.slug = $2', [sourceKey, PROD.split('/')[1]]);
212
226
  const id = randomUUID();
213
- await query(sql, 'INSERT INTO secrets (id, project_id, environment_id, key, current_version) VALUES ($1, $2, $3, $4, 0)', [id, holder!.project_id, holder!.environment_id, FORGED]);
214
- 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)
215
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [randomUUID(), holder!.project_id, holder!.environment_id, id, target!.project_id, target!.environment_id, target!.id, String(entry!.seq), admin.member]);
227
+ const now = new Date();
228
+ await query(sql, `INSERT INTO secrets (id, project_id, environment_id, key, created_at, updated_at, versions, source_id, source_set_at, source_set_by)
229
+ VALUES ($1, $2, $3, $4, $5, $5, 0, $6, $5, $7)`, [id, holder!.project_id, holder!.environment_id, FORGED, now, target!.id, admin.member]);
216
230
  return id;
217
231
  });
218
232
  try {
219
233
  const forgedRead = await outcome(reader.api.secrets.reveal(`${DEV}/${FORGED}`));
220
- // Read from its seal, the row is no reference at all, and its key holds no value; the vault would refuse it anyway.
221
- expect(['404 no_value', '404 not_found', '403 bad_claim'].includes(forgedRead), `a reference row the vault never made was read: ${forgedRead}`);
234
+ // A row the vault did not write is no row to it: the key is not there (0.16's `unknown_key`), and nothing is read through it.
235
+ expect(forgedRead === '404 unknown_key', `a reference row the vault never made was read: ${forgedRead}`);
222
236
  } finally {
223
- await using(deployment.database(), async (sql) => {
224
- await query(sql, 'DELETE FROM secret_references WHERE secret_id = $1', [forged]);
225
- await query(sql, 'DELETE FROM secrets WHERE id = $1', [forged]);
226
- });
237
+ await using(deployment.database(), (sql) => query(sql, 'DELETE FROM secrets WHERE id = $1', [forged]));
227
238
  }
228
239
 
229
240
  await admin.api.references.break(`${DEV}/${HELD}`);
230
- const after = await outcome(reader.api.secrets.reveal(`${DEV}/${HELD}`));
231
- expect(after.startsWith('409 '), `a broken reference still read: ${after}`);
241
+ // A key whose reference ended has no value until someone writes one (0.16's `no_value`; 0.14.7 answered 409).
242
+ const broken = await reader.api.secrets.reveal(`${DEV}/${HELD}`).then((answer) => answer, (error: unknown) => error);
243
+ const said = broken instanceof CoffreError ? `${broken.status} ${broken.reason ?? broken.code}` : 'let through';
244
+ expect(said === '404 no_value' && !JSON.stringify(broken instanceof Error ? { ...broken, message: broken.message } : broken).includes(canaries[source]!),
245
+ `a broken reference still read, or was refused other than as holding no value: ${said}`, broken instanceof Error ? broken.message : broken);
232
246
  } finally {
233
247
  await admin.api.secrets.update(`${DEV}/${HELD}`, { archived: true });
234
248
  }
@@ -255,23 +269,43 @@ export async function loosening({ admin, reader, leaver }: People): Promise<stri
255
269
  try {
256
270
  await refused('a viewer read a sensitive value', reader.api.secrets.reveal(path));
257
271
  expect((await leaver.api.secrets.list(DEV)).loosenable === false, 'a developer on dev is told they make a sensitive value a secret in place');
258
- await refused('a developer on dev made a sensitive value a secret in place', leaver.api.secrets.set(DEV, { [LOOSE]: { sensitivity: 'secret' } }));
272
+ const theirs = await loosen(leaver);
273
+ expect(theirs.status >= 400 && theirs.status < 500, `a developer on dev made a sensitive value a secret in place, answered ${theirs.status}`, await theirs.text());
259
274
  expect((await admin.api.secrets.list(DEV)).loosenable === true, 'the admin, an Owner of the whole instance, is told they do not loosen');
260
- const marked = await admin.api.secrets.set(DEV, { [LOOSE]: { sensitivity: 'secret' } });
261
- expect(!JSON.stringify(marked).includes(value), "the admin's loosening answered the value", marked);
275
+ const answer = await loosen(admin);
276
+ const text = await answer.text();
277
+ expect(answer.ok, `the admin's loosening answered ${answer.status}`, text);
278
+ expect(!text.includes(value), "the admin's loosening answered the value", text);
279
+ const marked = JSON.parse(text) as { keys: Record<string, unknown> };
262
280
  expect(JSON.stringify(marked.keys[LOOSE]) === JSON.stringify({ version: 2, sensitivity: 'secret' }), 'the loosening made no new secret version', marked.keys);
263
281
  const read = await reader.api.secrets.reveal(path);
264
282
  expect(read.values[LOOSE] === value, "the reader did not read the loosened value as dev's secret");
265
283
  const versions = (await admin.api.secrets.history(path)).versions.map((version) => version.sensitivity);
266
284
  expect(versions.join() === 'secret,sensitive', 'the loosening changed the old version too, or made none', versions);
267
- const logged = (await admin.api.audit.list({ path, limit: 50 })).entries.filter((entry) => entry.action === 'secret.loosen');
268
- expect(logged.length === 1 && logged[0]!.metadata.from === 'sensitive' && logged[0]!.metadata.to === 'secret', 'the loosening is not its own entry, saying both levels', logged);
285
+ // The page lists what the signer has published, within its minute. The entry's line for the key says the nature it
286
+ // `was` and the one it is now (docs/design/0.16.md, "A change that makes any key looser is logged `secret.loosen`").
287
+ let logged: AuditEntryView[] = [];
288
+ await until('the loosening on the audit page', async () => {
289
+ logged = (await admin.api.audit.list({ path, limit: 50 })).entries.filter((entry) => entry.action === 'secret.loosen');
290
+ return logged.length > 0;
291
+ }, PUBLISHED_SECONDS).catch(() => undefined);
292
+ const lines = (logged.length === 1 ? logged[0]!.metadata.keys : undefined) as { key?: unknown; was?: unknown; nature?: unknown }[] | undefined;
293
+ expect(lines?.some((line) => line.key === LOOSE && line.was === 'sensitive' && line.nature === 'secret') === true, 'the loosening is not its own entry, saying both levels', logged);
269
294
  } finally {
270
295
  await admin.api.secrets.set(DEV, { [LOOSE]: null });
271
296
  }
272
297
  return 'the admin makes a sensitive value a secret in place, answered no value, as a new version, logged as secret.loosen; a developer on dev may not';
273
298
  }
274
299
 
300
+ /**
301
+ * `{ sensitivity }` alone, a nature changed in place, as the page sends it:
302
+ * the client's types carry it again once 0.16 serves it (UNSERVED, in
303
+ * ../unserved.ts).
304
+ */
305
+ function loosen(person: Person): Promise<Response> {
306
+ return person.browser.send('PATCH', `/api/secrets/${DEV}`, { [LOOSE]: { sensitivity: 'secret' } });
307
+ }
308
+
275
309
  /**
276
310
  * Within a project, key names are not secret; values are. What dev is
277
311
  * missing is compared with every sibling: the reader, a viewer on dev who
@@ -1,33 +1,23 @@
1
- // Each component's database login writes its own tables and nothing else:
2
- // the app cannot write members, grants or the vault's entries, the vault
3
- // cannot write the app's tables, and neither can rewrite the log. Postgres
4
- // only: SQLite has no logins.
1
+ // Each component's database login writes what it must and nothing else
2
+ // (docs/design/0.16.md, "Four rules"): the app's only reads; the vault's
3
+ // writes every row and entry, but not 0.14's archived log, and neither
4
+ // changes the schema or steps around its privileges. 0.14 also held the
5
+ // vault's login off the app's tables and both off rewriting the log; in
6
+ // 0.16 the vault writes every table, the log's included, and the signer
7
+ // holds what the log is held to.
5
8
  import { using, type Sql } from '../database.ts';
6
9
  import type { Deployment } from '../harness.ts';
7
- import { expect, Skip } from '../report.ts';
10
+ import { expect } from '../report.ts';
8
11
  import { quoted, tables } from './storage.ts';
9
12
 
10
- const ZEROS = "decode(repeat('00', 32), 'hex')";
11
- const appendAs = (author: string) => `INSERT INTO audit_log
12
- (seq, author, key_id, occurred_at, actor, action, decision, prev_hash, mac, hash)
13
- VALUES (9000000000, '${author}', '${author}:0', 0, 'system:conformance', 'probe', 'allow', ${ZEROS}, ${ZEROS}, ${ZEROS})`;
14
- const IMMUTABLE = [
15
- "UPDATE audit_log SET action = 'rewritten' WHERE author = 'app'",
16
- "UPDATE audit_log SET action = 'rewritten' WHERE author = 'vault'",
17
- 'DELETE FROM audit_log', 'TRUNCATE audit_log', 'DROP TABLE audit_log',
18
- 'UPDATE secret_versions SET id = id', 'DELETE FROM secret_versions',
19
- 'DELETE FROM vault_members', 'CREATE TABLE conformance_probe (id integer)',
20
- 'CREATE TEMP TABLE conformance_probe (id integer)',
21
- 'ALTER TABLE audit_log DISABLE TRIGGER USER', "SET session_replication_role = 'replica'",
13
+ /** What neither login may do: make or drop a table, empty one, or turn off the database's own checks. */
14
+ const BYPASSES = [
15
+ 'CREATE TABLE conformance_probe (id integer)', 'CREATE TEMP TABLE conformance_probe (id integer)',
16
+ 'DROP TABLE log', 'TRUNCATE log', 'TRUNCATE secret_versions',
17
+ 'ALTER TABLE log_pending DISABLE TRIGGER USER', "SET session_replication_role = 'replica'",
22
18
  'SET ROLE coffre_owner',
23
19
  ];
24
20
 
25
- /** A column of each app table, for an update that would change nothing, if it were allowed. */
26
- const APP_COLUMNS: Record<string, string> = {
27
- projects: 'id', environments: 'id', secrets: 'id', secret_versions: 'id', identities: 'id',
28
- credentials: 'id', device_authorizations: 'id',
29
- };
30
-
31
21
  async function refuses(sql: Sql, statements: string[]): Promise<number> {
32
22
  for (const statement of statements) {
33
23
  await sql.exec('BEGIN');
@@ -45,7 +35,7 @@ async function refuses(sql: Sql, statements: string[]): Promise<number> {
45
35
  return statements.length;
46
36
  }
47
37
 
48
- /** Neither table nor column grants may let a login write outside its component. */
38
+ /** Neither table nor column grants may let a login write these tables. */
49
39
  async function noWrites(sql: Sql, names: readonly string[]): Promise<void> {
50
40
  const columns = await sql.query<{ table_name: string; column_name: string; insertable: boolean; updatable: boolean; deletable: boolean; truncatable: boolean }>(
51
41
  `SELECT table_name, column_name,
@@ -65,43 +55,30 @@ async function noWrites(sql: Sql, names: readonly string[]): Promise<void> {
65
55
  }
66
56
  }
67
57
 
58
+ /** The app's login writes no table: every row is the vault's. */
68
59
  export async function appLogin(deployment: Deployment): Promise<string> {
69
- if (deployment.kind === 'node') throw new Skip('SQLite has no database logins');
70
- expect(deployment.runtime !== null, 'the app runtime login is missing');
71
- const protectedTables = (await using(deployment.database(), tables)).filter((name) => name.startsWith('vault_'));
60
+ const all = await using(deployment.database(), tables);
72
61
  return using(deployment.runtime(), async (sql) => {
73
- await noWrites(sql, protectedTables);
74
- const statements = [...IMMUTABLE, appendAs('vault'), 'SET ROLE coffre_vault', 'DELETE FROM secrets'];
75
- for (const table of protectedTables) {
76
- statements.push(`INSERT INTO ${table} SELECT * FROM ${table} LIMIT 1`, `UPDATE ${table} SET principal = principal`,
77
- `DELETE FROM ${table}`, `TRUNCATE ${table}`);
78
- }
62
+ await noWrites(sql, all);
63
+ const statements = [...BYPASSES, 'SET ROLE coffre_vault',
64
+ "INSERT INTO log_pending (kept, body, mac) VALUES (true, '{}', decode(repeat('00', 36), 'hex'))",
65
+ 'UPDATE log SET body = body', 'DELETE FROM log', 'UPDATE log_head SET size = size',
66
+ 'UPDATE secret_versions SET id = id', 'DELETE FROM secrets', 'UPDATE principals SET role = role', 'UPDATE grants SET role = role'];
67
+ for (const table of all) statements.push(`DELETE FROM ${quoted(table)}`, `TRUNCATE ${quoted(table)}`);
79
68
  const count = await refuses(sql, statements);
80
- return `${count} writes and bypasses refused; no member or grant write privileges`;
69
+ return `${count} writes and bypasses refused; no write privilege on any of ${all.length} tables`;
81
70
  });
82
71
  }
83
72
 
73
+ /** The vault's login writes coffre's rows, never 0.14's archived log, and empties no table. */
84
74
  export async function vaultLogin(deployment: Deployment): Promise<string> {
85
- if (deployment.kind === 'node') throw new Skip('SQLite has no database logins');
86
- expect(deployment.vaultRuntime !== null, 'the vault runtime login is missing');
87
- const appTables = (await using(deployment.database(), tables)).filter((name) =>
88
- !['vault_members', 'vault_grants', 'audit_log', 'audit_chain_head'].includes(name));
75
+ const all = await using(deployment.database(), tables);
89
76
  return using(deployment.vaultRuntime(), async (sql) => {
90
- await noWrites(sql, appTables);
91
- const [head] = await sql.query<{ insertable: boolean; deletable: boolean; identity_updatable: boolean; truncatable: boolean }>(`SELECT
92
- has_table_privilege(current_user, 'audit_chain_head', 'INSERT') AS insertable,
93
- has_table_privilege(current_user, 'audit_chain_head', 'DELETE') AS deletable,
94
- has_table_privilege(current_user, 'audit_chain_head', 'TRUNCATE') AS truncatable,
95
- has_column_privilege(current_user, 'audit_chain_head', 'only_row', 'UPDATE') AS identity_updatable`);
96
- expect(!head.insertable && !head.deletable && !head.truncatable && !head.identity_updatable, 'the vault can replace the head row', head);
97
- const statements = [...IMMUTABLE, appendAs('app'), 'SET ROLE coffre_app',
98
- 'UPDATE audit_chain_head SET only_row = only_row', 'DELETE FROM audit_chain_head', 'TRUNCATE audit_chain_head',
99
- 'UPDATE vault_members SET principal = principal', 'UPDATE vault_grants SET role = role'];
100
- for (const table of appTables) {
101
- const column = APP_COLUMNS[table];
102
- if (column !== undefined) statements.push(`UPDATE ${quoted(table)} SET ${column} = ${column}`);
103
- }
77
+ await noWrites(sql, ['audit_archive']);
78
+ const statements = [...BYPASSES, 'SET ROLE coffre_app',
79
+ 'UPDATE audit_archive SET action = action', 'DELETE FROM audit_archive', 'INSERT INTO audit_archive SELECT * FROM audit_archive LIMIT 1'];
80
+ for (const table of all) statements.push(`TRUNCATE ${quoted(table)}`);
104
81
  const count = await refuses(sql, statements);
105
- return `${count} writes and bypasses refused; app tables have no write privileges`;
82
+ return `${count} writes and bypasses refused; 0.14's archived log not writable`;
106
83
  });
107
84
  }