@coffre/conformance 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
2
  "name": "@coffre/conformance",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Checks a coffre deployment from outside: what it must never do, whatever code it runs.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/erwinkn/coffre.git",
9
+ "directory": "packages/conformance"
10
+ },
11
+ "type": "module",
12
+ "engines": {
13
+ "node": ">=24"
14
+ },
15
+ "files": [
16
+ "bin",
17
+ "dist",
18
+ "src"
19
+ ],
20
+ "bin": {
21
+ "coffre-conformance": "./bin/coffre-conformance.js"
22
+ },
23
+ "exports": {
24
+ "./idp": {
25
+ "coffre:source": "./src/idp/index.ts",
26
+ "types": "./dist/idp.d.ts",
27
+ "default": "./dist/idp.js"
28
+ },
29
+ "./package.json": "./package.json"
30
+ },
31
+ "dependencies": {
32
+ "jose": "6.2.3",
33
+ "pg": "8.16.3"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "26.1.1",
37
+ "@types/pg": "8.20.0",
38
+ "oauth4webapi": "3.8.8",
39
+ "tsdown": "0.23.0",
40
+ "typescript": "5.9.3",
41
+ "@coffre/client": "0.1.0"
42
+ },
43
+ "scripts": {
44
+ "build": "tsdown",
45
+ "typecheck": "tsc --noEmit",
46
+ "test": "node --conditions=coffre:source --test"
47
+ }
6
48
  }
package/src/browser.ts ADDED
@@ -0,0 +1,67 @@
1
+ // A browser, reduced to what coffre sees of one: a cookie jar, and the
2
+ // headers a page on coffre's own origin sends with its calls.
3
+ import { createClient, type CoffreClient } from '@coffre/client';
4
+
5
+ export class Browser {
6
+ readonly origin: string;
7
+ readonly #jar = new Map<string, string>();
8
+
9
+ constructor(origin: string) {
10
+ this.origin = origin;
11
+ }
12
+
13
+ /** Whether coffre left this browser a cookie. */
14
+ get hasCookies(): boolean {
15
+ return this.#jar.size > 0;
16
+ }
17
+
18
+ /**
19
+ * A request as the browser makes it, cookies included, redirects not
20
+ * followed. Nothing else is added: a caller playing another site's page
21
+ * sets its own `origin`.
22
+ */
23
+ async fetch(path: string | Request, init: RequestInit = {}): Promise<Response> {
24
+ const request = typeof path === 'string' ? new Request(new URL(path, this.origin), init) : path;
25
+ const ours = new URL(request.url).origin === this.origin;
26
+ if (ours && this.#jar.size > 0) {
27
+ request.headers.set('cookie', [...this.#jar].map(([name, value]) => `${name}=${value}`).join('; '));
28
+ }
29
+ const response = await fetch(request, { redirect: 'manual' });
30
+ if (ours) {
31
+ for (const cookie of response.headers.getSetCookie()) {
32
+ const [pair = ''] = cookie.split(';');
33
+ const at = pair.indexOf('=');
34
+ const value = pair.slice(at + 1);
35
+ if (value === '' || /max-age=0/i.test(cookie)) this.#jar.delete(pair.slice(0, at));
36
+ else this.#jar.set(pair.slice(0, at), value);
37
+ }
38
+ }
39
+ return response;
40
+ }
41
+
42
+ /** A call one of coffre's pages makes: JSON, from its own origin. */
43
+ send(method: string, path: string, body?: unknown): Promise<Response> {
44
+ return this.fetch(path, {
45
+ method,
46
+ headers: { origin: this.origin, 'sec-fetch-site': 'same-origin', 'content-type': 'application/json' },
47
+ body: body === undefined ? undefined : JSON.stringify(body),
48
+ });
49
+ }
50
+
51
+ /** The API as this browser's pages call it. */
52
+ client(): CoffreClient {
53
+ return createClient({
54
+ url: this.origin,
55
+ transport: (request) => {
56
+ request.headers.set('origin', this.origin);
57
+ request.headers.set('sec-fetch-site', 'same-origin');
58
+ return this.fetch(request);
59
+ },
60
+ });
61
+ }
62
+ }
63
+
64
+ /** The API as the CLI or a service calls it, with a bearer token. */
65
+ export function bearer(origin: string, token: string): CoffreClient {
66
+ return createClient({ url: origin, headers: () => ({ authorization: `Bearer ${token}` }) });
67
+ }
@@ -0,0 +1,141 @@
1
+ // Who reaches what: members only, each to the places granted, from coffre's
2
+ // own pages, until they leave, and never too much at once.
3
+ import { bearer } from '../browser.ts';
4
+ import type { Deployment } from '../harness.ts';
5
+ import { expect, refused } from '../report.ts';
6
+ import { BULK, DEV, PROD, signIn, valuesIn, type Canaries, type People } from './people.ts';
7
+ import { pollDevice, startDevice } from './signin.ts';
8
+
9
+ export async function membersOnly(deployment: Deployment, { stranger }: People): Promise<string> {
10
+ const attempt = await signIn(deployment, stranger.browser, stranger.email);
11
+ expect(!attempt.ok, 'someone never admitted signed in');
12
+ expect(!stranger.browser.hasCookies, 'the refused sign-in left the browser a cookie');
13
+ expect((await stranger.browser.fetch('/api/me')).status === 401, 'the refused sign-in left a session');
14
+
15
+ const nobody = bearer(deployment.origin, 'coffre_cli_conformanceforgedtokenconformanceforged');
16
+ await refused('a made-up token read secrets', nobody.secrets.list(DEV));
17
+ for (const [method, path, body] of [
18
+ ['GET', `/api/secrets/${DEV}`, undefined],
19
+ ['POST', '/api/reveals', { path: DEV }],
20
+ ['PATCH', `/api/secrets/${DEV}`, { API_KEY: 'from nobody' }],
21
+ ['GET', '/api/audit', undefined],
22
+ ] as const) {
23
+ const response = await fetch(`${deployment.origin}${path}`, {
24
+ method,
25
+ headers: { origin: deployment.origin, 'content-type': 'application/json' },
26
+ body: body === undefined ? undefined : JSON.stringify(body),
27
+ });
28
+ expect(response.status === 401, `${method} ${path} answered ${response.status} to no one`);
29
+ }
30
+ return `a stranger's sign-in is ${attempt.ok ? 'let' : attempt.error}; no one and a made-up token get 401`;
31
+ }
32
+
33
+ export async function grantScoping({ reader, service, bulk }: People, canaries: Canaries): Promise<string> {
34
+ const read = await reader.api.secrets.reveal(DEV);
35
+ expect(same(read.values, valuesIn(canaries, DEV)), "a viewer on dev did not get dev's values", read.values);
36
+ await refused('a viewer on dev revealed prod', reader.api.secrets.reveal(PROD));
37
+ await refused('a viewer on dev listed prod', reader.api.secrets.list(PROD));
38
+ await refused('a viewer wrote a value', reader.api.secrets.set(DEV, { API_KEY: 'from the reader' }));
39
+ await refused('a viewer granted themselves more', reader.api.access.set(reader.member, { [PROD]: 'owner' }));
40
+ await refused('a viewer added a member', reader.api.members.add('user:friend@conformance.example'));
41
+ await refused('a viewer issued a service token', reader.api.tokens.issue(service.member, { expiresInDays: 1 }));
42
+ const me = await reader.api.me();
43
+ const places = me.environments.map((place) => `${place.project}/${place.environment}`);
44
+ expect(places.length === 1 && places[0] === DEV, 'a viewer on dev is told of other places', places);
45
+
46
+ const served = await service.api.secrets.reveal(DEV);
47
+ expect(same(served.values, valuesIn(canaries, DEV)), "a service viewer on dev did not get dev's values", served.values);
48
+ await refused('a service viewer on dev revealed prod', service.api.secrets.reveal(PROD));
49
+ await refused('a viewer on bulk revealed dev', bulk.api.secrets.reveal(DEV));
50
+ return 'a viewer on dev reads dev and nothing else, in a browser or with a token, and changes nothing';
51
+ }
52
+
53
+ /**
54
+ * A change sent with the session cookie from another site's page, or from
55
+ * no page coffre can vouch for, is refused (CSRF), and so is a reveal.
56
+ */
57
+ export async function crossSite({ admin }: People, canaries: Canaries): Promise<string> {
58
+ const foreign: Record<string, string>[] = [
59
+ { origin: 'https://attacker.example', 'sec-fetch-site': 'cross-site' },
60
+ { origin: 'https://attacker.example' },
61
+ { 'sec-fetch-site': 'same-site' },
62
+ {},
63
+ ];
64
+ for (const headers of foreign) {
65
+ for (const [method, path, body] of [
66
+ ['PATCH', `/api/secrets/${DEV}`, { API_KEY: 'from another site' }],
67
+ ['POST', '/api/reveals', { path: DEV }],
68
+ ['PUT', '/api/members/user:mallory@attacker.example', {}],
69
+ ['POST', '/auth/signout', undefined],
70
+ ] as const) {
71
+ const response = await admin.browser.fetch(path, {
72
+ method,
73
+ headers: { ...headers, 'content-type': 'application/json' },
74
+ body: body === undefined ? undefined : JSON.stringify(body),
75
+ });
76
+ const text = await response.text();
77
+ expect(response.status === 403, `${method} ${path} with ${JSON.stringify(headers)} answered ${response.status}`, text);
78
+ expect(!Object.values(canaries).some((value) => text.includes(value)), `${method} ${path} from another site carried a value`);
79
+ }
80
+ }
81
+ const after = await admin.api.secrets.reveal(DEV);
82
+ expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`], 'a value changed from another site', after.values);
83
+ const members = await admin.api.members.list();
84
+ expect(!members.members.some((entry) => entry.member.includes('mallory')), 'a member was added from another site');
85
+ return 'writes, reveals and sign-out with the cookie, from another site or no page at all: 403';
86
+ }
87
+
88
+ export async function offboarding(deployment: Deployment, { admin, leaver, service }: People, canaries: Canaries) {
89
+ const device = await startDevice(deployment);
90
+ await leaver.api.deviceLogins.decide(device.user_code, true);
91
+ // What they saw is what to rotate once they are gone.
92
+ await leaver.cli.secrets.reveal(DEV);
93
+ const { report } = await admin.api.members.remove(leaver.member);
94
+ const exposed = report.exposed.map((entry) => `${entry.project}/${entry.environment}/${entry.key}`).sort();
95
+ const expected = Object.keys(valuesIn(canaries, DEV)).map((key) => `${DEV}/${key}`).sort();
96
+ expect(same(exposed, expected), 'the offboarding report does not name what they read', report.exposed);
97
+
98
+ expect((await leaver.browser.fetch('/api/me')).status === 401, "a removed member's browser session still works");
99
+ await refused("a removed member's CLI session read secrets", leaver.cli.secrets.list(DEV));
100
+ const again = await signIn(deployment, leaver.browser, leaver.email);
101
+ expect(!again.ok, 'a removed member signed in again');
102
+
103
+ await admin.api.members.remove(service.member);
104
+ await refused("a removed service's token read secrets", service.api.secrets.reveal(DEV));
105
+ await admin.api.members.add(leaver.member);
106
+ await admin.api.access.set(leaver.member, { [DEV]: 'developer' });
107
+ await admin.api.members.add(service.member);
108
+ await admin.api.access.set(service.member, { [DEV]: 'viewer' });
109
+ expect((await leaver.browser.fetch('/api/me')).status === 401, 're-admission revived the browser session');
110
+ await refused('re-admission revived the CLI session', leaver.cli.secrets.reveal(DEV));
111
+ await refused('re-admission revived the service token', service.api.secrets.reveal(DEV));
112
+ expect(!(await pollDevice(deployment, device.device_code)).ok, 're-admission revived the old device approval');
113
+ const fresh = await signIn(deployment, leaver.browser, leaver.email);
114
+ expect(fresh.ok, 'a fresh provider callback could not sign the re-admitted member in');
115
+ await admin.api.members.remove(leaver.member);
116
+ await admin.api.members.remove(service.member);
117
+ return `the report names ${exposed.length} values; re-admission revives no old session, token or approval; a fresh provider callback works`;
118
+ }
119
+
120
+ /** Reading more values at once than the vault allows in its window is refused. */
121
+ export async function bulkLimit({ admin, bulk }: People, limit: number): Promise<string> {
122
+ const values: Record<string, string> = {};
123
+ for (let i = 0; i <= limit; i++) values[`BULK_${i}`] = `bulk-${i}`;
124
+ await admin.api.secrets.set(BULK, values);
125
+ const refusal = await refused(`${limit + 1} values were revealed at once`, bulk.api.secrets.reveal(BULK));
126
+ expect(refusal.status === 403 && refusal.code === 'bulk_limit' && refusal.reason === 'bulk_limit',
127
+ 'the bulk read was not refused as bulk_limit', refusal);
128
+ // Refused for how many, not for where: one of them still opens.
129
+ const one = await bulk.api.secrets.reveal(`${BULK}/BULK_0`);
130
+ expect(one.values.BULK_0 === 'bulk-0', 'a viewer on bulk could not read one of its values', one.values);
131
+ return `${limit + 1} values at once: ${refusal.status} ${refusal.reason ?? refusal.code}; one still opens`;
132
+ }
133
+
134
+ function same(a: unknown, b: unknown): boolean {
135
+ return JSON.stringify(sorted(a)) === JSON.stringify(sorted(b));
136
+ }
137
+
138
+ function sorted(value: unknown): unknown {
139
+ if (Array.isArray(value) || value === null || typeof value !== 'object') return value;
140
+ return Object.fromEntries(Object.entries(value).sort(([a], [b]) => (a < b ? -1 : 1)));
141
+ }
@@ -0,0 +1,267 @@
1
+ // What is written down: every value opened, by the vault that opened it;
2
+ // every version stored, naming the vault's wrap of its key; no value without
3
+ // its entry; and a log changed behind coffre's back is caught.
4
+ import { createHash } from 'node:crypto';
5
+
6
+ import type { AuditEntryView, CoffreClient } from '@coffre/client';
7
+
8
+ import { using } from '../database.ts';
9
+ import type { Deployment } from '../harness.ts';
10
+ import { expect, until } from '../report.ts';
11
+ import { appendOnlyLifted, entryFields, insertRow, LOG_REFUSAL, logRefuses, query } from './storage.ts';
12
+ import { DEV, PROJECT, valuesIn, type Canaries, type People } from './people.ts';
13
+
14
+ /** One read per returned value, committed by the vault under the response's operation. */
15
+ export async function revealAudited(deployment: Deployment, { admin, reader }: People, canaries: Canaries, purpose: 'reveal' | 'run'): Promise<string> {
16
+ const path = purpose === 'reveal' ? `${DEV}/API_KEY` : DEV;
17
+ const { operationId, values } = await reader.api.secrets.reveal(path);
18
+ expect(typeof operationId === 'string' && operationId.length > 0, 'the read returned no operation id');
19
+ const expected = purpose === 'reveal' ? { API_KEY: canaries[`${DEV}/API_KEY`] } : valuesIn(canaries, DEV);
20
+ expect(JSON.stringify(Object.entries(values).sort()) === JSON.stringify(Object.entries(expected).sort()), 'the read returned different values', values);
21
+ const entries = await everyAuditEntry(admin.api);
22
+ const bundle = entries.filter((entry) => entry.operationId === operationId);
23
+ const keys = bundle.map((entry) => entry.key).sort();
24
+ expect(JSON.stringify(keys) === JSON.stringify(Object.keys(expected).sort()), 'the read is not logged once per value', bundle);
25
+ expect(bundle.every((entry) => entry.author === 'vault' && entry.action === 'secret.read' && entry.decision === 'allow'),
26
+ "the read's entries are not the vault's allowed reads", bundle);
27
+ expect(bundle.every((entry) => entry.metadata.purpose === purpose && entry.requestId !== null && entry.requestId === bundle[0]!.requestId),
28
+ 'the reads do not name one request and its purpose', bundle);
29
+ const { keys: listed } = await admin.api.secrets.list(DEV);
30
+ for (const entry of bundle) {
31
+ expect(entry.version === listed.find((key) => key.key === entry.key)?.version, `the entry for ${String(entry.key)} names another version`, entry);
32
+ }
33
+ await using(deployment.database(), async (sql) => {
34
+ const rows = await query<{ author: string; action: string; decision: string; request_id: string | null; key: string; current_version: number; metadata: string }>(sql,
35
+ `SELECT l.author, l.action, l.decision, l.request_id, l.metadata, s.key, s.current_version
36
+ FROM audit_log l LEFT JOIN secrets s ON s.id = l.secret_id WHERE l.operation_id = $1`, [operationId]);
37
+ expect(JSON.stringify(rows.map((row) => row.key).sort()) === JSON.stringify(Object.keys(expected).sort()), 'the committed read is not once per value', rows);
38
+ for (const row of rows) {
39
+ const metadata = JSON.parse(row.metadata) as { purpose: string; version: number };
40
+ expect(row.author === 'vault' && row.action === 'secret.read' && row.decision === 'allow', 'the committed read is not the vault release', row);
41
+ expect(row.request_id !== null && row.request_id === rows[0].request_id && metadata.purpose === purpose, 'the committed reads have different requests or purposes', rows);
42
+ expect(metadata.version === row.current_version, 'the committed read names another version', row);
43
+ }
44
+ });
45
+ return `${bundle.length} values, each read once by the vault for ${purpose}, under ${operationId}`;
46
+ }
47
+
48
+ /** Full verification must reach the stored head, not just some intact prefix. */
49
+ export async function verification(deployment: Deployment, { admin }: People): Promise<string> {
50
+ const result = await admin.api.audit.verify();
51
+ expect(result.ok, 'the full log does not verify', result);
52
+ await using(deployment.database(), async (sql) => {
53
+ const [head] = await sql.query<{ next_seq: string | number; head_hash: Uint8Array }>('SELECT next_seq, head_hash FROM audit_chain_head');
54
+ const [last] = await sql.query<{ seq: string | number; hash: Uint8Array }>('SELECT seq, hash FROM audit_log ORDER BY seq DESC LIMIT 1');
55
+ expect(head !== undefined && last !== undefined, 'the head or newest entry is missing');
56
+ expect(BigInt(head.next_seq) === BigInt(last.seq) + 1n, 'the head does not name the newest entry');
57
+ expect(Buffer.from(head.head_hash).equals(Buffer.from(last.hash)), 'the head has another hash');
58
+ expect(result.through !== null && BigInt(result.through) === BigInt(last.seq), 'verification stopped before the head', { result, head: head.next_seq });
59
+ expect(BigInt(result.entries) === BigInt(head.next_seq), 'verification did not count every entry', result);
60
+ });
61
+ return `${result.entries} entries verified through the head at ${result.through}`;
62
+ }
63
+
64
+ /**
65
+ * Each heartbeat has the vault sign the log up to its last entry, in an
66
+ * entry of its own, after checking that the prefix it signed last is still
67
+ * there. Two, so the second covers the first; then the log is verified from
68
+ * its first entry, every checkpoint with it.
69
+ */
70
+ export async function checkpoints(deployment: Deployment, { admin }: People): Promise<string> {
71
+ const before = await admin.api.audit.verify();
72
+ expect(before.ok, 'the log does not verify', before);
73
+ await deployment.scheduled();
74
+ await deployment.scheduled();
75
+ const after = await admin.api.audit.verify();
76
+ expect(after.ok && after.checkpoint !== null, 'the audit log is not verified and checkpointed', after);
77
+ expect(after.checkpoint.seq > (before.through ?? -1), 'the checkpoint does not cover what was written before it', { before, after });
78
+ const { entries } = await admin.api.audit.list({ detail: '1', limit: 20 });
79
+ const signed = entries.filter((entry) => entry.action === 'audit.checkpoint' && entry.decision === 'allow');
80
+ expect(
81
+ signed.length >= 2 && signed.every((entry) => entry.author === 'vault' && entry.detail),
82
+ "the checkpoints are not the vault's entries, hidden as detail",
83
+ signed,
84
+ );
85
+ const [newest, previous] = signed as [AuditEntryView, AuditEntryView];
86
+ expect(newest.metadata.seq === after.checkpoint.seq, 'the newest checkpoint entry is not the one verified', { signed, after });
87
+ expect(previous.seq <= after.checkpoint.seq, 'the newest checkpoint does not cover the one before it', signed);
88
+ return `${after.entries} entries verified through ${after.through}, checkpointed at ${after.checkpoint.seq}`;
89
+ }
90
+
91
+ /**
92
+ * Every version the app says it stored names the vault's entry for the key
93
+ * it was sealed with, `key.wrap` for a write and `key.rewrap` for a restore:
94
+ * the same member, request and operation, the same secret and version. And
95
+ * every value read is the vault's to log: the app keeps no reads of its own.
96
+ */
97
+ export async function writesAgree(deployment: Deployment): Promise<string> {
98
+ return using(deployment.database(), async (sql) => {
99
+ const entries = await sql.query<{
100
+ seq: number | string; author: string; action: string; decision: string; actor: string; metadata: string;
101
+ project_id: string | null; environment_id: string | null; secret_id: string | null;
102
+ request_id: string | null; operation_id: string | null; related_seq: string | number | null;
103
+ }>('SELECT * FROM audit_log ORDER BY seq');
104
+ const bySeq = new Map(entries.map((entry) => [String(entry.seq), entry]));
105
+ const writes = entries.filter((entry) => entry.author === 'app' && entry.decision === 'allow'
106
+ && (entry.action === 'secret.write' || entry.action === 'secret.restore'));
107
+ for (const entry of entries.filter((row) => row.author === 'app' && row.decision === 'allow')) {
108
+ expect(entry.action !== 'secret.read', 'the app logged an allowed read of a value', entry);
109
+ }
110
+ for (const entry of writes) {
111
+ const seal = entry.action === 'secret.write' ? 'key.wrap' : 'key.rewrap';
112
+ const key = entry.related_seq === null ? undefined : bySeq.get(String(entry.related_seq));
113
+ expect(key !== undefined && key.author === 'vault' && key.action === seal && key.decision === 'allow',
114
+ `a ${entry.action} names no ${seal} of the vault's`, { entry, key });
115
+ for (const field of ['actor', 'request_id', 'operation_id', 'project_id', 'environment_id'] as const) {
116
+ expect(entry[field] === key[field], `the write and its seal have different ${field}`, { entry, key });
117
+ }
118
+ expect(entry.operation_id !== null && entry.request_id !== null, 'the committed write has no operation or request', entry);
119
+ const metadata = JSON.parse(entry.metadata) as { version: number; key: string };
120
+ const sealed = JSON.parse(key.metadata) as { secretId?: string; version: number; subject: string };
121
+ expect((sealed.secretId ?? key.secret_id) === entry.secret_id && sealed.version === metadata.version
122
+ && sealed.subject.split('/').at(-1) === metadata.key, 'the write names a seal of another secret or version', { entry, key });
123
+ }
124
+ const versions = await query<{ secret_id: string; version: number }>(sql,
125
+ `SELECT v.secret_id, v.version FROM secret_versions v JOIN secrets s ON s.id = v.secret_id
126
+ JOIN projects p ON p.id = s.project_id WHERE p.slug = $1`, [PROJECT]);
127
+ expect(versions.length > 0, 'no stored versions were inspected');
128
+ for (const version of versions) {
129
+ const matching = writes.filter((entry) => entry.secret_id === version.secret_id
130
+ && (JSON.parse(entry.metadata) as { version: number }).version === version.version);
131
+ expect(matching.length === 1, 'a stored version has no unique app write entry', { version, matching });
132
+ }
133
+ return `${versions.length} stored versions, each with one app write naming the vault's seal in the same operation`;
134
+ });
135
+ }
136
+
137
+ /**
138
+ * No value leaves without its entry: with the audit log refusing writes (a
139
+ * trigger, as a full disk or a lost connection would), the vault cannot log
140
+ * the read, so a reveal fails and carries nothing.
141
+ */
142
+ export async function noAuditNoValue(deployment: Deployment, { admin }: People, canaries: Canaries): Promise<string> {
143
+ let status = 0;
144
+ await using(deployment.database(), (sql) => logRefuses(sql, async () => {
145
+ const before = deployment.output().split(LOG_REFUSAL).length;
146
+ const response = await admin.browser.send('POST', '/api/reveals', { path: DEV });
147
+ const text = await response.text();
148
+ status = response.status;
149
+ const body = JSON.parse(text) as { error: string; message: string; reason?: string };
150
+ expect(status === 500 && body.error === 'internal_error' && body.reason === undefined,
151
+ 'the unlogged reveal did not fail as internal_error', { status, body });
152
+ expect(body.message === 'something went wrong; see the server log', 'the audit failure exposed another reason', body);
153
+ expect(!Object.values(canaries).some((value) => text.includes(value)), 'a reveal that could not be logged carried a value', text);
154
+ // A lost vault gives the same public error; the trigger must be its cause.
155
+ await until('the injected audit refusal in the processes\' output', async () => deployment.output().split(LOG_REFUSAL).length > before, 5);
156
+ }, 'secret.read'));
157
+ const after = await admin.api.secrets.reveal(DEV);
158
+ expect(after.values.API_KEY === canaries[`${DEV}/API_KEY`], 'reveals did not come back once the log took writes again');
159
+ return `a reveal the audit log would not take: ${status} internal_error, its injected cause in the log, and no value`;
160
+ }
161
+
162
+ /** A publicly chained entry still needs the vault's MAC. */
163
+ export async function forgedVaultEntry(deployment: Deployment, { admin }: People): Promise<string> {
164
+ const verify = () => admin.api.audit.verify();
165
+ expect((await verify()).ok, 'the log does not verify before the forged entry');
166
+ await using(deployment.database(), async (sql) => {
167
+ // An entry in the vault's name, chained after the last, with a MAC made up.
168
+ const [head] = await sql.query<{ next_seq: number | string; head_hash: Uint8Array }>(
169
+ 'SELECT next_seq, head_hash FROM audit_chain_head',
170
+ );
171
+ const [writer] = await sql.query<{ key_id: string }>("SELECT key_id FROM audit_log WHERE author = 'vault' ORDER BY seq DESC LIMIT 1");
172
+ expect(writer !== undefined, 'no vault key id to copy');
173
+ const seq = BigInt(head.next_seq);
174
+ const forged = {
175
+ seq, author: 'vault', key_id: writer.key_id, occurred_at: Date.now(), actor: 'user:forger@conformance.example',
176
+ action: 'secret.read', decision: 'allow', metadata: '{}',
177
+ };
178
+ const prevHash = Buffer.from(head.head_hash);
179
+ const mac = Buffer.alloc(32, 0x41);
180
+ const hash = createHash('sha256').update('coffre.audit.chain.v2').update(prevHash).update(entryFields(forged)).update(mac).digest();
181
+ await insertRow(sql, 'audit_log', { ...forged, prev_hash: prevHash, mac, hash });
182
+ await query(sql, 'UPDATE audit_chain_head SET next_seq = $1, head_hash = $2', [seq + 1n, hash]);
183
+ let inserted;
184
+ try {
185
+ inserted = await verify();
186
+ } finally {
187
+ await appendOnlyLifted(sql, () => query(sql, 'DELETE FROM audit_log WHERE seq = $1', [seq]));
188
+ await query(sql, 'UPDATE audit_chain_head SET next_seq = $1, head_hash = $2', [seq, prevHash]);
189
+ }
190
+ 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);
191
+ const removed = await verify();
192
+ expect(removed.ok, 'the log did not verify once the entry was gone', removed);
193
+ });
194
+ return 'a publicly chained vault entry with a made-up MAC was refused at its sequence';
195
+ }
196
+
197
+ /** Rewrite either author's entry and require a fault at that exact sequence. */
198
+ export async function rewrittenEntry(deployment: Deployment, { admin }: People, author: 'app' | 'vault'): Promise<string> {
199
+ expect((await admin.api.audit.verify()).ok, 'the log does not verify before the rewrite');
200
+ let seq = 0;
201
+ await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
202
+ const [row] = await query<{ seq: string | number; actor: string }>(sql,
203
+ 'SELECT seq, actor FROM audit_log WHERE author = $1 ORDER BY seq LIMIT 1', [author]);
204
+ expect(row !== undefined, `no ${author} entry to rewrite`);
205
+ seq = Number(row.seq);
206
+ await query(sql, 'UPDATE audit_log SET actor = $1 WHERE seq = $2', ['user:nobody@conformance.example', row.seq]);
207
+ try {
208
+ const result = await admin.api.audit.verify();
209
+ expect(!result.ok && result.failedAtSeq === Number(row.seq), 'a rewritten entry was not caught at its sequence', result);
210
+ } finally {
211
+ await query(sql, 'UPDATE audit_log SET actor = $1 WHERE seq = $2', [row.actor, row.seq]);
212
+ }
213
+ }));
214
+ expect((await admin.api.audit.verify()).ok, 'the restored entry did not verify');
215
+ return `entry ${seq}, the ${author}'s, rewritten: caught there, then put back`;
216
+ }
217
+
218
+ /** Remove an unreferenced entry, then put its exact bytes back. */
219
+ export async function missingEntry(deployment: Deployment, { admin }: People, position: 'middle' | 'first' | 'batch'): Promise<string> {
220
+ expect((await admin.api.audit.verify()).ok, 'the log does not verify before deletion');
221
+ let seq = 0;
222
+ await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
223
+ const [row] = await sql.query(`SELECT * FROM audit_log WHERE ${position !== 'middle' ? `seq = ${position === 'first' ? 0 : 1000}` :
224
+ "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`);
225
+ expect(row !== undefined, 'no unreferenced entry to delete');
226
+ seq = Number(row.seq);
227
+ const linked = await query(sql, 'SELECT * FROM audit_log WHERE related_seq = $1', [row.seq]);
228
+ await query(sql, 'UPDATE audit_log SET related_seq = NULL WHERE related_seq = $1', [row.seq]);
229
+ await query(sql, 'DELETE FROM audit_log WHERE seq = $1', [row.seq]);
230
+ try {
231
+ const result = await admin.api.audit.verify();
232
+ expect(!result.ok, 'a log with an entry missing verifies', result);
233
+ if (position !== 'middle') expect(result.reason.includes(`sequence gap: expected seq ${position === 'first' ? 0 : 1000}`), 'the sequence gap was not reported', result);
234
+ } finally {
235
+ await insertRow(sql, 'audit_log', row);
236
+ for (const entry of linked) await query(sql, 'UPDATE audit_log SET related_seq = $1 WHERE seq = $2', [entry.related_seq, entry.seq]);
237
+ }
238
+ }));
239
+ expect((await admin.api.audit.verify()).ok, 'the restored log did not verify');
240
+ const which = { first: 'the first', batch: 'the first past a page of 1,000', middle: 'a heartbeat' }[position];
241
+ return `entry ${seq}, ${which}, deleted: caught, then put back`;
242
+ }
243
+
244
+ /** Last: keep the head, delete its newest entries, and require verification to notice. */
245
+ export async function deletedTail(deployment: Deployment, { admin }: People): Promise<string> {
246
+ const intact = await admin.api.audit.verify();
247
+ expect(intact.ok && intact.checkpoint !== null, 'the log is not intact and checkpointed');
248
+ const signed = intact.checkpoint.seq;
249
+ await using(deployment.database(), (sql) => appendOnlyLifted(sql, async () => {
250
+ await query(sql, 'UPDATE audit_log SET related_seq = NULL WHERE seq >= $1', [signed]);
251
+ await query(sql, 'DELETE FROM audit_log WHERE seq >= $1', [signed]);
252
+ }));
253
+ const result = await admin.api.audit.verify();
254
+ expect(!result.ok && result.author === 'app' && result.reason.includes('removed from the end'), 'the log verifies with its newest entries deleted', result);
255
+ return 'the newest entries deleted, with the head left alone, were caught';
256
+ }
257
+
258
+ /** The whole log, detail included, newest first. */
259
+ export async function everyAuditEntry(api: CoffreClient): Promise<AuditEntryView[]> {
260
+ const all: AuditEntryView[] = [];
261
+ for (let before: number | undefined; ; ) {
262
+ const { entries } = await api.audit.list({ before, limit: 500, detail: '1' });
263
+ all.push(...entries);
264
+ if (entries.length < 500) return all;
265
+ before = entries.at(-1)!.seq;
266
+ }
267
+ }
@@ -0,0 +1,121 @@
1
+ // A value leaves coffre through a reveal and nowhere else: not in any
2
+ // answer to a GET, whoever asks, not in a page, and not in what it stores or
3
+ // prints. Every value set is a random canary, so finding one is a leak.
4
+ import { existsSync, readFileSync } from 'node:fs';
5
+
6
+ import { using } from '../database.ts';
7
+ import type { Deployment } from '../harness.ts';
8
+ import { expect } from '../report.ts';
9
+ import { getCalls, getUrls } from '../routes.ts';
10
+ import { scanTables, tables } from './storage.ts';
11
+ import { canary, BULK, DEV, PROD, PROJECT, SERVICE, type Canaries, type People } from './people.ts';
12
+
13
+ function calls(people: People) {
14
+ const places = [DEV, PROD, BULK].map((path) => {
15
+ const [project, environment] = path.split('/') as [string, string];
16
+ return { project, environment };
17
+ });
18
+ return getCalls({
19
+ places,
20
+ secrets: places.slice(0, 2).map((place) => ({ ...place, key: 'API_KEY' })),
21
+ members: [...[people.admin, people.reader, people.leaver, people.bulk].map((person) => person.member), SERVICE],
22
+ services: [SERVICE],
23
+ });
24
+ }
25
+
26
+ function pages(people: People): string[] {
27
+ const users = [people.admin, people.reader, people.leaver, people.bulk];
28
+ return [
29
+ '/',
30
+ '/login',
31
+ '/unregistered',
32
+ '/account',
33
+ '/access',
34
+ '/audit',
35
+ '/settings',
36
+ '/auth/device',
37
+ '/projects',
38
+ `/projects/${PROJECT}`,
39
+ ...[DEV, PROD, BULK].map((path) => `/projects/${path}`),
40
+ '/users',
41
+ ...users.map((person) => `/users/${encodeURIComponent(person.email)}`),
42
+ '/tokens',
43
+ `/tokens/${SERVICE.slice('token:'.length)}`,
44
+ ];
45
+ }
46
+
47
+ export async function canaryScan(deployment: Deployment, people: People, canaries: Canaries): Promise<string> {
48
+ const values = Object.values(canaries);
49
+ const leaks: string[] = [];
50
+ const look = (where: string, content: string | Uint8Array) => {
51
+ const bytes = Buffer.from(content);
52
+ const found = values.filter((value) => bytes.includes(value));
53
+ if (found.length > 0) leaks.push(`${where}: ${found.length} value${found.length === 1 ? '' : 's'}`);
54
+ };
55
+
56
+ const token = (value: string) => (url: string) => fetch(url, { headers: { authorization: `Bearer ${value}` } });
57
+ const browsers = {
58
+ 'the root admin': people.admin.browser,
59
+ 'the reader': people.reader.browser,
60
+ 'the bulk reader': people.bulk.browser,
61
+ 'the leaver, removed': people.leaver.browser,
62
+ 'the stranger': people.stranger.browser,
63
+ };
64
+ const callers: [string, (url: string) => Promise<Response>][] = [
65
+ ['no one', (url) => fetch(url, { redirect: 'manual' })],
66
+ ...Object.entries(browsers).map(([name, browser]): [string, (url: string) => Promise<Response>] => [name, (url) => browser.fetch(url)]),
67
+ ["the leaver's CLI, removed", token(people.leaver.cliToken)],
68
+ ['the service, removed', token(people.service.token)],
69
+ ];
70
+
71
+ let answers = 0;
72
+ const routes = calls(people);
73
+ for (const { key, url } of getUrls(deployment.origin, routes)) {
74
+ for (const [name, fetchAs] of callers) {
75
+ look(`${key} as ${name}`, await (await fetchAs(url)).text());
76
+ answers++;
77
+ }
78
+ }
79
+ const paths = pages(people);
80
+ for (const path of paths) {
81
+ for (const [name, fetchAs] of callers.slice(0, 1 + Object.keys(browsers).length)) {
82
+ look(`the page ${path} as ${name}`, await (await fetchAs(`${deployment.origin}${path}`)).text());
83
+ answers++;
84
+ }
85
+ }
86
+
87
+ const stored: string[] = [];
88
+ await using(deployment.database(), async (sql) => {
89
+ const names = await tables(sql);
90
+ // Prove the reader sees bytes before trusting an absence of plaintext.
91
+ const control = canary();
92
+ await sql.exec(`CREATE TABLE conformance_canary_probe (value ${sql.engine === 'postgres' ? 'bytea' : 'BLOB'})`);
93
+ try {
94
+ await sql.query(`INSERT INTO conformance_canary_probe VALUES (${sql.engine === 'postgres' ? '$1' : '?'})`, [Buffer.from(control)]);
95
+ let found = false;
96
+ await scanTables(sql, ['conformance_canary_probe'], (_, bytes) => { found ||= Buffer.from(bytes).includes(control); });
97
+ expect(found, 'the database scanner missed its planted binary canary');
98
+ } finally {
99
+ await sql.exec('DROP TABLE conformance_canary_probe');
100
+ }
101
+ await scanTables(sql, names, look);
102
+ stored.push(`${names.length} tables, with a binary positive control`);
103
+ });
104
+ if (deployment.databaseFile !== null) {
105
+ look("the database's file", files(deployment.databaseFile));
106
+ stored.push('the database file and WAL');
107
+ }
108
+ look("the processes' output", deployment.output());
109
+ stored.push("the processes' output");
110
+
111
+ expect(leaks.length === 0, 'a value was found outside a reveal', leaks);
112
+ return `${answers} answers from ${Object.keys(routes).length} GET routes and ${paths.length} pages as ${callers.length} callers, and ${stored.join(', ')}: no value`;
113
+ }
114
+
115
+ /** A SQLite file and its write-ahead log, as bytes: what anyone with the disk would read. */
116
+ function files(path: string): Buffer {
117
+ expect(existsSync(path), `the store file could not be found: ${path}`);
118
+ return Buffer.concat([path, `${path}-wal`]
119
+ .filter((file) => existsSync(file))
120
+ .map((file) => readFileSync(file)));
121
+ }